1. 程式人生 > >Numpy學習——陣列填充np.pad()函式的應用

Numpy學習——陣列填充np.pad()函式的應用

 原文地址:https://blog.csdn.net/zenghaitao0128/article/details/78713663

在卷積神經網路中,為了避免因為卷積運算導致輸出影象縮小和影象邊緣資訊丟失,常常採用影象邊緣填充技術,即在影象四周邊緣填充0,使得卷積運算後圖像大小不會縮小,同時也不會丟失邊緣和角落的資訊。在Python的numpy庫中,常常採用numpy.pad()進行填充操作,具體分析如下:

1. np.pad()函式

1)語法結構

pad(array, pad_width, mode, **kwargs)

返回值:陣列

2)引數解釋

array——表示需要填充的陣列;

pad_width——表示每個軸(axis)邊緣需要填充的數值數目。 引數輸入方式為:((before_1, after_1), … (before_N, after_N)),其中(before_1, after_1)表示第1軸兩邊緣分別填充before_1個和after_1個數值。取值為:{sequence, array_like, int}

mode——表示填充的方式(取值:str字串或使用者提供的函式),總共有11種填充模式;

3) 填充方式

‘constant’——表示連續填充相同的值,每個軸可以分別指定填充值,constant_values=(x, y)時前面用x填充,後面用y填充,預設值填充0

‘edge’——表示用邊緣值填充

‘linear_ramp’——表示用邊緣遞減的方式填充

‘maximum’——表示最大值填充

‘mean’——表示均值填充

‘median’——表示中位數填充

‘minimum’——表示最小值填充

‘reflect’——表示對稱填充

‘symmetric’——表示對稱填充

‘wrap’——表示用原陣列後面的值填充前面,前面的值填充後面

import numpy as np

1.1 常數填充模式——’constant’

在卷積神經網路中,通常採用constant填充方式!!

A = np.arange(95,99).reshape(2,2)    #原始輸入陣列
A
array([[95, 96],
       [97, 98]])

1.1.1 用例1

#在陣列A的邊緣填充constant_values指定的數值
#(3,2)表示在A的第[0]軸填充(二維陣列中,0軸表示行),即在0軸前面填充3個寬度的0,比如陣列A中的95,96兩個元素前面各填充了3個0;在後面填充2個0,比如陣列A中的97,98兩個元素後面各填充了2個0
#(2,3)表示在A的第[1]軸填充(二維陣列中,1軸表示列),即在1軸前面填充2個寬度的0,後面填充3個寬度的0
np.pad(A,((3,2),(2,3)),'constant',constant_values = (0,0))  #constant_values表示填充值,且(before,after)的填充值等於(0,0)
array([[ 0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0],
       [ 0,  0, 95, 96,  0,  0,  0],
       [ 0,  0, 97, 98,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0]])
#填充時,從前面軸,往後面軸依次填充
np.pad(A,((3,2),(2,3)),'constant',constant_values = (-2,2))   #填充值,前面填充改為-2,後面填充改為2
array([[-2, -2, -2, -2,  2,  2,  2],
       [-2, -2, -2, -2,  2,  2,  2],
       [-2, -2, -2, -2,  2,  2,  2],
       [-2, -2, 95, 96,  2,  2,  2],
       [-2, -2, 97, 98,  2,  2,  2],
       [-2, -2,  2,  2,  2,  2,  2],
       [-2, -2,  2,  2,  2,  2,  2]])
np.pad(A,((3,2),(2,3)),'constant',constant_values = ((0,0),(1,2)))    #0軸和1軸分別填充不同的值,先填充0軸,後填充1軸,存在1軸填充覆蓋0軸填充的情形
array([[ 1,  1,  0,  0,  2,  2,  2],
       [ 1,  1,  0,  0,  2,  2,  2],
       [ 1,  1,  0,  0,  2,  2,  2],
       [ 1,  1, 95, 96,  2,  2,  2],
       [ 1,  1, 97, 98,  2,  2,  2],
       [ 1,  1,  0,  0,  2,  2,  2],
       [ 1,  1,  0,  0,  2,  2,  2]])
np.pad(A,((3,2),(2,3)),'constant')     #,constant_values 預設,則預設填充均為0
array([[ 0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0],
       [ 0,  0, 95, 96,  0,  0,  0],
       [ 0,  0, 97, 98,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0]])

1.2 邊緣值填充模式——’edge’

B = np.arange(1,5).reshape(2,2)  #原始輸入陣列
B
array([[1, 2],
       [3, 4]])
np.pad(B,((1,2),(2,1)),'edge')   #注意先填充0軸,後面填充1軸,依次填充
array([[1, 1, 1, 2, 2],
       [1, 1, 1, 2, 2],
       [3, 3, 3, 4, 4],
       [3, 3, 3, 4, 4],
       [3, 3, 3, 4, 4]])

1.3 邊緣最大值填充模式——’maximum’

B = np.arange(1,5).reshape(2,2)  #原始輸入陣列
B
array([[1, 2],
       [3, 4]])
np.pad(B,((1,2),(2,1)),'maximum')    #maximum填充模式還有其他控制引數,比如stat_length,詳細見numpy庫
array([[4, 4, 3, 4, 4],
       [2, 2, 1, 2, 2],
       [4, 4, 3, 4, 4],
       [4, 4, 3, 4, 4],
       [4, 4, 3, 4, 4]])
C = np.arange(0,9).reshape(3,3)  #原始輸入陣列
C
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
np.pad(C,((3,2),(2,1)),'maximum')  
array([[8, 8, 6, 7, 8, 8],
       [8, 8, 6, 7, 8, 8],
       [8, 8, 6, 7, 8, 8],
       [2, 2, 0, 1, 2, 2],
       [5, 5, 3, 4, 5, 5],
       [8, 8, 6, 7, 8, 8],
       [8, 8, 6, 7, 8, 8],
       [8, 8, 6, 7, 8, 8]])