自学内容网 自学内容网

【pytorch深度学习】CIFAR10图像分类

任务描述:

通过简单的自定义神经网络,实现CIFAR10数据集图像分类任务

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.utils
import torch.utils.data
import torchvision
import torchvision.transforms as transforms
import torch.optim as optim

import matplotlib.pyplot as plt
import numpy as np

def show_img(img):
    """显示图片
    """
    img = img/2 + 0.5
    npimg = img.numpy()
    plt.imshow(np.transpose(npimg, (1,2,0)))
    plt.show()

# torchvision输出的是PILImage, 值的范围是[0, 1]
# 我们将其转化为张量数据, 并归一化为[-1, 1]
transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(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=4, shuffle=False, num_workers=2)
classes = ["plane", "car", "brid", "cat", "deer", "dog", "frog", "horse", "ship", "truck"]

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        # 输入为3通道, 输出为6通道, 卷积核为5
        self.conv1 = nn.Conv2d(3,6,5)
        # 输入为6通道,输出为16通道,卷积核为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(-1, self.num_flat_features(x))
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))

        x = self.fc3(x)

        return x
    
    def num_flat_features(self, x):
        size = x.size()[1:]
        num_features = 1
        for s in size:
            num_features *= s
        
        return num_features

net = Net()
# 交叉熵损失函数
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)

if __name__ == "__main__":
    for epoch in range(1):
        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(epoch+1, i+1, running_loss/2000)
                running_loss = 0
        
    print("Finished Training")

    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)

    correct = 0
    total = 0 

    with torch.no_grad():
        for data in testloader:
            images, labels = data
            outputs = net(images)

            value, predicted = torch.max(outputs.data, 1)
            total += labels.size(0)
            correct += (predicted==labels).sum()
    
    print(correct/total)

    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 testloader:
            images, labels = data
            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(classes[i], 100*class_correct[i]/class_total[i])


原文地址:https://blog.csdn.net/qq_42761751/article/details/142930305

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!