How do you combine image channels while retaining spatial dimensions?

tensordot sums products along specified pairs of axes and retains the uncontracted dimensions.

numpy.tensordot in practice

Two pixels with two channels each yield five and eleven under weights [1,2]. Contract the last image axis with the first weight axis to preserve spatial layout.

import numpy as np
np.tensordot([[[1,2],[3,4]]],[1,2],axes=([-1],[0]))

Result [[5, 11]]

中文

怎样只沿通道维度融合多通道图像?

tensordot 沿两组指定轴进行乘积求和,其余轴保留在输出中。

numpy.tensordot 算例

两个像素各有两个通道,用 [1,2] 加权后得到 5 和 11。指定图像末轴与权重首轴,可以保留原有的空间布局。

import numpy as np
np.tensordot([[[1,2],[3,4]]],[1,2],axes=([-1],[0]))

结果 [[5, 11]]

References / 参考来源

Keep reading / 继续阅读