当前位置:主页 > python教程 >

PyTorch之图像和Tensor填充的实例

发布:2022-07-06 22:25:09 52


给大家整理了PyTorch相关的编程文章,网友松英达根据主题投稿了本篇教程内容,涉及到PyTorch、图像、Tensor、填充相关内容,已被981网友关注,如果对知识点想更进一步了解可以在下方电子资料中获取。

在PyTorch中可以对图像和Tensor进行填充,如常量值填充,镜像填充和复制填充等。在图像预处理阶段设置图像边界填充的方式如下:

import vision.torchvision.transforms as transforms
 
img_to_pad = transforms.Compose([
    transforms.Pad(padding=2, padding_mode='symmetric'),
    transforms.ToTensor(),
   ])

对Tensor进行填充的方式如下:

import torch.nn.functional as F
 
feature = feature.unsqueeze(0).unsqueeze(0)
avg_feature = F.pad(feature, pad = [1, 1, 1, 1], mode='replicate')

这里需要注意一点的是,transforms.Pad只能对PIL图像格式进行填充,而F.pad可以对Tensor进行填充,目前F.pad不支持对2D Tensor进行填充,可以通过unsqueeze扩展为4D Tensor进行填充。

F.pad的部分源码如下:

@torch._jit_internal.weak_script
def pad(input, pad, mode='constant', value=0):
 # type: (Tensor, List[int], str, float) -> Tensor
 r"""Pads tensor.
 Pading size:
  The number of dimensions to pad is :math:`\left\lfloor\frac{\text{len(pad)}}{2}\right\rfloor`
  and the dimensions that get padded begins with the last dimension and moves forward.
  For example, to pad the last dimension of the input tensor, then `pad` has form
  `(padLeft, padRight)`; to pad the last 2 dimensions of the input tensor, then use
  `(padLeft, padRight, padTop, padBottom)`; to pad the last 3 dimensions, use
  `(padLeft, padRight, padTop, padBottom, padFront, padBack)`.
 Padding mode:
  See :class:`torch.nn.ConstantPad2d`, :class:`torch.nn.ReflectionPad2d`, and
  :class:`torch.nn.ReplicationPad2d` for concrete examples on how each of the
  padding modes works. Constant padding is implemented for arbitrary dimensions.
  Replicate padding is implemented for padding the last 3 dimensions of 5D input
  tensor, or the last 2 dimensions of 4D input tensor, or the last dimension of
  3D input tensor. Reflect padding is only implemented for padding the last 2
  dimensions of 4D input tensor, or the last dimension of 3D input tensor.
 .. include:: cuda_deterministic_backward.rst
 Args:
  input (Tensor): `Nd` tensor
  pad (tuple): m-elem tuple, where :math:`\frac{m}{2} \leq` input dimensions and :math:`m` is even.
  mode: 'constant', 'reflect' or 'replicate'. Default: 'constant'
  value: fill value for 'constant' padding. Default: 0
 Examples::
  >>> t4d = torch.empty(3, 3, 4, 2)
  >>> p1d = (1, 1) # pad last dim by 1 on each side
  >>> out = F.pad(t4d, p1d, "constant", 0) # effectively zero padding
  >>> print(out.data.size())
  torch.Size([3, 3, 4, 4])
  >>> p2d = (1, 1, 2, 2) # pad last dim by (1, 1) and 2nd to last by (2, 2)
  >>> out = F.pad(t4d, p2d, "constant", 0)
  >>> print(out.data.size())
  torch.Size([3, 3, 8, 4])
  >>> t4d = torch.empty(3, 3, 4, 2)
  >>> p3d = (0, 1, 2, 1, 3, 3) # pad by (0, 1), (2, 1), and (3, 3)
  >>> out = F.pad(t4d, p3d, "constant", 0)
  >>> print(out.data.size())
  torch.Size([3, 9, 7, 3])
 """
 assert len(pad) % 2 == 0, 'Padding length must be divisible by 2'
 assert len(pad) // 2 <= input.dim(), 'Padding length too large'
 if mode == 'constant':
  ret = _VF.constant_pad_nd(input, pad, value)
 else:
  assert value == 0, 'Padding mode "{}"" doesn\'t take in value argument'.format(mode)
  if input.dim() == 3:
   assert len(pad) == 2, '3D tensors expect 2 values for padding'
   if mode == 'reflect':
    ret = torch._C._nn.reflection_pad1d(input, pad)
   elif mode == 'replicate':
    ret = torch._C._nn.replication_pad1d(input, pad)
   else:
    ret = input # TODO: remove this when jit raise supports control flow
    raise NotImplementedError
 
  elif input.dim() == 4:
   assert len(pad) == 4, '4D tensors expect 4 values for padding'
   if mode == 'reflect':
    ret = torch._C._nn.reflection_pad2d(input, pad)
   elif mode == 'replicate':
    ret = torch._C._nn.replication_pad2d(input, pad)
   else:
    ret = input # TODO: remove this when jit raise supports control flow
    raise NotImplementedError
 
  elif input.dim() == 5:
   assert len(pad) == 6, '5D tensors expect 6 values for padding'
   if mode == 'reflect':
    ret = input # TODO: remove this when jit raise supports control flow
    raise NotImplementedError
   elif mode == 'replicate':
    ret = torch._C._nn.replication_pad3d(input, pad)
   else:
    ret = input # TODO: remove this when jit raise supports control flow
    raise NotImplementedError
  else:
   ret = input # TODO: remove this when jit raise supports control flow
   raise NotImplementedError("Only 3D, 4D, 5D padding with non-constant padding are supported for now")
 return ret

以上这篇PyTorch之图像和Tensor填充的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持码农之家。


参考资料

相关文章

  • pytorch转换矩阵的维数位置实例方法

    发布:2021-06-02

    今天小编就为大家分享一篇pytorch 转换矩阵的维数位置方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧


  • PyTorch线性回归和逻辑回归的相关实例知识点

    发布:2019-11-02

    这篇文章主要介绍了PyTorch线性回归和逻辑回归实战示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧


  • PyTorch源码解读之torchvision.models

    发布:2020-01-27

    今天小编就为大家分享一篇关于PyTorch源码解读之torchvision.models,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧


  • Pytorch 实现自定义参数层实例代码

    发布:2020-07-09

    今天小编就为大家发信息一篇Pytorch 实现自定义参数层的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧


  • 详解pytorch 调整某一维度数据顺序

    发布:2020-03-05

    今天小编就为大家分享一篇pytorch 调整某一维度数据顺序的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧


  • PyTorch上搭建简单神经网络实现回归和分类的示例

    PyTorch上搭建简单神经网络实现回归和分类的示例

    发布:2022-06-17

    给网友朋友们带来一篇关于PyTorch的教程,本篇文章主要介绍了PyTorch上搭建简单神经网络实现回归和分类的示例,现在分享给大家,也给大家做个参考。一起过来看看吧


  • pytorch按图像的顺序读取方法实例

    发布:2019-06-06

    今天小编就为大家分享一篇使用pytorch进行图像的顺序读取方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧


  • 关于pytorch相关部分矩阵变换函数的问题分析

    发布:2022-04-24

    这篇文章主要介绍了pytorch相关部分矩阵变换函数,包括tensor维度顺序变换BCHW顺序的调整,矩阵乘法相关函数,矩阵乘,点乘,求取矩阵对角线元素或非对角线元素的问题,本文给大家介绍的非常详细,需要的朋友可以参考下


  • pytorch多GPU训练实例与性能对比

    发布:2020-01-16

    今天小编就为大家分享一篇关于pytorch多GPU训练实例与性能对比分析,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧


网友讨论