1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
""" @author: zj @file: tensorboard-fashion-mnist.py @time: 2019-12-11 """
import matplotlib.pyplot as plt import numpy as np
import torch import torch.nn as nn import torch.nn.functional as F import torch.optim as optim
import torchvision import torchvision.transforms as transforms import torchvision.utils
learning_rate = 1e-3 moment = 0.9 epoches = 50 bsize = 256
classes = ('T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle Boot')
def load_data(bsize): transform = transforms.Compose( [transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])
trainset = torchvision.datasets.FashionMNIST('./data', download=True, train=True, transform=transform) testset = torchvision.datasets.FashionMNIST('./data', download=True, train=False, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=bsize, shuffle=True, num_workers=2)
testloader = torch.utils.data.DataLoader(testset, batch_size=bsize, shuffle=False, num_workers=2) return trainloader, testloader
class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.conv1 = nn.Conv2d(1, 6, 5) self.pool = nn.MaxPool2d(2, 2) self.conv2 = nn.Conv2d(6, 16, 5) self.fc1 = nn.Linear(16 * 4 * 4, 120) self.fc2 = nn.Linear(120, 84) self.fc3 = nn.Linear(84, 10)
def forward(self, x): x = self.pool(F.relu(self.conv1(x))) x = self.pool(F.relu(self.conv2(x))) x = x.view(-1, 16 * 4 * 4) x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x = self.fc3(x) return x
def compute_accuracy(loader, net, device): total_accu = 0.0 num = 0
for i, data in enumerate(loader, 0): inputs, labels = data[0].to(device), data[1].to(device)
outputs = net.forward(inputs) predicted = torch.argmax(outputs, dim=1) total_accu += torch.mean((predicted == labels).float()).item() num += 1 return total_accu / num
def draw(values, xlabel, ylabel, title, label): fig = plt.figure() plt.plot(list(range(len(values))), values, label=label)
plt.xlabel(xlabel) plt.ylabel(ylabel) plt.title(title)
plt.legend() plt.show()
def train(trainloader, testloader, net, criterion, optimizer, device): train_accu_list = list() test_accu_list = list() loss_list = list()
for epoch in range(epoches): num = 0 running_loss = 0.0 for i, data in enumerate(trainloader, 0): inputs, labels = data[0].to(device), data[1].to(device)
optimizer.zero_grad()
outputs = net(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step()
running_loss += loss.item() num += 1 avg_loss = running_loss / num print('[%d] loss: %.4f' % (epoch + 1, avg_loss)) loss_list.append(avg_loss)
train_accu = compute_accuracy(trainloader, net, device) test_accu = compute_accuracy(testloader, net, device) print('train: %.4f, test: %.4f' % (train_accu, test_accu)) train_accu_list.append(train_accu) test_accu_list.append(test_accu)
print('Finished Training') return train_accu_list, test_accu_list, loss_list
if __name__ == '__main__': device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") print(device)
net = Net().to(device) criterion = nn.CrossEntropyLoss().to(device) optimizer = optim.SGD(net.parameters(), lr=learning_rate, momentum=moment)
trainloader, testloader = load_data(bsize)
train_accu_list, test_accu_list, loss_list = train(trainloader, testloader, net, criterion, optimizer, device)
draw(train_accu_list, 'epoch', 'accuracy', 'train accuracy', 'fashion-mnist') draw(test_accu_list, 'epoch', 'accuracy', 'test accuracy', 'fashion-mnist') draw(loss_list, 'epoch', 'loss_value', 'loss', 'fashion-mnist')
|