import torch as t
import torchvision as tv
import torchvision.transforms as transforms
from torchvision.transforms import ToPILImage
show = ToPILImage()
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5)),
])
trainset = tv.datasets.CIFAR10(root = '/home/cy/data/',
train = True,
download = True,
transform = transform)
trainloader = t.utils.data.DataLoader(trainset,
batch_size = 4,
shuffle = True)
testset = tv.datasets.CIFAR10('/home/cy/data/',
train = False,
download = True,
transform = transform)
testloader = t.utils.data.DataLoader(testset,
batch_size = 4,
shuffle = False)
classes = ('plane','car','bird','cat','deer','dog','frog','horse','ship','truck')
Files already downloaded and verified
Files already downloaded and verified
(data,label) = trainset[100]
print(classes[label])
show((data+1)/2).resize((100,100))
ship
dataiter = iter(trainloader)
images,labels = dataiter.next()
print(' '.join('%11s'%classes[labels[j]] for j in range(4)))
show(tv.utils.make_grid((images+1)/2)).resize((400,100))
car plane truck dog
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net,self).__init__()
self.conv1 = nn.Conv2d(3,6,5)
self.conv2 = nn.Conv2d(6,16,5)
self.fc1 = nn.Linear(16*5*5,120)
self.fc2 = nn.Linear(120,84)
self.fc3 = nn.Linear(84,10)
def forward(self,x):
x = F.max_pool2d(F.relu(self.conv1(x)),(2,2))
x = F.max_pool2d(F.relu(self.conv2(x)),2)
x = x.view(x.size()[0],-1)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
net = Net()
print(net)
Net(
(conv1): Conv2d(3, 6, kernel_size=(5, 5), stride=(1, 1))
(conv2): Conv2d(6, 16, kernel_size=(5, 5), stride=(1, 1))
(fc1): Linear(in_features=400, out_features=120, bias=True)
(fc2): Linear(in_features=120, out_features=84, bias=True)
(fc3): Linear(in_features=84, out_features=10, bias=True)
)
from torch import optim
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(),lr=0.001,momentum=0.9)
from torch.autograd import Variable
for epoch in range(2):
running_loss = 0.0
for i,data in enumerate(trainloader,0):
inputs,labels = data
inputs,labels = Variable(inputs),Variable(labels)
optimizer.zero_grad()
outputs = net(inputs)
loss = criterion(outputs,labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
if i % 2000 == 1999:
print('[%d,%5d] loss: %.3f' % (epoch+1,i+1,running_loss/2000))
running_loss = 0.0
print('Finished Training')
[1, 2000] loss: 2.233
[1, 4000] loss: 1.905
[1, 6000] loss: 1.692
[1, 8000] loss: 1.621
[1,10000] loss: 1.523
[1,12000] loss: 1.482
[2, 2000] loss: 1.449
[2, 4000] loss: 1.387
[2, 6000] loss: 1.366
[2, 8000] loss: 1.341
[2,10000] loss: 1.326
[2,12000] loss: 1.284
Finished Training
dataiter = iter(testloader)
images,labels = dataiter.next()
print('实际的label: ',' '.join('%08s'%classes[labels[j]] for j in range(4)))
show(tv.utils.make_grid(images/2-0.5)).resize((400,100))
实际的label: cat ship ship plane
outputs = net(Variable(images))
_,predicted = t.max(outputs.data,1)
print('预测结果: ',' '.join('%5s'%classes[predicted[j]] for j in range(4)))
预测结果: cat ship ship ship
correct = 0
total = 0
for data in testloader:
images,labels = data
outputs = net(Variable(images))
_,predicted = t.max(outputs.data,1)
total += labels.size(0)
correct += (predicted == labels).sum()
print('10000张测试集中的准确率为: %d %%' % (100*correct//total))
10000张测试集中的准确率为: 53 %
if t.cuda.is_available():
net.cuda()
images = images.cuda()
labels = labels.cuda()
output = net(Variable(images))
loss = criterion(output,Variable(labels))