【Pytorch】CIFAR-10

# 3 stages + res 85% acc
import torch
import torchvision
import torchvision.transforms as transforms
device = torch.device("cuda:0") # run on GPU
print(device)

transform = transforms.Compose(
    [transforms.RandomHorizontalFlip(), # data augmentation
     transforms.RandomGrayscale(), # data augmentation
     transforms.ToTensor(), # range [0, 255] -> [0.0,1.0]
     transforms.Normalize(
         mean = (0.5,0.5,0.5), # 3 dimension  -mean / st
         std = (0.5,0.5,0.5)) #
     ])

transform1 = transforms.Compose(
    [transforms.ToTensor(), # range [0, 255] -> [0.0,1.0]
     transforms.Normalize(
         mean = (0.5,0.5,0.5), # 3 dimension  -mean / st
         std = (0.5,0.5,0.5)) #
     ])

trainset = torchvision.datasets.CIFAR10(
    root = "../../install_file/", # download file
    train = True, # download training dataset
    download=True, # download
    transform = transform #  range [0, 255] -> [0.0,1.0])
)

#print(trainset.train_data.size())

trainload = torch.utils.data.DataLoader(
    trainset,
    batch_size=100,
    shuffle = True,
    num_workers = 2
)

testset = torchvision.datasets.CIFAR10(
    root = "../../install_file/", # download file
    train = False, # download testing dataset
    download=True, # download
    transform = transform1 #  range [0, 255] -> [0.0,1.0])
)

testload = torch.utils.data.DataLoader(
    testset,
    batch_size=100,
    shuffle = False,
    num_workers = 2
)

classes = ('plan','car','bird','cat','deer','dog','frog','horse','ship','truck')

# show some training image
import matplotlib.pyplot as plt
import numpy as np

def imshow(img):
    img = img /2 +0.5 # [-1,1] to [0,1]
    print(type(img))
    npimg = img.numpy() # tensor to numpy()
    plt.imshow(np.transpose(npimg,(1,2,0))) # C,H,W to H,W,C
    plt.show()
#
# # get some random training images
# dataiter = iter(trainload)
# images, labels = dataiter.next()
# print(images.size()) # torch.Size([4, 3, 32, 32])
# print(labels.size()) # torch.Size([4])

# # show images
# imshow(torchvision.utils.make_grid(images))
# # print labels
# print(' '.join('%5s' % classes[labels[j]] for j in range(4)))

# Building Networks
import torch.nn as nn
import torch.nn.functional as F

class Net(nn.Module):
    def __init__(self):
        super(Net,self).__init__()
        self.conv0 = nn.Conv2d(3, 64, 5, 1, 2)
        self.bn0 = nn.BatchNorm2d(64)
        self.relu0 = nn.ReLU()

        # stage 1  32,32
        self.conv1 = nn.Conv2d(64,64,3, 1, 1)
        self.bn1 = nn.BatchNorm2d(64)
        self.relu1 = nn.ReLU()

        self.conv2 = nn.Conv2d(64,128,1)
        self.bn2 = nn.BatchNorm2d(128)
        self.relu2 = nn.ReLU()

        self.conv3 = nn.Conv2d(128,128,3, 1, 1)
        self.bn3 = nn.BatchNorm2d(128)
        self.relu3 = nn.ReLU()

        self.res1 = nn.Conv2d(64,128,1)
        self.pool1 = nn.MaxPool2d(2,2)

        # stage 2 16,16
        self.conv4 = nn.Conv2d(128, 128, 3, 1, 1)
        self.bn4 = nn.BatchNorm2d(128)
        self.relu4 = nn.ReLU()

        self.conv5 = nn.Conv2d(128, 256, 1)
        self.bn5 = nn.BatchNorm2d(256)
        self.relu5 = nn.ReLU()

        self.conv6 = nn.Conv2d(256, 256, 3, 1, 1)
        self.bn6 = nn.BatchNorm2d(256)
        self.relu6 = nn.ReLU()

        self.res2 = nn.Conv2d(128,256,1)
        self.pool2 = nn.MaxPool2d(2, 2)

        # stage 3  8,8
        self.conv7 = nn.Conv2d(256, 256, 3, 1, 1)
        self.bn7 = nn.BatchNorm2d(256)
        self.relu7 = nn.ReLU()

        self.conv8 = nn.Conv2d(256, 512, 1)
        self.bn8 = nn.BatchNorm2d(512)
        self.relu8 = nn.ReLU()

        self.conv9 = nn.Conv2d(512, 512, 3, 1, 1)
        self.bn9 = nn.BatchNorm2d(512)
        self.relu9 = nn.ReLU()

        self.res3 = nn.Conv2d(256,512,1)
        self.pool3 = nn.MaxPool2d(2, 2)  # 512,4,4

        self.fc1 = nn.Linear(512*4*4,1024) # 120
        self.drop1 = nn.Dropout2d()
        self.fc2 = nn.Linear(1024,84) # 84
        self.drop2 = nn.Dropout2d()
        self.fc3 = nn.Linear(84,10) # 10

    def forward(self,x):
        x = self.relu0(self.bn0(self.conv0(x)))

        # stage 1
        short_cut1 = x
        x = self.relu1(self.bn1(self.conv1(x)))
        x = self.relu2(self.bn2(self.conv2(x)))
        x = self.relu3(self.bn3(self.conv3(x)))
        x = x + self.res1(short_cut1)
        x = self.pool1(x)

        # stage 2
        short_cut2 = x
        x = self.relu4(self.bn4(self.conv4(x)))
        x = self.relu5(self.bn5(self.conv5(x)))
        x = self.relu6(self.bn6(self.conv6(x)))
        x = x + self.res2(short_cut2)
        x = self.pool2(x)

        # stage 3
        short_cut3 = x
        x = self.relu7(self.bn7(self.conv7(x)))
        x = self.relu8(self.bn8(self.conv8(x)))
        x = self.relu9(self.bn9(self.conv9(x)))
        x = x + self.res3(short_cut3)
        x = self.pool3(x)
        x = x.view(x.size(0),-1)

        # fc
        x = self.drop1(self.fc1(x))
        x = self.drop2(self.fc2(x))
        x = self.fc3(x)
        return x

net = Net()
net.to(device)
#print(net)

import torch.optim as optim

criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(),
                      lr = 0.01,
                      momentum=0.9)

for epoch in range(20):
    running_loss = 0.0
    total = 0
    correct = 0
    for i,data in enumerate(trainload,start=0):
        # input
        inputs, labels = data
        inputs,labels = inputs.to(device),labels.to(device)
        # optimize
        optimizer.zero_grad() # 1 清空梯度
        outputs = net(inputs) # 2 前向传播
        loss = criterion(outputs,labels) # 3 计算loss
        loss.backward() # 4 反向传播
        optimizer.step() # 5 更新参数
        running_loss += loss.item()

        if i%250==249: # iteration
            _, predicted = torch.max(outputs.data, 1)
            total += labels.size(0)
            correct += (predicted == labels).sum().item()
            print("epoch:",epoch+1,"iteration:",i+1,"losses:",running_loss /249,"Acc per 2.5w:%",100.0 * correct / total) # mean loss
            running_loss = 0.0 # clear loss
            total = 0
            correct = 0
print('Finished Training')

# compute total acc
correct = 0
total = 0
with torch.no_grad():
    for data in testload:
        images, labels = data
        images, labels = images.to(device), labels.to(device) # run on GPU
        outputs = net(images)
        _, predicted = torch.max(outputs.data, dim=1) # 10 to 1
        total += labels.size(0) # batch size
        correct += (predicted == labels).sum().item()
print('Accuracy of the network on the 10000 test images: %d %%' % (100 * correct / total))

# compute acc of each class
class_correct = list(0. for i in range(10))
class_total = list(0. for i in range(10))
with torch.no_grad():
    for data in testload:
        images, labels = data
        images, labels = images.to(device), labels.to(device)  # run on GPU
        outputs = net(images)
        _, predicted = torch.max(outputs, 1)
        c = (predicted == labels).squeeze()
        for i in range(4):
            label = labels[i]
            class_correct[label] += c[i].item()
            class_total[label] += 1

for i in range(10):
    print('Accuracy of %5s : %2d %%' % (
        classes[i], 100 * class_correct[i] / class_total[i]))

out

epoch: 1 iteration: 500 losses: 1.140420000955283 Acc per 2.5w:% 65.0
...
epoch: 19 iteration: 250 losses: 0.11879985174351189 Acc per 2.5w:% 97.0
epoch: 19 iteration: 500 losses: 0.1169162421416207 Acc per 2.5w:% 98.0
epoch: 20 iteration: 250 losses: 0.09421757892163164 Acc per 2.5w:% 91.0
epoch: 20 iteration: 500 losses: 0.11657897175764702 Acc per 2.5w:% 95.0
Finished Training
Accuracy of the network on the 10000 test images: 85 %
Accuracy of  plan : 88 %
Accuracy of   car : 93 %
Accuracy of  bird : 73 %
Accuracy of   cat : 74 %
Accuracy of  deer : 81 %
Accuracy of   dog : 68 %
Accuracy of  frog : 92 %
Accuracy of horse : 84 %
Accuracy of  ship : 93 %
Accuracy of truck : 93 %

你可能感兴趣的:(pytorch,深度学习,python)