深度可分离卷积的Depth,Stack,Channel Multiplier

通道数目的不同

单通道的卷积

下面的代码测试了仅仅一个属性(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(默认的数据

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值