初始化手写数字识别

This commit is contained in:
2023-11-20 17:34:04 +08:00
commit 3a76ff507f
8 changed files with 241 additions and 0 deletions

54
train/VGG16Net.py Normal file
View File

@@ -0,0 +1,54 @@
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