CIFAR10图像识别
import torch
import torch.nn as nn
import torchvision
from torchvision import transforms, datasets
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5))
])
trainset = datasets.CIFAR10(root = './CIFAR10', train = True, download = True, transform = transform)
testset = datasets.CIFAR10(root = '.CIFAR', train = False, download = True, transform = transform)
from torch.utils.data.dataloader import DataLoader
BATCH_SIZE = 4
train_loader = DataLoader(trainset, batch_size = BATCH_SIZE, shuffle = True, pin_memory = True)
test_loader = DataLoader(testset, batch_size = BATCH_SIZE, shuffle = True, pin_memory = True)
import matplotlib.pyplot as plt
import numpy as np
classes = ('plane', 'car', 'bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck')
def imshow(img):
img = img / 2 + 0.5
np_img = img.numpy()
plt.imshow(np.transpose(np_img, (1,2,0)))
plt.show()
imgs,labs = next(iter(train_loader))
print(imgs.shape)
print(labs.shape)
imshow(torchvision.utils.make_grid(imgs))
print(' '.join(' %4s' % classes[labs[i]] for i in range(BATCH_SIZE)))
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.pool = nn.MaxPool2d(2, 2)
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 = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 16*5*5)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
net = Net().to('cpu')
import torch.optim as optim
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr = 0.001, momentum = 0.9)
EPOCHS = 1
for epoch in range(EPOCHS):
train_loss = 0.0
for i, (datas, labels) in enumerate(train_loader):
datas, labels = datas.to('cpu'), labels.to('cpu')
optimizer.zero_grad()
outputs = net(datas)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
train_loss += loss.item()
print('Epoch : %d, Batch: %5d, Loss: %.3f'%
(epoch + 1, i + 1, train_loss/len(train_loader.dataset)))
PATH = './cifar_net.pth'
torch.save(net.state_dict(), PATH)
model = Net()
model.load_state_dict(torch.load(PATH))
correct = 0
total = 0
flag = True
with torch.no_grad():
for i, (datas, labels) in enumerate(test_loader):
outputs = model(datas)
_, predicted = torch.max(outputs.data, dim = 1)
total += labels.size(0)
correct += (predicted == labels).sum()
print('在10000张测试集图片上的准确率为: {:.3f}%'.format(correct / total *100))
class_correct = list(0. for i in range(10))
total = list(0. for i in range(10))
with torch.no_grad():
for (image, labels) in test_loader:
outputs = model(datas)
_, predicted = torch.max(outputs, dim = 1)
c = (predicted == labels).squeeze()
if labels.shape[0] == 4:
for i in range(BATCH_SIZE):
label = labels[i]
class_correct[label] += c[i].item()
total[label] += 1
for i in range(10):
print('正确率{:%5s : %2d %%}'% (classes[i], 100 * class_correct[i] / total[i]))