CIFAR10 資料集分類
阿新 • • 發佈:2020-08-01
import torch import torchvision import torchvision.transforms as transforms import matplotlib.pyplot as plt import numpy as np import torch.nn as nn import torch.nn.functional as F import torch.optim as optim # 使用GPU訓練,可以在選單 "程式碼執行工具" -> "更改執行時型別" 裡進行設定 device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") transform = transforms.Compose( [transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform) trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True, num_workers=2) testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform) testloader = torch.utils.data.DataLoader(testset, batch_size=8, shuffle=False, num_workers=2) classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck') #下面展示 CIFAR10 裡面的一些圖片: def imshow(img): plt.figure(figsize=(8,8)) img = img / 2 + 0.5 # 轉換到 [0,1] 之間 npimg = img.numpy() plt.imshow(np.transpose(npimg, (1, 2, 0))) plt.show() # 得到一組影象 images, labels = iter(trainloader).next() # 展示影象 imshow(torchvision.utils.make_grid(images)) # 展示第一行影象的標籤 for j in range(8): print(classes[labels[j]]) #接下來定義網路,損失函式和優化器 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 # 網路放到GPU上 net = Net().to(device) criterion = nn.CrossEntropyLoss() optimizer = optim.Adam(net.parameters(), lr=0.001) #訓練網路: for epoch in range(10): # 重複多輪訓練 for i, (inputs, labels) in enumerate(trainloader): inputs = inputs.to(device) labels = labels.to(device) # 優化器梯度歸零 optimizer.zero_grad() # 正向傳播 + 反向傳播 + 優化 outputs = net(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step() # 輸出統計資訊 if i % 100 == 0: print('Epoch: %d Minibatch: %5d loss: %.3f' %(epoch + 1, i + 1, loss.item())) print('Finished Training') #現在我們從測試集中取出8張圖片: # 得到一組影象 images, labels = iter(testloader).next() # 展示影象 imshow(torchvision.utils.make_grid(images)) # 展示影象的標籤 for j in range(8): print(classes[labels[j]]) 我們把圖片輸入模型,看看CNN把這些圖片識別成什麼: outputs = net(images.to(device)) _, predicted = torch.max(outputs, 1) # 展示預測的結果 for j in range(8): print(classes[predicted[j]]) #網路在整個資料集上的表現: correct = 0 total = 0 for data in testloader: images, labels = data images, labels = images.to(device), labels.to(device) outputs = net(images) _, predicted = torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted == labels).sum().item() print('Accuracy of the network on the 10000 test images: %d %%' % ( 100 * correct / total))`