I have a batch of b
m x n
images stored in an array x
, and a convolutional filter f
of size p x q
that I'd like to apply to each image (then use sum pooling and store in an array y
) in the batch, i.e. all(np.allclose(y[i][j][k], (x[i, j:j+p, k:k+q] * f).sum()) for i in range(b) for j in range(m-p+1) for k in range(n-q+1))
is true.
Adapting this answer, I could write the following:
b, m, n, p, q = 6, 5, 4, 3, 2
x = np.arange(b*m*n).reshape((b, m, n))
f = np.arange(p*q).reshape((p, q))
y = []
for i in range(b):
shape = f.shape + tuple(np.subtract(x[i].shape, f.shape) + 1)
strides = x[i].strides * 2
M = np.lib.stride_tricks.as_strided(x[i], shape=shape, strides=strides)
y.append(np.einsum('ij,ijkl->kl', f, M))
assert all(np.allclose(y[i][j][k], (x[i, j:j+p, k:k+q] * f).sum()) for i in range(b) for j in range(m-p+1) for k in range(n-q+1))
but I think there's a way to do it with just one einsum
, which would be useful to me because b
is usually between 100 and 1000.
How do I adapt my approach to use just one einsum
? Also, for my purposes, I can't bring in scipy
or any other dependencies besides numpy
.