《PyTorch深度学习快速入门教程》笔记

PyTorch深度学习快速入门教程(绝对通俗易懂!)【小土堆】_哔哩哔哩_bilibiliicon-default.png?t=M3K6https://www.bilibili.com/video/BV1hE411t7RN?p=1

目录

一、准备工作

1、环境配置

2、两个辅助函数

3、三种编程方式

二、数据相关

1、Dataset的使用 - 创建数据集

2、tensorboard的使用 - 可视化

3、transforms的使用 - 图片处理

4、torchvision的使用 - 获取外部数据集

5、dataloader的使用 - 加载数据

三、模型相关

1、Module的使用 - 模型框架

2、convolution layers卷积层 - 特征提取

3、 pooling layers池化层 - 特征降维

4、 non-linear activations非线性激活 - 非线性变换

5、linear layers线性层 - 线性变换

6、sequential贯序模型 - 序列化操作

四、评估及优化

1、损失函数

2、优化器 optimizer

五、实战运行

1、外部模型的使用

2、模型训练流程

3、GPU的使用

4、训练模型测试


一、准备工作

1、环境配置

      anaconda + pycharm + pytorch(以下教程更详细)

人工智能新手环境搭建指南anaconda+pytorch+pycharm_哔哩哔哩_bilibiliicon-default.png?t=M3K6https://www.bilibili.com/video/BV1Kp4y147Rw?spm_id_from=333.337.top_right_bar_window_custom_collection.content.click

2、两个辅助函数

      dir()  获取包内文件

      help()  获取方法说明,方法不加括号

# python console中运行
In[2]:import torch
In[3]:dir(torch)  # 获取torch中文件列表
In[4]:help(torch)  # 获取torch详细解释

3、三种编程方式

      python文件:所有行为一块,一次全部运行

      python console:每一行为一块,回车运行

      jupyter:任意行为一块,Shift + Enter 运行

二、数据相关

1、Dataset的使用 - 创建数据集

      from torch.utils.data import Dataset

      DataSet  提供一种方式去获取数据及其label

      Dataloader  为后续提供不同的数据形式

      coding:自定义数据集的实现        

from torch.utils.data import Dataset
from PIL import Image
import os


class MyData(Dataset):
    def __init__(self, root_dir, label_dir):
        self.root_dir = root_dir
        self.label_dir = label_dir
        self.path = os.path.join(self.root_dir, self.label_dir)  # 拼接路径
        self.image_path = os.listdir(self.path)  # 文件夹生成列表

    def __getitem__(self, idx):
        img_name = self.image_path[idx]
        img_item_path = os.path.join(self.path, img_name)  # 图像路径
        img = Image.open(img_item_path)  # 读取图像
        label = self.label_dir
        return img, label

    def __len__(self):
        return len(self.image_path)


root_dir = "dataset/train"
ants_label_dir = "ants"
bees_label_dir = "bees"
ants_dataset = MyData(root_dir, ants_label_dir)
bees_dataset = MyData(root_dir, bees_label_dir)
train_dataset = ants_dataset + bees_dataset

2、tensorboard的使用 - 可视化

      from torch.utils.tensorboard import SummaryWriter

      .add_image(title,图像,step,dataformat)  tensorboard中添加图像

      .add_scalar(标题,x轴,y轴)  tensorboard中添加曲线

      coding:add_image、add_scalar的实现

from torch.utils.tensorboard import SummaryWriter
import numpy as np
from PIL import Image

writer = SummaryWriter("logs")  # 指定事务文件夹

img_path = "dataset2/train/ants_image/0013035.jpg"
img_PIL = Image.open(img_path)
img_array = np.array(img_PIL)

writer.add_image("test", img_array, 1, dataformats='HWC')

for i in range(100):
    writer.add_scalar("y=3x", 3*i, i)

writer.close()
# Terminal中获取可视化网址
(pytorch) D:\22\pytorch>tensorboard --logdir=logs --port=6007

3、transforms的使用 - 图片处理

      from torchvision import transforms

      .ToTensor()  将图像转换为tensor类

      .Normalize((RGB均值), (RGB方差))  归一化到[-1,1],缩小数据间差距

      .Resize( (高,宽) )  调整图片尺寸

      .Compose( [操作1,操作2] )  将多个操作整合为列表,传入图像进行操作

      .RandomCrop(边长)  随机裁剪

       注:图片格式

              Image.open()  PIL格式

              cv2.imread()  narrys格式

              transforms.ToTensor()  tensor格式

      coding:各个方法的使用

from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms

# __call__()通过 对象名(参数) 调用
# 普通函数通过 对象名.方法名(参数) 调用

writer = SummaryWriter("logs")
img = Image.open("dataset2/train/ants_image/0013035.jpg")

# ToTensor
trans_to_tensor = transforms.ToTensor()
img_tensor = trans_to_tensor(img)
writer.add_image("ToTensor", img_tensor)

# Normalize
trans_norm = transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
img_norm = trans_norm(img_tensor)  # 传入tensor类型
writer.add_image("Normalize", img_norm)

# Resize
trans_resize = transforms.Resize((512, 512))
img_resize = trans_resize(img)  # 传入PIL类型,返回PIL类型
img_resize = trans_to_tensor(img_resize)
writer.add_image("Resize", img_resize)

# Compose
trans_resize2 = transforms.Resize(512)
trans_comp = transforms.Compose([trans_resize2, trans_to_tensor])  # 传入实例列表
img_resize2 = trans_comp(img)  # 公共参数
writer.add_image("Resize", img_resize2, 1)

# RandomCrop
trans_random = transforms.RandomCrop(512)
trans_comp2 = transforms.Compose([trans_random, trans_to_tensor])
for i in range(10):
    img_random = trans_comp2(img)
    writer.add_image("RandomCrop", img_random, i)

writer.close()

4、torchvision的使用 - 获取外部数据集

      import torchvision

      torchvision.datasets.数据集名( 路径,类型,transform操作,下载)  获取数据集

      coding:获取CIFAR10数据集

import torchvision
import ssl
from torch.utils.tensorboard import SummaryWriter

# 解决证书过期问题
ssl._create_default_https_context = ssl._create_unverified_context  

# 定义Compose中的transform操作
dataset_transform = torchvision.transforms.Compose([torchvision.transforms.ToTensor(), ])

# 获取训练集和测试集,进行transform操作
train_set = torchvision.datasets.CIFAR10("./dataset3", train=True, transform=dataset_transform, download=True)
test_set = torchvision.datasets.CIFAR10("./dataset3", train=False, transform=dataset_transform, download=True)

"""
# 输出格式
print(test_set[0])
print(test_set.classes)

img, target = test_set[0]
print(img)
print(target)
print(test_set.classes[target])
img.show()"""

# 可视化
writer = SummaryWriter("logs")
for i in range(10):
    img, target = train_set[i]
    writer.add_image("torchvision_dataset", img, i)
writer.close()

5、dataloader的使用 - 加载数据

      from torch.utils.data import DataLoader

      DataLoader(数据集, 每批数量, 打乱次序, 并发数量, 删除余数)  批量加载数据

      coding:批量加载CIFAR10数据集 并可视化

import torchvision
from torch.utils.data import DataLoader

# 获取数据集
from torch.utils.tensorboard import SummaryWriter

test_data = torchvision.datasets.CIFAR10("./dataset3", train=True, transform=torchvision.transforms.ToTensor(), download=True)
# 加载数据
data_loader = DataLoader(test_data, batch_size=64, shuffle=True, num_workers=0, drop_last=False)

# 输出图像信息
img, target = test_data[0]
print(img.shape)
print(target)

# 图像可视化
writer = SummaryWriter("logs")
step = 0
for data in data_loader:
    imgs, targets = data
    # print(imgs.shape)
    # print(targets)
    writer.add_images("dataloader", imgs, step)
    step = step+1

三、模型相关

1、Module的使用 - 模型框架

      from torch import nn

      class Model(nn.Moudle):  继承实现,重写init和forward方法

      注:类调用过程

              __init__()  创建类对象

              __call__()  使类对象可以直接调用call中的方法,而不是 类名.方法名

              forward()  在__call__()中被调用,故给类对象传参可直接调用forward()方法

      coding:深度学习模型 框架实现

import torch
from torch import nn


class Models(nn.Module):

    def __init__(self) -> None:
        super().__init__()

    def forward(self, input):
        output = input + 1
        return output


models = Models()
x = torch.tensor(1.0)
output = models(x)
print(output)

2、convolution layers卷积层 - 特征提取

      from torch.nn import Conv2d 

      Conv2d(in_channels=3, out_channels=6, kernel_size=3, stride=1, padding=0)  卷积计算

      注:torch.reshanpe(img,(-1, 3, 32, 32))  修改图像形状

      coding:卷积函数的理解

import torch
import torch.nn.functional as F

# 输入图像
input = torch.tensor([[1, 2, 0, 3, 1], [0, 1, 2, 3, 1], [1, 2, 1, 0, 0],
                      [5, 2, 3, 1, 1], [2, 1, 0, 1, 1]])
# 卷积核
kernel = torch.tensor([[1, 2, 1], [0, 1, 0], [2, 1, 0]])

# 尺寸变换
input = torch.reshape(input, (1, 1, 5, 5))
kernel = torch.reshape(kernel, (1, 1, 3, 3))

# 卷积
output = F.conv2d(input, kernel, stride=1, padding=1)
print(output)

      coding:深度学习模型内实现卷积层 并可视化

import torch
import torchvision
from torch import nn
from torch.nn import Conv2d
from torch.utils.data import DataLoader

# 获取数据集 dataset
from torch.utils.tensorboard import SummaryWriter

dataset = torchvision.datasets.CIFAR10("./dataset3", train=False,
                                       transform=torchvision.transforms.ToTensor(), download=True)
# 加载数据 dataloader
dataloader = DataLoader(dataset, batch_size=64)


# 深度学习模型内实现卷积
class Mode(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        # 卷积
        self.conv1 = Conv2d(in_channels=3, out_channels=6, kernel_size=3, stride=1, padding=0)  # 类的实例

    def forward(self, input):
        input = self.conv1(input)
        return input


# 提取特征并可视化
writer = SummaryWriter("logs")
modes = Mode()  # 创建实例
step = 0
for data in dataloader:
    imgs, targets = data
    output = modes(imgs)  # 调用forward
    writer.add_images("input", imgs, step)
    output = torch.reshape(output, [-1, 3, 30, 30])
    writer.add_images("output", output, step)
    step = step+1
writer.close()

3、 pooling layers池化层 - 特征降维

      from torch.nn import MaxPool2d

      MaxPool2d(kernel_size=3, ceil_mode=True)  最大池化计算

      coding:深度学习模型内实现池化层 并可视化

import torchvision
from torch import nn
from torch.nn import MaxPool2d
from torch.utils.data import DataLoader

from torch.utils.tensorboard import SummaryWriter

# 数据集
dataset = torchvision.datasets.CIFAR10("./dataset3", train=False,
                                       transform=torchvision.transforms.ToTensor(), download=True)
# 加载数据
dataloader = DataLoader(dataset, batch_size=64)


# 深度学习模型实现池化
class Mode(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.maxpool1 = MaxPool2d(kernel_size=3, ceil_mode=True)

    def forward(self, x):
        x = self.maxpool1(x)
        return x


# 特征降维并可视化
modes = Mode()
writer = SummaryWriter("logs")
step = 0
for data in dataloader:
    imgs, targets = data
    output = modes(imgs)
    writer.add_images("input", imgs, step)
    writer.add_images("output", output, step)
    step += 1

writer.close()

4、 non-linear activations非线性激活 - 非线性变换

      from torch.nn import Sigmoid,ReLU

      ReLU()、Sigmoid()  非线性函数,使模型具有拟合任意函数的能力

      coding:深度学习模型内实现非线性激活层 并可视化

import torchvision
from torch import nn
from torch.nn import ReLU, Sigmoid
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

dataset = torchvision.datasets.CIFAR10("./dataset3", train=False,
                                       transform=torchvision.transforms.ToTensor(), download=True)
dataloader = DataLoader(dataset, batch_size=64)


# 非线性激活
class Nonlinear(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        # self.relu1 = ReLU()
        self.sigmoid1 = Sigmoid()

    def forward(self, x):
        # x = self.relu1(x)
        x = self.sigmoid1(x)
        return x


writer = SummaryWriter("logs")
nonlinear = Nonlinear()
step = 0
for data in dataloader:
    imgs, tragets = data
    output = nonlinear(imgs)
    writer.add_images("input", imgs, step)
    writer.add_images("output", output, step)
    step += 1
writer.close()

5、linear layers线性层 - 线性变换

      from torch.nn import Linear

      Linear(196600, 10)  线性变换

      注:torch.flatten(imgs)  扁平化图像到一维

      coding:深度学习模型内实现线性层 并可视化

import torch
import torchvision
from torch import nn
from torch.nn import Linear
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

dataset = torchvision.datasets.CIFAR10("./dataset3", train=False,
                                       transform=torchvision.transforms.ToTensor(), download=True)
dataloader = DataLoader(dataset, batch_size=64, drop_last=True)


# 线性激活
class Mode(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.linear1 = Linear(196600, 10)

    def forward(self, input):
        output = self.linear1(input)
        return output


# 线性变换并可视化
writer = SummaryWriter("logs")
mode = Mode()
step = 0
for data in dataset:
    imgs, targets = data
    output= torch.flatten(imgs)
    output = mode(output)
    writer.add_images("input", imgs, step)
    writer.add_images("output", output, step)
    step += 1
writer.close()

6、sequential贯序模型 - 序列化操作

      from torch.nn import Sequential

      Sequential( Conv2d(3, 32, 5, padding=2), MaxPool2d(2), )  序列化操作

      注:torch.ones(64, 3, 32, 32)  生成全1图像

      coding:深度学习模型内实现序列化操作 并可视化

import torch
import torchvision
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

dataset = torchvision.datasets.CIFAR10("./dataset3", train=False,
                                       transform=torchvision.transforms.ToTensor(), download=True)
dataloader = DataLoader(dataset, batch_size=64, drop_last=True)


"""
# 模型没有实现Sequential()
class Mode(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.conv1 = Conv2d(3,32,kernel_size=5,padding=2, stride=1)  # 卷积
        self.maxpool1 = MaxPool2d(2)  # 池化
        self.conv2 = Conv2d(32, 32, kernel_size=5, padding=2)  # 卷积
        self.maxpool2 = MaxPool2d(2)  # 池化
        self.conv3 = Conv2d(32, 64, kernel_size=5, padding=2)  # 卷积
        self.maxpool3 = MaxPool2d(2)  # 池化
        self.flatten = Flatten()  # 扁平化
        self.linear1 = Linear(1024, 64)  # 线性化
        self.linear2 = Linear(64, 10)  # 线性化

    def forward(self, x):
        x = self.conv1(x)
        x = self.maxpool1(x)
        x = self.conv2(x)
        x = self.maxpool2(x)
        x = self.conv3(x)
        x = self.maxpool3(x)
        x = self.flatten(x)
        x = self.linear1(x)
        x = self.linear2(x)
        return x"""


# 模型实现Sequential()
class Mode(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.mode1 = Sequential(
            Conv2d(3, 32, kernel_size=5, padding=2, stride=1),
            MaxPool2d(2),
            Conv2d(32, 32, kernel_size=5, padding=2),
            MaxPool2d(2),
            Conv2d(32, 64, kernel_size=5, padding=2),
            MaxPool2d(2),
            Flatten(),
            Linear(1024, 64),
            Linear(64, 10)
        )

    def forward(self, x):
        x = self.mode1(x)
        return x


# 序列化操作并可视化
mode = Mode()
input = torch.ones(64, 3, 32, 32)
output = mode(input)
print(output.shape)
writer = SummaryWriter("logs")
writer.add_graph(mode, input)
writer.close()

四、评估及优化

1、损失函数

      from torch.nn import L1Loss

      L1Loss(reduction='sum')  计算损失值(均值或总和)

      from torch.nn import MSELoss

      MSELoss()  均方损失

      from torch.nn import CrossEntropyLoss

      CrossEntropyLoss()  交叉熵损失,实际输出与目标之间的差距

      backward()  反向传播,计算出梯度用于优化

      coding:不同损失函数的使用

import torch
from torch.nn import L1Loss, MSELoss, CrossEntropyLoss

input = torch.tensor([1, 2, 3], dtype=torch.float32)
target = torch.tensor([1, 2, 5], dtype=torch.float32)

input = torch.reshape(input, (1, 1, 1, 3))  # BCHW
target = torch.reshape(target, (1, 1, 1, 3))

# L1Loss
loss = L1Loss(reduction='sum')
result = loss(input, target)
print(result)

# MSELoss
loss_mse = MSELoss()
reslut_mse = loss_mse(input, target)
print(result)

# CrossEntropyLoss  未匹配时较大,匹配时较小
x = torch.tensor([0.1, 0.2, 0.3])
y = torch.tensor([1])
x = torch.reshape(x, (1, 3))
loss_cross = CrossEntropyLoss()
result_cross = loss_cross(x, y)
print(result_cross)

      coding:损失函数的实现

import torchvision

from torch import nn
from torch.nn import Sequential, Conv2d, MaxPool2d, Flatten, Linear
from torch.utils.data import DataLoader

# dataset
dataset = torchvision.datasets.CIFAR10("./dataset3", train=False,transform=torchvision.transforms.ToTensor(), download=True)

# dataloader
dataloader = DataLoader(dataset, batch_size=64, drop_last=True)

# Sequential Mode
class Mode(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.mode1 = Sequential(
            Conv2d(3, 32, kernel_size=5, padding=2, stride=1),
            MaxPool2d(2),
            Conv2d(32, 32, kernel_size=5, padding=2),
            MaxPool2d(2),
            Conv2d(32, 64, kernel_size=5, padding=2),
            MaxPool2d(2),
            Flatten(),
            Linear(1024, 64),
            Linear(64, 10)
        )

    def forward(self, x):
        x = self.mode1(x)
        return x


loss = nn.CrossEntropyLoss()  # 损失函数对象
mode = Mode()  # mode实例
for data in dataloader:
    imgs, targets = data
    output = mode(imgs)  # sequential处理
    result_loss = loss(output, targets)  # 损失函数计算
    result_loss.backward()  # 损失函数梯度计算
    print(result_loss)

2、优化器 optimizer

      import torch

      optims = torch.optim.SGD(mode.parameters(), lr=0.01)  指定优化算法

      optims.zero_grad()  梯度清零

      result_loss.backward()  反向传播(求梯度)

      optims.step()  参数调优

      coding:优化器的实现

import torch
import torchvision
from torch import nn
from torch.nn import Sequential, Conv2d, MaxPool2d, Flatten, Linear
from torch.utils.data import DataLoader

dataset = torchvision.datasets.CIFAR10("./dataset3", train=False,
                                       transform=torchvision.transforms.ToTensor(), download=True)
dataloader = DataLoader(dataset, batch_size=1, drop_last=True)


class Mode(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.mode1 = Sequential(
            Conv2d(3, 32, kernel_size=5, padding=2, stride=1),
            MaxPool2d(2),
            Conv2d(32, 32, kernel_size=5, padding=2),
            MaxPool2d(2),
            Conv2d(32, 64, kernel_size=5, padding=2),
            MaxPool2d(2),
            Flatten(),
            Linear(1024, 64),
            Linear(64, 10)
        )

    def forward(self, x):
        x = self.mode1(x)
        return x


loss = nn.CrossEntropyLoss()  # 损失函数对象
mode = Mode()  # 模型对象
optim = torch.optim.SGD(mode.parameters(), lr=0.01)  # 优化器

for epoch in range(20):
    running_loss = 0.0
    for data in dataloader:
        imgs, targets = data
        output = mode(imgs)
        result_loss = loss(output, targets)

        optim.zero_grad()  # 梯度清零
        result_loss.backward()  # 反向传播(求梯度)
        optim.step()  # 参数调优

        running_loss += result_loss
    print(running_loss)

五、实战运行

1、外部模型的使用

      vgg16 = torchvision.models.vgg16(pretrained=False)  加载模型

      vgg16 = torchvision.models.vgg16(pretrained=True)  加载模型和参数

      coding:外部模型的使用:增加层、修改层、保存、加载

import torch
import torchvision

# pretrained 预训练模型
# progress 进度条显示
from torch.nn import Linear

vgg16_false = torchvision.models.vgg16(pretrained=False)
vgg16_true = torchvision.models.vgg16(pretrained=True)

train_data = torchvision.datasets.CIFAR10("./dataset3", train=True,
                                          transform=torchvision.transforms.ToTensor(), download=True)

vgg16_true.classifier.add_module("add_linear", Linear())  # 增加层
vgg16_false.classifier[6] = Linear(4096, 10)  # 修改层

# 方式一
torch.save(vgg16_false, "vgg16_method1.pth")  # 保存模型结构和参数

model = torch.load("vgg16_method1.pth")  # 加载

# 方式二
torch.save(vgg16_true.state_dict(), "vgg16_method2.pth")  # 保存参数

vgg16 = torchvision.models.vgg16(pretrained=False)  # 调用模型
vgg16.load_state_dict(torch.load("vgg16_method2.pth"))  # 加载参数

2、模型训练流程

      数据连接、数据加载、创建模型、损失函数、优化器、训练(根据误差优化)、测试

      coding:模型的搭建

import torch
from torch import nn
from torch.nn import Sequential, Conv2d, MaxPool2d, Flatten, Linear


# 搭建神经网络
class Mode(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.modes = Sequential(
            Conv2d(3, 32, 5, 1, padding=2),
            MaxPool2d(2),
            Conv2d(32, 32, 5, 1, padding=2),
            MaxPool2d(2),
            Conv2d(32, 64, 5, 1, padding=2),
            MaxPool2d(2),
            Flatten(),
            Linear(1024, 64),
            Linear(64, 10)
        )

    def forward(self, x):
        x = self.modes(x)
        return x


if __name__ == '__main__':
    modes = Mode()
    input = torch.ones((64, 3, 32, 32))
    output = modes(input)
    print(output.shape)

      coding:模型训练

import torch
import torchvision
from torch import nn
from torch.nn import Sequential, Conv2d, MaxPool2d, Flatten, Linear
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
from demo18_model import Mode


# 训练集和测试集
train_data = torchvision.datasets.CIFAR10("./dataset3", train=True,
                                          transform=torchvision.transforms.ToTensor(), download=True)
test_data = torchvision.datasets.CIFAR10("./dataset3", train=False,
                                         transform=torchvision.transforms.ToTensor(), download=True)

# 数据集大小
train_data_size = len(train_data)
test_data_size = len(test_data)
print("训练数据集的大小是:{}".format(train_data_size))
print("测试数据集的大小时:{}".format(test_data_size))

# 数据加载
train_dataloader = DataLoader(train_data, batch_size=64)
test_dataloader = DataLoader(test_data, batch_size=64)


# 创建模型实例
modes = Mode()
# 损失函数
loss_fn = nn.CrossEntropyLoss()
# 优化器
learning_rate = 0.01
optim = torch.optim.SGD(modes.parameters(), lr=learning_rate)

# 参数设置
total_train_step = 0  # 训练次数
total_test_step = 0  # 测试次数
epoch = 10  # 训练轮次

writer = SummaryWriter("logs")

for i in epoch:
    print("-----------第{}轮训练开始-----------".format(i+1))
    # 训练
    modes.train()
    for data in train_dataloader:
        imgs, targets = data
        outputs = modes(imgs)  # 训练结果
        loss = loss_fn(outputs, targets)  # 损失值

        optim.zero_grad()  # 梯度清零
        loss.backward()  # 反向传递
        optim.step()  # 优化

        total_train_step += 1
        if total_train_step % 100 == 0:
            print("训练次数:{},Loss:{}".format(total_train_step, loss.item()))
            writer.add_scalar("train_loss", loss.item(), total_train_step)  # 记录损失

    # 测试
    modes.eval()
    total_test_lose = 0
    total_accuracy = 0  # 正确率
    with torch.no_grad:  # 强制不计算梯度相关
        for data in test_dataloader:
            imgs, targets = data
            outputs = modes(imgs)
            loss = loss_fn(outputs, targets)
            total_test_lose += loss.item()
            accuracy = outputs.argmax(1)  # argmax() 获取结果最大值位置,1为横向比较,0为纵向比较
            total_accuracy += (accuracy == targets).sum()  # 与目标相同的个数

    print("整体测试集上的Loss:{}".format(total_test_lose))
    print("整体测试集的正确率:{}".format(total_accuracy/test_data_size))
    writer.add_scalar("test_loss", total_test_lose, total_test_step)
    writer.add_scalar("test_accuracy", total_accuracy / test_data_size, total_test_step)
    total_test_step += 1

    torch.save(modes, "modes_{}.pth".format(i))
    print("模型已保存")

writer.close()



3、GPU的使用

a、modes = modes.cuda()  模型、数据、损失函数可使用

b、device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")  定义设备

3modes = modes.to(device)  调用设备,模型、数据、损失函数可使用

      coding:GPU的使用-方式1

import torch
import torchvision
import time
from torch import nn
from torch.nn import Sequential, Conv2d, MaxPool2d, Flatten, Linear
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

# from demo18_model import Mode

# 训练集和测试集
train_data = torchvision.datasets.CIFAR10("./dataset3", train=True,
                                          transform=torchvision.transforms.ToTensor(), download=True)
test_data = torchvision.datasets.CIFAR10("./dataset3", train=False,
                                         transform=torchvision.transforms.ToTensor(), download=True)

# 数据集大小
train_data_size = len(train_data)
test_data_size = len(test_data)
print("训练数据集的大小是:{}".format(train_data_size))
print("测试数据集的大小时:{}".format(test_data_size))

# 数据加载
train_dataloader = DataLoader(train_data, batch_size=64)
test_dataloader = DataLoader(test_data, batch_size=64)


# 创建模型
class Mode(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.modes = Sequential(
            Conv2d(3, 32, 5, 1, padding=2),
            MaxPool2d(2),
            Conv2d(32, 32, 5, 1, padding=2),
            MaxPool2d(2),
            Conv2d(32, 64, 5, 1, padding=2),
            MaxPool2d(2),
            Flatten(),
            Linear(1024, 64),
            Linear(64, 10)
        )

    def forward(self, x):
        x = self.modes(x)
        return x

modes = Mode()
if torch.cuda.is_available():
    modes = modes.cuda()

# 损失函数
loss_fn = nn.CrossEntropyLoss()
if torch.cuda.is_available():
    loss_fn = loss_fn.cuda()
# 优化器
learning_rate = 0.01
optim = torch.optim.SGD(modes.parameters(), lr=learning_rate)

# 参数设置
total_train_step = 0  # 训练次数
total_test_step = 0  # 测试次数
epoch = 10  # 训练轮次

writer = SummaryWriter("logs")

start_time = time.time()
for i in epoch:
    print("-----------第{}轮训练开始-----------".format(i+1))
    # 训练
    modes.train()
    for data in train_dataloader:
        imgs, targets = data
        if torch.cuda.is_available():
            imgs = imgs.cuda()
            targets = targets.cuda()
        outputs = modes(imgs)  # 训练结果
        loss = loss_fn(outputs, targets)  # 损失值

        optim.zero_grad()  # 梯度清零
        loss.backward()  # 反向传递
        optim.step()  # 优化

        total_train_step += 1
        if total_train_step % 100 == 0:
            end_time = time.time()
            print(end_time-start_time)
            print("训练次数:{},Loss:{}".format(total_train_step, loss.item()))
            writer.add_scalar("train_loss", loss.item(), total_train_step)  # 记录损失

    # 测试
    modes.eval()
    total_test_lose = 0
    total_accuracy = 0  # 正确率
    with torch.no_grad:  # 强制不计算梯度相关
        for data in test_dataloader:
            imgs, targets = data
            if torch.cuda.is_available():
                imgs = imgs.cuda()
                targets = targets.cuda()
            outputs = modes(imgs)
            loss = loss_fn(outputs, targets)
            total_test_lose += loss.item()
            accuracy = outputs.argmax(1)  # argmax() 获取结果最大值位置,1为横向比较,0为纵向比较
            total_accuracy += (accuracy == targets).sum()  # 与目标相同的个数

    print("整体测试集上的Loss:{}".format(total_test_lose))
    print("整体测试集的正确率:{}".format(total_accuracy/test_data_size))
    writer.add_scalar("test_loss", total_test_lose, total_test_step)
    writer.add_scalar("test_accuracy", total_accuracy / test_data_size, total_test_step)
    total_test_step += 1

    torch.save(modes, "modes_{}.pth".format(i))
    print("模型已保存")

writer.close()

      coding:GPU的使用-方式2

import torch
import torchvision
import time
from torch import nn
from torch.nn import Sequential, Conv2d, MaxPool2d, Flatten, Linear
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

# from demo18_model import Mode
# 定义设备
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")


# 训练集和测试集
train_data = torchvision.datasets.CIFAR10("./dataset3", train=True,
                                          transform=torchvision.transforms.ToTensor(), download=True)
test_data = torchvision.datasets.CIFAR10("./dataset3", train=False,
                                         transform=torchvision.transforms.ToTensor(), download=True)

# 数据集大小
train_data_size = len(train_data)
test_data_size = len(test_data)
print("训练数据集的大小是:{}".format(train_data_size))
print("测试数据集的大小时:{}".format(test_data_size))

# 数据加载
train_dataloader = DataLoader(train_data, batch_size=64)
test_dataloader = DataLoader(test_data, batch_size=64)


# 创建模型
class Mode(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.modes = Sequential(
            Conv2d(3, 32, 5, 1, padding=2),
            MaxPool2d(2),
            Conv2d(32, 32, 5, 1, padding=2),
            MaxPool2d(2),
            Conv2d(32, 64, 5, 1, padding=2),
            MaxPool2d(2),
            Flatten(),
            Linear(1024, 64),
            Linear(64, 10)
        )

    def forward(self, x):
        x = self.modes(x)
        return x

modes = Mode()
modes = modes.to(device)

# 损失函数
loss_fn = nn.CrossEntropyLoss()
loss_fn = loss_fn.to(device)
# 优化器
learning_rate = 0.01
optim = torch.optim.SGD(modes.parameters(), lr=learning_rate)

# 参数设置
total_train_step = 0  # 训练次数
total_test_step = 0  # 测试次数
epoch = 10  # 训练轮次

writer = SummaryWriter("logs")

start_time = time.time()
for i in range(epoch):
    print("-----------第{}轮训练开始-----------".format(i+1))
    # 训练
    modes.train()
    for data in train_dataloader:
        imgs, targets = data
        imgs = imgs.to(device)
        targets = targets.to(device)
        outputs = modes(imgs)  # 训练结果
        loss = loss_fn(outputs, targets)  # 损失值

        optim.zero_grad()  # 梯度清零
        loss.backward()  # 反向传递
        optim.step()  # 优化

        total_train_step += 1
        if total_train_step % 100 == 0:
            end_time = time.time()
            print(end_time-start_time)
            print("训练次数:{},Loss:{}".format(total_train_step, loss.item()))
            writer.add_scalar("train_loss", loss.item(), total_train_step)  # 记录损失

    # 测试
    modes.eval()
    total_test_lose = 0
    total_accuracy = 0  # 正确率
    with torch.no_grad():  # 强制不计算梯度相关
        for data in test_dataloader:
            imgs, targets = data
            imgs = imgs.to(device)
            targets = targets.to(device)
            outputs = modes(imgs)
            loss = loss_fn(outputs, targets)
            total_test_lose += loss.item()
            accuracy = outputs.argmax(1)  # argmax() 获取结果最大值位置,1为横向比较,0为纵向比较
            total_accuracy += (accuracy == targets).sum()  # 与目标相同的个数

    print("整体测试集上的Loss:{}".format(total_test_lose))
    print("整体测试集的正确率:{}".format(total_accuracy/test_data_size))
    writer.add_scalar("test_loss", total_test_lose, total_test_step)
    writer.add_scalar("test_accuracy", total_accuracy / test_data_size, total_test_step)
    total_test_step += 1

    torch.save(modes, "modes_{}.pth".format(i))
    print("模型已保存")

writer.close()

4、训练模型测试

      coding:训练结果测试

import torch
import torchvision
from PIL import Image
from torch import nn
from torch.nn import Sequential, Conv2d, MaxPool2d, Flatten, Linear

# 测试图像
image_path = "images/dog.png"
image = Image.open(image_path)
image = image.convert("RGB")

transform = torchvision.transforms.Compose([torchvision.transforms.Resize((32, 32)),
                                            torchvision.transforms.ToTensor()])
image = transform(image)


# 定义模型
class Mode(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.modes = Sequential(
            Conv2d(3, 32, 5, 1, padding=2),
            MaxPool2d(2),
            Conv2d(32, 32, 5, 1, padding=2),
            MaxPool2d(2),
            Conv2d(32, 64, 5, 1, padding=2),
            MaxPool2d(2),
            Flatten(),
            Linear(1024, 64),
            Linear(64, 10)
        )

    def forward(self, x):
        x = self.modes(x)
        return x


# 加载模型
model = torch.load("modes_9.pth", map_location="cuda")

print(model)
image = torch.reshape(image, (1, 3, 32, 32))
image = image.cuda()

model.eval()
with torch.no_grad():
    output = model(image)
print(output)

print(output.argmax(1))

你可能感兴趣的:(计算机视觉入门记录,python,pytorch,计算机视觉)