所有内容均来源于【李哥考研复试项目班】
一、AlexNet
1、 特征图维度变化
输入图片 3 * 244 * 244
输出 参数量
Conv2d(3, 64, 11, 4, 2) (224-11+2*2)/4+1=55 64*3*11*11+64(偏置) =
64 * 55 * 55 23296
Pool(3, 2) 64 * 27 * 27 0
3个一pool,每次走两步
Conv2d(64, 192, 5, 1, 2) (27-5+2*2)/1+1=27 192*64*5*5+64=
192 * 27 * 27 307392
Pool(3, 2) 192 * 13 * 13 0
Conv2d(192, 384, 3, 1, 1) (13-3+2*1)/1+1=13 384*192*3*3+384=
384 * 13 * 13 663936
Conv2d(384, 256, 3, 1, 1) (13-3+2*1)/1+1=13 256*384*3*3+256=
256 * 13 * 13 884992
Conv2d(256, 256, 3, 1, 1) (13-3+2*1)/1+1=13 256*256*3*3*+256=
256 * 13 * 13 590080
Pool(3, 2) 256 * 6 * 6 0
adaPool(6) 256 * 6 * 6 0
拉直 256*6*6=9216个数
Linner(9216, 4096) 4096 9216*4096+4096=37752832
Linner(4096, 4096) 4096 4096*4096+4096=16781312
Linner(4096, 1000) 1000 4096*1000+1000=4097000
2、代码
import torch
import torchvision.models as models
import torch.nn as nn
class myAlexNet(nn.Module):
def __init__(self, out_dim):
super(myAlexNet, self).__init__()
self.conv1 = nn.Conv2d(3, 64, 11, 4, 2)
# 定义了一个卷积层,3个输入通道,64个输出通道,卷积核大小为11x11,步长为4,填充为2
self.pool1 = nn.MaxPool2d(3, 2)
# 定义池化层,赤化窗口大小为3*3,步长为2
self.conv2 = nn.Conv2d(64, 192, 5, 1, 2)
self.pool2 = nn.MaxPool2d(3, 2)
self.conv3 = nn.Conv2d(192, 384, 3, 1, 1)
self.conv4 = nn.Conv2d(384, 256, 3, 1, 1)
self.conv5 = nn.Conv2d(256, 256, 3, 1, 1)
self.pool3 = nn.MaxPool2d(3, 2)
self.pool4 = nn.AdaptiveAvgPool2d(6)
self.fc1 = nn.Linear(9216, 4096)
# 定义全连接层,将卷积层的