Pytorch|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 xnet=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=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))

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

    推荐阅读