笔记来源:【深度学习直播课精选01】深度学习拒绝调包!从0构建自己的神经网络架构哔哩哔哩bilibili
感谢Up主:TsaiTsai
直接使用经典网络可能存在的问题
-
经典网络结构太深 -
实际自己的数据样本较小,不适合经典网络
查看Pytorch中包含的经典网络
import torch
import torch.nn as nn
from torchvision import models as m
?
#查看所包含的模型,模型名称大写的为父类,小写的为子类
dir(m)
视频课程中的网络搭建
基于vgg16_bn和resnet18,前者主要使用其卷积层,后者主要使用其残差层
# 实例化两个网络模型
vgg16_bn_ = m.vgg16_bn()
resnet18_ = m.resnet18()
选择vgg16_bn的7-13层
vgg16_bn_.features[7:14]
输出:
Sequential(
(7): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(8): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(9): ReLU(inplace=True)
(10): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(11): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(12): ReLU(inplace=True)
(13): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
)
选择resnet18的layer3
resnet18_.layer3
输出:
Sequential(
(0): BasicBlock(
? (conv1): Conv2d(128, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
? (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
? (relu): ReLU(inplace=True)
? (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
? (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
? (downsample): Sequential(
? ? (0): Conv2d(128, 256, kernel_size=(1, 1), stride=(2, 2), bias=False)
? ? (1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
? )
)
(1): BasicBlock(
? (conv1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
? (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
? (relu): ReLU(inplace=True)
? (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
? (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
网络搭建
# 学习能力越强的部分越放在后面
?
# 输入32*32,通道数为1
?
class MyNet(nn.Module):
? ?def __init__(self):
? ? ? ?super().__init__()
? ? ? ?# block1
? ? ? ?self.conv1 = nn.Sequential(nn.Conv2d(1, 64, kernel_size=3, stride=1, padding=1),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?nn.BatchNorm2d(64),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?nn.ReLU(inplace=True))
? ? ? ?# block2+block3
? ? ? ?self.block2 = vgg16_bn_.features[7:14] ?# 16*16
? ? ? ?self.block3 = resnet18_.layer3 # 8*8
? ? ? ?self.avgpool = resnet18_.avgpool #所有特征图尺寸缩小为1*1
? ? ? ?self.fc = nn.Linear(in_features=256, out_features=100, bias=True) ?#参考resnet18
? ? ? ?
? ?
? ?def forward(self, x):
? ? ? ?x = self.conv1(x)
? ? ? ?x = self.block2(x)
? ? ? ?x = self.block3(x)
? ? ? ?x = self.avgpool(x) # (batch_size, 256, 1, 1)
? ? ? ?x = x.view(x.shape[0], 256)
? ? ? ?x = self.fc(x)
? ? ? ?return x
检查模型的正确性
使用库torchinfo · PyPI
from torchinfo import summary
net = MyNet()
summary(net, input_size=(10, 1, 32, 32), depth=3, device="cpu")
输出:
|