Pytorch|Pytorch AlexNet Fashion-MNIST

pytorch 实现 AlexNet on Fashion-MNIST

from __future__ import print_function import cv2 import torch import time import torch.nn as nn import torch.nn.functional as F import torchvision import torchvision.transforms as transforms from torch import optim from torch.autograd import Variable from torch.utils.data import DataLoader from torchvision.transforms import ToPILImage show=ToPILImage() import numpy as np import matplotlib.pyplot as plt# batchSize=16##load data transform = transforms.Compose([transforms.Resize(224),transforms.ToTensor(),transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),])trainset = torchvision.datasets.FashionMNIST(root='./data', train=True, download=True, transform=transform) trainloader = torch.utils.data.DataLoader(trainset, batch_size=batchSize, shuffle=True, num_workers=0)testset = torchvision.datasets.FashionMNIST(root='./data', train=False, download=True, transform=transform) testloader = torch.utils.data.DataLoader(testset, batch_size=batchSize, shuffle=False, num_workers=0)classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')def imshow(img): img = img / 2 + 0.5 npimg = img.numpy() plt.imshow(np.transpose(npimg, (1, 2, 0)))####network class AlexNet(nn.Module): def __init__(self): super(AlexNet,self).__init__() self.conv1 = nn.Conv2d(in_channels=1,out_channels=96,kernel_size=11,stride=4) self.pool1 = nn.MaxPool2d(kernel_size=3,stride=2) self.conv2 = nn.Conv2d(in_channels=96,out_channels=256,kernel_size=5,padding=2) self.pool2 = nn.MaxPool2d(kernel_size=3, stride=2) self.conv3 = nn.Conv2d(in_channels=256,out_channels=384,kernel_size=3,padding=1) self.conv4 = nn.Conv2d(in_channels=384, out_channels=384, kernel_size=3, padding=1) self.conv5 = nn.Conv2d(in_channels=384, out_channels=256, kernel_size=3, padding=1) self.pool3 = nn.MaxPool2d(kernel_size=3, stride=2) self.dense1 = nn.Linear(256*5*5,4096) self.drop1 = nn.Dropout(0.5) self.dense2 = nn.Linear(4096,4096) self.drop2 = nn.Dropout(0.5) self.dense3 = nn.Linear(4096,10)def forward(self,x): x=self.pool1(F.relu(self.conv1(x))) x=self.pool2(F.relu(self.conv2(x))) x=self.pool3(F.relu(self.conv5(F.relu(self.conv4(F.relu(self.conv3(x))))))) x=x.view(-1,256*5*5) x=self.dense3(self.drop2(F.relu(self.dense2(self.drop1(F.relu(self.dense1(x))))))) return xnet=AlexNet().cuda() print (net) criterion=nn.CrossEntropyLoss() optimizer=optim.SGD(net.parameters(),lr=0.01,momentum=0.9)#train print ("training begin") for epoch in range(3): start = time.time() running_loss=0 for i,data in enumerate(trainloader,0): # print (inputs,labels) image,label=dataimage=image.cuda() label=label.cuda() image=Variable(image) label=Variable(label)# imshow(torchvision.utils.make_grid(image)) # plt.show() # print (label) optimizer.zero_grad()# print (image.shape) outputs=net(image) # print (outputs) loss=criterion(outputs,label)loss.backward() optimizer.step()running_loss+=loss.dataif i%100==99: end=time.time() print ('[epoch %d,imgs %5d] loss: %.7ftime: %0.3f s'%(epoch+1,(i+1)*16,running_loss/100,(end-start))) start=time.time() running_loss=0 print ("finish training")#test net.eval() correct=0 total=0 for data in testloader: images,labels=data images=images.cuda() labels=labels.cuda() outputs=net(Variable(images)) _,predicted=torch.max(outputs,1) total+=labels.size(0) correct+=(predicted==labels).sum() print('Accuracy of the network on the %d test images: %d %%' % (total , 100 * correct / total))

运行结果,包含model结构和training过程
AlexNet( (conv1): Conv2d(1, 96, kernel_size=(11, 11), stride=(4, 4)) (pool1): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False) (conv2): Conv2d(96, 256, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2)) (pool2): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False) (conv3): Conv2d(256, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) (conv4): Conv2d(384, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) (conv5): Conv2d(384, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) (pool3): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False) (dense1): Linear(in_features=6400, out_features=4096, bias=True) (drop1): Dropout(p=0.5) (dense2): Linear(in_features=4096, out_features=4096, bias=True) (drop2): Dropout(p=0.5) (dense3): Linear(in_features=4096, out_features=10, bias=True) ) training begin [epoch 1,imgs1600] loss: 2.2890975time: 9.980 s [epoch 1,imgs3200] loss: 1.4525503time: 9.179 s [epoch 1,imgs4800] loss: 1.0446196time: 9.155 s [epoch 1,imgs6400] loss: 0.8922504time: 8.961 s [epoch 1,imgs8000] loss: 0.7806346time: 9.075 s [epoch 1,imgs9600] loss: 0.7174604time: 9.016 s [epoch 1,imgs 11200] loss: 0.6114922time: 9.016 s [epoch 1,imgs 12800] loss: 0.5857614time: 8.901 s [epoch 1,imgs 14400] loss: 0.6223359time: 8.949 s [epoch 1,imgs 16000] loss: 0.5584998time: 9.084 s [epoch 1,imgs 17600] loss: 0.5521011time: 9.180 s [epoch 1,imgs 19200] loss: 0.5479669time: 8.993 s [epoch 1,imgs 20800] loss: 0.5314963time: 9.064 s [epoch 1,imgs 22400] loss: 0.4544642time: 9.003 s [epoch 1,imgs 24000] loss: 0.5179688time: 8.966 s [epoch 1,imgs 25600] loss: 0.5091115time: 8.922 s [epoch 1,imgs 27200] loss: 0.4726944time: 8.930 s [epoch 1,imgs 28800] loss: 0.5053027time: 9.014 s [epoch 1,imgs 30400] loss: 0.4166897time: 9.020 s [epoch 1,imgs 32000] loss: 0.4328879time: 8.932 s [epoch 1,imgs 33600] loss: 0.4253058time: 9.403 s [epoch 1,imgs 35200] loss: 0.4359113time: 9.122 s [epoch 1,imgs 36800] loss: 0.4193576time: 8.914 s [epoch 1,imgs 38400] loss: 0.4524114time: 8.944 s [epoch 1,imgs 40000] loss: 0.4164613time: 8.953 s [epoch 1,imgs 41600] loss: 0.4382341time: 8.880 s [epoch 1,imgs 43200] loss: 0.4314862time: 8.910 s [epoch 1,imgs 44800] loss: 0.4143034time: 8.950 s [epoch 1,imgs 46400] loss: 0.3816758time: 8.916 s [epoch 1,imgs 48000] loss: 0.4256237time: 8.906 s [epoch 1,imgs 49600] loss: 0.4051017time: 8.911 s [epoch 1,imgs 51200] loss: 0.4079942time: 8.884 s [epoch 1,imgs 52800] loss: 0.3776795time: 9.175 s [epoch 1,imgs 54400] loss: 0.4000866time: 9.351 s [epoch 1,imgs 56000] loss: 0.3899635time: 8.957 s [epoch 1,imgs 57600] loss: 0.3561532time: 8.880 s [epoch 1,imgs 59200] loss: 0.3521189time: 8.901 s [epoch 2,imgs1600] loss: 0.3371298time: 8.953 s [epoch 2,imgs3200] loss: 0.3809072time: 8.903 s [epoch 2,imgs4800] loss: 0.2906542time: 9.140 s [epoch 2,imgs6400] loss: 0.3422534time: 9.130 s [epoch 2,imgs8000] loss: 0.3366346time: 9.583 s [epoch 2,imgs9600] loss: 0.4095851time: 9.004 s [epoch 2,imgs 11200] loss: 0.3683361time: 9.139 s [epoch 2,imgs 12800] loss: 0.3670321time: 9.033 s [epoch 2,imgs 14400] loss: 0.3675788time: 8.967 s [epoch 2,imgs 16000] loss: 0.3839977time: 8.878 s [epoch 2,imgs 17600] loss: 0.3414059time: 8.880 s [epoch 2,imgs 19200] loss: 0.3568817time: 8.951 s [epoch 2,imgs 20800] loss: 0.3301966time: 8.942 s [epoch 2,imgs 22400] loss: 0.3844147time: 9.034 s [epoch 2,imgs 24000] loss: 0.3546369time: 9.124 s [epoch 2,imgs 25600] loss: 0.3212983time: 8.872 s [epoch 2,imgs 27200] loss: 0.3141496time: 8.929 s [epoch 2,imgs 28800] loss: 0.3500620time: 8.922 s [epoch 2,imgs 30400] loss: 0.3502237time: 8.876 s [epoch 2,imgs 32000] loss: 0.3444326time: 8.936 s [epoch 2,imgs 33600] loss: 0.3662793time: 8.989 s [epoch 2,imgs 35200] loss: 0.3541445time: 8.896 s [epoch 2,imgs 36800] loss: 0.3400903time: 8.894 s [epoch 2,imgs 38400] loss: 0.3303362time: 9.109 s [epoch 2,imgs 40000] loss: 0.3685826time: 9.480 s [epoch 2,imgs 41600] loss: 0.3493139time: 8.906 s [epoch 2,imgs 43200] loss: 0.3210229time: 8.934 s [epoch 2,imgs 44800] loss: 0.2959242time: 8.987 s [epoch 2,imgs 46400] loss: 0.3419413time: 8.979 s [epoch 2,imgs 48000] loss: 0.3301732time: 8.961 s [epoch 2,imgs 49600] loss: 0.2846430time: 8.878 s [epoch 2,imgs 51200] loss: 0.3187753time: 8.916 s [epoch 2,imgs 52800] loss: 0.3046340time: 8.920 s [epoch 2,imgs 54400] loss: 0.3499675time: 8.881 s [epoch 2,imgs 56000] loss: 0.3251576time: 8.873 s [epoch 2,imgs 57600] loss: 0.2728085time: 8.892 s [epoch 2,imgs 59200] loss: 0.2839503time: 8.897 s [epoch 3,imgs1600] loss: 0.2816468time: 8.991 s [epoch 3,imgs3200] loss: 0.2831629time: 8.936 s [epoch 3,imgs4800] loss: 0.3210972time: 8.939 s [epoch 3,imgs6400] loss: 0.3047401time: 8.878 s [epoch 3,imgs8000] loss: 0.3169303time: 8.941 s [epoch 3,imgs9600] loss: 0.2817588time: 8.871 s [epoch 3,imgs 11200] loss: 0.3128562time: 8.899 s [epoch 3,imgs 12800] loss: 0.3000189time: 8.913 s [epoch 3,imgs 14400] loss: 0.3094940time: 8.886 s [epoch 3,imgs 16000] loss: 0.2587585time: 8.901 s [epoch 3,imgs 17600] loss: 0.3190380time: 8.899 s [epoch 3,imgs 19200] loss: 0.2923077time: 8.905 s [epoch 3,imgs 20800] loss: 0.3032117time: 8.879 s [epoch 3,imgs 22400] loss: 0.2899254time: 8.890 s [epoch 3,imgs 24000] loss: 0.2929463time: 9.043 s [epoch 3,imgs 25600] loss: 0.3146794time: 9.392 s [epoch 3,imgs 27200] loss: 0.2543717time: 9.057 s [epoch 3,imgs 28800] loss: 0.2957610time: 8.926 s [epoch 3,imgs 30400] loss: 0.2982574time: 8.904 s [epoch 3,imgs 32000] loss: 0.2745237time: 8.939 s [epoch 3,imgs 33600] loss: 0.3175772time: 8.882 s [epoch 3,imgs 35200] loss: 0.2485971time: 8.887 s [epoch 3,imgs 36800] loss: 0.2745326time: 8.884 s [epoch 3,imgs 38400] loss: 0.2902154time: 8.884 s [epoch 3,imgs 40000] loss: 0.2942073time: 8.919 s [epoch 3,imgs 41600] loss: 0.2801945time: 9.017 s [epoch 3,imgs 43200] loss: 0.2783984time: 8.896 s [epoch 3,imgs 44800] loss: 0.3430609time: 8.900 s [epoch 3,imgs 46400] loss: 0.2901186time: 8.953 s [epoch 3,imgs 48000] loss: 0.2836992time: 8.894 s [epoch 3,imgs 49600] loss: 0.2810960time: 8.876 s [epoch 3,imgs 51200] loss: 0.3076264time: 8.876 s [epoch 3,imgs 52800] loss: 0.2853616time: 8.881 s [epoch 3,imgs 54400] loss: 0.2660266time: 8.896 s [epoch 3,imgs 56000] loss: 0.2867737time: 8.903 s [epoch 3,imgs 57600] loss: 0.2866637time: 8.893 s [epoch 3,imgs 59200] loss: 0.2618496time: 8.872 s finish training Accuracy of the network on the 10000 test images: 89 %

    推荐阅读