深度可分离卷积 (Depth-wise Separabel Convolution)

深度可分离卷积

深度可分离卷积 = depth-wise conv + point-wise conv。
depth-wise conv:输入特征图每个通道作为一个group,用一个kernel处理。
point-wise conv:kernel size =1 的普通卷积。

import torch
import torch.nn as nn

stride = 1
kernel_size = 3
in_ch = 10
out_ch = 3

conv_normal = nn.Conv2d(in_channels=in_ch,
                        out_channels=out_ch,
                        kernel_size=kernel_size,
                        stride=stride)

input = torch.ones(1, 10, 20, 20)  # input feature map
out_conv = conv_normal(input)
print('normal conv out :', out_conv.size())

# depth-separable conv
# 1.depth-wise conv
group_size = input.size(1)
depthwise_conv = nn.Conv2d(in_channels=in_ch,
                           out_channels=group_size,
                           kernel_size=kernel_size,
                           stride=stride,
                           groups=group_size)
out_depth_conv = depthwise_conv(input)
print('depth-wise conv out :', out_depth_conv.size())

# 2.point-wise conv
in_ch = out_depth_conv.size(1)
point_conv = nn.Conv2d(in_channels=in_ch,
                       out_channels=out_ch,
                       kernel_size=1,
                       stride=stride)
out_point_conv = point_conv(out_depth_conv)
print('point conv out :', out_point_conv.size())
normal conv out : torch.Size([1, 3, 18, 18])
depth-wise conv out : torch.Size([1, 10, 18, 18])
point conv out : torch.Size([1, 3, 18, 18])
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值