Pytorch:卷积神经网络-空洞卷积

Pytorch: 空洞卷积神经网络

Copyright: Jingmin Wei, Pattern Recognition and Intelligent System, School of Artificial and Intelligence, Huazhong University of Science and Technology

Pytorch教程专栏链接

文章目录

*

+ Pytorch: 空洞卷积神经网络
@[toc]

+
* 空洞卷积神经网络搭建
* 数据预处理
* 空洞卷积神经网络的训练和预测

本教程不商用,仅供学习和参考交流使用,如需转载,请联系本人。

相对于普通卷积,空洞卷积通过在卷积核中添加空洞( 0 0 0 元素),从而增大感受野,获取更多信息。感受野为在卷积神经网络中,决定某一层输出结果中一个元素对应的输入层的区域大小,通俗解释就是特征映射上的一个点对应输入图上的区域大小。

对于一个 3 × 3 3\times3 3 ×3 的 2 2 2-空洞卷积运算,实际的卷积核大小还是 3 × 3 3\times3 3 ×3 。但是空洞为 1 1 1 ,这样卷积核就会扩充一个 7 × 7 7\times7 7 ×7 的图像块,但只有 9 9 9 个红色的点会有权重取值进行卷积操作。也可以理解为卷积核的大小为 7 × 7 7\times7 7 ×7 ,但只有图中的 9 9 9 个点的权重不为 0 0 0 ,其他均为 0 0 0 。实际卷积权重只有 3 × 3 3\times3 3 ×3 ,但感受野实际为 7 × 7 7\times7 7 ×7 。对于 15 × 15 15\times15 1 5 ×1 5 的,实际卷积只有 9 × 9 9\times9 9 ×9 。

在 nn.Conv2d() 函数中,调节 dilation 的取值,即可进行不同大小卷积核的空洞卷积运算。

我们搭建的空洞卷积神经网络有两个空洞卷积层,两个池化层和两个全连接层,分类器依旧包含 10 10 1 0 个神经元,除了卷积方式差异,与前文识别 FashionMNIST 的网络结构完全相同。

空洞卷积神经网络搭建

import numpy as np
import pandas as pd
from sklearn.metrics import accuracy_score, confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns
import copy
import time
import torch
import torch.nn as nn
from torch.optim import Adam
import torch.utils.data as Data
from torchvision import transforms
from torchvision.datasets import FashionMNIST
class MyConvDilaNet(nn.Module):
    def __init__(self):
        super(MyConvDilaNet, self).__init__()

        self.conv1 = nn.Sequential(
            nn.Conv2d(
                in_channels = 1,
                out_channels = 16,
                kernel_size = 3,
                stride = 1,
                padding = 1,
                dilation = 2,
            ),
            nn.ReLU(),
            nn.AvgPool2d(
                kernel_size = 2,
                stride = 2,
            ),

        )
        self.conv2 = nn.Sequential(
            nn.Conv2d(16, 32, 3, 1, 0, dilation = 2),
            nn.ReLU(),
            nn.AvgPool2d(2, 2),
        )
        self.classifier = nn.Sequential(
            nn.Linear(32 * 4 * 4, 256),
            nn.ReLU(),
            nn.Linear(256, 128),
            nn.ReLU(),
            nn.Linear(128, 10),
        )

    def forward(self, x):

        x = self.conv1(x)
        x = self.conv2(x)
        x = x.view(x.size(0), -1)
        output = self.classifier(x)

        return output

数据预处理

数据预处理部分和上文相同。


train_data = FashionMNIST(
    root = './data/FashionMNIST',
    train = True,
    transform = transforms.ToTensor(),
    download = False
)

train_loader = Data.DataLoader(
    dataset = train_data,
    batch_size = 64,
    shuffle = False,
    num_workers = 2,
)

print(len(train_loader))

for step, (b_x, b_y) in enumerate(train_loader):
    if step > 0:
        break

batch_x = b_x.squeeze().numpy()
batch_y = b_y.numpy()
label = train_data.classes
label[0] = 'T-shirt'

plt.figure(figsize = (12, 5))
for i in np.arange(len(batch_y)):
    plt.subplot(4, 16, i + 1)
    plt.imshow(batch_x[i, :, :], cmap = plt.cm.gray)
    plt.title(label[batch_y[i]], size = 9)
    plt.axis('off')
    plt.subplots_adjust(wspace = 0.05)

test_data = FashionMNIST(
    root = './data/FashionMNIST',
    train = False,
    download = False
)

test_data_x = test_data.data.type(torch.FloatTensor) / 255.0
test_data_x = torch.unsqueeze(test_data_x, dim = 1)
test_data_y = test_data.targets

print(test_data_x.shape)
print(test_data_y.shape)
938
torch.Size([10000, 1, 28, 28])
torch.Size([10000])

Pytorch:卷积神经网络-空洞卷积

myconvdilanet = MyConvDilaNet()
from torchsummary import summary
summary(myconvdilanet, input_size=(1, 28, 28))
Input size (MB): 0.00
Forward/backward pass size (MB): 0.24
Params size (MB): 0.65
Estimated Total Size (MB): 0.89
0 Train Loss: 0.8922 Train Acc: 0.6718
0 Val Loss: 0.6322 Val Acc: 0.7498
Train and Val complete in 1m 2s
Epoch 1/24
2 Train Loss: 0.5331 Train Acc: 0.7948
2 Val Loss: 0.5047 Val Acc: 0.8107
Train and Val complete in 3m 3s
Epoch 3/24
4 Train Loss: 0.4540 Train Acc: 0.8320
4 Val Loss: 0.4491 Val Acc: 0.8369
Train and Val complete in 4m 54s
Epoch 5/24
6 Train Loss: 0.4047 Train Acc: 0.8512
6 Val Loss: 0.4075 Val Acc: 0.8531
Train and Val complete in 6m 45s
Epoch 7/24
8 Train Loss: 0.3690 Train Acc: 0.8655
8 Val Loss: 0.3762 Val Acc: 0.8653
Train and Val complete in 8m 36s
Epoch 9/24
10 Train Loss: 0.3440 Train Acc: 0.8751
10 Val Loss: 0.3552 Val Acc: 0.8710
Train and Val complete in 10m 26s
Epoch 11/24
12 Train Loss: 0.3250 Train Acc: 0.8812
12 Val Loss: 0.3412 Val Acc: 0.8762
Train and Val complete in 12m 21s
Epoch 13/24
14 Train Loss: 0.3092 Train Acc: 0.8870
14 Val Loss: 0.3299 Val Acc: 0.8810
Train and Val complete in 14m 26s
Epoch 15/24
16 Train Loss: 0.2956 Train Acc: 0.8921
16 Val Loss: 0.3182 Val Acc: 0.8838
Train and Val complete in 16m 30s
Epoch 17/24
18 Train Loss: 0.2836 Train Acc: 0.8961
18 Val Loss: 0.3093 Val Acc: 0.8872
Train and Val complete in 18m 33s
Epoch 19/24
20 Train Loss: 0.2728 Train Acc: 0.9004
20 Val Loss: 0.3020 Val Acc: 0.8911
Train and Val complete in 20m 52s
Epoch 21/24
22 Train Loss: 0.2625 Train Acc: 0.9038
22 Val Loss: 0.2960 Val Acc: 0.8942
Train and Val complete in 22m 55s
Epoch 23/24
24 Train Loss: 0.2531 Train Acc: 0.9062
24 Val Loss: 0.2907 Val Acc: 0.8942
Train and Val complete in 24m 58s

使用折线图可视化训练过程:


plt.figure(figsize = (12, 4))
plt.subplot(1, 2, 1)
plt.plot(train_process.epoch, train_process.train_loss_all, 'ro-', label = 'Train loss')
plt.plot(train_process.epoch, train_process.val_loss_all, 'bs-', label = 'Val loss')
plt.legend()
plt.xlabel('epoch')
plt.ylabel('Loss')

plt.subplot(1, 2, 2)
plt.plot(train_process.epoch, train_process.train_acc_all, 'ro-', label = 'Train acc')
plt.plot(train_process.epoch, train_process.val_acc_all, 'bs-', label = 'Val acc')
plt.legend()
plt.xlabel('epoch')
plt.ylabel('Acc')

plt.show()


Pytorch:卷积神经网络-空洞卷积

计算空洞卷积模型的泛化能力:


myconvdilanet.eval()
output = myconvdilanet(test_data_x)
pre_lab = torch.argmax(output, 1)
acc = accuracy_score(test_data_y, pre_lab)
print(test_data_y)
print(pre_lab)
print('测试集上的预测精度为', acc)
tensor([9, 2, 1,  ..., 8, 1, 5])
tensor([9, 2, 1,  ..., 8, 1, 5])
测试集上的预测精度为 0.8841

使用热力图,观察每类数据上的预测情况:


conf_mat = confusion_matrix(test_data_y, pre_lab)
df_cm = pd.DataFrame(conf_mat, index = label, columns = label)
heatmap = sns.heatmap(df_cm, annot = True, fmt = 'd', cmap = 'YlGnBu')
heatmap.yaxis.set_ticklabels(heatmap.yaxis.get_ticklabels(), rotation = 0, ha = 'right')
heatmap.xaxis.set_ticklabels(heatmap.xaxis.get_ticklabels(), rotation = 45, ha = 'right')
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()

Pytorch:卷积神经网络-空洞卷积

Original: https://blog.csdn.net/weixin_44979150/article/details/122778696
Author: 宅家的小魏
Title: Pytorch:卷积神经网络-空洞卷积

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/519073/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球