Pytorch深度学习记录:对CIFAR-10的深度学习模型搭建与测试

本文详细记录了使用Pytorch构建深度学习模型处理CIFAR-10数据集的过程,包括数据集介绍、下载与读取、模型搭建(卷积层、池化层、残差网络、全连接层)、训练与测试、模型保存与加载,以及实际测试效果。
摘要由CSDN通过智能技术生成


正在学习深度学习中,主要用于复习与巩固。欢迎大家批评指正,一起讨论学习,进步。

CIFAR-10介绍

The CIFAR-10 and CIFAR-100 are labeled subsets of the 80 million tiny images dataset. They were collected by Alex Krizhevsky, Vinod Nair, and Geoffrey Hinton.
CIFAR-10 和 CIFAR-100 是 8000 万个微小图像数据集的标记子集。它们由 Alex Krizhevsky、Vinod Nair 和 Geoffrey Hinton 收集。
The dataset is divided into five training batches and one test batch, each with 10000 images. The test batch contains exactly 1000 randomly-selected images from each class. The training batches contain the remaining images in random order, but some training batches may contain more images from one class than another. Between them, the training batches contain exactly 5000 images from each class.
数据集分为五个训练批次和一个测试批次,每个批次有 10000 张图像。测试批次恰好包含来自每个类别的 1000 个随机选择的图像。训练批次包含随机顺序的剩余图像,但一些训练批次可能包含来自一个类的图像多于另一个。在它们之间,训练批次恰好包含来自每个类别的 5000 张图像。
Here are the classes in the dataset, as well as 10 random images from each:
以下是数据集中的类,以及每个类的 10 张随机图像
CIFAR-10
The classes are completely mutually exclusive. There is no overlap between automobiles and trucks. “Automobile” includes sedans, SUVs, things of that sort. “Truck” includes only big trucks. Neither includes pickup trucks.
这些类是完全互斥的。汽车和卡车之间没有重叠。“汽车”包括轿车、SUV 之类的东西。“卡车”只包括大卡车。两者都不包括皮卡车。

下载、分类与读入数据集

数据集下载

网页下载:(http://www.cs.toronto.edu/~kriz/cifar.html)
百度网盘连接:链接:https://pan.baidu.com/s/1rwgBPp9fQ33goR77_0sj6Q
提取码:8wpb

包含的文件:
包含的文件

解压与分类数据集

为方便数据处理,将图片按照10个类别存放在各自的文件夹中。
处理后效果图:
分为测试集文件夹和训练集文件夹
分为测试集文件夹和训练集文件夹

测试集和训练集中都包含了10个类别的文件夹
测试集和训练集中都包含了10个类别的文件夹

训练集中每个类别有5000张图片,测试集有1000张
训练集中每个类别有5000张图片,测试集有1000张
代码如下:

import os
from imageio import imsave

def unpickle(file):
    import pickle
    with open(file, 'rb') as fo:
        dict = pickle.load(fo, encoding='bytes')
    return dict


# 训练集
filename = 'cifar-10-batches-py文件夹的路径'  # 图片的路径
meta = unpickle(filename + '/batches.meta')
label_name = meta[b'label_names']
print(label_name) #打印标签

for i in range(len(label_name)):  #建立文件夹train
    file = label_name[i].decode()
    path = '想要建立文件夹train的路径' + file
    isExist = os.path.exists(path)
    if not isExist:
        os.makedirs(path)

for i in range(1, 6):
    content = unpickle(filename + '/data_batch_' + str(i))  # 解压后的每个data_batch_
    for j in range(10000):
        img = content[b'data'][j]
        img = img.reshape(3, 32, 32)
        img = img.transpose(1, 2, 0)
        img_name = '建立文件夹train的路径' + label_name[content[b'labels'][j]].decode() + '/batch_' + str(i) + '_num_' + str(j) + '.jpeg'
        imsave(img_name, img)


# 训练集改名
path = '建立文件夹train的路径'
filelist = os.listdir(path)
for item in filelist:
    pathnew=os.path.join(path,item)
    imagelist = os.listdir(pathnew)
    j = 1
    for
  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值