55 lines
1.3 KiB
Python
55 lines
1.3 KiB
Python
from torch import nn
|
|
from torch.utils.data import DataLoader
|
|
|
|
# 定义 VGG16 网络结构
|
|
class VGG16(nn.Module):
|
|
def __init__(self):
|
|
super(VGG16, self).__init__()
|
|
|
|
self.conv1 = nn.Sequential(
|
|
#32*1*28*28
|
|
nn.Conv2d(1, 16, kernel_size=3, padding=1),
|
|
#16*28*28
|
|
nn.ReLU(),
|
|
nn.Conv2d(16, 16, kernel_size=3, padding=1),
|
|
#16*28*28
|
|
nn.ReLU()
|
|
)
|
|
|
|
self.conv2 = nn.Sequential(
|
|
nn.Conv2d(16, 32, kernel_size=3, padding=1),
|
|
nn.ReLU(),
|
|
nn.Conv2d(32, 32, kernel_size=3, padding=1),
|
|
nn.ReLU()
|
|
)
|
|
|
|
self.conv3 = nn.Sequential(
|
|
nn.Conv2d(32, 64, kernel_size=3, padding=1),
|
|
nn.ReLU(),
|
|
nn.Conv2d(64, 64, kernel_size=3, padding=1),
|
|
nn.ReLU(),
|
|
nn.Conv2d(64, 64, kernel_size=3, padding=1),
|
|
nn.ReLU()
|
|
)
|
|
|
|
self.conv4 = nn.Sequential(
|
|
nn.Conv2d(64, 128, kernel_size=3, padding=1),
|
|
nn.ReLU(),
|
|
nn.Conv2d(128, 128, kernel_size=3, padding=1),
|
|
nn.ReLU()
|
|
)
|
|
|
|
self.fc = nn.Linear(128*28*28, 100)
|
|
self.fc1 = nn.Linear(100, 10)
|
|
|
|
def forward(self, x):
|
|
x = self.conv1(x)
|
|
x = self.conv2(x)
|
|
x = self.conv3(x)
|
|
x = self.conv4(x)
|
|
x = nn.Flatten()(x)
|
|
x = self.fc(x)
|
|
x = self.fc1(x)
|
|
return x
|
|
|