最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PyTorch之圖像和Tensor填充的實例

 更新時間:2019年08月18日 15:07:58   作者:鵲踏枝-碼農  
今天小編就為大家分享一篇PyTorch之圖像和Tensor填充的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

在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填充的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • Celery批量異步調用任務一直等待結果問題

    Celery批量異步調用任務一直等待結果問題

    這篇文章主要介紹了Celery批量異步調用任務一直等待結果問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Python棧類實例分析

    Python棧類實例分析

    這篇文章主要介紹了Python棧類,實例分析了Python實現(xiàn)棧的入棧、出棧、移除、判定為空的相關技巧,需要的朋友可以參考下
    2015-06-06
  • python識別文字(基于tesseract)代碼實例

    python識別文字(基于tesseract)代碼實例

    這篇文章主要介紹了python識別文字(基于tesseract)代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-08-08
  • Python中變量的輸入輸出實例代碼詳解

    Python中變量的輸入輸出實例代碼詳解

    這篇文章主要介紹了Python中變量的輸入輸出問題,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下
    2019-07-07
  • python用循環(huán)新建多個列表的代碼實例

    python用循環(huán)新建多個列表的代碼實例

    當我們處理數(shù)據(jù)時,有時候需要創(chuàng)建多個列表以存儲不同類型或不同條件下的數(shù)據(jù),在Python中,我們可以利用循環(huán)來快速、高效地創(chuàng)建這些列表,本文將介紹如何使用循環(huán)在Python中創(chuàng)建多個列表,并提供代碼實例,需要的朋友可以參考下
    2024-04-04
  • 樹莓派與PC端在局域網內運用python實現(xiàn)即時通訊

    樹莓派與PC端在局域網內運用python實現(xiàn)即時通訊

    這篇文章主要為大家詳細介紹了樹莓派與PC端在局域網內運用python實現(xiàn)即時通訊,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • python常用函數(shù)random()函數(shù)詳解

    python常用函數(shù)random()函數(shù)詳解

    這篇文章主要介紹了python常用函數(shù)random()函數(shù),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-02-02
  • 如何將anaconda安裝配置的mmdetection環(huán)境離線拷貝到另一臺電腦

    如何將anaconda安裝配置的mmdetection環(huán)境離線拷貝到另一臺電腦

    這篇文章主要介紹了如何將anaconda安裝配置的mmdetection環(huán)境離線拷貝到另一臺電腦,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • 使用Python中的greenlet包實現(xiàn)并發(fā)編程的入門教程

    使用Python中的greenlet包實現(xiàn)并發(fā)編程的入門教程

    這篇文章主要介紹了使用Python中的greenlet包實現(xiàn)并發(fā)編程的入門教程,Python由于GIL的存在并不能實現(xiàn)真正的多線程并發(fā),greenlet可以做到一個相對的替換方案,需要的朋友可以參考下
    2015-04-04
  • Python2和Python3.6環(huán)境解決共存問題

    Python2和Python3.6環(huán)境解決共存問題

    這篇文章主要介紹了Python2和Python3.6環(huán)境解決共存問題,需要的朋友可以參考下
    2018-11-11

最新評論

广灵县| 北宁市| 马尔康县| 缙云县| 平乐县| 吉首市| 修水县| 临朐县| 建瓯市| 天长市| 友谊县| 秦皇岛市| 林口县| 江津市| 舟山市| 葫芦岛市| 洛浦县| 南江县| 大宁县| 雷州市| 张家川| 揭东县| 沿河| 北海市| 交城县| 浦北县| 富平县| 霍邱县| 麟游县| 福清市| 阆中市| 岐山县| 天峻县| 四子王旗| 湖口县| 湄潭县| 墨竹工卡县| 白朗县| 陈巴尔虎旗| 合肥市| 芜湖县|