卷积神经网络开放题论文作业

卷积神经网络开放题

问题一:

  • 阅读提出上述两种网络的相关论文,试从数据集的预处理、激活函数的使用、训练方法的改进以及模型结构的变化等角度,从理论层面分析比较LeNet与AlexNet的结构差异,并尝试解释AlexNet为什么会具有对计算机视觉任务优越的处理性能。

1.AlexNet使用了更加简单的ReLU激活函数,而LeNet使用的是Sigmoid激活函数

2.AlexNet还在LeNet的基础上增加了三个卷积层

3.AlexNet用Dropout 来控制全连接层的模型复杂的,防止过拟合

4.AlexNet引入了数据增强,如翻转、裁剪和颜色变化,从而进一步扩大数据集来缓解过拟合。

5.AlexNet使用的是最大池化,抓住最重要的特征,在模型上会有稀疏的作用,而LeNet使用的是平均池化

6.AlexNet的卷积通道数是LeNet的数十倍,多的卷积通道数代表的是更多的特征

  • AlexNet对Fashion-MNIST数据集来说可能过于复杂,请尝试对模型进行简化来使训练更快,同时保证分类准确率(accuracy)不明显下降(不低于85%),并将简化后的结构、节省的训练时间以及下降的准确率等相关指标以表格的形式进行总结分析。
简化的操作训练时间准确率
将全连接层的参数由1000->10;epochs->3265.6 s0.863
删除网络中群连接层4096到4096的那一层213.5 s0.887
------------------------
删除输出通道数数和输入通道数相等的卷积层190.9 s0.886
增大学习率lr:0.001->0.00480.0 s0.890
------------------------
减小学习率 lr:0.004→0.00280.3 s0.908
import time
import torch
from torch import nn, optim
import torchvision

import sys
sys.path.append("..") 
import d2lzh_pytorch as d2l
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

print(torch.__version__)
print(torchvision.__version__)
print(device)



class AlexNet(nn.Module):
    def __init__(self):
        super(AlexNet, self).__init__()
        self.conv = nn.Sequential(
            nn.Conv2d(1, 96, 11, 4), # in_channels, out_channels, kernel_size, stride, padding
            nn.ReLU(),
            nn.MaxPool2d(3, 2), # kernel_size, stride
            # 减小卷积窗口,使用填充为2来使得输入与输出的高和宽一致,且增大输出通道数
            nn.Conv2d(96, 256, 5, 1, 2),
            nn.ReLU(),
            nn.MaxPool2d(3, 2),
            # 连续3个卷积层,且使用更小的卷积窗口。除了最后的卷积层外,进一步增大了输出通道数。
            # 前两个卷积层后不使用池化层来减小输入的高和宽
            nn.Conv2d(256, 384, 3, 1, 1),
            nn.ReLU(),
            #nn.Conv2d(384, 384, 3, 1, 1),
            #nn.ReLU(),
            nn.Conv2d(384, 256, 3, 1, 1),
            nn.ReLU(),
            nn.MaxPool2d(3, 2)
        )
         # 这里全连接层的输出个数比LeNet中的大数倍。使用丢弃层来缓解过拟合
        self.fc = nn.Sequential(
            nn.Linear(256*5*5, 4096),
            nn.ReLU(),
            nn.Dropout(0.5),
#             nn.Linear(4096, 4096),
#             nn.ReLU(),
            nn.Dropout(0.5),
            # 输出层。由于这里使用Fashion-MNIST,所以用类别数为10,而非论文中的1000
            nn.Linear(4096, 10),
        )

    def forward(self, img):
        feature = self.conv(img)
        output = self.fc(feature.view(img.shape[0], -1))
        return output
				
				
				
net = AlexNet()
print(net)



def load_data_fashion_mnist(batch_size, resize=None, root='~/Datasets/FashionMNIST'):
    """Download the fashion mnist dataset and then load into memory."""
    trans = []
    if resize:
        trans.append(torchvision.transforms.Resize(size=resize))
    trans.append(torchvision.transforms.ToTensor())
    
    transform = torchvision.transforms.Compose(trans)
    mnist_train = torchvision.datasets.FashionMNIST(root=root, train=True, download=True, transform=transform)
    mnist_test = torchvision.datasets.FashionMNIST(root=root, train=False, download=True, transform=transform)

    train_iter = torch.utils.data.DataLoader(mnist_train, batch_size=batch_size, shuffle=True, num_workers=4)
    test_iter = torch.utils.data.DataLoader(mnist_test, batch_size=batch_size, shuffle=False, num_workers=4)

    return train_iter, test_iter




batch_size = 128
# 如出现“out of memory”的报错信息,可减小batch_size或resize
train_iter, test_iter = load_data_fashion_mnist(batch_size, resize=224)



lr, num_epochs = 0.002, 3
optimizer = torch.optim.Adam(net.parameters(), lr=lr)
d2l.train_ch5(net, train_iter, test_iter, batch_size, optimizer, device, num_epochs)

问题二:

  • LeNet与AlexNet在接近图像输入的卷积模块中都引入了较大尺寸的卷积核,如 5 × 5 5\times 5 5×5或者 7 × 7 7\times 7 7×7的卷积窗口来捕捉更大范围的图像信息,试分析VGG每个基础块的固定设计是否会影响到图像的粗粒度信息提取,并且对比不同结构模块输出的特征图进行对比。

不会,VGG通过重复使用简单的基础块来构建复杂的模型,可以通过增添vggblock来时模型复杂,或者减少block来适应简单的模型,虽然一个个基础块是固定了,但方便了我们来设计模型,而且每个基础块中卷积层的数量和输出通道数依然是由我们来确定的,而且其使用的卷积核为3*3stride为1,图像在经历卷积层后其长和宽并没有改变好几个3*3的卷积核的叠加,就相当于一个大的卷积核,所以我认为VGG基础块的固定设计不会影响到图像的粗粒度的信息提取。

  • 尝试将Fashion-MNIST中图像的高和宽由224改为96,试分析VGG网络的参数变化情况,并且对比模型训练时间、分类准确率(accuracy)等实验指标受到的影响,以表格的形式进行总结分析。

首先加入沿用原来的VGG网络只改变最后面全连接层的参数,我们会发现参数减少了,因为图像的高和宽减少了,这样一来模型的训练时间也会相应的减少,分类准确率会降低。

训练时间分类准确率VGG参数变化
降低降低改变全连接层参数减少其数目
-------------------------------------------
增加降低降低基础块的数量,降低模型的复杂度
------------------------
降低升高减少每个基础块中卷积层的数量
输入图像的大小训练时间准确率
224*22496.7s0.911
96*9625.50.912
import time
import torch
from torch import nn, optim

import sys
sys.path.append("..") 
import d2lzh_pytorch as d2l
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

print(torch.__version__)
print(device)







def vgg_block(num_convs, in_channels, out_channels): #卷积层个数,输入通道数,输出通道数
    blk = []
    for i in range(num_convs):
        if i == 0:
            blk.append(nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1))
        else:
            blk.append(nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1))
        blk.append(nn.ReLU())
    blk.append(nn.MaxPool2d(kernel_size=2, stride=2)) # 这里会使宽高减半
    return nn.Sequential(*blk)
		
		
conv_arch = ((1, 1, 64), (1, 64, 128), (2, 128, 256), (2, 256, 512), (2, 512, 512))
# 经过5个vgg_block, 宽高会减半5次, 变成 224/32 = 7
fc_features = 512 * 3 * 3 # c * w * h
fc_hidden_units = 4096 # 任意



def vgg(conv_arch, fc_features, fc_hidden_units=4096):
    net = nn.Sequential()
    # 卷积层部分
    for i, (num_convs, in_channels, out_channels) in enumerate(conv_arch):
        # 每经过一个vgg_block都会使宽高减半
        net.add_module("vgg_block_" + str(i+1), vgg_block(num_convs, in_channels, out_channels))
    # 全连接层部分
    net.add_module("fc", nn.Sequential(d2l.FlattenLayer(),
                                 nn.Linear(fc_features, fc_hidden_units),
                                 nn.ReLU(),
                                 nn.Dropout(0.5),
                                 nn.Linear(fc_hidden_units, fc_hidden_units),
                                 nn.ReLU(),
                                 nn.Dropout(0.5),
                                 nn.Linear(fc_hidden_units, 10)
                                ))
    return net


ratio = 8
small_conv_arch = [(1, 1, 64//ratio), (1, 64//ratio, 128//ratio), (2, 128//ratio, 256//ratio), 
                   (2, 256//ratio, 512//ratio), (2, 512//ratio, 512//ratio)]
net = vgg(small_conv_arch, fc_features // ratio, fc_hidden_units // ratio)
print(net)


batchsize=16
#batch_size = 64
# 如出现“out of memory”的报错信息,可减小batch_size或resize
# train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size, resize=96)

lr, num_epochs = 0.001, 5
optimizer = torch.optim.Adam(net.parameters(), lr=lr)
d2l.train_ch5(net, train_iter, test_iter, batch_size, optimizer, device, num_epochs)

问题三:

  • 对比AlexNet、VGG和NiN、GoogLeNet的模型参数尺寸,从理论的层面分析为什么后两个网络可以显著减小模型参数尺寸?

1.NiN是串联了多个由卷积层和"全连接层"构成的小网络来构建一个深层网络(使用了1*1的卷积层来代替了全连接层,1x1的卷积层,可以看成全连接层,其中空间维度(高和宽)上的每个元素相当于样本,通道相当于特征。因此,NIN使用1x1卷积层来替代全连接层,从而使空间信息能够自然传递到后面的层。)

2.而且其去掉了最后的三个全连接层,而是用了一个平均池化层来作为代替(通过调整输出的通道数使其输出仍为类别数),这里的全局平均池化层的设计显著的减少了模型的参数尺寸同时也缓解了过拟合,但是这个设计也会造成模型的训练时间的增加。
而且其使用1*1的卷积核也会减少计算的参数。

3.GoogLeNet应该也是使用了全局平均池化层的设计显著的减少了模型的参数尺寸但其后面又引入了dense所以其全局平均池化层输出的通道不一定是类别数

  • GoogLeNet有数个后续版本,包括加入批量归一化层 [7]、对Inception块做调整 [8] 和加入残差连接 [9],请尝试实现并运行它们,然后观察实验结果,以表格的形式进行总结分析。
V1使用了1*1的卷积核来压缩通道数
V2在Inception的内部采用标准化卷积核
V3将“标准化”推广到一般情况,并加入了BN
V4在V3的基础上选定了更合适的超参
Inception-ResNet网络引入residual connection直连,把Inception和ResNet结合起来

参考文献

[1] Xiao, H., Rasul, K., & Vollgraf, R. (2017). Fashion-mnist: a novel image dataset for benchmarking machine learning algorithms. arXiv preprint arXiv:1708.07747.

[2] LeCun, Y., Bottou, L., Bengio, Y., & Haffner, P. (1998). Gradient-based learning applied to document recognition. Proceedings of the IEEE, 86(11), 2278-2324.

[3] Krizhevsky, A., Sutskever, I., & Hinton, G. E. (2012). Imagenet classification with deep convolutional neural networks. In Advances in neural information processing systems (pp. 1097-1105).

[4] Simonyan, K., & Zisserman, A. (2014). Very deep convolutional networks for large-scale image recognition. arXiv preprint arXiv:1409.1556.

[5] Lin, M., Chen, Q., & Yan, S. (2013). Network in network. arXiv preprint arXiv:1312.4400.

[6] Szegedy, C., Liu, W., Jia, Y., Sermanet, P., Reed, S., & Anguelov, D. & Rabinovich, A.(2015). Going deeper with convolutions. In Proceedings of the IEEE conference on computer vision and pattern recognition (pp. 1-9).

[7] Ioffe, S., & Szegedy, C. (2015). Batch normalization: Accelerating deep network training by reducing internal covariate shift. arXiv preprint arXiv:1502.03167.

[8] Szegedy, C., Vanhoucke, V., Ioffe, S., Shlens, J., & Wojna, Z. (2016). Rethinking the inception architecture for computer vision. In Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition (pp. 2818-2826).

[9] Szegedy, C., Ioffe, S., Vanhoucke, V., & Alemi, A. A. (2017, February). Inception-v4, inception-resnet and the impact of residual connections on learning. In Proceedings of the AAAI Conference on Artificial Intelligence (Vol. 4, p. 12).

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值