Pytorch VGG Fashion-Mnist

from __future__ import print_function
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=4

##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
def vgg_block(num_convs, in_channels, num_channels):
    layers=[]
    for i in range(num_convs):
        layers+=[nn.Conv2d(in_channels=in_channels, out_channels=num_channels, kernel_size=3, padding=1)]
        in_channels=num_channels
    layers +=[nn.ReLU()]
    layers +=[nn.MaxPool2d(kernel_size=2, stride=2)]
    return nn.Sequential(*layers)

class VGG(nn.Module):
    def __init__(self):
        super(VGG,self).__init__()
        self.conv_arch=((1,1,64),(1,64,128),(2,128,256),(2,256,512),(2,512,512))
        layers=[]
        for (num_convs,in_channels,num_channels) in self.conv_arch:
            layers+=[vgg_block(num_convs,in_channels,num_channels)]
        self.features=nn.Sequential(*layers)
        self.dense1 = nn.Linear(512*7*7,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.features(x)
        x=x.view(-1,512*7*7)
        x=self.dense3(self.drop2(F.relu(self.dense2(self.drop1(F.relu(self.dense1(x)))))))
        return x

net=VGG().cuda()
print (net)
criterion=nn.CrossEntropyLoss()
optimizer=optim.SGD(net.parameters(),lr=0.05,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))

运行过程

VGG(
  (features): Sequential(
    (0): Sequential(
      (0): Conv2d(1, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
      (1): ReLU()
      (2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    )
    (1): Sequential(
      (0): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
      (1): ReLU()
      (2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    )
    (2): Sequential(
      (0): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
      (1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
      (2): ReLU()
      (3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    )
    (3): Sequential(
      (0): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
      (1): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
      (2): ReLU()
      (3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    )
    (4): Sequential(
      (0): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
      (1): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
      (2): ReLU()
      (3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    )
  )
  (dense1): Linear(in_features=25088, 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  3200] loss: 2.3026707  time: 15.394 s
[epoch 1,imgs  6400] loss: 2.3014610  time: 15.450 s
[epoch 1,imgs  9600] loss: 1.8131558  time: 15.473 s
[epoch 1,imgs 12800] loss: 0.8434033  time: 15.501 s
[epoch 1,imgs 16000] loss: 0.6466572  time: 15.528 s
[epoch 1,imgs 19200] loss: 0.6216396  time: 15.553 s
[epoch 1,imgs 22400] loss: 0.5298573  time: 15.532 s
[epoch 1,imgs 25600] loss: 0.4837729  time: 15.555 s
[epoch 1,imgs 28800] loss: 0.4647788  time: 15.555 s
[epoch 1,imgs 32000] loss: 0.4379662  time: 15.596 s
[epoch 1,imgs 35200] loss: 0.4372665  time: 15.597 s
[epoch 1,imgs 38400] loss: 0.3839526  time: 15.572 s
[epoch 1,imgs 41600] loss: 0.3818478  time: 15.589 s
[epoch 1,imgs 44800] loss: 0.3983816  time: 15.584 s
[epoch 1,imgs 48000] loss: 0.4151393  time: 15.595 s
[epoch 1,imgs 51200] loss: 0.3526680  time: 15.594 s
[epoch 1,imgs 54400] loss: 0.3504642  time: 15.565 s
[epoch 1,imgs 57600] loss: 0.3589660  time: 15.579 s
[epoch 2,imgs  3200] loss: 0.3329561  time: 15.724 s
[epoch 2,imgs  6400] loss: 0.2850565  time: 15.577 s
[epoch 2,imgs  9600] loss: 0.3067477  time: 15.597 s
[epoch 2,imgs 12800] loss: 0.2949037  time: 15.579 s
[epoch 2,imgs 16000] loss: 0.3086796  time: 15.568 s
[epoch 2,imgs 19200] loss: 0.3252580  time: 15.565 s
[epoch 2,imgs 22400] loss: 0.3126533  time: 15.550 s
[epoch 2,imgs 25600] loss: 0.3175748  time: 15.579 s
[epoch 2,imgs 28800] loss: 0.3038767  time: 15.582 s
[epoch 2,imgs 32000] loss: 0.2744634  time: 15.568 s
[epoch 2,imgs 35200] loss: 0.2927028  time: 15.548 s
[epoch 2,imgs 38400] loss: 0.2745134  time: 15.587 s
[epoch 2,imgs 41600] loss: 0.2694345  time: 15.557 s
[epoch 2,imgs 44800] loss: 0.2710356  time: 15.534 s
[epoch 2,imgs 48000] loss: 0.2731565  time: 15.559 s
[epoch 2,imgs 51200] loss: 0.2908022  time: 15.573 s
[epoch 2,imgs 54400] loss: 0.3037302  time: 15.540 s
[epoch 2,imgs 57600] loss: 0.2780806  time: 15.531 s
[epoch 3,imgs  3200] loss: 0.2490531  time: 15.697 s
[epoch 3,imgs  6400] loss: 0.2555809  time: 15.526 s
[epoch 3,imgs  9600] loss: 0.2373066  time: 15.553 s
[epoch 3,imgs 12800] loss: 0.2285895  time: 15.538 s
[epoch 3,imgs 16000] loss: 0.2293788  time: 15.551 s
[epoch 3,imgs 19200] loss: 0.2417339  time: 15.542 s
[epoch 3,imgs 22400] loss: 0.2406960  time: 15.545 s
[epoch 3,imgs 25600] loss: 0.2292875  time: 15.546 s
[epoch 3,imgs 28800] loss: 0.2601902  time: 15.511 s
[epoch 3,imgs 32000] loss: 0.2164138  time: 15.534 s
[epoch 3,imgs 35200] loss: 0.2401492  time: 15.528 s
[epoch 3,imgs 38400] loss: 0.2482483  time: 15.533 s
[epoch 3,imgs 41600] loss: 0.2268627  time: 15.540 s
[epoch 3,imgs 44800] loss: 0.2333435  time: 15.540 s
[epoch 3,imgs 48000] loss: 0.2614851  time: 15.522 s
[epoch 3,imgs 51200] loss: 0.2280996  time: 15.531 s
[epoch 3,imgs 54400] loss: 0.2277545  time: 15.527 s
[epoch 3,imgs 57600] loss: 0.2422604  time: 15.505 s
finish training
Accuracy of the network on the 10000 test images: 91 %

你可能感兴趣的:(Pytorch VGG Fashion-Mnist)