import torch
import torch.nn as nn
from torchvision import transforms, datasets
from torch.utils.data import DataLoader
import torch.nn.functional as F
import matplotlib.pyplot as plt
batch_size = 64
correct_list = []
loss_list = []
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
train_dataset = datasets.MNIST(root="dataset/mnist",
train=True,
download=True,
transform=transform)
train_loader = DataLoader(dataset=train_dataset,
shuffle=True,
batch_size=batch_size)
test_dataset = datasets.MNIST(root="dataset/mnist",
train=False,
download=True,
transform=transform)
test_loader = DataLoader(dataset=test_dataset,
shuffle=False,
batch_size=batch_size)
class Inception(nn.Module):
def __init__(self, in_channels):
super(Inception, self).__init__()
self.branch_pool = nn.Conv2d(in_channels=in_channels,
out_channels=24,
kernel_size=1)
self.branch1x1_1 = nn.Conv2d(in_channels=in_channels,
out_channels=16,
kernel_size=1)
self.branch1x1_2 = nn.Conv2d(in_channels=in_channels,
out_channels=16,
kernel_size=1)
self.branch1x1_3 = nn.Conv2d(in_channels=in_channels,
out_channels=16,
kernel_size=1)
self.branch5x5 = nn.Conv2d(in_channels=16,
out_channels=24,
kernel_size=5,
padding=2)
self.branch3x3_1 = nn.Conv2d(in_channels=16,
out_channels=24,
kernel_size=3,
padding=1)
self.branch3x3_2 = nn.Conv2d(in_channels=24,
out_channels=24,
kernel_size=3,
padding=1)
def forward(self, x):
branch_pool = F.avg_pool2d(x, kernel_size=3,
padding=1,
stride=1)
branch_pool = self.branch_pool(branch_pool)
branch1x1 = self.branch1x1_1(x)
branch5x5 = self.branch1x1_2(x)
branch5x5 = self.branch5x5(branch5x5)
branch3x3 = self.branch1x1_3(x)
branch3x3 = self.branch3x3_1(branch3x3)
branch3x3 = self.branch3x3_2(branch3x3)
outputs = [branch_pool, branch1x1, branch5x5, branch3x3]
return torch.cat(outputs, dim=1)
class GoogleNet(nn.Module):
def __init__(self):
super(GoogleNet, self).__init__()
self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
self.conv2 = nn.Conv2d(88, 20, kernel_size=5)
self.incep1 = Inception(in_channels=10)
self.incep2 = Inception(in_channels=20)
self.mp = nn.MaxPool2d(2)
self.fc = nn.Linear(1408, 10)
def forward(self, x):
in_size = x.size(0)
x = F.relu(self.mp(self.conv1(x)))
x = self.incep1(x)
x = F.relu(self.mp(self.conv2(x)))
x = self.incep2(x)
x = x.view(in_size, -1)
x = self.fc(x)
return x
model = GoogleNet()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.to(device)
# construct loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.5)
# training cycle forward, backward, update
def train(epoch):
running_loss = 0.0
for batch_idx, data in enumerate(train_loader, 0):
inputs, target = data
inputs, target = inputs.to(device), target.to(device)
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, target)
loss.backward()
optimizer.step()
running_loss += loss.item()
if batch_idx % 300 == 299:
print('[%d, %5d] loss: %.3f %%' % (epoch + 1, batch_idx + 1, running_loss / 300))
loss_list.append(running_loss / 300)
running_loss = 0.0
def test():
correct = 0
total = 0
with torch.no_grad():
for data in test_loader:
images, labels = data
images, labels = images.to(device), labels.to(device)
y_hat = model(images)
_, predicted = torch.max(y_hat.data, dim=1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Accuracy on test set: %.3f %%' % (100 * correct / total))
correct_list.append(correct)
if __name__ == '__main__':
for epoch in range(10):
train(epoch)
test()
plt.plot(range(len(loss_list)), loss_list)
plt.xlabel("update times")
plt.ylabel("Loss")
plt.show()```