【Pytorch】CIFAR-10

该博客介绍了如何使用PyTorch构建一个ResNet网络,并应用于CIFAR10数据集进行图像分类。通过数据增强、归一化等预处理步骤,模型在训练集上达到98%的准确率,并最终在测试集上获得85%的准确率。
摘要由CSDN通过智能技术生成
# 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训练CIFAR-10数据集,可以按照以下步骤进行操作: 1. 导入所需的PyTorch库: ```python import torch import torchvision import torchvision.transforms as transforms import torch.nn as nn import torch.optim as optim ``` 2. 定义训练和测试数据集的转换方式: ```python transform = transforms.Compose( [transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) ``` 3. 加载CIFAR-10数据集并创建数据加载器: ```python trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform) trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, 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=4, shuffle=False, num_workers=2) ``` 4. 定义神经网络模型: ```python 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() ``` 5. 定义损失函数和优化器: ```python criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9) ``` 6. 进行模型的训练和测试: ```python for epoch in range(2): running_loss = 0.0 for i, data in enumerate(trainloader, 0): # 获取输入 inputs, labels = data # 梯度清零 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') # 在测试集上测试模型 correct = 0 total = 0 with torch.no_grad(): for data in testloader: images, labels = data 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: %.2f %%' % (100 * correct / total)) ``` 通过以上步骤,我们可以使用PyTorch训练CIFAR-10数据集,并获得模型的准确率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值