padding操作是给图像外围加像素点。
为了实际说明操作过程,这里我们使用一张实际的图片来做一下处理。
这张图片是大小是(256,256),使用pad来给它加上一个黑色的边框。具体代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
import torch.nn,functional as F
import torch
from PIL import Image
im = Image. open ( "heibai.jpg" , 'r' )
X = torch.Tensor(np.asarray(im))
print ( "shape:" ,X.shape)
dim = ( 10 , 10 , 10 , 10 )
X = F.pad(X,dim, "constant" ,value = 0 )
padX = X.data.numpy()
padim = Image.fromarray(padX)
padim = padim.convert( "RGB" ) #这里必须转为RGB不然会
padim.save( "padded.jpg" , "jpeg" )
padim.show()
print ( "shape:" ,padX.shape)
|
输出:
1
2
|
shape: torch.Size([ 256 , 256 ])
shape: ( 276 , 276 )
|
可以看出给原图四个方向给加上10维度的0,维度变为256+10+10得到的图像如下:
我们在举几个简单例子:
1
2
3
4
5
6
7
8
9
10
11
12
|
x = np.asarray([[[ 1 , 2 ],[ 1 , 2 ]]])
X = torch.Tensor(x)
print (X.shape)
pad_dims = (
2 , 2 ,
2 , 2 ,
1 , 1 ,
)
X = F.pad(X,pad_dims, "constant" )
print (X.shape)
print (X)
|
输出:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
torch.Size([ 1 , 2 , 2 ])
torch.Size([ 3 , 6 , 6 ])
tensor([[[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ]],
[[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 1. , 2. , 0. , 0. ],
[ 0. , 0. , 1. , 2. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ]],
[[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ]]])
|
可以知若pid_sim为(2,2,2,2,1,1)则原维度变化是2+2+2=6,1+1+1=3.也就是第一个(2,2) pad的是最后一个维度,第二个(2,2)pad是倒数第二个维度,第三个(1,1)pad是第一个维度。
再举一个四维度的,但是只pad三个维度:
1
2
3
4
5
6
7
8
9
10
11
12
|
x = np.asarray([[[[ 1 , 2 ],[ 1 , 2 ]]]])
X = torch.Tensor(x) #(1,2,2)
print (X.shape)
pad_dims = (
2 , 2 ,
2 , 2 ,
1 , 1 ,
)
X = F.pad(X,pad_dims, "constant" ) #(1,1,12,12)
print (X.shape)
print (X)
|
输出:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
torch.Size([ 1 , 1 , 2 , 2 ])
torch.Size([ 1 , 3 , 6 , 6 ])
tensor([[[[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ]],
[[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 1. , 2. , 0. , 0. ],
[ 0. , 0. , 1. , 2. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ]],
[[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ]]]])
|
再举一个四维度的,pad四个维度:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
x = np.asarray([[[[ 1 , 2 ],[ 1 , 2 ]]]])
X = torch.Tensor(x) #(1,2,2)
print (X.shape)
pad_dims = (
2 , 2 ,
2 , 2 ,
1 , 1 ,
2 , 2
)
X = F.pad(X,pad_dims, "constant" ) #(1,1,12,12)
print (X.shape)
print (X)
|
输出:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
torch.Size([ 1 , 1 , 2 , 2 ])
torch.Size([ 5 , 3 , 6 , 6 ])
tensor([[[[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ]],
[[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ]],
[[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. ]]],
.........太多了
|
以上这篇pytorch 中pad函数toch.nn.functional.pad()的用法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/geter_CS/article/details/88052206