-
Notifications
You must be signed in to change notification settings - Fork 24
/
conv4.py
59 lines (50 loc) · 2.03 KB
/
conv4.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
58
import torch
import torch.nn as nn
import collections
import math
class Conv4(torch.nn.Module):
def __init__(self, flatten=True):
super(Conv4, self).__init__()
self.feature_size = 64
self.name = "conv4"
self.layer1 = nn.Sequential(collections.OrderedDict([
('conv', nn.Conv2d(3, 8, kernel_size=3, stride=1, padding=1, bias=False)),
('bn', nn.BatchNorm2d(8)),
('relu', nn.ReLU()),
('avgpool', nn.AvgPool2d(kernel_size=2, stride=2))
]))
self.layer2 = nn.Sequential(collections.OrderedDict([
('conv', nn.Conv2d(8, 16, kernel_size=3, stride=1, padding=1, bias=False)),
('bn', nn.BatchNorm2d(16)),
('relu', nn.ReLU()),
('avgpool', nn.AvgPool2d(kernel_size=2, stride=2))
]))
self.layer3 = nn.Sequential(collections.OrderedDict([
('conv', nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=1, bias=False)),
('bn', nn.BatchNorm2d(32)),
('relu', nn.ReLU()),
('avgpool', nn.AvgPool2d(kernel_size=2, stride=2))
]))
self.layer4 = nn.Sequential(collections.OrderedDict([
('conv', nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1, bias=False)),
('bn', nn.BatchNorm2d(64)),
('relu', nn.ReLU()),
#('avgpool', nn.AvgPool2d(kernel_size=4))
('glbpool', nn.AdaptiveAvgPool2d(1))
]))
self.is_flatten = flatten
self.flatten = nn.Flatten()
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def forward(self, x):
h = self.layer1(x)
h = self.layer2(h)
h = self.layer3(h)
h = self.layer4(h)
if(self.is_flatten): h = self.flatten(h)
return h