小編給大家分享一下PyTorch之圖像和Tensor填充的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都做網(wǎng)站、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的細(xì)河網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!在PyTorch中可以對(duì)圖像和Tensor進(jìn)行填充,如常量值填充,鏡像填充和復(fù)制填充等。在圖像預(yù)處理階段設(shè)置圖像邊界填充的方式如下:
import vision.torchvision.transforms as transforms img_to_pad = transforms.Compose([ transforms.Pad(padding=2, padding_mode='symmetric'), transforms.ToTensor(), ])
對(duì)Tensor進(jìn)行填充的方式如下:
import torch.nn.functional as F feature = feature.unsqueeze(0).unsqueeze(0) avg_feature = F.pad(feature, pad = [1, 1, 1, 1], mode='replicate')
這里需要注意一點(diǎn)的是,transforms.Pad只能對(duì)PIL圖像格式進(jìn)行填充,而F.pad可以對(duì)Tensor進(jìn)行填充,目前F.pad不支持對(duì)2D Tensor進(jìn)行填充,可以通過(guò)unsqueeze擴(kuò)展為4D Tensor進(jìn)行填充。
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填充的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。