Pytorch学习记录(自用>>>持续更新)

本文详细介绍了从数据预处理到模型训练的深度学习流程,包括张量操作、数据加载、卷积神经网络(CNN)、池化操作、损失函数、反向传播和优化器的使用。通过TensorBoard进行可视化,并展示了GPU训练的优势。最后,通过加载和测试预训练模型,验证了训练的有效性。
摘要由CSDN通过智能技术生成

目录

数据集

图片阶段

什么是张量

 tensor类型

常用方法,裁剪归一化啥的

加载官网数据集

dataloader

卷积神经

卷积核使用小案例

最大池化实现

神经网络(Loss+反向传播)

优化器,多epoch参数优化

gpu训练

训练模型测试环节


数据集

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


class MyData(Dataset):
    # root_dir train   label_dir ants
    def __init__(self, root_dir, label_dir):
        self.root_dir = root_dir
        self.label_dir = label_dir
        # self.path=...train/ants
        self.path = os.path.join(self.root_dir, self.label_dir)
        # self.img_path 每一个图片
        self.img_path = os.listdir(self.path)

    def __getitem__(self, idx):
        img_name = self.img_path[idx]
        img_item_path = os.path.join(self.root_dir, self.label_dir, img_name)
        img = Image.open(img_item_path)
        label = self.label_dir
        return img, label

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


root_dir = "hymenoptera_data/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

需要np类型,图片需要转换

图片阶段

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

writer = SummaryWriter("logs")
image_path = "hymenoptera_data/train/ants/6240338_93729615ec.jpg"
img = Image.open(image_path)
img_array = np.array(img)
print(type(img_array))
print(img_array.shape)

# y=x
writer.add_image("test", img_array, 2, dataformats='HWC')
for i in range(100):
    writer.add_scalar("y=2x", i * 2, i)
writer.close()

 终端输入 ,可以使用tensorboard

tensorboard --logdir logs

command+p查看输入参数

什么是张量

 tensor类型

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

# 通过transform.ToTensor去看两个问题
# 1、transforms该如何使用
# 2、为什么我们需要Tensor数据类型

img_path = "hymenoptera_data/train/bees/21399619_3e61e5bb6f.jpg"
img = Image.open(img_path)

writer = SummaryWriter("logs")

tensor_trans = transforms.ToTensor()
tensor_img = tensor_trans(img)

writer.add_image("tensor_img", tensor_img)

writer.close()

常用方法,裁剪归一化啥的

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

writer = SummaryWriter("logs2")
img = Image.open("hymenoptera_data/train/bees/21399619_3e61e5bb6f.jpg")

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

# Normalize
print(img_tensor[0][0][0])
trans_norm = transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
img_norm = trans_norm(img_tensor)
print(img_norm[0][0][0])
writer.add_image("Normalize", img_norm)

# Resize
print(img.size)
trans_resize=transforms.Resize((512,512))
# img PIL -> resize -> img_resize PIL
img_resize=trans_resize(img)
# img_resize PIL -> totensor ->img_resize tensor
img_resize=trans_totensor(img_resize)
writer.add_image("Resize", img_resize,0)
print(img_resize)


# Compose -resize -2
trans_resize_2=transforms.Resize(512)
# PIL -> PIL -> tensor
trans_compose=transforms.Compose([trans_resize_2,trans_totensor])
img_resize_2=trans_compose(img)
writer.add_image("Resize",img_resize_2,1)


# RandomCrop 随机裁剪
trans_random=transforms.RandomCrop(256)
trans_compose_2=transforms.Compose([trans_random,trans_totensor])
for i in range(10):
    img_crop=trans_compose_2(img)
    writer.add_image("RandomCrop",img_crop,i)




writer.close()

加载官网数据集

import torchvision
from torch.utils.tensorboard import SummaryWriter

dataset_transform = torchvision.transforms.Compose([
    torchvision.transforms.ToTensor()
])
train_set = torchvision.datasets.CIFAR10(root="./downloadDataset",
                                         train=True,
                                         transform=dataset_transform,
                                         download=True
                                         )
test_set = torchvision.datasets.CIFAR10(root="./downloadDataset",
                                        train=False,
                                        transform=dataset_transform,
                                        download=False
                                        )
# img,target=test_set[0]
# print(test_set[0])
# print(test_set.classes)
# print(target)
# img.show()

writer = SummaryWriter("cifar10")
for i in range(10):
    img, target = test_set[i]
    writer.add_image("test_test", img, i)

writer.close()

dataloader

卷积神经

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

print(input.shape)
print(kernel.shape)

# stride步长  padding=1周围填充
output = F.conv2d(input, kernel, stride=1)

print(output)

padding=1

卷积核使用小案例

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

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


class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        # out是in倍数关系
        self.conv1 = Conv2d(in_channels=3, out_channels=6, kernel_size=3, stride=1, padding=0)

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


tudui = Tudui()

writer = SummaryWriter("./08logs")
step = 0
for data in dataloader:
    imgs, targets = data
    output = tudui(imgs)
    print(imgs.shape)
    print(output.shape)
    output = torch.reshape(output, (-1, 3, 30, 30))
    print(output.shape)
    writer.add_images("09img", output, step)
    step = step + 1

writer.close()

最大池化实现

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

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


class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.maxpool1 = MaxPool2d(kernel_size=3, ceil_mode=True)

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


tudui = Tudui()

writer = SummaryWriter("09logs_maxpool")
step = 0

for data in dataloader:
    imgs, targets = data
    writer.add_images("input", imgs, step)
    output = tudui(imgs)
    writer.add_images("output", output, step)
    step = step + 1

writer.close()

 

神经网络(Loss+反向传播)

import torch
import torchvision.datasets

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

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

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

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

loss=nn.CrossEntropyLoss()
tudui = Tudui()

for data in dataloader:
    imgs,targets=data
    outputs=tudui(imgs)
    result_loss=loss(outputs,targets)
    result_loss.backward()
    print(result_loss)

优化器,多epoch参数优化

import torch
import torchvision.datasets

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

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

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

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

loss=nn.CrossEntropyLoss()
tudui = Tudui()
# 定义优化器
# params (iterable) – 待优化参数的iterable或者是定义了参数组的dict
# lr (float) – 学习率
# momentum (float, 可选) – 动量因子(默认:0)
# weight_decay (float, 可选) – 权重衰减(L2惩罚)(默认:0)
# dampening (float, 可选) – 动量的抑制因子(默认:0)
# nesterov (bool, 可选) – 使用Nesterov动量(默认:False)
optim=torch.optim.SGD(tudui.parameters(),lr=0.01)

for epoch in range(20):
    running_loss=0.0
    for data in dataloader:
        imgs,targets=data
        outputs=tudui(imgs)
        result_loss=loss(outputs,targets)
        # 优化器
        optim.zero_grad()
        # 需要梯度
        result_loss.backward()
        # 开始调优
        optim.step()
        running_loss=running_loss+result_loss
    print(running_loss)

训练神经网络模型套路整合

 train

import torchvision

from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

from model import *

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

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

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

# 创建神经网络
tudui = Tudui()

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

# 优化器
learning_rate = 0.01
optimize = torch.optim.SGD(tudui.parameters(), lr=learning_rate)

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

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

for i in range(epoch):
    print("---------第{}轮训练开始----------".format(i + 1))
    # 训练步骤
    for data in train_dataloader:
        imgs, targets = data
        outputs = tudui(imgs)
        loss = loss_fn(outputs, targets)
        # 优化器优化模型
        optimize.zero_grad()
        loss.backward()
        optimize.step()

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

    # 测试步骤
    total_test_loss = 0
    total_accuracy = 0
    with torch.no_grad():
        for data in test_dataloader:
            imgs, targets = data
            outputs = tudui(imgs)
            loss = loss_fn(outputs, targets)
            total_test_loss = total_test_loss + loss
            accuracy = (outputs.argmax(1) == targets).sum()
            accuracy=accuracy.item()
            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_loss)
    total_test_step = total_test_step + 1

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

writer.close()

model

import torch

# 准备数据集
from torch import nn
from torch.nn import Sequential, Conv2d, MaxPool2d, Linear
from torch.nn.modules.flatten import Flatten


class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.model = Sequential(
            Conv2d(3, 32, 5, padding=2),
            MaxPool2d(2),
            Conv2d(32, 32, 5, padding=2),
            MaxPool2d(2),
            Conv2d(32, 64, 5, padding=2),
            MaxPool2d(2),
            Flatten(),
            Linear(1024, 64),
            Linear(64, 10)
        )

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


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

gpu训练

代码相比于上面进行了微调

import time

import torchvision

from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

from model import *
import torch

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

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

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

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


# 创建神经网络
class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.model = Sequential(
            Conv2d(3, 32, 5, padding=2),
            MaxPool2d(2),
            Conv2d(32, 32, 5, padding=2),
            MaxPool2d(2),
            Conv2d(32, 64, 5, padding=2),
            MaxPool2d(2),
            Flatten(),
            Linear(1024, 64),
            Linear(64, 10)
        )

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


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

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

# 优化器
learning_rate = 0.01
optimize = torch.optim.SGD(tudui.parameters(), lr=learning_rate)

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

# 添加tensorboard
writer = SummaryWriter("./logs_train")
for i in range(epoch):
    start_time=time.time()
    print("---------第{}轮训练开始----------".format(i + 1))
    # 训练步骤
    for data in train_dataloader:
        imgs, targets = data
        if torch.cuda.is_available():
            imgs = imgs.cuda()
            targets = targets.cuda()
        outputs = tudui(imgs)
        loss = loss_fn(outputs, targets)
        # 优化器优化模型
        optimize.zero_grad()
        loss.backward()
        optimize.step()

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

    # 测试步骤
    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 = tudui(imgs)
            loss = loss_fn(outputs, targets)
            total_test_loss = total_test_loss + loss
            accuracy = (outputs.argmax(1) == targets).sum()
            accuracy = accuracy.item()
            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(tudui, "tudui_{}.pth".format(i))
    print("模型已保存")

writer.close()

训练模型测试环节

test

import torchvision.transforms
from PIL import Image

import torch

# 准备数据集
from torch import nn
from torch.nn import Sequential, Conv2d, MaxPool2d, Linear
from torch.nn.modules.flatten import Flatten

image_path="./images/dog.jpeg"
image=Image.open(image_path)
print(image)

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

image=transform(image)
print(image.shape)



class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.model = Sequential(
            Conv2d(3, 32, 5, padding=2),
            MaxPool2d(2),
            Conv2d(32, 32, 5, padding=2),
            MaxPool2d(2),
            Conv2d(32, 64, 5, padding=2),
            MaxPool2d(2),
            Flatten(),
            Linear(1024, 64),
            Linear(64, 10)
        )

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


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


model=torch.load("tudui_4.pth")
image=torch.reshape(image,(1,3,32,32))
model.eval()
with torch.no_grad():
    output=model(image)
print(output)
print(output.argmax(1))

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江河地笑

实践是检验真理的唯一标准

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值