加入收藏 | 设为首页 | 会员中心 | 我要投稿 西安站长网 (https://www.029zz.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 创业 > 正文

高性能PyTorch是如何炼成的?整理的10条脱坑指南

发布时间:2020-06-23 15:22:32 所属栏目:创业 来源:站长网
导读:副标题#e# 如何用最少的精力,完成最高效的 PyTorch 训练?一位有着 PyTorch 两年使用经历的 Medium 博主最近分享了他在这方面的 10 个真诚建议。 在 Efficient PyTorch 这一部分中,作者提供了一些识别和消除 I/O 和 CPU 瓶颈的技巧。第二部分阐述了一些高

将 RGB 图像保持在每个通道深度 8 位。可以轻松地在 GPU 上将图像转换为浮点形式或者标准化。 在数据集中用 uint8 或 uint16 数据类型代替 long。 class MySegmentationDataset(Dataset):   ...   def __getitem__(self, index):     image = cv2.imread(self.images[index])     target = cv2.imread(self.masks[index])      # No data normalization and type casting here     return torch.from_numpy(image).permute(2,0,1).contiguous(),            torch.from_numpy(target).permute(2,0,1).contiguous()  class Normalize(nn.Module):     # https://github.com/BloodAxe/pytorch-toolbelt/blob/develop/pytorch_toolbelt/modules/normalize.py     def __init__(self, mean, std):         super().__init__()         self.register_buffer("mean", torch.tensor(mean).float().reshape(1, len(mean), 1, 1).contiguous())         self.register_buffer("std", torch.tensor(std).float().reshape(1, len(std), 1, 1).reciprocal().contiguous())      def forward(self, input: torch.Tensor) -> torch.Tensor:         return (input.to(self.mean.type) - self.mean) * self.std  class MySegmentationModel(nn.Module):   def __init__(self):     self.normalize = Normalize([0.221 * 255], [0.242 * 255])     self.loss = nn.CrossEntropyLoss()    def forward(self, image, target):     image = self.normalize(image)     output = self.backbone(image)      if target is not None:       loss = self.loss(output, target.long())       return loss      return output 

(编辑:西安站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

推荐文章
    热点阅读