import numpy as np
def zero_pad(X, pad):
"""
Pad with zeros all images of the dataset X. The padding is applied to the height and width of an image,
as illustrated in Figure 1.
Argument:
X -- python numpy array of shape (m, n_H, n_W, n_C) representing a batch of m images
pad -- integer, amount of padding around each image on vertical and horizontal dimensions
Returns:
X_pad -- padded image of shape (m, n_H + 2*pad, n_W + 2*pad, n_C)
"""
return np.pad(X, ((0, 0), (pad, pad), (pad, pad), (0, 0)), 'constant')
np.random.seed(1)
x = np.random.randn(4, 3, 3, 2) # (m, n_H, n_W, n_C) representing a batch of m images
x_pad = zero_pad(x, 2)
fig, axarr = plt.subplots(1, 2, figsize=(15, 10));
axarr[0].set_title('x');
axarr[0].imshow(x[0,:,:,0]);
axarr[1].set_title('x_pad');
axarr[1].imshow(x_pad[0,:,:,0]);
fig, axarr = plt.subplots(1, 2);
axarr[0].set_title('x');
axarr[0].imshow(x[0,:,:,0]);
axarr[1].set_title('x_pad');
axarr[1].imshow(x_pad[0,:,:,0]);