DDR爱好者之家 Design By 杰米
在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填充的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2024年11月26日
2024年11月26日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]