批量卷积2d在numpy中没有秘诀? [英] Batch convolution 2d in numpy without scipy?

查看:116
本文介绍了批量卷积2d在numpy中没有秘诀?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一批b m x n图像存储在数组x中,还有一个大小为p x q的卷积滤波器f,我想将其应用于每个图像(然后使用求和池并批量存储在数组y中,即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))为true.

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))

但是我认为有一种方法可以只使用一个einsum,这对我很有用,因为b通常在100到1000之间.

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.

如何调整我的方法以仅使用一个einsum?另外,出于我的目的,除了numpy之外,我不能引入scipy或任何其他依赖项.

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.

推荐答案

只需将shape设置为5d,然后获取strides即可匹配shape.

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)

如果b变大,现在M可能会变大,但这可以解决玩具问题.

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

这篇关于批量卷积2d在numpy中没有秘诀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆