Batch convolution 2d in numpy without scipy?

2019-05-25 00:52发布

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.

1条回答
女痞
2楼-- · 2019-05-25 01:24

Just need to get shape to be 5d and get the strides to match the shape.

shape = f.shape + (x.shape[0],) + tuple(np.subtract(x.shape[1:], f.shape) + 1)
strides = (x.strides * 2)[1:]
M = np.lib.stride_tricks.as_strided(x, shape=shape, strides=strides)
y = np.einsum('pq,pqbmn->bmn', f, M)

now M might get really big if b gets really big, but it works on your toy problem.

查看更多
登录 后发表回答