Pytorch学习笔记
【小土堆】 视频链接:https://www.bilibili.com/video/BV1hE411t7RN?p=1
1. torchvision数据集dataset的使用
import torchvision
train_set = torchvision.datasets.CIFAR10(root="../dataset", train=True,
transform=data_transforms, download=True)
test_set = torchvision.datasets.CIFAR10(root="../dataset", train=False,
transform=data_transforms, download=True)
print(test_set[0])
print(test_set.classes)
img, target = test_set[0]
print(img)
print(target)
img.show()
2. DataLoader的使用
test_dataloader = DataLoader(dataset=test_set, batch_size=64, shuffle=True,
num_workers=0, drop_last=False)
3. 神经网络的基本骨架 nn.Module的使用
input → forward → output #forward函数就是神经网络执行的步骤
class Tudui(nn.Module):
def __init__(self):
super().__init__()
def forward(self, input):
output = input + 1
return output
tudui = Tudui()
x = torch.tensor(1.0)
output = tudui(x)
print(output)
4. nn.convolution的使用
展示conv2d的工作过程
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, padding=0)
print(output)
tensor([[[[10, 12, 12],
[18, 16, 16],
[13, 9, 3]]]])
训练中使用conv2d函数
dateset = torchvision.datasets.CIFAR10("../data", train=False,
transform=torchvision.transforms.ToTensor(), download=True)
dataloader = DataLoader(dateset, batch_size=64)
class Tudui(nn.Module):
def __init__(self):
super().__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
tudui = Tudui()
for data in dataloader:
imgs, targets = data
output = tudui(imgs)
print(imgs.shape)
print(output.shape)
5. nn.MaxPool2d最大池化的使用
最大池化的作用
保留主要特征的同时减少参数(降低纬度,类似PCA)和计算量,防止过拟合
具体计算过程
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))
print(input.shape)
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()
output = tudui(input)
print(output)
tensor([[[[2., 3.],
[5., 1.]]]])
6. 搭建小实战和sequential的使用
class Tudui(nn.Module):
def __init__(self):
super().__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()
input = torch.ones((64, 3, 32, 32))
output = tudui(input)
print(output.shape)
writer = SummaryWriter("logs_seq")
writer.add_graph(tudui, input)
writer.close()
7. 损失函数和优化器
损失函数的工作原理
input = torch.tensor([1, 2, 3], dtype=torch.float32)
target = torch.tensor([1, 2, 5], dtype=torch.float32)
input = torch.reshape(input, (1, 1, 1, 3))
target = torch.reshape(target, (1, 1, 1, 3))
loss = L1Loss()
result = loss(input, target)
print(result)
{(1-1)+(2-2)+(5-3)}/3 = 0.667
tensor(0.6667)
损失函数和优化器的使用
dataset = torchvision.datasets.CIFAR10(root="./dataset", train=False,
transform=torchvision.transforms.ToTensor(), download=True)
dataloader = DataLoader(dataset, batch_size=64)
class Tudui(nn.Module):
def __init__(self):
super().__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()
loss = nn.CrossEntropyLoss()
optim = torch.optim.SGD(tudui.parameters(), lr=0.01)
for opoc in range(20):
all_loss = 0
for data in dataloader:
imgs, target = data
output = tudui(imgs)
result_loss = loss(output, target)
optim.zero_grad()
result_loss.backward()
optim.step()
all_loss = all_loss+result_loss
print(all_loss)
8. 模型修改
vgg16_true = torchvision.models.vgg16(pretrained=True)
vgg16_false = torchvision.models.vgg16(pretrained=False)
print(vgg16_true)
vgg16_true.classifier.add_module("add_linear", nn.Linear(in_features=1000, out_features=10))
print(vgg16_true)
9. 模型保存和加载
保存
vgg16 = torchvision.models.vgg16(pretrained=False)
torch.save(vgg16, "vgg16_menthod1.pth")
torch.save(vgg16.state_dict(), "vgg16_method2.pth")
加载(与保存方式一一对应)
model1 = torch.load("vgg16_menthod1.pth")
print(model1)
vgg16 = torchvision.models.vgg16(pretrained=False)
vgg16.load_state_dict(torch.load("vgg16_method2.pth"))
print(vgg16)
陷阱
使用方式1加载模型时需要提供定义模型的类
10. 训练模型完整步骤(非常重要!!)
模型类
class Tudui(nn.Module):
def __init__(self):
super().__init__()
self.model = 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.model(x)
return x
if __name__ == '__main__':
tudui = Tudui()
input = torch.ones((64, 3, 32, 32))
output = tudui(input)
print(output.shape)
训练步骤
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)
total_test_len = len(test_data)
train_dataloader = DataLoader(train_data, batch_size=64)
test_dataloader = DataLoader(test_data, batch_size=64)
tudui = Tudui()
total_train_step = 0
total_test_step = 0
epoch = 10
write = SummaryWriter("../logs_train")
learning_rate = 0.01
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(tudui.parameters(), lr=learning_rate)
for i in range(epoch):
print("----------第{}轮训练开始----------".format(i + 1))
tudui.train()
for data in train_dataloader:
imgs, targets = data
output = tudui(imgs)
loss = loss_fn(output, 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()))
write.add_scalar("train_loss", loss.item(), global_step=total_train_step)
tudui.eval()
total_test_loss = 0
total_accuracy = 0
with torch.no_grad():
for data in test_dataloader:
imgs, targets = data
output = tudui(imgs)
loss = loss_fn(output, targets)
total_test_loss = total_test_loss + loss
accuracy = (output.argmax(1) == targets).sum()
total_accuracy = total_accuracy + accuracy
print("整体测试集上的Loss:{}".format(total_test_loss))
print("整体测试集上的正确率:{}".format(total_accuracy / total_test_len))
write.add_scalar("test_loss", total_test_loss, global_step=total_train_step)
total_test_step = total_test_step + 1
torch.save(tudui, "tudui_{}.pth".format(i))
print("模型已保存")
write.close()
测试准确率 (argmax的用法)
output = torch.tensor([[0.1, 0.2],
[0.05, 0.4]])
print(output.argmax(1))
print(output.argmax(0))
preds = output.argmax(1)
targets = torch.tensor([0, 1])
print(preds == targets)
print((preds == targets).sum())
使用GPU训练
tudui = tudui.cuda()
loss_fn = loss_fn.cuda()
imgs = imgs.cuda()
targets = targets .cuda()
device = torch.device("cpu") or torch.device("cuda:0")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
tudui = tudui.to(device)
loss_fn = loss_fn.to(device)
imgs = imgs.to(device)
targets = targets.to(device)
11. 完整的模型验证套路
from src.model import Tudui
image_path = "../imgs/airplan.png"
image = Image.open(image_path)
image = image.convert('RGB')
transform = torchvision.transforms.Compose([
torchvision.transforms.Resize((32, 32)),
torchvision.transforms.ToTensor()
])
image = transform(image)
model = torch.load("tudui_36.pth", map_location=torch.device('cpu'))
print(model)
image = torch.reshape(image, (1, 3, 32, 32))
model.eval()
with torch.no_grad():
output = model(image)
print(output.argmax(1))
12. 小知识
Anaconda常用指令
创建环境: conda create -n "env_name" python=3.6
激活环境: conda activate name
环境地址: C:\Users\Administrator\Anaconda3\envs
查看package包:pip list
查找环境以及根目录:conda info --envs
打开图片的格式
打开方式 | Image.open(img_path) | transforms.ToTensor() | cv2.imread(img_path) |
---|
数据类型 | PIL | tensor | numpy |
构建工具的使用
python中–call–函数的作用:python中call和init的区别
TensorBoard
有效地展示tensorflow在运行过程中的计算图、各种指标随着时间的变化趋势以及训练中使用到的数据信息。
from torch.utils.tensorboard import SummaryWriter
write = SummaryWriter("p10")
for i in range(10):
img, target=test_set[i]
write.add_image("test_set", img, i)
write.close()
transforms
-
作用: 对图片进行预处理 -
torchvision.transforms.Composese: 将一些方法组合起来
import torchvision
data_transforms = torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
torchvision.transforms.Resize((512, 512))
])
Ctrl + P | Alt + Enter |
---|
显示需要的参数类型 | 为报错提示解决方案 |
-
pycharm设置不区分大小写提示:file—setting—code Completion 将Math case取消
end…
|