知行编程网知行编程网  2022-06-02 18:00 知行编程网 隐藏边栏 |   抢沙发  6 
文章评分 0 次,平均分 0.0

PyTorch Cookbook(常用代码段集锦)

来自 | 知乎   作者 | 张皓

链接 | https://zhuanlan.zhihu.com/p/59205847

编辑 | 深度学习这件小事

本文仅作学术分享,如有侵权,请联系后台删文处理
PyTorch Cookbook(常用代码段集锦)

本文代码基于PyTorch 1.0版本,需要用到以下包

import torchvision

   1. 基础配置

检查PyTorch版本

torch.cuda.get_device_name(0)   # GPU type

更新PyTorch

PyTorch将被安装在anaconda3/lib/python3.7/site-packages/torch/目录下。

conda update pytorch torchvision -c pytorch

固定随机种子

torch.cuda.manual_seed_all(0)

指定程序运行在特定GPU卡上

在命令行指定环境变量

CUDA_VISIBLE_DEVICES=0,1 python train.py

或在代码中指定

os.environ['CUDA_VISIBLE_DEVICES'] = '0,1'

判断是否有CUDA支持

torch.cuda.is_available()

设置为cuDNN benchmark模式

Benchmark模式会提升计算速度,但是由于计算中有随机性,每次网络前馈结果略有差异。

torch.backends.cudnn.benchmark = True

如果想要避免这种结果波动,设置

torch.backends.cudnn.deterministic = True

清除GPU存储

有时Control-C中止运行后GPU存储没有及时释放,需要手动清空。在PyTorch内部可以

torch.cuda.empty_cache()

或在命令行可以先使用ps找到程序的PID,再使用kill结束该进程

 [pid]

或者直接重置没有被清空的GPU

nvidia-smi --gpu-reset -i [gpu_id]

   2. 张量处理

张量基本信息

tensor.dim()    # Number of dimensions.

数据类型转换

tensor = tensor.long()

torch.Tensor与np.ndarray转换

tensor = torch.from_numpy(ndarray.copy()).float()  # If ndarray has negative stride

torch.Tensor与PIL.Image转换

PyTorch中的张量默认采用N×D×H×W的顺序,并且数据范围在[0, 1],需要进行转置和规范化。

tensor = torchvision.transforms.functional.to_tensor(PIL.Image.open(path))  # Equivalently way

np.ndarray与PIL.Image转换

ndarray = np.asarray(PIL.Image.open(path))

从只包含一个元素的张量中提取值

这在训练时统计loss的变化过程中特别有用。否则这将累积计算图,使GPU存储占用量越来越大。

value = tensor.item()

张量形变

张量形变常常需要用于将卷积层特征输入全连接层的情形。相比torch.view,torch.reshape可以自动处理输入张量不连续的情况。

tensor = torch.reshape(tensor, shape)

打乱顺序

tensor = tensor[torch.randperm(tensor.size(0))]  # Shuffle the first dimension

水平翻转

PyTorch不支持tensor[::-1]这样的负步长操作,水平翻转可以用张量索引实现。

long()]

复制张量

有三种复制的方式,对应不同的需求。

tensor.detach.clone()()   # |        New         |          No                |

拼接张量

注意torch.cat和torch.stack的区别在于torch.cat沿着给定的维度拼接,而torch.stack会新增一维。例如当参数是3个10×5的张量,torch.cat的结果是30×5的张量,而torch.stack的结果是3×10×5的张量。

tensor = torch.stack(list_of_tensors, dim=0)

将整数标记转换成独热(one-hot)编码

PyTorch中的标记默认从0开始。

one_hot.scatter_(dim=1, index=torch.unsqueeze(tensor, dim=1), src=torch.ones(N, num_classes).long())

得到非零/零元素

torch.nonzero(tensor == 0).size(0)  # Number of zero elements

判断两个张量相等

torch.equal(tensor1, tensor2)     # int tensor

张量扩展

torch.reshape(tensor, (64, 512, 1, 1)).expand(64, 512, 7, 7)

矩阵乘法

result = tensor1 * tensor2

计算两组数据之间的两两欧式距离

dist = torch.sqrt(torch.sum((X1[:,None,:] - X2) ** 2, dim=2))

   3. 模型定义

卷积层

最常用的卷积层配置是

)

如果卷积层配置比较复杂,不方便计算输出大小时,可以利用如下可视化工具辅助

Convolution Visualizer

https://ezyang.github.io/convolution-visualizer/index.html


GAP(Global average pooling)层

gap = torch.nn.AdaptiveAvgPool2d(output_size=1)

双线性汇合(bilinear pooling)[1]

X = torch.nn.functional.normalize(X)                  # L2 normalization

多卡同步BN(Batch normalization)

当使用torch.nn.DataParallel将代码运行在多张GPU卡上时,PyTorch的BN层默认操作是各卡上数据独立地计算均值和标准差,同步BN使用所有卡上的数据一起计算BN层的均值和标准差,缓解了当批量大小(batch size)比较小时对均值和标准差估计不准的情况,是在目标检测等任务中一个有效的提升性能的技巧。

Synchronized-BatchNorm-PyTorch

https://github.com/vacancy/Synchronized-BatchNorm-PyTorch

现在PyTorch官方已经支持同步BN操作

                                 track_running_stats=True)

将已有网络的所有BN层改为同步BN层

        return module

类似BN滑动平均

如果要实现类似BN滑动平均的操作,在forward函数中要使用原地(inplace)操作给滑动平均赋值。

        self.running_mean += momentum * (current - self.running_mean)

计算模型整体参数量

num_parameters = sum(torch.numel(parameter) for parameter in model.parameters())

类似Keras的model.summary()输出模型信息

pytorch-summary

https://github.com/sksq96/pytorch-summary

模型权值初始化

注意model.modules()和model.children()的区别:model.modules()会迭代地遍历模型的所有子层,而model.children()只会遍历模型下的一层。

layer.weight = torch.nn.Parameter(tensor)

部分层使用预训练模型

注意如果保存的模型是torch.nn.DataParallel,则当前的模型也需要是torch.nn.DataParallel。torch.nn.DataParallel(model).module == model。

model.load_state_dict(torch.load('model,pth'), strict=False)

将在GPU保存的模型加载到CPU

model.load_state_dict(torch.load('model,pth', map_location='cpu'))

   4. 数据准备、特征提取与微调

图像分块打散(image shuffle)/区域混淆机制(region confusion mechanism,RCM)[2]

)

得到视频数据基本信息

video.release()

TSN每段(segment)采样一帧视频[3]

return [frame_indices[i] for i in range(K)]

提取ImageNet预训练模型某层的卷积特征

    conv_representation = model(image)

提取ImageNet预训练模型多层的卷积特征

            return conv_representation

其他预训练模型

pretrained-models.pytorch

https://github.com/Cadene/pretrained-models.pytorch

微调全连接层

optimizer = torch.optim.SGD(model.fc.parameters(), lr=1e-2, momentum=0.9, weight_decay=1e-4)

以较大学习率微调全连接层,较小学习率微调卷积层

optimizer = torch.optim.SGD(parameters, lr=1e-2, momentum=0.9, weight_decay=1e-4)

   5. 模型训练

常用训练和验证数据预处理

其中ToTensor操作会将PIL.Image或形状为H×W×D,数值范围为[0, 255]的np.ndarray转换为形状为D×H×W,数值范围为[0.0, 1.0]的torch.Tensor。

])

训练基本代码框架

        optimizer.step()

标记平滑(label smoothing)[4]

    optimizer.step()

Mixup[5]

step()

L1正则化

loss.backward()

不对偏置项进行L2正则化/权值衰减(weight decay)

optimizer = torch.optim.SGD(parameters, lr=1e-2, momentum=0.9, weight_decay=1e-4)

梯度裁剪(gradient clipping)

torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=20)

计算Softmax输出的准确率

accuruacy = num_correct / labels.size(0)

可视化模型前馈的计算图

pytorchviz

https://github.com/szagoruyko/pytorchviz

可视化学习曲线

有Facebook自己开发的Visdom和Tensorboard(仍处于实验阶段)两个选择。

facebookresearch/visdom

https://github.com/facebookresearch/visdom


Tensorboard

https://pytorch.org/docs/stable/tensorboard.html


             win='Learning rate', update='append', opts=options.lr)

得到当前学习率

    all_lr.append(param_group['lr'])

学习率衰减

    train(...); val(...)

保存与加载断点

注意为了能够恢复训练,我们需要同时保存模型和优化器的状态,以及当前的训练轮数。

 start_epoch)

计算准确率、查准率(precision)、查全率(recall)

    recall = tp / tp_fn * 100

   6. 模型测试

计算每个类别的查准率(precision)、查全率(recall)、F1和总体指标

assert confusion_mat.shape == (num_classes, num_classes)

将各类结果写入电子表格

                     '', '', '', '', ''])

   7. PyTorch其他注意事项

模型定义

  • 建议有参数的层和汇合(pooling)层使用torch.nn模块定义,激活函数直接使用torch.nn.functional。torch.nn模块和torch.nn.functional的区别在于,torch.nn模块在计算时底层调用了torch.nn.functional,但torch.nn模块包括该层参数,还可以应对训练和测试两种网络状态。使用torch.nn.functional时要注意网络状态,如


    x = torch.nn.functional.dropout(x, p=0.5, training=self.training)
  • model(x)前用model.train()和model.eval()切换网络状态。

  • 不需要计算梯度的代码块用with torch.no_grad()包含起来。model.eval()和torch.no_grad()的区别在于,model.eval()是将网络切换为测试状态,例如BN和随机失活(dropout)在训练和测试阶段使用不同的计算方法。torch.no_grad()是关闭PyTorch张量的自动求导机制,以减少存储使用和加速计算,得到的结果无法进行loss.backward()。

  • torch.nn.CrossEntropyLoss的输入不需要经过Softmax。torch.nn.CrossEntropyLoss等价于torch.nn.functional.log_softmax + torch.nn.NLLLoss。

  • loss.backward()前用optimizer.zero_grad()清除累积梯度。optimizer.zero_grad()和model.zero_grad()效果一样。

PyTorch性能与调试

  • torch.utils.data.DataLoader中尽量设置pin_memory=True,对特别小的数据集如MNIST设置pin_memory=False反而更快一些。num_workers的设置需要在实验中找到最快的取值。

  • 用del及时删除不用的中间变量,节约GPU存储。

  • 使用inplace操作可节约GPU存储,如

x = torch.nn.functional.relu(x, inplace=True)

此外,还可以通过torch.utils.checkpoint前向传播时只保留一部分中间结果来节约GPU存储使用,在反向传播时需要的内容从最近中间结果中计算得到。

  • 减少CPU和GPU之间的数据传输。例如如果你想知道一个epoch中每个mini-batch的loss和准确率,先将它们累积在GPU中等一个epoch结束之后一起传输回CPU会比每个mini-batch都进行一次GPU到CPU的传输更快。

  • 使用半精度浮点数half()会有一定的速度提升,具体效率依赖于GPU型号。需要小心数值精度过低带来的稳定性问题。

  • 时常使用assert tensor.size() == (N, D, H, W)作为调试手段,确保张量维度和你设想中一致。

  • 除了标记y外,尽量少使用一维张量,使用n*1的二维张量代替,可以避免一些意想不到的一维张量计算结果。

  • 统计代码各部分耗时

print(profile)

或者在命令行运行

python -m torch.utils.bottleneck main.py

   致谢

感谢 @些许流年、 @El tnoto 、 @FlyCharles 的勘误,感谢 @oatmeal 提供的更简洁的方法。由于作者才疏学浅,更兼时间和精力所限,代码中错误之处在所难免,敬请读者批评指正。

   参考资料

  • PyTorch官方代码:pytorch/examples

  • PyTorch论坛:PyTorch Forums

  • PyTorch文档:pytorch.org/docs/stable

  • 其他基于PyTorch的公开实现代码,无法一一列举

   参考

  1. ^T.-Y. Lin, A. RoyChowdhury, and S. Maji. Bilinear CNN models for fine-grained visual recognition. In ICCV, 2015.

  2. ^Y. Chen, Y. Bai, W. Zhang, and T. Mei. Destruction and construction learning for fine-grained image recognition. In CVPR, 2019.

  3. ^L. Wang, Y. Xiong, Z. Wang, Y. Qiao, D. Lin, X. Tang, and L. V. Gool. Temporal segment networks: Towards good practices for deep action recognition. In ECCV, 2016.

  4. ^C. Szegedy, V. Vanhoucke, S. Ioffe, J. Shlens, and Z. Wojna: Rethinking the Inception architecture for computer vision. In CVPR, 2016.

  5. ^H. Zhang, M. Cissé, Y. N. Dauphin, and D. Lopez-Paz. mixup: Beyond empirical risk minimization. In ICLR, 2018.


<pre style="letter-spacing: 0.544px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><p style="max-width: 100%;letter-spacing: 0.544px;white-space: normal;color: rgb(0, 0, 0);font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;widows: 1;line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.5px;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;font-size: 16px;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.5px;box-sizing: border-box !important;overflow-wrap: break-word !important;">—</span></strong>完<strong style="max-width: 100%;font-size: 16px;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.5px;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;font-size: 16px;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.5px;box-sizing: border-box !important;overflow-wrap: break-word !important;">—</span></strong></span></strong></span></strong></p><section style="max-width: 100%;letter-spacing: 0.544px;white-space: normal;font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;widows: 1;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section powered-by="xiumi.us" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="margin-top: 15px;margin-bottom: 25px;max-width: 100%;opacity: 0.8;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section powered-by="xiumi.us" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="margin-top: 15px;margin-bottom: 25px;max-width: 100%;opacity: 0.8;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section><p style="margin-bottom: 15px;padding-right: 0em;padding-left: 0em;max-width: 100%;color: rgb(127, 127, 127);font-size: 12px;font-family: sans-serif;line-height: 25.5938px;letter-spacing: 3px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 16px;font-family: 微软雅黑;caret-color: red;box-sizing: border-box !important;overflow-wrap: break-word !important;">为您推荐</span></strong></span></p><p style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;box-sizing: border-box !important;overflow-wrap: break-word !important;">有你的学校吗?2020软科中国大学排名发布!</p><p style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 14px;">GitHub重大更新:在线开发上线,是时候卸载IDE了</span></p><p style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;box-sizing: border-box !important;overflow-wrap: break-word !important;">李沐团队半年离开六人,MxNet是否英雄落幕?<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></p><p style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="color: rgb(87, 107, 149);-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;">史上最烂的项目:苦撑12年,600多万行代码...</span></p><p style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;box-sizing: border-box !important;overflow-wrap: break-word !important;">一文概览2D人体姿态估计</p></section></section></section></section></section></section></section></section>
PyTorch Cookbook(常用代码段集锦)

本篇文章来源于: 深度学习这件小事

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写

发表评论

表情 格式 链接 私密 签到
扫一扫二维码分享