Numpy Learning - Application of Array Filling np.pad() Function

In convolution network, to avoid shrinking the output image and losing image edge information due to convolution operation, image edge filling technology is often used, that is, filling 0 around the edge of the image so that the image size will not be reduced after convolution operation, and the information of edges and corners will not be lost.In Python's numpy library, the numpy.pad() Fill in as follows:

1. np.pad() function

1)Grammatical Structure

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

Return value: array

2)Parameter Interpretation

Array - Indicates an array that needs to be filled;

pad_width - Indicates the number of values to fill at the edge of each axis.
Parameter input method is: ((before_1, after_1),...(before_N, after_N)), where (before_1, after_1) indicates that the first axis is filled with before_1 and after_1 values on both edges, respectively.Values are: {sequence, array_like, int}

Mode - Represents the mode of padding (value: str string or user-provided function), with a total of 11 padding modes;

3) fill style

'constant'- means that the same values can be filled continuously, with each axis being assigned a fill value. constant_values=(x,y) is filled with X before, y after, and 0 by default

'edge'- means fill with edge values

'linear_ramp'- means fill in a decreasing edge

'maximum'- indicates maximum fill

'mean'- means mean fill

'median'- means median fill

'minimum'- denotes minimum fill

'reflect'- symmetric fill

'symmetric'- symmetric fill

'wrap'- means to fill the front with the value after the original array and the back with the value before

import numpy as np

1.1 Constant Fill Mode -'constant'

In convolution neural networks, constant filling is usually used!!

A = np.arange(95,99).reshape(2,2)    #Original input array
A
array([[95, 96],
       [97, 98]])

1.1.1 Use Case 1

#Fill the edge of array A with the value specified by constant_values
#(3,2) Represents the filling of the [0] axis of A (in a two-dimensional array, the 0-axis represents the row), that is, the filling of three widths of 0 in front of the 0-axis, such as 95,96 in array A, with 30 in front of each of the two elements; and the filling of two zeros in the back, such as 97,98 in array A, with 20 after each of the two elements
#(2,3) Represents filling the [1] axis of A (in a two-dimensional array, the 1 axis represents the column), that is, filling two widths of 0 in front of the 1 axis and three widths of 0 behind the 1 axis.
np.pad(A,((3,2),(2,3)),'constant',constant_values = (0,0))  #constant_values denotes the fill value, and (before, after) the fill value is equal to (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]])
#When filling, fill from the front axis to the back axis in turn
np.pad(A,((3,2),(2,3)),'constant',constant_values = (-2,2))   #Fill value, with -2 in front and 2 in back
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)))    #The 0-axis and 1-axis are filled with different values, the 0-axis is filled first, then the 1-axis is filled, there is a case where the 1-axis fills cover the 0-axis filling.
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 defaults, default padding is 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 Value Fill Mode -'edge'

B = np.arange(1,5).reshape(2,2)  #Original input array
B
array([[1, 2],
       [3, 4]])
np.pad(B,((1,2),(2,1)),'edge')   #Notice that the 0-axis is filled first, followed by the 1-axis, followed by the
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 Edge Maximum Fill Mode -'maximum'

B = np.arange(1,5).reshape(2,2)  #Original input array
B
array([[1, 2],
       [3, 4]])
np.pad(B,((1,2),(2,1)),'maximum')    #maximum fill mode has other control parameters, such as stat_length, as detailed in the numpy Library
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)  #Original input array
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]])

Keywords: network Python

Added by runei on Mon, 20 May 2019 02:54:20 +0300