PyTorch——深度可分離卷積(一)


1、深度可分離卷積

Depthwise Separable Convolution

(一)結構

實質上是將標准卷積分成了兩步:depthwise卷積pointwise卷積

標准卷積:

depthwise卷積:

pointwise卷積:

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())

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM