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 x

net=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=data


        image=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.data

        if i%100==99:
            end=time.time()
            print ('[epoch %d,imgs %5d] loss: %.7f  time: %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,imgs  1600] loss: 2.2890975  time: 9.980 s
[epoch 1,imgs  3200] loss: 1.4525503  time: 9.179 s
[epoch 1,imgs  4800] loss: 1.0446196  time: 9.155 s
[epoch 1,imgs  6400] loss: 0.8922504  time: 8.961 s
[epoch 1,imgs  8000] loss: 0.7806346  time: 9.075 s
[epoch 1,imgs  9600] loss: 0.7174604  time: 9.016 s
[epoch 1,imgs 11200] loss: 0.6114922  time: 9.016 s
[epoch 1,imgs 12800] loss: 0.5857614  time: 8.901 s
[epoch 1,imgs 14400] loss: 0.6223359  time: 8.949 s
[epoch 1,imgs 16000] loss: 0.5584998  time: 9.084 s
[epoch 1,imgs 17600] loss: 0.5521011  time: 9.180 s
[epoch 1,imgs 19200] loss: 0.5479669  time: 8.993 s
[epoch 1,imgs 20800] loss: 0.5314963  time: 9.064 s
[epoch 1,imgs 22400] loss: 0.4544642  time: 9.003 s
[epoch 1,imgs 24000] loss: 0.5179688  time: 8.966 s
[epoch 1,imgs 25600] loss: 0.5091115  time: 8.922 s
[epoch 1,imgs 27200] loss: 0.4726944  time: 8.930 s
[epoch 1,imgs 28800] loss: 0.5053027  time: 9.014 s
[epoch 1,imgs 30400] loss: 0.4166897  time: 9.020 s
[epoch 1,imgs 32000] loss: 0.4328879  time: 8.932 s
[epoch 1,imgs 33600] loss: 0.4253058  time: 9.403 s
[epoch 1,imgs 35200] loss: 0.4359113  time: 9.122 s
[epoch 1,imgs 36800] loss: 0.4193576  time: 8.914 s
[epoch 1,imgs 38400] loss: 0.4524114  time: 8.944 s
[epoch 1,imgs 40000] loss: 0.4164613  time: 8.953 s
[epoch 1,imgs 41600] loss: 0.4382341  time: 8.880 s
[epoch 1,imgs 43200] loss: 0.4314862  time: 8.910 s
[epoch 1,imgs 44800] loss: 0.4143034  time: 8.950 s
[epoch 1,imgs 46400] loss: 0.3816758  time: 8.916 s
[epoch 1,imgs 48000] loss: 0.4256237  time: 8.906 s
[epoch 1,imgs 49600] loss: 0.4051017  time: 8.911 s
[epoch 1,imgs 51200] loss: 0.4079942  time: 8.884 s
[epoch 1,imgs 52800] loss: 0.3776795  time: 9.175 s
[epoch 1,imgs 54400] loss: 0.4000866  time: 9.351 s
[epoch 1,imgs 56000] loss: 0.3899635  time: 8.957 s
[epoch 1,imgs 57600] loss: 0.3561532  time: 8.880 s
[epoch 1,imgs 59200] loss: 0.3521189  time: 8.901 s
[epoch 2,imgs  1600] loss: 0.3371298  time: 8.953 s
[epoch 2,imgs  3200] loss: 0.3809072  time: 8.903 s
[epoch 2,imgs  4800] loss: 0.2906542  time: 9.140 s
[epoch 2,imgs  6400] loss: 0.3422534  time: 9.130 s
[epoch 2,imgs  8000] loss: 0.3366346  time: 9.583 s
[epoch 2,imgs  9600] loss: 0.4095851  time: 9.004 s
[epoch 2,imgs 11200] loss: 0.3683361  time: 9.139 s
[epoch 2,imgs 12800] loss: 0.3670321  time: 9.033 s
[epoch 2,imgs 14400] loss: 0.3675788  time: 8.967 s
[epoch 2,imgs 16000] loss: 0.3839977  time: 8.878 s
[epoch 2,imgs 17600] loss: 0.3414059  time: 8.880 s
[epoch 2,imgs 19200] loss: 0.3568817  time: 8.951 s
[epoch 2,imgs 20800] loss: 0.3301966  time: 8.942 s
[epoch 2,imgs 22400] loss: 0.3844147  time: 9.034 s
[epoch 2,imgs 24000] loss: 0.3546369  time: 9.124 s
[epoch 2,imgs 25600] loss: 0.3212983  time: 8.872 s
[epoch 2,imgs 27200] loss: 0.3141496  time: 8.929 s
[epoch 2,imgs 28800] loss: 0.3500620  time: 8.922 s
[epoch 2,imgs 30400] loss: 0.3502237  time: 8.876 s
[epoch 2,imgs 32000] loss: 0.3444326  time: 8.936 s
[epoch 2,imgs 33600] loss: 0.3662793  time: 8.989 s
[epoch 2,imgs 35200] loss: 0.3541445  time: 8.896 s
[epoch 2,imgs 36800] loss: 0.3400903  time: 8.894 s
[epoch 2,imgs 38400] loss: 0.3303362  time: 9.109 s
[epoch 2,imgs 40000] loss: 0.3685826  time: 9.480 s
[epoch 2,imgs 41600] loss: 0.3493139  time: 8.906 s
[epoch 2,imgs 43200] loss: 0.3210229  time: 8.934 s
[epoch 2,imgs 44800] loss: 0.2959242  time: 8.987 s
[epoch 2,imgs 46400] loss: 0.3419413  time: 8.979 s
[epoch 2,imgs 48000] loss: 0.3301732  time: 8.961 s
[epoch 2,imgs 49600] loss: 0.2846430  time: 8.878 s
[epoch 2,imgs 51200] loss: 0.3187753  time: 8.916 s
[epoch 2,imgs 52800] loss: 0.3046340  time: 8.920 s
[epoch 2,imgs 54400] loss: 0.3499675  time: 8.881 s
[epoch 2,imgs 56000] loss: 0.3251576  time: 8.873 s
[epoch 2,imgs 57600] loss: 0.2728085  time: 8.892 s
[epoch 2,imgs 59200] loss: 0.2839503  time: 8.897 s
[epoch 3,imgs  1600] loss: 0.2816468  time: 8.991 s
[epoch 3,imgs  3200] loss: 0.2831629  time: 8.936 s
[epoch 3,imgs  4800] loss: 0.3210972  time: 8.939 s
[epoch 3,imgs  6400] loss: 0.3047401  time: 8.878 s
[epoch 3,imgs  8000] loss: 0.3169303  time: 8.941 s
[epoch 3,imgs  9600] loss: 0.2817588  time: 8.871 s
[epoch 3,imgs 11200] loss: 0.3128562  time: 8.899 s
[epoch 3,imgs 12800] loss: 0.3000189  time: 8.913 s
[epoch 3,imgs 14400] loss: 0.3094940  time: 8.886 s
[epoch 3,imgs 16000] loss: 0.2587585  time: 8.901 s
[epoch 3,imgs 17600] loss: 0.3190380  time: 8.899 s
[epoch 3,imgs 19200] loss: 0.2923077  time: 8.905 s
[epoch 3,imgs 20800] loss: 0.3032117  time: 8.879 s
[epoch 3,imgs 22400] loss: 0.2899254  time: 8.890 s
[epoch 3,imgs 24000] loss: 0.2929463  time: 9.043 s
[epoch 3,imgs 25600] loss: 0.3146794  time: 9.392 s
[epoch 3,imgs 27200] loss: 0.2543717  time: 9.057 s
[epoch 3,imgs 28800] loss: 0.2957610  time: 8.926 s
[epoch 3,imgs 30400] loss: 0.2982574  time: 8.904 s
[epoch 3,imgs 32000] loss: 0.2745237  time: 8.939 s
[epoch 3,imgs 33600] loss: 0.3175772  time: 8.882 s
[epoch 3,imgs 35200] loss: 0.2485971  time: 8.887 s
[epoch 3,imgs 36800] loss: 0.2745326  time: 8.884 s
[epoch 3,imgs 38400] loss: 0.2902154  time: 8.884 s
[epoch 3,imgs 40000] loss: 0.2942073  time: 8.919 s
[epoch 3,imgs 41600] loss: 0.2801945  time: 9.017 s
[epoch 3,imgs 43200] loss: 0.2783984  time: 8.896 s
[epoch 3,imgs 44800] loss: 0.3430609  time: 8.900 s
[epoch 3,imgs 46400] loss: 0.2901186  time: 8.953 s
[epoch 3,imgs 48000] loss: 0.2836992  time: 8.894 s
[epoch 3,imgs 49600] loss: 0.2810960  time: 8.876 s
[epoch 3,imgs 51200] loss: 0.3076264  time: 8.876 s
[epoch 3,imgs 52800] loss: 0.2853616  time: 8.881 s
[epoch 3,imgs 54400] loss: 0.2660266  time: 8.896 s
[epoch 3,imgs 56000] loss: 0.2867737  time: 8.903 s
[epoch 3,imgs 57600] loss: 0.2866637  time: 8.893 s
[epoch 3,imgs 59200] loss: 0.2618496  time: 8.872 s
finish training
Accuracy of the network on the 10000 test images: 89 %

你可能感兴趣的:(Pytorch AlexNet Fashion-MNIST)