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

任务描述:

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

python 复制代码
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])
相关推荐
打工仔折腾 AI7 小时前
在 Ubuntu ARM64 部署 Q1 V3.0 MuJoCo 模型:加载、单步推理与执行器响应解析
linux·运维·人工智能·python·深度学习·ubuntu·性能优化
陈嘿萌12 小时前
Fusion Anything Model:面向通用多模态融合的基座模型
图像处理·人工智能·深度学习·计算机视觉·学术研究与理论基础
老鱼说AI13 小时前
SNN脉冲神经网络:全连接、卷积与循环SNN
人工智能·深度学习·神经网络·机器学习·自然语言处理
程序猿编码13 小时前
纯 C++ 手写 LLM 推理引擎:分页 KV 缓存、连续批、多卡张量并行全部内置
c++·深度学习·缓存·大模型推理·自动微分·模型推理
在所不辞兄14 小时前
【零基础学智能仿真-62】从应变—应力计算弹性常数
人工智能·深度学习·神经网络·机器学习·有限元·智能仿真
STLearner14 小时前
KDD 2026 | (2月轮)时空数据(Spatial-Temporal)论文总结时空【下】(人群移动,城市科学(资源调度,)
大数据·人工智能·python·深度学习·机器学习·语言模型·数据挖掘
数聚天成DeepSData15 小时前
第三方地图工具导出的 Landsat 影像,使用条件和 USGS 官方下载的一样吗
人工智能·深度学习·机器学习·数据集·deepsdata
啥都鼓捣的小yao15 小时前
Vision Transformer(ViT)深入浅出
人工智能·深度学习·transformer
计算机毕业编程指导师16 小时前
计算机毕设推荐:基于深度学习的动物图像识别方法研究(Python+Django+PyTorch)
pytorch·python·深度学习·计算机·毕业设计·课程设计·图像识别
X54先生(人文科技)16 小时前
ELR-SELLM_硅基伙伴通过对话碰撞个性涌现的推理架构_v10
人工智能·深度学习·架构·零知识证明