CNN 卷积层输出计算
Posted ^_^|
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CNN 卷积层输出计算相关的知识,希望对你有一定的参考价值。
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__()
# The arguments for commonly used modules:
# torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding)
# torch.nn.MaxPool2d(kernel_size, stride, padding)
# input image size: [3, 128, 128]
# Conv2d/MaxPool2d compute: [(input_size + 2*padding - kernel_size)/stride]+1 []中内容向上取整
self.cnn_layers = nn.Sequential(
nn.Conv2d(3, 64, 3, 1, 1), # (128+2-3)/1+1=128
nn.BatchNorm2d(64),
nn.ReLU(),
nn.MaxPool2d(2, 2, 0), # (128+0-2)/2+1=64
nn.Conv2d(64, 128, 3, 1, 1), # (64+2-3)/1+1=64
nn.BatchNorm2d(128),
nn.ReLU(),
nn.MaxPool2d(2, 2, 0), # (64+0-2)/2+1=32
nn.Conv2d(128, 256, 3, 1, 1), # (32+2-3)/1+1=32
nn.BatchNorm2d(256),
nn.ReLU(),
nn.MaxPool2d(4, 4, 0), # (32+0-4)/4+1=8
)
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):
# input (x): [batch_size, 3, 128, 128]
# output: [batch_size, 11]
# Extract features by convolutional layers.
x = self.cnn_layers(x)
# The extracted feature map must be flatten before going to fully-connected layers.
x = x.flatten(1)
# The features are transformed by fully-connected layers to obtain the final logits.
x = self.fc_layers(x)
return x
Tips
- 3 × 3 3 \\times 3 3×3的卷积核(步幅为1)不改变图片大小
- flatten()函数作用
以上是关于CNN 卷积层输出计算的主要内容,如果未能解决你的问题,请参考以下文章