pytorch学习记录四【cifar示例完整代码 / 利用GPU进行训练 / 测试】

1.item:

import torch
a = torch.tensor(5)
print(a)  # tensor(5)
print(a.item())  # 5

2.求准确率的小test

import torch

# 预测概率 此处有两个输入。每行代表一个输入图片的预测输出。例如第一行代表:为第0种种类的概率为0.1,为第1种种类的概率为0.2
outputs = torch.tensor([[0.1, 0.2],
                        [0.3, 0.4]])
# 预测结果
preds = outputs.argmax(1)  # tensor([1, 1])    参数1表示横向看。表示的意思是:每行的概率匹配。 比如这里代表两个图片都是第一种种类
# 真实结果
targets = torch.tensor([0, 1])  # 这是真实结果
# 准确个数
print((preds == targets).sum())  # 表示我们的预测是否准确。为false + true = 1

3.cifar示例完整代码

pytorch学习记录四【cifar示例完整代码 / 利用GPU进行训练 / 测试】_第1张图片
model.py

# 搭建神经网络
import torch
from torch import nn

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

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

if __name__ == '__main__':
	# 可以利用这里进行测试
    test_cifar = cifar_model()
    input = torch.ones((64, 3, 32, 32))
    output = test_cifar(input)
    print(output.shape)  # torch.Size([64, 10]) # 检查模型的正确性

训练train.py

import torch
import torchvision
from tensorboardX import SummaryWriter
from torch import nn
from torch.utils.data import DataLoader
from cifar_src.model import *

# 准备数据集
train_data = torchvision.datasets.CIFAR10("../cifar_data", train=True, transform=torchvision.transforms.ToTensor(), download=True)
test_data = torchvision.datasets.CIFAR10("../cifar_data", train=False, transform=torchvision.transforms.ToTensor(), download=True)

# length 长度
train_data_size = len(train_data)  # 50000
test_data_size = len(test_data)  # 10000
print("训练数据集的长度为:{}".format(train_data_size))
print("测试数据集的长度为:{}".format(test_data_size))

# 利用Dataloader来加载数据集
train_dataloader = DataLoader(train_data, batch_size=64)
test_dataloader = DataLoader(test_data, batch_size=64)

# 创建网络模型
cifarr = cifar_model()

# 损失函数
loss_fn = nn.CrossEntropyLoss()

# 优化器
learning_rate = 1e-2
optimizer = torch.optim.SGD(cifarr.parameters(), lr=learning_rate)

# 设置训练网络的一些参数
# 记录训练的次数
total_train_step = 0
# 记录测试的次数
total_test_step = 0
# 训练的轮数
epoch = 10

# 添加tensorboard
writer = SummaryWriter("../cifar_log")

for i in range(epoch):
    print("————————第{}轮训练开始————————".format(i+1))

    # 训练步骤开始
    cifarr.train()
    for data in train_dataloader:
        imgs, targets = data
        outputs = cifarr(imgs)
        loss = loss_fn(outputs, targets)

        # 优化器优化模型
        optimizer.zero_grad()  # 优化器梯度清零
        loss.backward()  # 反向传播,计算梯度
        optimizer.step()  # 根据梯度和优化器,更新权重

        total_train_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)

    # 验证步骤
    cifarr.eval()
    total_test_loss = 0
    total_accuracy = 0  # 总体的准确率
    with torch.no_grad():  # 强制之后的内容不进行计算图构建
        for data in test_dataloader:
            imgs, targets = data
            outputs = cifarr(imgs)
            loss = loss_fn(outputs, targets)
            total_test_loss = total_test_loss + loss.item()
            accuracy = (outputs.argmax(1) == targets).sum()
            total_accuracy = total_accuracy + accuracy
    print("整体测试集上的loss:{}".format(total_test_loss))
    print("整体测试集上的正确率:{}".format(total_accuracy/test_data_size))
    writer.add_scalar("test_loss", total_test_loss, total_test_step)
    writer.add_scalar("test_accuracy", total_accuracy/test_data_size, total_test_step)
    total_test_step = total_test_step + 1

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


writer.close()

pytorch学习记录四【cifar示例完整代码 / 利用GPU进行训练 / 测试】_第2张图片
pytorch学习记录四【cifar示例完整代码 / 利用GPU进行训练 / 测试】_第3张图片

4.利用GPU进行训练1:.cuda:

命令行nvidia-smi
pytorch学习记录四【cifar示例完整代码 / 利用GPU进行训练 / 测试】_第4张图片

利用GPU训练的方法:
网络模型数据(输入,标注)损失函数调用.cuda
pytorch学习记录四【cifar示例完整代码 / 利用GPU进行训练 / 测试】_第5张图片
pytorch学习记录四【cifar示例完整代码 / 利用GPU进行训练 / 测试】_第6张图片
pytorch学习记录四【cifar示例完整代码 / 利用GPU进行训练 / 测试】_第7张图片
加入GPU的完整代码

import time
import torch
import torchvision
from tensorboardX import SummaryWriter
from torch import nn
from torch.utils.data import DataLoader

# 准备数据集
train_data = torchvision.datasets.CIFAR10("../cifar_data", train=True, transform=torchvision.transforms.ToTensor(), download=True)
test_data = torchvision.datasets.CIFAR10("../cifar_data", train=False, transform=torchvision.transforms.ToTensor(), download=True)

# length 长度
train_data_size = len(train_data)  # 50000
test_data_size = len(test_data)  # 10000
print("训练数据集的长度为:{}".format(train_data_size))
print("测试数据集的长度为:{}".format(test_data_size))

# 利用Dataloader来加载数据集
train_dataloader = DataLoader(train_data, batch_size=64)
test_dataloader = DataLoader(test_data, batch_size=64)

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

    def forward(self, x):
        x = self.model1(x)
        return x
cifarr = cifar_model()
if torch.cuda.is_available():
    cifarr = cifarr.cuda()

# 损失函数
loss_fn = nn.CrossEntropyLoss()
if torch.cuda.is_available():
    loss_fn = loss_fn.cuda()

# 优化器
learning_rate = 1e-2
optimizer = torch.optim.SGD(cifarr.parameters(), lr=learning_rate)

# 设置训练网络的一些参数
# 记录训练的次数
total_train_step = 0
# 记录测试的次数
total_test_step = 0
# 训练的轮数
epoch = 10

# 添加tensorboard
writer = SummaryWriter("../cifar_log")
start_time = time.time()
for i in range(epoch):
    print("————————第{}轮训练开始————————".format(i+1))

    # 训练步骤开始
    cifarr.train()
    for data in train_dataloader:
        imgs, targets = data
        if torch.cuda.is_available():
            imgs = imgs.cuda()
            targets = targets.cuda()
        outputs = cifarr(imgs)
        loss = loss_fn(outputs, targets)

        # 优化器优化模型
        optimizer.zero_grad()  # 优化器梯度清零
        loss.backward()  # 反向传播,计算梯度
        optimizer.step()  # 根据梯度和优化器,更新权重

        total_train_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)

    # 验证步骤
    cifarr.eval()
    total_test_loss = 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 = cifarr(imgs)
            loss = loss_fn(outputs, targets)
            total_test_loss = total_test_loss + loss.item()
            accuracy = (outputs.argmax(1) == targets).sum()
            total_accuracy = total_accuracy + accuracy
    print("整体测试集上的loss:{}".format(total_test_loss))
    print("整体测试集上的正确率:{}".format(total_accuracy/test_data_size))
    writer.add_scalar("test_loss", total_test_loss, total_test_step)
    writer.add_scalar("test_accuracy", total_accuracy/test_data_size, total_test_step)
    total_test_step = total_test_step + 1

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


writer.close()

5.利用GPU进行训练2:.to(device):

在这里插入图片描述
pytorch学习记录四【cifar示例完整代码 / 利用GPU进行训练 / 测试】_第8张图片
pytorch学习记录四【cifar示例完整代码 / 利用GPU进行训练 / 测试】_第9张图片
pytorch学习记录四【cifar示例完整代码 / 利用GPU进行训练 / 测试】_第10张图片
完整代码:

import time
import torch
import torchvision
from tensorboardX import SummaryWriter
from torch import nn
from torch.utils.data import DataLoader

# 定义训练的设备
# device = torch.device("cuda")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# 准备数据集
train_data = torchvision.datasets.CIFAR10("../cifar_data", train=True, transform=torchvision.transforms.ToTensor(), download=True)
test_data = torchvision.datasets.CIFAR10("../cifar_data", train=False, transform=torchvision.transforms.ToTensor(), download=True)

# length 长度
train_data_size = len(train_data)  # 50000
test_data_size = len(test_data)  # 10000
print("训练数据集的长度为:{}".format(train_data_size))
print("测试数据集的长度为:{}".format(test_data_size))

# 利用Dataloader来加载数据集
train_dataloader = DataLoader(train_data, batch_size=64)
test_dataloader = DataLoader(test_data, batch_size=64)

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

    def forward(self, x):
        x = self.model1(x)
        return x
cifarr = cifar_model()
cifarr = cifarr.to(device)

# 损失函数
loss_fn = nn.CrossEntropyLoss()
loss_fn = loss_fn.to(device)

# 优化器
learning_rate = 1e-2
optimizer = torch.optim.SGD(cifarr.parameters(), lr=learning_rate)

# 设置训练网络的一些参数
# 记录训练的次数
total_train_step = 0
# 记录测试的次数
total_test_step = 0
# 训练的轮数
epoch = 10

# 添加tensorboard
writer = SummaryWriter("../cifar_log")
start_time = time.time()
for i in range(epoch):
    print("————————第{}轮训练开始————————".format(i+1))

    # 训练步骤开始
    cifarr.train()
    for data in train_dataloader:
        imgs, targets = data
        imgs = imgs.to(device)
        targets = targets.to(device)
        outputs = cifarr(imgs)
        loss = loss_fn(outputs, targets)

        # 优化器优化模型
        optimizer.zero_grad()  # 优化器梯度清零
        loss.backward()  # 反向传播,计算梯度
        optimizer.step()  # 根据梯度和优化器,更新权重

        total_train_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)

    # 验证步骤
    cifarr.eval()
    total_test_loss = 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 = cifarr(imgs)
            loss = loss_fn(outputs, targets)
            total_test_loss = total_test_loss + loss.item()
            accuracy = (outputs.argmax(1) == targets).sum()
            total_accuracy = total_accuracy + accuracy
    print("整体测试集上的loss:{}".format(total_test_loss))
    print("整体测试集上的正确率:{}".format(total_accuracy/test_data_size))
    writer.add_scalar("test_loss", total_test_loss, total_test_step)
    writer.add_scalar("test_accuracy", total_accuracy/test_data_size, total_test_step)
    total_test_step = total_test_step + 1

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

writer.close()

6.测试

注意,如果是用GPU训练的网络结构进行测试,那测试时输入的图片也要用cuda
否则会报这个错
pytorch学习记录四【cifar示例完整代码 / 利用GPU进行训练 / 测试】_第11张图片
正确完整代码:
pytorch学习记录四【cifar示例完整代码 / 利用GPU进行训练 / 测试】_第12张图片

test.py

from PIL import Image
import time

import torch
import torchvision
from tensorboardX import SummaryWriter
from torch import nn
from torch.utils.data import DataLoader
from torchvision import transforms

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")


imgae_path = "../cifar_img/dog3.jpg"
image = Image.open(imgae_path)
image = image.convert('RGB')

transform = torchvision.transforms.Compose([transforms.Resize((32, 32)), transforms.ToTensor()])
image = transform(image)
print(image.shape)  # 因为cifar网络要求输入32*32的图片


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

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

model = torch.load("cifar_9.pth")
print(model)

image = torch.reshape(image, (1, 3, 32, 32))
image = image.to(device)

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

print(output.argmax(1))

pytorch学习记录四【cifar示例完整代码 / 利用GPU进行训练 / 测试】_第13张图片
输出7
至于看7是什么,可以在训练数据的这里打断点
pytorch学习记录四【cifar示例完整代码 / 利用GPU进行训练 / 测试】_第14张图片

7.最终的完整代码

目录

pytorch学习记录四【cifar示例完整代码 / 利用GPU进行训练 / 测试】_第15张图片

model.py

# 搭建神经网络
import torch
from torch import nn

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

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

if __name__ == '__main__':
    test_cifar = cifar_model()
    input = torch.ones((64, 3, 32, 32))
    output = test_cifar(input)
    print(output.shape)  # torch.Size([64, 10]) # 检查模型的正确性

train.py

import time
import torch
import torchvision
from tensorboardX import SummaryWriter
from torch import nn
from torch.utils.data import DataLoader

# 定义训练的设备
# device = torch.device("cuda")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# 准备数据集
train_data = torchvision.datasets.CIFAR10("../cifar_data", train=True, transform=torchvision.transforms.ToTensor(), download=True)
test_data = torchvision.datasets.CIFAR10("../cifar_data", train=False, transform=torchvision.transforms.ToTensor(), download=True)

# length 长度
train_data_size = len(train_data)  # 50000
test_data_size = len(test_data)  # 10000
print("训练数据集的长度为:{}".format(train_data_size))
print("测试数据集的长度为:{}".format(test_data_size))

# 利用Dataloader来加载数据集
train_dataloader = DataLoader(train_data, batch_size=64)
test_dataloader = DataLoader(test_data, batch_size=64)

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

    def forward(self, x):
        x = self.model1(x)
        return x
cifarr = cifar_model()
cifarr = cifarr.to(device)

# 损失函数
loss_fn = nn.CrossEntropyLoss()
loss_fn = loss_fn.to(device)

# 优化器
learning_rate = 1e-2
optimizer = torch.optim.SGD(cifarr.parameters(), lr=learning_rate)

# 设置训练网络的一些参数
# 记录训练的次数
total_train_step = 0
# 记录测试的次数
total_test_step = 0
# 训练的轮数
epoch = 10

# 添加tensorboard
writer = SummaryWriter("../cifar_log")
start_time = time.time()
for i in range(epoch):
    print("————————第{}轮训练开始————————".format(i+1))

    # 训练步骤开始
    cifarr.train()
    for data in train_dataloader:
        imgs, targets = data
        imgs = imgs.to(device)
        targets = targets.to(device)
        outputs = cifarr(imgs)
        loss = loss_fn(outputs, targets)

        # 优化器优化模型
        optimizer.zero_grad()  # 优化器梯度清零
        loss.backward()  # 反向传播,计算梯度
        optimizer.step()  # 根据梯度和优化器,更新权重

        total_train_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)

    # 验证步骤
    cifarr.eval()
    total_test_loss = 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 = cifarr(imgs)
            loss = loss_fn(outputs, targets)
            total_test_loss = total_test_loss + loss.item()
            accuracy = (outputs.argmax(1) == targets).sum()
            total_accuracy = total_accuracy + accuracy
    print("整体测试集上的loss:{}".format(total_test_loss))
    print("整体测试集上的正确率:{}".format(total_accuracy/test_data_size))
    writer.add_scalar("test_loss", total_test_loss, total_test_step)
    writer.add_scalar("test_accuracy", total_accuracy/test_data_size, total_test_step)
    total_test_step = total_test_step + 1

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


writer.close()

test.py

from PIL import Image
import time

import torch
import torchvision
from tensorboardX import SummaryWriter
from torch import nn
from torch.utils.data import DataLoader
from torchvision import transforms

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")


imgae_path = "../cifar_img/dog3.jpg"
image = Image.open(imgae_path)
image = image.convert('RGB')

transform = torchvision.transforms.Compose([transforms.Resize((32, 32)), transforms.ToTensor()])
image = transform(image)
print(image.shape)  # 因为cifar网络要求输入32*32的图片


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

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

model = torch.load("cifar_9.pth")
print(model)

image = torch.reshape(image, (1, 3, 32, 32))
image = image.to(device)

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

print(output.argmax(1))

你可能感兴趣的:(pytroch学习,pytorch,python,深度学习)