This repository has been archived on 2023-11-23. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
handwrittenNum/train/VGG16Net.py
2023-11-20 23:53:12 +08:00

69 lines
1.6 KiB
Python

'''
@作者:你遇了我321640253@qq.com
@文件:VGG16Net.py
@创建时间:2023 11 20
模型网络结构
'''
import torch
from torch import nn
from torchsummary import summary
# 定义 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
def getSummary(size:tuple):
model = VGG16()
model = model.to(torch.device('cuda' if torch.cuda.is_available() else 'cpu'))
summary(model, size)