1. 题目描述
在这个例子中网络结构如下所示
(网络结构取自李宏毅老师的HW3)
需要注意的是
,当我们在计算卷积的特征图的维度时,常用以下公式:
O
u
t
p
u
t
D
i
m
=
(
I
n
p
u
t
D
i
m
−
K
e
r
n
e
l
S
i
z
e
+
2
∗
P
a
d
d
i
n
g
)
S
t
r
i
d
e
+
1
OutputDim = \frac{(InputDim-KernelSize+2*Padding)}{Stride} + 1
O
u
tp
u
t
D
im
=
St
r
i
d
e
(
I
n
p
u
t
D
im
−
Ker
n
e
lS
i
ze
+
2
∗
P
a
dd
in
g
)
+
1
- 当分子不能整除Stride的时候,输出维度默认向下取整,而对于MaxPooling的情况则相反(向上取整)
- kernel size, output dim, input dim 这些通常都只考虑正方形的输入输出,所以上面的公式只考虑一个维度即可
- stride对于卷积核的平移时,不论横轴还是纵轴都需要移动相同的stride
2. 代码实现
class MyResNet(nn.Module):
def __init__(self):
super(MyResNet, self).__init__()
# input 128 * 128 * 3
# Out = (Input - Kernel + 2 * Padding) / Stride + 1
# layer1 O = (128 - 3 + 2)/1 + 1 = 128
self.cnn_layer1 = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(64)
)
# layer2 O = (128 - 3 + 2)/1 + 1 = 128
self.cnn_layer2 = nn.Sequential(
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(64)
)
# layer3 O = (128 - 3 + 2)/2 + 1 = 64
self.cnn_layer3 = nn.Sequential(
nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(128)
)
# layer4 O = (64 - 3 + 2)/1 + 1 = 64
self.cnn_layer4 = nn.Sequential(
nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(128)
)
# layer5 O = (64 - 3 + 2)/2 + 1 = 32
self.cnn_layer5 = nn.Sequential(
nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(256)
)
# layer6 O = (64 - 3 + 2)/1 +1 = 32
self.cnn_layer6 = nn.Sequential(
nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(256)
)
# fc
self.fc_layer = nn.Sequential(
nn.Linear(32*32*256, 256),
nn.ReLU(),
nn.Linear(256, 11)
)
self.relu = nn.ReLU()
def forward(self, x):
# 第一层不做加法,但是保存
x1 = self.cnn_layer1(x)
x1 = self.relu(x1)
res1 = x1 # 第一层出来的作为残差记录
# 第二层做加法
x2 = self.cnn_layer2(x1)
x2 = x2 + res1 # 第一次残差操作
x2 = self.relu(x2)
# 第三层不做加法,但是保存
x3 = self.cnn_layer3(x2)
x3 = self.relu(x3)
res3 = x3
# 第四层做加法
x4 = self.cnn_layer4(x3)
x4 = x4 + res3
x4 = self.relu(x4)
# 第五层不做加法,但是保存
x5 = self.cnn_layer5(x4)
x5 = self.relu(x5)
res5 = x5
# 第六层做加法
x6 = self.cnn_layer6(x5)
x6 = x6 + res5
x6 = self.relu(x6)
# 第七层全连接分类
xout = x6.flatten(1) #(256,32,32) --> (256, 32*32)
xout = self.fc_layer(xout)
return xout
验证
随便找个图片数据集load进来就行,这里就不细说了
data_iter = iter(train_loader)
images, labels = next(data_iter) # 取出一个batch
image = images[0] # batch中的第一章图片
print(images.shape)
print(image.shape)
image_np = np.transpose(image.numpy(), (1, 2, 0))
fig, ax = plt.subplots()
ax.imshow(image_np) # 其实是一个tensor
模型实例化
modeltest = MyResNet().to(device)
images, labels = next(data_iter) # 取出一个batch
outs = modeltest(images.to(device))
运行了之后没报错就说明维度的输入输出没问题
写在最后
各位看官,都看到这里了,麻烦动动手指头给博主来个点赞8,您的支持作者最大的创作动力哟!
才疏学浅,若有纰漏,恳请斧正
本文章仅用于各位作为学习交流之用,不作任何商业用途,若涉及版权问题请速与作者联系,望悉知