PyTorch之圖像和Tensor填充的實(shí)例
在PyTorch中可以對圖像和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(), ])
對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只能對PIL圖像格式進(jìn)行填充,而F.pad可以對Tensor進(jìn)行填充,目前F.pad不支持對2D Tensor進(jìn)行填充,可以通過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填充的實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- 詳解PyTorch手寫數(shù)字識別(MNIST數(shù)據(jù)集)
- PyTorch CNN實(shí)戰(zhàn)之MNIST手寫數(shù)字識別示例
- pytorch cnn 識別手寫的字實(shí)現(xiàn)自建圖片數(shù)據(jù)
- 使用pytorch進(jìn)行圖像的順序讀取方法
- PyTorch讀取Cifar數(shù)據(jù)集并顯示圖片的實(shí)例講解
- pytorch + visdom CNN處理自建圖片數(shù)據(jù)集的方法
- pytorch 把MNIST數(shù)據(jù)集轉(zhuǎn)換成圖片和txt的方法
- pytorch 數(shù)據(jù)集圖片顯示方法
- 畫pytorch模型圖,以及參數(shù)計(jì)算的方法
- Pytorch實(shí)現(xiàn)的手寫數(shù)字mnist識別功能完整示例
相關(guān)文章
Django 權(quán)限認(rèn)證(根據(jù)不同的用戶,設(shè)置不同的顯示和訪問權(quán)限)
這篇文章主要介紹了Django 權(quán)限認(rèn)證(根據(jù)不同的用戶,設(shè)置不同的顯示和訪問權(quán)限),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07python3獲取控制臺輸入的數(shù)據(jù)的具體實(shí)例
在本篇內(nèi)容里小編給大家分享的是一篇關(guān)于python3獲取控制臺輸入的數(shù)據(jù)的具體實(shí)例內(nèi)容,需要的朋友們可以學(xué)習(xí)下。2020-08-08從np.random.normal()到正態(tài)分布的擬合操作
這篇文章主要介紹了從np.random.normal()到正態(tài)分布的擬合操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06python輸入一個水仙花數(shù)(三位數(shù)) 輸出百位十位個位實(shí)例
這篇文章主要介紹了python輸入一個水仙花數(shù)(三位數(shù)) 輸出百位十位個位實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05python網(wǎng)絡(luò)編程示例(客戶端與服務(wù)端)
這篇文章主要介紹了python網(wǎng)絡(luò)編程示例,提供了客戶端與服務(wù)端,需要的朋友可以參考下2014-04-04