簡體   English   中英

使用廣播在多批圖像上計算乘法(逐元素)

[英]Compute multiplication (element-wise) over multiple batches of images using broadcasting

我目前正在研究 mnist 數據集以創建 CNN。 我的輸入是

X:  Array of shape (batch_size, n_channels, image_height, image_width)
F: The filter to apply. Array of shape (n_channels, filter_height, filter_width)

我能夠在單個過濾器上計算元素乘法,如下所示:

index : tuple pointing to the top-left corner of where Kernel to be placed
f_shape = np.shape(F)
np.multiply(X[:, :, index[0]:index[0] + f_shape[1], index[1]:index[1] + f_shape[2]], F)

但是現在,我想計算多個過濾器上的元素乘法。 所以我的輸入將是:

X:  Array of shape (batch_size, n_channels, image_height, image_width)
F: The filter to apply. Array of shape (n_filters, n_channels, filter_height, filter_width)

我無法找出使用廣播解決此問題的有效 numpy 操作。

對於這兩種情況,您都需要skimage.util.view_as_windows 此外, np.multiply不做點積, np.dot做。 或者在這種情況下(在跟蹤多個維度時) np.einsum

from skimage.util import view_as_windows

x_window = view_as_windows(X, (n_channels, filter_height, filter_width)).squeeze()

single_filter_mult = np.einsum('ijkmnp, mnp -> ijkmnp', x_window, F)
single_filter_dot = np.einsum('ijkmnp, mpq -> ijklmq', x_window, F)
multi_filter_mult = np.einsum('ijkmnp, lmnp -> ijklmnp', x_window, F_multi)
multi_filter_dot = np.einsum('ijkmnp, lmpq -> ijklmnq', x_window, F_multi)

現在*_filter_*[index]將給出預期的 output。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM