# -*- coding:utf-8 -*-
"""
开发时间:2024/8/7 15:54
@author:xd
"""
import torch
from torch import nn
import torch.nn.functional as F
class Inception(nn.Module):
def __init__(self, in_channels, ch1x1, ch3x3red, ch3x3, ch5x5red, ch5x5, ch_pool):
super(Inception, self).__init__()
self.branch1 = nn.Sequential(
nn.Conv2d(in_channels, ch1x1, kernel_size=1),
nn.ReLU()
)
self.branch2 = nn.Sequential(
nn.Conv2d(in_channels, ch3x3red, kernel_size=1),
nn.ReLU(),
nn.Conv2d(ch3x3red, ch3x3, kernel_size=3, stride=1, padding=1),
nn.ReLU()
)
self.branch3 = nn.Sequential(
nn.Conv2d(in_channels, ch5x5red, kernel_size=1),
nn.ReLU(),
nn.Conv2d(ch5x5red, ch5x5, kernel_size=5, stride=1, padding=2),
nn.ReLU()
)
self.branch4 = nn.Sequential(
nn.MaxPool2d(3, 1, 1),
nn.Conv2d(in_channels, ch_pool, kernel_size=1),
nn.ReLU()
)
def forward(self, x):
branch1 = self.branch1(x)
branch2 = self.branch2(x)
branch3 = self.branch3(x)
branch4 = self.branch4(x)
x = torch.cat([branch1, branch2, branch3, branch4], dim=1)
return x
class Aux_Classifier(nn.Module):
def __init__(self, in_channels, num_classes):
super(Aux_Classifier, self).__init__()
self.averagepool = nn.AvgPool2d(kernel_size=5, stride=3)
self.conv = nn.Conv2d(in_channels, 128, kernel_size=1)
self.fc1 = nn.Linear(128 * 4 * 4, 1024)
self.fc2 = nn.Linear(1024, num_classes)
self.softmax = nn.Softmax(dim=1)
def forward(self, x):
x = self.averagepool(x)
x = F.relu(self.conv(x))
x = torch.flatten(x, 1)
x = F.relu(self.fc1(x))
x = self.fc2(x)
x = self.softmax(x)
return x
class GoogleNet(nn.Module):
def __init__(self, num_classes=1000, is_train=True):
super(GoogleNet, self).__init__()
self.is_train = is_train
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3)
self.max_pool1 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.conv2 = nn.Conv2d(64, 64, kernel_size=1)
self.conv3 = nn.Conv2d(64, 192, kernel_size=3, stride=1, padding=1)
self.max_pool2 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.inception1 = Inception(192, 64, 96, 128, 16, 32, 32)
self.inception2 = Inception(256, 128, 128, 192, 32, 96, 64)
self.max_pool3 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.inception3 = Inception(480, 192, 96, 208, 16, 48, 64)
self.aux_classifier1 = Aux_Classifier(512, num_classes)
self.inception4 = Inception(512, 160, 112, 224, 24, 64, 64)
self.inception5 = Inception(512, 128, 128, 256, 24, 64, 64)
self.inception6 = Inception(512, 112, 144, 288, 32, 64, 64)
self.aux_classifier2 = Aux_Classifier(528, num_classes)
self.inception7 = Inception(528, 256, 160, 320, 32, 128, 128)
self.max_pool4 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.inception8 = Inception(832, 256, 160, 320, 32, 128, 128)
self.inception9 = Inception(832, 384, 192, 384, 48, 128, 128)
self.avg_pool = nn.AvgPool2d(kernel_size=7, stride=1)
self.dropout = nn.Dropout(0.4)
self.fc = nn.Linear(1024, num_classes)
self.softmax = nn.Softmax(dim=1)
def forward(self, x):
x = F.relu(self.conv1(x))
x = self.max_pool1(x)
x = F.relu(self.conv2(x))
x = F.relu(self.conv3(x))
x = self.max_pool2(x)
x = self.inception1(x)
x = self.inception2(x)
x = self.max_pool3(x)
x = self.inception3(x)
if self.is_train:
aux1 = self.aux_classifier1(x)
x = self.inception4(x)
x = self.inception5(x)
x = self.inception6(x)
if self.is_train:
aux2 = self.aux_classifier2(x)
x = self.inception7(x)
x = self.max_pool4(x)
x = self.inception8(x)
x = self.inception9(x)
x = self.avg_pool(x)
x = self.dropout(x)
x = torch.flatten(x, 1)
x = self.fc(x)
x = self.softmax(x)
if self.is_train:
return aux1, aux2, x
return x
def test():
is_train = True
model = GoogleNet(num_classes=10, is_train=is_train)
print(model)
input = torch.zeros((1, 3, 224, 224))
if is_train:
aux1, aux2, x = model(input)
print(aux1, aux2, x)
else:
x = model(input)
print(x)
if __name__ == "__main__":
test()
GoogleNet代码复现
最新推荐文章于 2024-09-10 19:20:56 发布