-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathmodel.py
101 lines (91 loc) · 3.31 KB
/
model.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.functional as F
import numpy as np
import torch.optim as optim
import math
class fire(nn.Module):
def __init__(self, inplanes, squeeze_planes, expand_planes):
super(fire, self).__init__()
self.conv1 = nn.Conv2d(inplanes, squeeze_planes, kernel_size=1, stride=1)
self.bn1 = nn.BatchNorm2d(squeeze_planes)
self.relu1 = nn.ReLU(inplace=True)
self.conv2 = nn.Conv2d(squeeze_planes, expand_planes, kernel_size=1, stride=1)
self.bn2 = nn.BatchNorm2d(expand_planes)
self.conv3 = nn.Conv2d(squeeze_planes, expand_planes, kernel_size=3, stride=1, padding=1)
self.bn3 = nn.BatchNorm2d(expand_planes)
self.relu2 = nn.ReLU(inplace=True)
# using MSR initilization
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.in_channels
m.weight.data.normal_(0, math.sqrt(2./n))
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu1(x)
out1 = self.conv2(x)
out1 = self.bn2(out1)
out2 = self.conv3(x)
out2 = self.bn3(out2)
out = torch.cat([out1, out2], 1)
out = self.relu2(out)
return out
class SqueezeNet(nn.Module):
def __init__(self):
super(SqueezeNet, self).__init__()
self.conv1 = nn.Conv2d(3, 96, kernel_size=3, stride=1, padding=1) # 32
self.bn1 = nn.BatchNorm2d(96)
self.relu = nn.ReLU(inplace=True)
self.maxpool1 = nn.MaxPool2d(kernel_size=2, stride=2) # 16
self.fire2 = fire(96, 16, 64)
self.fire3 = fire(128, 16, 64)
self.fire4 = fire(128, 32, 128)
self.maxpool2 = nn.MaxPool2d(kernel_size=2, stride=2) # 8
self.fire5 = fire(256, 32, 128)
self.fire6 = fire(256, 48, 192)
self.fire7 = fire(384, 48, 192)
self.fire8 = fire(384, 64, 256)
self.maxpool3 = nn.MaxPool2d(kernel_size=2, stride=2) # 4
self.fire9 = fire(512, 64, 256)
self.conv2 = nn.Conv2d(512, 10, kernel_size=1, stride=1)
self.avg_pool = nn.AvgPool2d(kernel_size=4, stride=4)
self.softmax = nn.LogSoftmax(dim=1)
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.in_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):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool1(x)
x = self.fire2(x)
x = self.fire3(x)
x = self.fire4(x)
x = self.maxpool2(x)
x = self.fire5(x)
x = self.fire6(x)
x = self.fire7(x)
x = self.fire8(x)
x = self.maxpool3(x)
x = self.fire9(x)
x = self.conv2(x)
x = self.avg_pool(x)
x = self.softmax(x)
return x
def fire_layer(inp, s, e):
f = fire(inp, s, e)
return f
def squeezenet(pretrained=False):
net = SqueezeNet()
# inp = Variable(torch.randn(64,3,32,32))
# out = net.forward(inp)
# print(out.size())
return net
# if __name__ == '__main__':
# squeezenet()