1.dir()和help()指令的使用
import torch
dir(torch) #打开torch这个文件夹
dir(torch.cuda) #打开torch文件夹下的cuda文件夹
dir(torch.cuda.is_available)#此时打开的结果为下面所示的有杠杠的内容,此时表示is_available是一个函数。
['__annotations__',
'__call__',
'__class__',
'__closure__',
'__code__',
'__defaults__',
'__delattr__',
'__dict__']
help(torch.cuda.is_available) #用help函数查看此函数是怎么用的
Help on function is_available in module torch.cuda:
is_available() -> bool
Returns a bool indicating if CUDA is currently available. #返回一个布尔值看cuda是否可用
2.name.py文件、python控制台和jupyter写代码的区别
3.Dateset和Dateloader使用方法
本质内容就是对数据的筛选和后面的编号及分类,包括train文件和label(是什么),label中是一个txt文件。
from torch.utils.data import Dataset #打开一些文件夹,进入Dateset中
help(Dataset) #查看使用方法
Dateset?? #更直白的jupyter查看使用方法的代码
4.TensorBoard的使用
先使用tensorboard中的summarywriter库中的函数(add_scalar和add_image)来生成相应的日志文件,再用tensorboard打开此日志文件。
pip install tensorboard #安装
from touch.utils.tensorboard import SummaryWriter #tensorboard的调用
writer=SummaryWriter("logs") #文件保存地址的命名,为logs
for i in range(100):
writer.add_scalar("y=x",i,i) #y=x是生成图像的命名,后面是先x后y的关系赋值
writer.close() #中间一部分是代码的运行生成结果文件
tensorboard --logdir=logs #打开目标文件夹下的生成的结果,点击链接会生成一个可视化的窗口
tensorboard --logdir=logs --port=6007 #port用来指定端口到6007,避免某一个端口太卡
writer.add_image("train", img_array, 1, dataformats='HWC') #图像函数的格式,名称、检测图像等
5.Transforms的使用
from PIL import Image
img_path="d\..."
Image.opean(img_path) #用Image.opean函数打开图片,格式为PIL格式
import cv2
cv2.imread() #用此函数将图片生成为numpy格式
from torchvesion import transforms #transforms的引入
a=transforms.ToTensor() #另a为transforms中的totensor函数
img=a() #a()就是转义过后的图片
使用transforms的目的是使图片变为tensor格式,而后用tensorboard进行一个可视化的展示。
6.torchvision和transforms联合的使用
torchvision内置了很多常见的数据集,可以直接使用命令进行数据集的下载。
import torchvision
a=torchvision.transforms.Compose([torchvision.transforms.ToTensor()])
torchision.datesets.CIFAR10(root="./datesets",train=true,transforms=a,download=true) #这里就是进入了torchvion的数据库里面,选择一个数据集进行后面括号内的配置(储存位置,是否下载等),其中transforms体现的就是两者的联动
7.DataLoader的使用
from torch.utils.data import DataLoader
DataLoader(dataset=~~,batch_size=4,shuffle=True,num_workers=0,drop_last=Fase)
#DataLoader函数的使用,包括定义数据集,设定每次取出的数据数量,是否打乱顺序,是否开启多线程和是否舍余
8.神经网络的引入
import torch
from torch import nn
class sth(nn.Module)
9.如何实现卷积
import torch
import torch.nn.functional as F #导入卷积函数,torch.nn.functional函数的使用
input=torch.tensor([[1,8,5],
[2,5,8],
[5,8,4]]) #二维矩阵的表达方式
kernel=torch.tensor([[2,5],
[3,7]]) #卷积核矩阵,卷积核就是权重weight
input=torch.reshape(input,(1,1,5,5))
kernel=torch.reshape(kernel,(1,1,3,3)) #重新定义形状,1步进、1通道和3x3矩阵
output=F.conv2d(input,kernel,stride=1,padding=0) #输入、卷积核、步进和填充
10.torch.nn 卷积层函数的使用
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("../data",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.conv1 = Conv2d(in_channels=3, out_channels=6, kernel_size=3, stride=1, padding=0) #卷积函数,输入三通道、输出六通道、卷积核3x3、步进是1和0填充
def forward(self, x):
x = self.conv1(x)
return x
tudui = Tudui()
writer = SummaryWriter("../logs") #可视化操作
step = 0
for data in dataloader:
imgs, targets = data
output = tudui(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
11.torch.nn池化层
最大池化的作用:压缩数据,保留特征。在保留图片的特征的情况下,减少图片相应的数据量,可以使检测更为快捷。马赛克的效果。
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
dataset = torchvision.datasets.CIFAR10("../data", train=False, download=True,
transform=torchvision.transforms.ToTensor())
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=False) #最大池化,卷积核尺寸和是否舍余
def forward(self, input):
output = self.maxpool1(input)
return output
tudui = Tudui()
writer = SummaryWriter("../logs_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()
12.非线性激活
![](https://i-blog.csdnimg.cn/blog_migrate/faadebe8ddf6f7b8dd4c7b39f35e21b1.png)
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("../data", train=False, download=True,
transform=torchvision.transforms.ToTensor())
dataloader = DataLoader(dataset, batch_size=64)
class Tudui(nn.Module):
def __init__(self):
super(Tudui, self).__init__()
self.relu1 = ReLU()
self.sigmoid1 = Sigmoid()
def forward(self, input):
output = self.sigmoid1(input)
return output
tudui = Tudui()
writer = SummaryWriter("../logs_relu")
step = 0
for data in dataloader:
imgs, targets = data
writer.add_images("input", imgs, global_step=step)
output = tudui(imgs)
writer.add_images("output", output, step)
step += 1
writer.close()
13.sequential的使用方法
import torch
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential
from torch.utils.tensorboard import SummaryWriter
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
tudui = Tudui()
print(tudui)
input = torch.ones((64, 3, 32, 32))
output = tudui(input)
print(output.shape)
writer = SummaryWriter("../logs_seq")
writer.add_graph(tudui, input)
writer.close()
sequential可以使卷积操作简化许多
14.损失函数loss functions
反向传播:backward
15.优化器torch.optim
import torch
import torchvision
from torch import nn
from torch.nn import Sequential, Conv2d, MaxPool2d, Flatten, Linear
from torch.optim.lr_scheduler import StepLR
from torch.utils.data import DataLoader
dataset = torchvision.datasets.CIFAR10("../data", 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()
optim = torch.optim.SGD(tudui.parameters(), lr=0.01)
scheduler = StepLR(optim, step_size=5, gamma=0.1)
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()
scheduler.step()
running_loss = running_loss + result_loss
print(running_loss)
16.现有网络模型的训练和修改
import torchvision
# train_data = torchvision.datasets.ImageNet("../data_image_net", split='train', download=True,
# transform=torchvision.transforms.ToTensor())
from torch import nn
vgg16_false = torchvision.models.vgg16(pretrained=False)
vgg16_true = torchvision.models.vgg16(pretrained=True)
print(vgg16_true)
train_data = torchvision.datasets.CIFAR10('../data', train=True, transform=torchvision.transforms.ToTensor(),
download=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)
17.模型的保存
import torch
import torchvision
from torch import nn
vgg16 = torchvision.models.vgg16(pretrained=False)
# 保存方式1,模型结构+模型参数
torch.save(vgg16, "vgg16_method1.pth")
# 保存方式2,模型参数(官方推荐)
torch.save(vgg16.state_dict(), "vgg16_method2.pth")
# 陷阱
class Tudui(nn.Module):
def __init__(self):
super(Tudui, self).__init__()
self.conv1 = nn.Conv2d(3, 64, kernel_size=3)
def forward(self, x):
x = self.conv1(x)
return x
tudui = Tudui()
torch.save(tudui, "tudui_method1.pth")
import torch
from model_save import *
# 方式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)
# 陷阱1 #方式1加载时,要把以下代码引入
# class Tudui(nn.Module):
# def __init__(self):
# super(Tudui, self).__init__()
# self.conv1 = nn.Conv2d(3, 64, kernel_size=3)
#
# def forward(self, x):
# x = self.conv1(x)
# return x
model = torch.load('tudui_method1.pth')
print(model)
18.完整的模型训练套路
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(root="../data", train=True, transform=torchvision.transforms.ToTensor(),
download=True)
test_data = torchvision.datasets.CIFAR10(root="../data", 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)
# 创建网络模型 一般都单独在一个py文件中
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()
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)
19.如何调用GPU进行训练
# 定义训练的设备
device = torch.device("cuda")
tudui = tudui.to(device)
loss_fn = loss_fn.to(device)
imgs = imgs.to(device)
20.完整的模型训练套路