-
Notifications
You must be signed in to change notification settings - Fork 8
/
effnet.py
57 lines (49 loc) · 2.05 KB
/
effnet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'''
EffNet: AN EFFICIENT STRUCTURE FOR CONVOLUTIONAL NEURAL NETWORKS
Implementation in Pytorch of Effnet.
https://arxiv.org/abs/1801.06434
'''
import torch.nn as nn
class Flatten(nn.Module):
def forward(self, x):
x = x.view(x.size()[0], -1)
return x
class EffNet(nn.Module):
def __init__(self, nb_classes=10, include_top=True, weights=None):
super(EffNet, self).__init__()
self.block1 = self.make_layers(32, 64)
self.block2 = self.make_layers(64, 128)
self.block3 = self.make_layers(128, 256)
self.flatten = Flatten()
self.linear = nn.Linear(4096, nb_classes)
self.include_top = include_top
self.weights = weights
def make_layers(self, ch_in, ch_out):
layers = [
nn.Conv2d(3, ch_in, kernel_size=(1,1), stride=(1,1), bias=False, padding=0, dilation=(1,1)) if ch_in ==32 else nn.Conv2d(ch_in, ch_in, kernel_size=(1,1),stride=(1,1), bias=False, padding=0, dilation=(1,1)) ,
self.make_post(ch_in),
# DepthWiseConvolution2D
nn.Conv2d(ch_in, 1 * ch_in, groups=ch_in, kernel_size=(1, 3),stride=(1,1), padding=(0,1), bias=False, dilation=(1,1)),
self.make_post(ch_in),
nn.MaxPool2d(kernel_size=(2,1), stride=(2,1)),
# DepthWiseConvolution2D
nn.Conv2d(ch_in, 1 * ch_in, groups=ch_in, kernel_size=(3, 1), stride=(1,1), padding=(1,0), bias=False, dilation=(1,1)),
self.make_post(ch_in),
nn.Conv2d(ch_in, ch_out, kernel_size=(1, 2), stride=(1, 2), bias=False, padding=(0,0), dilation=(1,1)),
self.make_post(ch_out),
]
return nn.Sequential(*layers)
def make_post(self, ch_in):
layers = [
nn.LeakyReLU(0.3),
nn.BatchNorm2d(ch_in, momentum=0.99)
]
return nn.Sequential(*layers)
def forward(self, x):
x = self.block1(x)
x = self.block2(x)
x = self.block3(x)
if self.include_top:
x = self.flatten(x)
x = self.linear(x)
return x