笔者在复现顶会论文时,发现大多作文采用PyTorch而非tensorflow+keras搭建,因此需要补充学习PyTorch基础。同时,记录学习过程~
1.相关代码教程
2.B站视频
在官网根据需要生成命令行即可。需要注意的是:所选择的cuda版本需小于系统的版本
nvcc -V #查看cuda版本
验证是否成功安装
python #进入python环境
import torch
torch.cuda.is_available() #返回true则安装成功
dir: 查看工具包的子类
help: 查看具体某个函数的作用
from torch.utils.data import Dataset
from PIL import Image
import os
#定义MyData类继承Dataset抽象类,并且定义初始化全局变量,重写__getitem__和__len__方法
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 = "src/dataset/train"
ants_label_dir = "ants"
ants_dataset = MyData(root_dir,ants_label_dir)
数据如何加载
import torchvision
# 准备的测试数据集
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
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)
writer = SummaryWriter("logs")
for epoch in range(2):
step = 0
for data in test_loader:
imgs, targets = data
# print(imgs.shape)
# print(targets)
writer.add_images("Epoch: {}".format(epoch), imgs, step)
step = step + 1
writer.close()
TensorBoard 是一组用于数据可视化的工具
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter("logs") //记录事件文件至log文件夹
for i in range(100):
writer.add_scalar("y=x",i,i)
writer.close()
执行完上述测试样例后,需要将目录定位到logs的父目录,然后使用如下指令
tensorboard --logdir=logs //默认6006端口,当然亦可以修改
tensorboard --logdir=logs --port=6007
注意:为避免历史logs对绘图的影响,一般需要将logs文件夹清空,重新执行tensorboard --logdir=logs
from torch.utils.tensorboard import SummaryWriter
import numpy as np
from PIL import Image
writer = SummaryWriter("logs")
image_path = "dataset/train/ants/148715752_302c84f5a4.jpg"
img_PIL = Image.open(image_path)
img_array = np.array(img_PIL) #将img变量转为numpy,符合add_image的传参需求
print(type(img_array))
print(img_array.shape)
#dataformats默认是(1, H, W),否则需指定
writer.add_image("train", img_array, 2, dataformats='HWC')
writer.close()
本质是一个工具包,可以对图片进行工作操作
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
from PIL import Image
writer = SummaryWriter("logs")
img = Image.open("dataset/train/ants/0013035.jpg")
print(img)
#ToTensor
trans_totensor = transforms.ToTensor()
img_tensor = trans_totensor(img)
writer.add_image("ToTensor", img_tensor)
writer.close()
#Normalize
print(img_tensor[0][0][0])
#归一化公式
#output[channel] = (input[channel] - mean[channel]) / std[channel]
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)
writer.close()
简单的使用
import torch
from torch import nn
class Net(nn.Module):
def __init__(self) -> None:
super().__init__()
def forward(self, input):
output = input + 1.0
return output
net = Net()
x = torch.tensor(1.0)
output = net(x)
print(output)
Loss作用:
1.比较网络输出玉实际标签的误差;
2.为反向传播时的梯度更新提供依据。
# 保存方式1,模型结构+模型参数
torch.save(vgg16, "vgg16_method1.pth")
# 保存方式2,模型参数(官方推荐)
torch.save(vgg16.state_dict(), "vgg16_method2.pth")
# 方式1-》保存方式1,加载模型
import torchvision
from torch import nn
model = torch.load("vgg16_method1.pth")
# print(model)
# 方式2,加载模型
vgg16 = torchvision.models.vgg16(pretrained=False)
vgg16.load_state_dict(torch.load("vgg16_method2.pth"))
# model = torch.load("vgg16_method2.pth")
print(vgg16)
搭建模型 model.py
import torch
from torch import nn
# 搭建神经网络
class Tudui(nn.Module):
def __init__(self):
super(Tudui, 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__':
tudui = Tudui()
input = torch.ones((64, 3, 32, 32))
output = tudui(input)
print(output.shape)
训练&测试 train.py
import torchvision
from torch.utils.tensorboard import SummaryWriter
from model import *
# 准备数据集
from torch import nn
from torch.utils.data import DataLoader
train_data = torchvision.datasets.CIFAR10("./dataset", train=False, transform=torchvision.transforms.ToTensor())
test_data = torchvision.datasets.CIFAR10("./dataset", train=False, transform=torchvision.transforms.ToTensor())
# 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)
# 创建网络模型
tudui = Tudui()
# 损失函数
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(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))
# 训练步骤开始
tudui.train()
for data in train_dataloader:
imgs, targets = data
outputs = tudui(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)
# 测试步骤开始
tudui.eval()
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.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(tudui, "tudui_{}.pth".format(i))
print("模型已保存")
writer.close()
在6.1版本上修改4处
# 创建网络模型
tudui = Tudui()
if torch.cuda.is_available():
tudui = tudui.cuda()
# 损失函数
loss_fn = nn.CrossEntropyLoss()
if torch.cuda.is_available():
loss_fn = loss_fn.cuda()
for i in range(epoch):
print("-------第 {} 轮训练开始-------".format(i+1))
# 训练步骤开始
tudui.train()
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)
# 定义训练的设备
device = torch.device("cuda:0")
tudui = tudui.to(device)
loss_fn = loss_fn.to(device)
需要在anaconda prompt激活所需要的环境,然后命令行输入
jupyter notebook
linux环境下需要先打开anaconda prompt