Tuesday, 29 December 2020

Convolutional layer in Python using Numpy - with Strides

My question is highly related to this one, from which I copied the definition: Convolutional layer in Python using Numpy.

I am trying to implement a convolutional layer in Python using Numpy. The input is a 4-dimensional array of shape [N, H, W, C], where:

  • N: Batch size
  • H: Height of image
  • W: Width of image
  • C: Number of channels

The convolutional filter is also a 4-dimensional array of shape [F, F, Cin, Cout], where

  • F: Height and width of a square filter
  • Cin: Number of input channels (Cin = C)
  • Cout: Number of output channels

With no padding and a stride of S, the output should be a 4-dimensional array of shape [N, (H - F + 1) // S, (W - F + 1) // S, Cout].

For a fixed stride of S=1, this can be done efficiently with the following code:

from numpy.lib.stride_tricks import as_strided

def conv2d(a, b):
    Hout = a.shape[1] - b.shape[0] + 1
    Wout = a.shape[2] - b.shape[1] + 1

    a = as_strided(a, (a.shape[0], Hout, Wout, b.shape[0], b.shape[1], a.shape[3]), a.strides[:3] + a.strides[1:])

    return np.tensordot(a, b, axes=3)

Can anyone help me on how to implement a variable stride of S? As mentioned above I understand the output shape, just not the strides.



from Convolutional layer in Python using Numpy - with Strides

No comments:

Post a Comment