MCPcopy
hub / github.com/ddbourgin/numpy-ml / pad2D

Function pad2D

numpy_ml/neural_nets/utils/utils.py:252–306  ·  view source on GitHub ↗

Zero-pad a 4D input volume `X` along the second and third dimensions. Parameters ---------- X : :py:class:`ndarray ` of shape `(n_ex, in_rows, in_cols, in_ch)` Input volume. Padding is applied to `in_rows` and `in_cols`. pad : tuple, int, or 'same'

(X, pad, kernel_shape=None, stride=None, dilation=0)

Source from the content-addressed store, hash-verified

250
251
252def pad2D(X, pad, kernel_shape=None, stride=None, dilation=0):
253 """
254 Zero-pad a 4D input volume `X` along the second and third dimensions.
255
256 Parameters
257 ----------
258 X : :py:class:`ndarray <numpy.ndarray>` of shape `(n_ex, in_rows, in_cols, in_ch)`
259 Input volume. Padding is applied to `in_rows` and `in_cols`.
260 pad : tuple, int, or 'same'
261 The padding amount. If 'same', add padding to ensure that the output of
262 a 2D convolution with a kernel of `kernel_shape` and stride `stride`
263 has the same dimensions as the input. If 2-tuple, specifies the number
264 of padding rows and colums to add *on both sides* of the rows/columns
265 in `X`. If 4-tuple, specifies the number of rows/columns to add to the
266 top, bottom, left, and right of the input volume.
267 kernel_shape : 2-tuple
268 The dimension of the 2D convolution kernel. Only relevant if p='same'.
269 Default is None.
270 stride : int
271 The stride for the convolution kernel. Only relevant if p='same'.
272 Default is None.
273 dilation : int
274 The dilation of the convolution kernel. Only relevant if p='same'.
275 Default is 0.
276
277 Returns
278 -------
279 X_pad : :py:class:`ndarray <numpy.ndarray>` of shape `(n_ex, padded_in_rows, padded_in_cols, in_channels)`
280 The padded output volume.
281 p : 4-tuple
282 The number of 0-padded rows added to the (top, bottom, left, right) of
283 `X`.
284 """
285 p = pad
286 if isinstance(p, int):
287 p = (p, p, p, p)
288
289 if isinstance(p, tuple):
290 if len(p) == 2:
291 p = (p[0], p[0], p[1], p[1])
292
293 X_pad = np.pad(
294 X,
295 pad_width=((0, 0), (p[0], p[1]), (p[2], p[3]), (0, 0)),
296 mode="constant",
297 constant_values=0,
298 )
299
300 # compute the correct padding dims for a 'same' convolution
301 if p == "same" and kernel_shape and stride is not None:
302 p = calc_pad_dims_2D(
303 X.shape, X.shape[1:3], kernel_shape, stride, dilation=dilation
304 )
305 X_pad, p = pad2D(X, p)
306 return X_pad, p
307
308
309def dilate(X, d):

Callers 10

test_convFunction · 0.90
calc_conv_out_dimsFunction · 0.85
im2colFunction · 0.85
conv2DFunction · 0.85
deconv2D_naiveFunction · 0.85
conv2D_naiveFunction · 0.85
_backward_naiveMethod · 0.85
forwardMethod · 0.85
backwardMethod · 0.85
_bwdMethod · 0.85

Calls 1

calc_pad_dims_2DFunction · 0.85

Tested by 1

test_convFunction · 0.72