一:写在前面
本人是一名普通大二在读本科生,在学习深度学习方面的相关知识,从pytorch开始入手,这篇笔记是根据b站up主“我是土堆”的教程展开,如有遗漏或者不明白的地方,可以去原视频处观看,也欢迎留言讨论。
链接:【PyTorch深度学习快速入门教程(绝对通俗易懂!)【小土堆】】https://www.bilibili.com/video/BV1hE411t7RN?vd_source=bca1fe2b3fb6844fd58ee0163950bd4b
P1-P5章节内容为视频相关环境配置教程,在此就不过多赘述,有需要的朋友可以去参考其他博主的。(嫌麻烦的朋友建议直接tb找商家配置,会省去很多不必要的麻烦,本人亲身经历)
P6
pytorch中读取数据主要分为两类
1.Dataset -- 提供一种方式去获取数据及其label
2.Dataloader -- 为后面的网络提供不同的数据形式
所用数据集链接:
蚂蚁蜜蜂/练手数据集:链接: https://pan.baidu.com/s/1jZoTmoFzaTLWh4lKBHVbEA 密码: 5suq
P7
课程代码(部分)
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.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 = "G:/深度学习/pytorch(xiaotudui)/demo/hymenoptera_data/train" ##指定根目录
ants_label_dir = "G:/深度学习/pytorch(xiaotudui)/demo/hymenoptera_data/train/ants" #指定“蚂蚁”目录
bees_label_dir = "G:/深度学习/pytorch(xiaotudui)/demo/hymenoptera_data/train/bees" #指定“蜜蜂”目录
ants_dataset = MyData(root_dir,ants_label_dir)
bees_dataset = MyData(root_dir,bees_label_dir)
print(ants_dataset[0]) #查看“蚂蚁”第一张图片数据
img, label = ants_dataset[0]
img.show() #打开“蚂蚁”第一张图片
print(bees_dataset[0]) #查看“蜜蜂”第一张图片数据
img, label = bees_dataset[0]
img.show() #打开“蜜蜂”第一张图片
train_dataset = ants_dataset + bees_dataset #将“蚂蚁”数据集与“蜜蜂”数据集集合成一个数据集 顺序为“蚂蚁”+“蜜蜂”
Tensorboard使用
Tensorboard(1)
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter("logs")
#定义y = x
for i in range(100):
writer.add_scalar("y = x", i, i)
writer.close()
打开图表
运行代码后,在左侧会出现logs文件夹(如下)
在终端输入
tensorboard --logdir=logs
会出现如下情况
随后点击链接即可打开图表
也可输入
tensorboard --logdir=logs --port=6007
进行参数的设定,避免多人共同打开错误
注意:如果命名同一个函数,多次运行,会出现图像拟合错误
如出现这种情况,将logs文件夹下生成文件删除并重新运行正确代码即可
Tensorboard(2)
writer.add_image()读取数据为numpy型数据
课程代码
from torch.utils.tensorboard import SummaryWriter
import numpy as np
from PIL import Image
writer = SummaryWriter("logs")
image_path = "G:\深度学习\pytorch(xiaotudui)\demo\练手数据集/train/ants_image/0013035.jpg"
img_PIL = Image.open(image_path)
img_array = np.array(img_PIL)
print(type(img_array))
print(img_array.shape)
writer.add_image("train", img_array, 1, dataformats='HWC')
for i in range(100):
writer.add_scalar("y = 2x", 2*i, i) #y = x
writer.close()
Transforms使用
Transforms(1)
transform像一个工具箱,我们将需要处理的图片经过其中的工具进行处理,可以得到我们想要的结果
因python环境会把“\”解释为转义字符,所以up主在视频中讲解了绝对路径中“\”会出现的问题,本人在这里有一个解决办法,即将路径中所有“\”换成“/”即可
如何使用tensor格式
from PIL import Image
from torchvision import transforms
#绝对路径 "G:\深度学习\pytorch(xiaotudui)\demo\练手数据集\train\ants_image\0013035.jpg"
#相对路径 “练手数据集/train/ants_image/0013035.jpg"
img_path = "练手数据集/train/ants_image/0013035.jpg"
img = Image.open(img_path)
# 1 transforms该如何使用 (python)
tensor_trans = transforms.ToTensor()
tensor_img = tensor_trans(img)
print(tensor_img)
这段代码实现了将img_path路径对应的图片转换成tensor格式
输出结果为
实际常用用途
Transforms(2)
tensor格式包含了神经网络中所需理论基础的参数
课程代码
from torch.utils.data import Dataset, DataLoader
from PIL import Image
import os
from torchvision import transforms
from torch.utils.tensorboard import SummaryWriter
#绝对路径 "G:\深度学习\pytorch(xiaotudui)\demo\练手数据集\train\ants_image\0013035.jpg"
#相对路径 “练手数据集/train/ants_image/0013035.jpg"
img_path = "练手数据集/train/ants_image/0013035.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()
常见的Transforms
from PIL import Image
from torchvision import transforms
from torch.utils.tensorboard import SummaryWriter
img_path = "练手数据集/train/ants_image/0013035.jpg"
img = Image.open(img_path)
writer = SummaryWriter("logs")
1.Totensor
#Totensor
tensor_trans = transforms.ToTensor()
tensor_img = tensor_trans(img)
writer.add_image("Tensor_img", tensor_img)
2.Normalize(归一化)
#Normalize
print(tensor_img[0][0][0])
trans_norm = transforms.Normalize([0.5,0.5,0.5],[0.5,0.5,0.5])
img_norm = trans_norm(tensor_img)
print(img_norm[0][0][0])
writer.add_image("Normalize",img_norm)
归一化公式为:
output[channel] = (input[channel] - mean[channel]) / std[channel]
归一化前后输出结果对比:
3.Resize(对图片进行放大缩小)
#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 = tensor_trans(img_resize)
writer.add_image("Resize",img_resize)
print(img_resize)
运行结果:
4.Compose
#Compose -> resize -2
trans_resize_2 = transforms.Resize(512)
#PIL -> PIL -> tensor
trans_compose = transforms.Compose([trans_resize_2,tensor_trans])
img_resize_2 = trans_compose(img)
writer.add_image("Resize",img_resize_2,1)
5.RandomCrop(随机裁剪)
#RandomCrop
trans_random = transforms.RandomCrop(512)
trans_compose_2 = transforms.Compose([trans_random,tensor_trans])
for i in range(10):
img_crop = trans_compose_2(img)
writer.add_image("RandomCrop",img_crop,i)
注意:使用RandomCrop进行裁剪时,裁剪尺寸需小于图片大小,若报错,可将代码中512调小重试
总结使用方法
关注输入和输出类型,多看官方文档
关注方法需要什么函数
不知道类型时
print;print(type());debug
Torchvision中的数据集使用
下载CIFAR10数据集
import torchvision
train_set = torchvision.datasets.CIFAR10(root="./dataset", train=True, download=True)
test_set = torchvision.datasets.CIFAR10(root="./dataset", train=False, download=True)
使用数据集
import torchvision
from torch.utils.tensorboard import SummaryWriter
dataset_transform = torchvision.transforms.Compose([
torchvision.transforms.ToTensor()
])
train_set = torchvision.datasets.CIFAR10(root="./dataset", train=True, transform=dataset_transform, download=True)
test_set = torchvision.datasets.CIFAR10(root="./dataset", 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()
#
# print(test_set[0])
writer = SummaryWriter("p10")
for i in range(10):
img, target = test_set[i]
writer.add_image("test_set", img, i)
writer.close()
DataLoader的使用
测试数据集中第一张图片及target
import torchvision
# 准备的测试数据集
from torch.utils.data import DataLoader
test_data = torchvision.datasets.CIFAR10("./dataset", train=False, transform=torchvision.transforms.ToTensor())
test_loader = DataLoader(dataset=test_data, batch_size=64, shuffle=True, num_workers=0, drop_last=True)
# 测试数据集中第一张图片及target
img, target = test_data[0]
print(img.shape)
print(target)
各个参数作用
- dataset:表示要加载的数据集,可以是一个PyTorch的Dataset对象。
- batch_size:表示每个batch中的样本数,一般来说,batch_size越大,模型训练的效率越高,但同时也会占用更多的内存。一般建议使用2的幂次方作为batch_size,例如64、128等。(举例说明,如果你有一个包含 1000 个测试样本的数据集,并且设置
batch_size
为 64,那么DataLoader
将会将这 1000 个样本分成若干个大小为 64 的批次,最后一个批次可能会包含不足 64 个样本。) - shuffle:表示是否在每个epoch开始的时候打乱数据集。如果为True,每个epoch开始时数据集都会被重新打乱,这样可以避免模型对数据集中顺序的依赖,从而提高模型的泛化能力。
- num_workers:表示用于数据加载的子进程数。如果设置为0,表示数据将在主进程中加载;如果设置为大于0的值,表示数据将在多个子进程中并行加载,可以加快数据加载的速度。一般建议将其设置为CPU核心数的一半左右。
- drop_last:表示在数据集不能被batch_size整除时,是否丢弃最后一个batch。如果为True,表示丢弃最后一个batch,否则最后一个batch的大小可能小于batch_size。
神经网络的基本使用
前向传播
官方forward流程
输出比输入加一的简单神经网络
# 作者:EternalAurora
import torch
from torch import nn
class Aurora(nn.Module):
def __init__(self):
super().__init__()
def forward(self, input):
output = input + 1
return output
aurora = Aurora()
x = torch.tensor(1.0)
output = aurora(x)
print(output)
卷积
卷积操作是深度学习神经网络中的基础操作,建议还不知道是什么意思的同学先去学一下什么是卷积,本文在此不做过多赘述
实现上述操作代码
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) #采用cov2d卷积
print(output)
conv2d参数讲解:
stride:
上述conv2d中stride参数设置为1,代表卷积核移动步长为1,若改为2,输出结果也会发生相应变化
output = F.conv2d(input,kernel,stride=1) #步长为1
print(output)
output = F.conv2d(input,kernel,stride=2) #步长为2
print(output)
输出结果
padding
对原始矩阵进行边缘填充操作,padding为1时,原始输入矩阵会变成如下样式,随后进行上述卷积操作
新的输出结果
output3 = F.conv2d(input,kernel,stride=1,padding=1)
print(output3)
神经网络-卷积层
import torch
import torchvision
from torch import nn
from torch.nn import Conv2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
dataset = torchvision.datasets.CIFAR10("./dataset", train=False, transform=torchvision.transforms.ToTensor(),
download=True) #准备数据集
dataloader = DataLoader(dataset, batch_size=64)
class Aurora(nn.Module):
def __init__(self):
super(Aurora, self).__init__()
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
aurora = Aurora()
writer = SummaryWriter("./logs")
step = 0
for data in dataloader:
imgs, targets = data
output = aurora(imgs)
print(imgs.shape)
print(output.shape)
# torch.Size([64, 3, 32, 32])
writer.add_images("input", imgs, step)
# torch.Size([64, 6, 30, 30]) -> [xxx, 3, 30, 30]
output = torch.reshape(output, (-1, 3, 30, 30))
writer.add_images("output", output, step)
step = step + 1
writer.close()
对CIFAR10数据集进行卷积操作,并将将3通道转换成6通道
神经网络-池化层(最大池化)
参数
ceil_mode
代码
import torch
import torchvision
from torch import nn
from torch.nn import MaxPool2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
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]],dtype=torch.float32)
input = torch.reshape(input,(-1,1,5,5))
class Aurora(nn.Module):
def __init__(self):
super(Aurora, self).__init__()
self.maxpool1 = MaxPool2d(kernel_size=3, ceil_mode=True)
def forward(self, input):
output = self.maxpool1(input)
return output
aurora = Aurora()
output = aurora(input)
print(output)
结果
self.maxpool1 = MaxPool2d(kernel_size=3, ceil_mode=False)
作用
保留数据特征,减少数据量,增加训练速度
神经网络-非线性激活
Relu-大于等于0的数返回原值,小于0的数取0
inplace=True:对原变量进行替换
inplace=Flase: 不对原变量进行替换(默认)
import torch
import torchvision
from torch import nn
from torch.nn import ReLU, Sigmoid
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
input = torch.tensor([[1, -0.5],
[-1, 3]])
input = torch.reshape(input, (-1, 1, 2, 2))
print(input.shape)
class Aurora(nn.Module):
def __init__(self):
super(Aurora, self).__init__()
self.relu1 = ReLU()
def forward(self, input):
output = self.relu1(input)
return output
aurora = Aurora()
output = aurora(input)
print(output)
运行结果
神经网络-线性层及其他层介绍
线性层
import torch
import torchvision
from torch import nn
from torch.nn import ReLU, Sigmoid
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
input = torch.tensor([[1, -0.5],
[-1, 3]])
input = torch.reshape(input, (-1, 1, 2, 2))
print(input.shape)
dataset = torchvision.datasets.CIFAR10("./dataset", train=False, download=True,
transform=torchvision.transforms.ToTensor())
dataloader = DataLoader(dataset, batch_size=64)
class Aurora(nn.Module):
def __init__(self):
super(Aurora, self).__init__()
self.relu1 = ReLU()
self.sigmoid1 = Sigmoid()
def forward(self, input):
output = self.sigmoid1(input)
return output
aurora = Aurora()
writer = SummaryWriter("./logs")
step = 0
for data in dataloader:
imgs, targets = data
writer.add_images("input", imgs, global_step=step)
output = aurora(imgs)
writer.add_images("output", output, step)
step += 1
writer.close()
torch.flatten()
import torch
import torchvision
from torch import nn
from torch.nn import Linear
from torch.utils.data import DataLoader
dataset = torchvision.datasets.CIFAR10("./dataset", train=False, transform=torchvision.transforms.ToTensor(),
download=True)
dataloader = DataLoader(dataset, batch_size=64, drop_last=True)
class Aurora(nn.Module):
def __init__(self):
super(Aurora, self).__init__()
self.linear1 = Linear(196608, 10)
def forward(self, input):
output = self.linear1(input)
return output
aurora = Aurora()
for data in dataloader:
imgs, targets = data
print(imgs.shape)
output = torch.flatten(imgs)
print(output.shape)
output = aurora(output)
print(output.shape)
结果:
神经网络-搭建小实战
复刻CIFAR -10神经层![](https://i-blog.csdnimg.cn/blog_migrate/157b92bea45f69bacfc8a6dd48deba85.png)
class Aurora(nn.Module):
def __init__(self):
super(Aurora, 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
输入一个数据(64,3,32,32)
在tensorboard中查看神经网络
writer = SummaryWriter("./logs_seq")
writer.add_graph(aurora, input)
writer.close()
损失函数与反向传播
LossFuction
1:计算实际输出与目标之间的差距
2:为我们更新输出提供一定的依据
L1Loss:
import torch
from torch.nn import L1Loss
from torch import nn
inputs = torch.tensor([1, 2, 3], dtype=torch.float32)
targets = torch.tensor([1, 2, 5], dtype=torch.float32)
inputs = torch.reshape(inputs, (1, 1, 1, 3))
targets = torch.reshape(targets, (1, 1, 1, 3))
loss = L1Loss(reduction='sum') #采用损失求和
result = loss(inputs, targets)
print(result)
MSELOSS(平方差)
loss_mse = nn.MSELoss()
result_mse = loss_mse(inputs,targets)
print(result_mse)
CrossEntropyLoss
x = torch.tensor([0.1, 0.2, 0.3])
y = torch.tensor([1])
x = torch.reshape(x, (1, 3))
loss_cross = nn.CrossEntropyLoss()
result_cross = loss_cross(x, y)
print(result_cross)
反向传播
尝试如何调整神经网络中的参数才会导致最终的loss变小(因为从loss开始推导参数,和网络的顺序相反,所以叫反向传播)
梯度可以理解为斜率
优化器
torch.optim
dataloader = DataLoader(dataset, batch_size=1)
class Aurora(nn.Module):
def __init__(self):
super(Aurora, 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()
aurora = Aurora()
optim = torch.optim.SGD(aurora.parameters(), lr=0.01) #定义优化器 设置学习速率为0.01
for epoch in range(20):
running_loss = 0.0
for data in dataloader:
imgs, targets = data
outputs = aurora(imgs)
result_loss = loss(outputs, targets)
optim.zero_grad() #梯度设置为0
result_loss.backward() #反向传播
optim.step()
running_loss = running_loss + result_loss #每一轮整体误差的总和
print(running_loss)
现有网络模型的使用及修改
pretrained=False时,使用默认参数
pretrained=True时,使用数据集中训练好的参数
对vgg_true进行输出线性层修改,将1000改为10
vgg16_true.classifier.add_module('add_linear', nn.Linear(1000, 10))
对vgg_flase进行输出线性层修改,将1096改为10
vgg16_false.classifier[6] = nn.Linear(4096, 10)
总代码
import torchvision
import os
from torch import nn
os.environ['TORCH_HOME'] = 'G:\深度学习\pytorch(xiaotudui)/vgg16' #修改模型下载路径
vgg16_false = torchvision.models.vgg16(pretrained=False)
vgg16_true = torchvision.models.vgg16(pretrained=True)
print(vgg16_true)
vgg16_true.classifier.add_module('add_linear', nn.Linear(1000, 10))
print(vgg16_true)
print(vgg16_false)
vgg16_false.classifier[6] = nn.Linear(4096, 10)
print(vgg16_false)
网络模型的保存与读取
方式一:
import torch
import torchvision
from torch import nn
vgg16 = torchvision.models.vgg16(pretrained=False)
# 保存方式1,模型结构+模型参数
torch.save(vgg16, "vgg16_method1.pth")
import torch
#方式一 -> 保存方式1,加载模型
model = torch.load("G:\深度学习\pytorch(xiaotudui)\demo/vgg16_method1.pth")
print(model)
注意:
使用方式一时,在进行加载自己的网络模型时,会报错,无法访问到定义的方式
解决方法:
一:将网络模型复制到加载模型的代码页中(较麻烦,不用)
二:使用from model_save(编写网络模型的代码页) import * ,将所有定义引入到新的代码页
方式二
#保存方式2,模型参数(官方推荐)
torch.save(vgg16.state_dict(), "vgg16_method2.pth")
#方式二 -> 保存方式2,加载模型
model = torch.load("G:\深度学习\pytorch(xiaotudui)\demo/vgg16_method2.pth")
print(model)
注意:
方式二会将模型以字典的形式进行保存,所以加载时需要将字典形式还原
vgg16 = torchvision.models.vgg16(pretrained=False)
vgg16.load_state_dict(torch.load("vgg16_method2.pth"))
print(vgg16)
完整的模型训练套路:
import torchvision
from torch.utils.tensorboard import SummaryWriter
from model import *
#准备数据集
from torch.utils.data import DataLoader
train_data = torchvision.datasets.CIFAR10(root="./dataset", train=True, transform=torchvision.transforms.ToTensor(),
download=True) #导入训练数据集
test_data = torchvision.datasets.CIFAR10(root="./dataset", train=False, transform=torchvision.transforms.ToTensor(),
download=True) #导入测试数据集
# length 长度
train_data_size = len(train_data)
test_data_size = len(test_data)
# 如果train_data_size=10, 训练数据集的长度为:10
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)
#创建网络模型
aurora = Aurora()
#损失函数
loss_fn = nn.CrossEntropyLoss()
# 优化器
# learning_rate = 0.01
# 1e-2=1 x (10)^(-2) = 1 /100 = 0.01
learning_rate = 1e-2
optimizer = torch.optim.SGD(aurora.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))
# 训练步骤开始
aurora.train() #让网络进入训练状态
for data in train_dataloader:
imgs, targets = data
outputs = aurora(imgs)
loss = loss_fn(outputs, targets)
# 优化器优化模型
optimizer.zero_grad() #梯度清零
loss.backward() #反向传播
optimizer.step()
total_train_step = total_train_step + 1 #训练次数+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)
# 测试步骤开始
aurora.eval()
total_test_loss = 0
total_accuracy = 0
with torch.no_grad(): #不进行梯度优化
for data in test_dataloader:
imgs, targets = data
outputs = aurora(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(aurora, "aurora_{}.pth".format(i))
print("模型已保存")
writer.close()
import torch
from torch import nn
# 搭建神经网络
class Aurora(nn.Module):
def __init__(self):
super(Aurora, self).__init__()
self.model = nn.Sequential(
nn.Conv2d(3, 32, 5, 1, 2),
nn.MaxPool2d(2),
nn.Conv2d(32, 32, 5, 1, 2),
nn.MaxPool2d(2),
nn.Conv2d(32, 64, 5, 1, 2),
nn.MaxPool2d(2),
nn.Flatten(),
nn.Linear(64*4*4, 64),
nn.Linear(64, 10)
)
def forward(self, x):
x = self.model(x)
return x
if __name__ == '__main__':
aurora = Aurora()
input = torch.ones((64, 3, 32, 32))
output = aurora(input)
print(output.shape) #验证输出是否正确
利用GPU加速
网络模型,数据(输入,标注),损失函数可以用gpu加速
方式一:
xxx.cuda()
import torch
import torchvision
from torch import nn
from torch.utils.tensorboard import SummaryWriter
#from model import *
#准备数据集
from torch.utils.data import DataLoader
train_data = torchvision.datasets.CIFAR10(root="./dataset", train=True, transform=torchvision.transforms.ToTensor(),
download=True) #导入训练数据集
test_data = torchvision.datasets.CIFAR10(root="./dataset", train=False, transform=torchvision.transforms.ToTensor(),
download=True) #导入测试数据集
# length 长度
train_data_size = len(train_data)
test_data_size = len(test_data)
# 如果train_data_size=10, 训练数据集的长度为:10
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 Aurora(nn.Module):
def __init__(self):
super(Aurora, self).__init__()
self.model = nn.Sequential(
nn.Conv2d(3, 32, 5, 1, 2),
nn.MaxPool2d(2),
nn.Conv2d(32, 32, 5, 1, 2),
nn.MaxPool2d(2),
nn.Conv2d(32, 64, 5, 1, 2),
nn.MaxPool2d(2),
nn.Flatten(),
nn.Linear(64*4*4, 64),
nn.Linear(64, 10)
)
def forward(self, x):
x = self.model(x)
return x
aurora = Aurora()
if torch.cuda.is_available():
aurora=aurora.cuda()
#损失函数
loss_fn = nn.CrossEntropyLoss()
if torch.cuda.is_available():
loss_fn = loss_fn.cuda()
# 优化器
# learning_rate = 0.01
# 1e-2=1 x (10)^(-2) = 1 /100 = 0.01
learning_rate = 1e-2
optimizer = torch.optim.SGD(aurora.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))
# 训练步骤开始
aurora.train() #让网络进入训练状态
for data in train_dataloader:
imgs, targets = data
if torch.cuda.is_available():
imgs=imgs.cuda()
targets=targets.cuda()
outputs = aurora(imgs)
loss = loss_fn(outputs, targets)
# 优化器优化模型
optimizer.zero_grad() #梯度清零
loss.backward() #反向传播
optimizer.step()
total_train_step = total_train_step + 1 #训练次数+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)
# 测试步骤开始
aurora.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 = aurora(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(aurora, "aurora_{}.pth".format(i))
print("模型已保存")
writer.close()
方式二:
device = torch.device("cuda:0")
xxx.to(device)
import torch
import torchvision
from torch import nn
from torch.utils.tensorboard import SummaryWriter
#from model import *
# 定义训练的设备
device = torch.device("cuda:0")
#准备数据集
from torch.utils.data import DataLoader
train_data = torchvision.datasets.CIFAR10(root="./dataset", train=True, transform=torchvision.transforms.ToTensor(),
download=True) #导入训练数据集
test_data = torchvision.datasets.CIFAR10(root="./dataset", train=False, transform=torchvision.transforms.ToTensor(),
download=True) #导入测试数据集
# length 长度
train_data_size = len(train_data)
test_data_size = len(test_data)
# 如果train_data_size=10, 训练数据集的长度为:10
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 Aurora(nn.Module):
def __init__(self):
super(Aurora, self).__init__()
self.model = nn.Sequential(
nn.Conv2d(3, 32, 5, 1, 2),
nn.MaxPool2d(2),
nn.Conv2d(32, 32, 5, 1, 2),
nn.MaxPool2d(2),
nn.Conv2d(32, 64, 5, 1, 2),
nn.MaxPool2d(2),
nn.Flatten(),
nn.Linear(64*4*4, 64),
nn.Linear(64, 10)
)
def forward(self, x):
x = self.model(x)
return x
aurora = Aurora()
aurora = aurora.to(device)
#损失函数
loss_fn = nn.CrossEntropyLoss()
loss_fn = loss_fn.to(device)
# 优化器
# learning_rate = 0.01
# 1e-2=1 x (10)^(-2) = 1 /100 = 0.01
learning_rate = 1e-2
optimizer = torch.optim.SGD(aurora.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))
# 训练步骤开始
aurora.train() #让网络进入训练状态
for data in train_dataloader:
imgs, targets = data
imgs = imgs.to(device)
targets = targets.to(device)
outputs = aurora(imgs)
loss = loss_fn(outputs, targets)
# 优化器优化模型
optimizer.zero_grad() #梯度清零
loss.backward() #反向传播
optimizer.step()
total_train_step = total_train_step + 1 #训练次数+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)
# 测试步骤开始
aurora.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 = aurora(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(aurora, "aurora_{}.pth".format(i))
print("模型已保存")
writer.close()
完整的模型验证套路
利用已经训练好的数据集,给它提供输入
import torch
import torchvision
from PIL import Image
from model import *
image_path = "G:\深度学习\pytorch(xiaotudui)\demo\imgs/airplane.png"
image = Image.open(image_path)
print(image)
image = image.convert('RGB')
transform = torchvision.transforms.Compose([torchvision.transforms.Resize((32, 32)),
torchvision.transforms.ToTensor()])
image = transform(image)
print(image.shape)
model = torch.load("aurora_9.pth", map_location=torch.device('cpu')) #将用gpu训练的模型映射到cpu上
print(model)
image = torch.reshape(image, (1, 3, 32, 32))
model.eval()
with torch.no_grad():
output = model(image)
print(output)
print(output.argmax(1))