【Datawhale组队学习Pytorch】Task 完结篇

【项目简介】

        PyTorch是利用深度学习进行数据科学研究的重要工具,在灵活性、可读性和性能上都具备相当的优势,近年来已成为学术界实现深度学习算法最常用的框架。考虑到PyTorch的学习兼具理论储备和动手训练,两手都要抓两手都要硬的特点,我们开发了 《深入浅出PyTorch》课程,期望以组队学习的形式,帮助大家从入门到熟练掌握PyTorch工具,进而实现自己的深度学习算法。学习的先修要求是,会使用Python编程,了解包括神经网络在内的机器学习算法,勤于动手实践。

        我们的愿景是:通过组队学习,大家能够掌握由浅入深地PyTorch的基本知识和内容,经过自己的动手实践加深操作的熟练度。同时通过项目实战,充分锻炼编程能力,掌握PyTorch进行深度学习的基本流程,提升解决实际问题的能力。

        

【教程地址】
在线教程链接:深入浅出PyTorch — 深入浅出PyTorch
Github在线教程:GitHub - datawhalechina/thorough-pytorch: PyTorch入门教程,在线阅读地址:https://datawhalechina.github.io/thorough-pytorch/
Gitee在线教程:https://gitee.com/datawhalechina/thorough-pytorch 
b站视频:深入浅出Pytorch_哔哩哔哩_bilibili

教程目录直达


Anaconda Pytorch CUDA GPU 版本匹配问题整理(linux远程服务器)

安装配置笔记链接:http://t.csdn.cn/fCyg1

Pytorch流程梳理
数据预处理

格式统一

数据变换

划分训练集测试集

(代码截图)
基本配置

导包

GPU环境

超参数

数据读入

自定义Dataset类

DataLoader

可视化检测

模型构建

神经网络的构造 class MLP(nn.Module)

常见的层自定义可反复调用(全连接层、卷积层、池化层与循环层等等)

一个神经网络的典型训练过程如下:

*定义包含一些可学习参数(或者叫权重)的神经网络

*在输入数据集上迭代

*通过网络处理输入

*计算 loss (输出和正确答案的距离)

*将梯度反向传播给网络的参数

*更新网络的权重,一般使用一个简单的规则:weight = weight - learning_rate * gradient

模型初始化

torch.nn.init 常见的初始化函数

初始化函数的封装

损失函数

在深度学习中常见的损失函数及其定义方式

PyTorch中损失函数的调用

训练和评估model.train()   # 训练状态
model.eval()   # 验证/测试状态
可视化

可视化网络结构

可视化CNN卷积核

可视化CNN特征图

可视化CNN显著图(class activation map)

TensorBoard工具

优化器torch.optim
PyTorch的模型部署

1. 定义超分辨模型
# 导入相关包
import io
import numpy as np
from torch import nn
import torch.utils.model_zoo as model_zoo
import torch.onnx
import torch.nn as nn
import torch.nn.init as init

# 定义超分辨网络
class SuperResolutionNet(nn.Module):
    def __init__(self, upscale_factor, inplace=False):
        super(SuperResolutionNet, self).__init__()

        self.relu = nn.ReLU(inplace=inplace)
        self.conv1 = nn.Conv2d(1, 64, (5, 5), (1, 1), (2, 2))
        self.conv2 = nn.Conv2d(64, 64, (3, 3), (1, 1), (1, 1))
        self.conv3 = nn.Conv2d(64, 32, (3, 3), (1, 1), (1, 1))
        self.conv4 = nn.Conv2d(32, upscale_factor ** 2, (3, 3), (1, 1), (1, 1))
        self.pixel_shuffle = nn.PixelShuffle(upscale_factor)

        self._initialize_weights()

    def forward(self, x):
        x = self.relu(self.conv1(x))
        x = self.relu(self.conv2(x))
        x = self.relu(self.conv3(x))
        x = self.pixel_shuffle(self.conv4(x))
        return x
    
    # 模型初始化
    def _initialize_weights(self):
        init.orthogonal_(self.conv1.weight, init.calculate_gain('relu'))
        init.orthogonal_(self.conv2.weight, init.calculate_gain('relu'))
        init.orthogonal_(self.conv3.weight, init.calculate_gain('relu'))
        init.orthogonal_(self.conv4.weight)

# 实例化模型
torch_model = SuperResolutionNet(upscale_factor=3)
2. 模型导出为ONNX格式
model_url = 'https://s3.amazonaws.com/pytorch/test_data/export/superres_epoch100-44c6958e.pth'
batch_size = 1    # just a random number
# 加载预训练得到权重
map_location = lambda storage, loc: storage
if torch.cuda.is_available():
    map_location = None
torch_model.load_state_dict(model_zoo.load_url(model_url, map_location=map_location))

# 将模型设置为推理模式
torch_model.eval()
# Input to the model
x = torch.randn(batch_size, 1, 224, 224, requires_grad=True)
torch_out = torch_model(x)

# 导出模型
torch.onnx.export(torch_model,               # model being run
                  x,             # model input (or a tuple for multiple inputs)
                  "super_resolution.onnx",   # where to save the model (can be a file or file-like object)
                  export_params=True,        # store the trained parameter weights inside the model file
                  opset_version=10,   # the ONNX version to export the model to
                  do_constant_folding=True,  # whether to execute constant folding for optimization
                  input_names = ['input'],   # the model's input names
                  output_names = ['output'], # the model's output names
                  # variable length axes
                  dynamic_axes={'input' : {0 : 'batch_size'},    
                                'output' : {0 : 'batch_size'}})
3. 检验ONNX模型
import onnx
# 我们可以使用异常处理的方法进行检验
try:
    # 当我们的模型不可用时,将会报出异常
    onnx.checker.check_model("super_resolution.onnx")
except onnx.checker.ValidationError as e:
    print("The model is invalid: %s"%e)
else:
    # 模型可用时,将不会报出异常,并会输出“The model is valid!”
    print("The model is valid!")
4. 使用ONNX Runtime进行推理
import onnxruntime

ort_session = onnxruntime.InferenceSession("super_resolution.onnx")

# 将张量转化为ndarray格式
def to_numpy(tensor):
    return tensor.detach().cpu().numpy() if tensor.requires_grad else tensor.cpu().numpy()

# 构建输入的字典和计算输出结果
ort_inputs = {ort_session.get_inputs()[0].name: to_numpy(x)}
ort_outs = ort_session.run(None, ort_inputs)

# 比较使用PyTorch和ONNX Runtime得出的精度
np.testing.assert_allclose(to_numpy(torch_out), ort_outs[0], rtol=1e-03, atol=1e-05)

print("Exported model has been tested with ONNXRuntime, and the result looks good!")
5. 进行实际预测并可视化
from PIL import Image
import torchvision.transforms as transforms

# 读取图片
img = Image.open("/cat_224x224.jpg")
# 对图片进行resize操作
resize = transforms.Resize([224, 224])
img = resize(img)

img_ycbcr = img.convert('YCbCr')
img_y, img_cb, img_cr = img_ycbcr.split()

to_tensor = transforms.ToTensor()
img_y = to_tensor(img_y)
img_y.unsqueeze_(0)
# 构建输入的字典并将value转换位array格式
ort_inputs = {ort_session.get_inputs()[0].name: to_numpy(img_y)}
ort_outs = ort_session.run(None, ort_inputs)
img_out_y = ort_outs[0]
img_out_y = Image.fromarray(np.uint8((img_out_y[0] * 255.0).clip(0, 255)[0]), mode='L')

# 保存最后得到的图片
final_img = Image.merge(
    "YCbCr", [
        img_out_y,
        img_cb.resize(img_out_y.size, Image.BICUBIC),
        img_cr.resize(img_out_y.size, Image.BICUBIC),
    ]).convert("RGB")

final_img.save("/cat_superres_with_ort.jpg")

知识点

  • 自动求导:PyTorch 中,所有神经网络的核心是 autograd 包。torch.Tensor 是这个包的核心类。.requires_grad = True,那么它将会追踪对于该张量的所有操作。当完成计算后可以通过调用 .backward(),来自动计算所有的梯度。这个张量的所有梯度将会自动累加到.grad属性。如果 Tensor 是一个标量(即它包含一个元素的数据),则不需要为 backward() 指定任何参数,但是如果它有更多的元素,则需要指定一个gradient参数,该参数是形状匹配的张量。创建张量:x = torch.ones(2, 2, requires_grad=True)

  • 梯度:进行反向传播,这里out 是一个标量,因此out.backward()和 out.backward(torch.tensor(1.)) 等价。print(x.grad)输出导数。数学上,若有向量函数y→=f(x→),那么 y→ 关于 x→ 的梯度就是一个雅可比矩阵。注意:grad在反向传播过程中是累加的(accumulated),这意味着每一次运行反向传播,梯度都会累加之前的梯度,所以一般在反向传播之前需把梯度清零。x.grad.data.zero_()

  • 并行计算:在编写程序中,当我们使用了 .cuda() 时,其功能是让我们的模型或者数据从CPU迁移到GPU(0)当中,通过GPU开始计算。当我们的服务器上有多个GPU,应该指明使用的GPU是哪一块,不设置的话,tensor.cuda()方法会默认将tensor保存到第一块GPU上,等价于tensor.cuda(0),这将会导致爆出out of memory的错误。常见的并行的方法:1.网络结构分布到不同的设备中(Network partitioning) 2.同一层的任务分布到不同数据中(Layer-wise partitioning) 3.不同的数据分布到不同的设备中,执行相同的任务(Data parallelism)现在的主流方式是数据并行的方式(Data parallelism).

  • 损失函数:就是模型的负反馈

  • 设置模型的状态:如果是训练状态,那么模型的参数应该支持反向传播的修改;如果是验证/测试状态,则不应该修改模型参数。在PyTorch中,模型的状态设置非常简便,如下的两个操作二选一即可:model.train()   # 训练状态   model.eval()   # 验证/测试状态

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值