-
Notifications
You must be signed in to change notification settings - Fork 0
/
squeeze_net.py
139 lines (119 loc) · 5.58 KB
/
squeeze_net.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import six
import numpy as np
import functools
import chainer.links as L
import chainer.functions as F
from collections import defaultdict
import nutszebra_chainer
class BN_ReLU_Conv(nutszebra_chainer.Model):
def __init__(self, in_channel, out_channel, filter_size=(3, 3), stride=(1, 1), pad=(1, 1)):
super(BN_ReLU_Conv, self).__init__(
conv=L.Convolution2D(in_channel, out_channel, filter_size, stride, pad),
bn=L.BatchNormalization(in_channel),
)
def weight_initialization(self):
self.conv.W.data = self.weight_relu_initialization(self.conv)
self.conv.b.data = self.bias_initialization(self.conv, constant=0)
def __call__(self, x, train=False):
return self.conv(F.relu(self.bn(x, test=not train)))
def count_parameters(self):
return functools.reduce(lambda a, b: a * b, self.conv.W.data.shape)
class FireModule(nutszebra_chainer.Model):
def __init__(self, in_size, s1x1, e1x1, e3x3):
super(FireModule, self).__init__()
modules = []
modules.append(('bn_relu_conv_s_1x1', BN_ReLU_Conv(in_size, s1x1, filter_size=(1, 1), stride=(1, 1), pad=(0, 0))))
modules.append(('bn_relu_conv_e_1x1', BN_ReLU_Conv(s1x1, e1x1, filter_size=(1, 1), stride=(1, 1), pad=(0, 0))))
modules.append(('bn_relu_conv_e_3x3', BN_ReLU_Conv(s1x1, e3x3, filter_size=(3, 3), stride=(1, 1), pad=(1, 1))))
# register layers
[self.add_link(*link) for link in modules]
self.modules = modules
def weight_initialization(self):
self.bn_relu_conv_s_1x1.weight_initialization()
self.bn_relu_conv_e_1x1.weight_initialization()
self.bn_relu_conv_e_3x3.weight_initialization()
def __call__(self, x, train=False):
h = self.bn_relu_conv_s_1x1(x, train=train)
h1 = self.bn_relu_conv_e_1x1(h, train=train)
h2 = self.bn_relu_conv_e_3x3(h, train=train)
return F.concat((h1, h2), axis=1)
def count_parameters(self):
count = 0
count += self.bn_relu_conv_s_1x1.count_parameters()
count += self.bn_relu_conv_e_1x1.count_parameters()
count += self.bn_relu_conv_e_3x3.count_parameters()
return count
class SqueezeNet(nutszebra_chainer.Model):
def __init__(self, category_num):
super(SqueezeNet, self).__init__()
modules = []
modules = []
modules += [('conv1', L.Convolution2D(3, 96, (7, 7), (2, 2), (2, 2)))]
# fire module(in_size, s1x1, e1x1, e3x3)
modules += [('fire2', FireModule(96, 16, 64, 64))]
modules += [('fire3', FireModule(128, 16, 64, 64))]
modules += [('fire4', FireModule(128, 32, 128, 128))]
modules += [('fire5', FireModule(256, 32, 128, 128))]
modules += [('fire6', FireModule(256, 48, 192, 192))]
modules += [('fire7', FireModule(384, 48, 192, 192))]
modules += [('fire8', FireModule(384, 64, 256, 256))]
modules += [('fire9', FireModule(512, 64, 256, 256))]
modules += [('bn_relu_conv10', BN_ReLU_Conv(512, category_num, (1, 1), (1, 1), (0, 0)))]
# register layers
[self.add_link(*link) for link in modules]
self.modules = modules
self.name = 'squeeze_res_net'
def count_parameters(self):
count = 0
count += functools.reduce(lambda a, b: a * b, self.conv1.W.data.shape)
count += self.fire2.count_parameters()
count += self.fire3.count_parameters()
count += self.fire4.count_parameters()
count += self.fire5.count_parameters()
count += self.fire6.count_parameters()
count += self.fire7.count_parameters()
count += self.fire8.count_parameters()
count += self.fire9.count_parameters()
count += self.bn_relu_conv10.count_parameters()
return count
def weight_initialization(self):
self.conv1.W.data = self.weight_relu_initialization(self.conv1)
self.conv1.b.data = self.bias_initialization(self.conv1, constant=0)
# *****fire modules*****
for i in six.moves.range(2, 10):
self['fire{}'.format(i)].weight_initialization()
self.bn_relu_conv10.weight_initialization()
def __call__(self, x, train=True):
h = self.conv1(x)
h = F.max_pooling_2d(h, ksize=(3, 3), stride=(2, 2), pad=(1, 1))
h = self.fire2(h, train=train)
h = self.fire3(h, train=train) + h
h = self.fire4(h, train=train)
h = F.max_pooling_2d(h, ksize=(3, 3), stride=(2, 2), pad=(1, 1))
h = self.fire5(h, train=train) + h
h = self.fire6(h, train=train)
h = self.fire7(h, train=train) + h
h = self.fire8(h, train=train)
h = F.max_pooling_2d(h, ksize=(3, 3), stride=(2, 2), pad=(1, 1))
h = F.dropout(self.fire9(h, train=train) + h, ratio=0.5, train=train)
h = self.bn_relu_conv10(h, train=train)
num, categories, y, x = h.data.shape
# global average pooling
h = F.reshape(F.average_pooling_2d(h, (y, x)), (num, categories))
return h
def calc_loss(self, y, t):
loss = F.softmax_cross_entropy(y, t)
return loss
def accuracy(self, y, t, xp=np):
y.to_cpu()
t.to_cpu()
indices = np.where((t.data == np.argmax(y.data, axis=1)) == True)[0]
accuracy = defaultdict(int)
for i in indices:
accuracy[t.data[i]] += 1
indices = np.where((t.data == np.argmax(y.data, axis=1)) == False)[0]
false_accuracy = defaultdict(int)
false_y = np.argmax(y.data, axis=1)
for i in indices:
false_accuracy[(t.data[i], false_y[i])] += 1
return accuracy, false_accuracy