o
u
t
p
u
t
_
s
i
z
e
=
?
i
n
p
u
t
_
s
i
z
e
+
2
×
p
a
d
d
i
n
g
?
k
e
r
n
e
l
_
s
i
z
e
s
t
r
i
d
e
?
+
1
output\_size = \lceil \frac{input\_size + 2\times padding - kernel\_size}{stride} \rceil + 1
output_size=?strideinput_size+2×padding?kernel_size??+1
class Classifier(nn.Module):
def __init__(self):
super(Classifier, self).__init__()
self.cnn_layers = nn.Sequential(
nn.Conv2d(3, 64, 3, 1, 1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.MaxPool2d(2, 2, 0),
nn.Conv2d(64, 128, 3, 1, 1),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.MaxPool2d(2, 2, 0),
nn.Conv2d(128, 256, 3, 1, 1),
nn.BatchNorm2d(256),
nn.ReLU(),
nn.MaxPool2d(4, 4, 0),
)
self.fc_layers = nn.Sequential(
nn.Linear(256 * 8 * 8, 256),
nn.ReLU(),
nn.Linear(256, 256),
nn.ReLU(),
nn.Linear(256, 11)
)
def forward(self, x):
x = self.cnn_layers(x)
x = x.flatten(1)
x = self.fc_layers(x)
return x
Tips
-
3
×
3
3 \times 3
3×3的卷积核(步幅为1)不改变图片大小
- flatten()函数作用
|