通道数目的不同
单通道的卷积
下面的代码测试了仅仅一个属性(depth是1)的深度卷积,其结果和普通卷积是一样的:
async function depthwiseConv2dTestSingleDepth() {
const fSize = 2;
const pad = 'valid';
const stride = 1;
const chMul = 1;
const inDepth = 1;
const x = tf.tensor4d(
[
0, 1, 2, 5, 0, 5,
0, 1, 5
],
[1, 3, 3, inDepth]);
const w = tf.tensor4d(
[1.0, 2.0, 2.0, 2.0],
[fSize, fSize, inDepth, chMul],
);
const result = tf.depthwiseConv2d(x, w, stride, pad);
result.print();
}
输出是:
Tensor
[[[[12],
[15]],
[[7 ],
[22]]]]
如下图(依次是:输入数据,滤波器,输出)演示了输出12的参与卷积的数据:
双通道的卷积
下面是深度是2的情况(NHWC)。输入数据是两个通道(假设是红色,黄色),每个通道大小是2x2。因此输入数据的形状是:1x2x2x2。卷积提供了两个窗口(假设是红色,黄色),每个窗口提取一个通道的属性。滤波器的形状是2x2x2x1。因此输出数据是两个通道。同时pading模式是valid,因此每个通道输出数据的大小是1x1。输出数据的形状是:1x1x2。
/*
[ [ [[6, 2],]]]
*/
async function depthwiseConv2dTestMultipleDepth3() {
const fSize = 2;
const pad = 'valid';
const stride = 1;
//const dilation = 2;
const inDepth = 2;
const x = tf.tensor4d(
[
0, 1, 3, 1,
0, 2, 2, 1,
],
[1, fSize, fSize, inDepth]);
const w =
tf.stack(
[
tf.tensor2d(
[2,0,1,3], [fSize, fSize]),
tf.tensor2d(
[1,0,0,1], [fSize, fSize])
],
2).expandDims(3);// as tf.Tensor4D;
const result = tf.depthwiseConv2d(x, w, stride, pad);
result.print();
}
输入数据是下面的:
[
0, 1, 3, 1,
0, 2, 2, 1,
],
但是实际上,由于深度为2,而且数据格式是NHWC