I didn't understand how to use ** np.pad **, so I will leave the result of my investigation as a memorandum.
#One-dimensional array
img = np.array([1, 2, 3])
print(img)
print('img.shape = ', img.shape)
img = np.pad(img, [(1,2)], 'constant')
print(img)
ʻImg = np.pad (img, [(left 0 padding, right 0 padding)],'constant') `
#A two-dimensional array
img = np.array([[1, 2, 3], [4, 5, 6]])
print(img)
print('img.shape = ', img.shape)
img = np.pad(img, [(1,2), (3, 4)], 'constant')
print(img)
ʻImg = np.pad (img, [(top 0 padding, bottom 0 padding), (left 0 padding, right 0 padding)],'constant') `
#3D array
img = np.array( [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [0, 1, 2]]])
print(img)
print('img.shape = ', img.shape)
img = np.pad(img, [(1,0), (1, 1), (1, 1)], 'constant')
print(img)
Until now, only image padding was performed, but this time, specify how many ** 0 images ** (all filled with 0s) should be inserted before and after the image. Here, I put one in front. In other words
ʻImg = np.pad (img, [(0 images before, 0 images after), (0 padded top, 0 padded bottom), (0 padded left, 0 padded right)],'constant' ) `
#4D array
img = np.array([[[[1, 2, 3], [4, 5, 6]], [[7, 8, 9],[0, 1, 2]]]])
print(img)
print('img.shape = ', img.shape)
img = np.pad(img, [(1,0), (1,0), (1, 1), (1, 1)], 'constant')
print(img)
This time, add 0 images for the number of images created so far (padded image + 0 images) for the specified set before and after. Here, I put one set in front. In other words
ʻImg = np.pad (img, [(0 front 0 image set, back 0 image set), (0 front, 0 back), (0 top 0 padding, 0 bottom 0 padding), (left) 0 padding, right 0 padding)],'constant') `
Consider the case of padding a 4-dimensional array img (mini-batch size, number of channels, height, width). Let ** p_h ** be the vertical padding and ** p_w ** be the horizontal padding. No padding in batch or channel direction is required, so
img = np.pad(img, [(0, 0), (0, 0), (p_h, p_h), (p_w, p_w)], 'constant')
Recommended Posts