1、深度可分离卷积

Depthwise Separable Convolution

(一)结构

实质上是将标准卷积分成了两步:depthwise卷积pointwise卷积

标准卷积:

PyTorch——深度可分离卷积(一)

depthwise卷积:

PyTorch——深度可分离卷积(一)

pointwise卷积:

PyTorch——深度可分离卷积(一)

2、代码实现

[32, 3, 224, 224] ——> [32, 64, 112, 112]

 1 import torch
 2 import torch.nn as nn
 3 import torch.nn.functional as F
 4 
 5 class Block(nn.Module):
 6     "Depthwise conv + Pointwise conv"
 7     def __init__(self, in_channels, out_channels, stride=1):
 8         super(Block, self).__init__()
 9         self.conv1 = nn.Conv2d(in_channels, in_channels, kernel_size=3, stride=2, padding=1, groups=in_channels, bias=False)
10         self.bn1 = nn.BatchNorm2d(in_channels)
11         self.conv2 = nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, padding=0, bias=False)
12         self.bn2 = nn.BatchNorm2d(out_channels)
13 
14     def forward(self, x):
15         x = self.conv1(x)
16         x = self.bn1(x)
17         x = F.relu(x)
18         x = self.conv2(x)
19         x = self.bn2(x)
20         x = F.relu(x)
21         return x
22 
23 input = torch.randn(32, 3, 224, 224)
24 block = Block(3, 64)
25 out = block(input)
26 print(out.size())