刚听完土哥的入门pytorch,试着写一个不完善的vgg16
from torch import nn
from torch.nn.modules.flatten import Flatten
dilation默认为1,计算第一个卷积的步长和填充:
224 = (224+2xpadding-1x(3-1)-1)/stride + 1
因为padding和stride都为整数,当padding=1,stride=1时,等式成立。
图片为3通道,输出后维64通道,卷积核为3x3,第一个卷积层为:
nn.Conv2d(in_channels=3,out_channels=64,kernel_size=3,stride=1,padding=1),
# 第二个尺寸不变,步长跟填充不变
nn.Conv2d(64,64,3,1,1),
在第一个池化这里,图片长宽都缩小二倍,当池化核为2x2时,取最显著的一个特征可达到这个标准,因此池化核为2,输入长和宽为224,输出长和宽为112,计算池化的步长
112 = (224 + 2*padding - 1x(2-1)-1)/stride + 1
因为padding和stride都为整数,当padding=0,stride=2时,等式成立。padding默认为0可不写
nn.MaxPool2d(kernel_size=2,stride=2)
完整模型代码如下:
from torch import nn
from torch.nn.modules.flatten import Flatten
class Vgg16(nn.Module):
def __init__(self,category):
super(Vgg16,self).__init__()
self.model = nn.Sequential(
nn.Conv2d(in_channels=3,out_channels=64,kernel_size=3,stride=1,padding=1),
nn.Conv2d(64,64,3,1,1),
nn.MaxPool2d(kernel_size=2,stride=2),
nn.Conv2d(64,128,3,1,1),
nn.Conv2d(128,128,3,1,1),
nn.MaxPool2d(2,stride=2),
nn.Conv2d(128,256,3,1,1),
nn.Conv2d(256,256,3,1,1),
nn.Conv2d(256,256,3,1,1),
nn.MaxPool2d(2,stride=2),
nn.Conv2d(256,512,3,1,1),
nn.Conv2d(512,512,3,1,1),
nn.Conv2d(512,512,3,1,1),
nn.MaxPool2d(2,stride=2),
nn.Conv2d(512, 512, 3, 1, 1),
nn.Conv2d(512, 512, 3, 1, 1),
nn.Conv2d(512, 512, 3, 1, 1),
nn.MaxPool2d(2, stride=2),
Flatten(),
nn.Linear(512 * 7 * 7, 4096),
nn.Linear(4096,4096),
nn.Linear(4096, category)
)