-
Notifications
You must be signed in to change notification settings - Fork 0
/
squeezenet.py
96 lines (71 loc) · 2.87 KB
/
squeezenet.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
import numpy as np
import keras
from keras.models import Model
from keras.layers import Input, Convolution2D, MaxPooling2D
from keras.layers import Activation, Dropout, GlobalAveragePooling2D, concatenate
mean = np.array([0.485, 0.456, 0.406], dtype='float32')
std = np.array([0.229, 0.224, 0.225], dtype='float32')
def preprocess_input(x):
x /= 255.0
x -= mean
x /= std
return x
# a building block of the SqueezeNet architecture
def fire_module(number, x, squeeze, expand, weight_decay=None, trainable=False):
module_name = 'fire' + number
if trainable and weight_decay is not None:
kernel_regularizer = keras.regularizers.l2(weight_decay)
else:
kernel_regularizer = None
x = Convolution2D(
squeeze, (1, 1),
name=module_name + '/' + 'squeeze',
trainable=trainable,
kernel_regularizer=kernel_regularizer
)(x)
x = Activation('relu')(x)
a = Convolution2D(
expand, (1, 1),
name=module_name + '/' + 'expand1x1',
trainable=trainable,
kernel_regularizer=kernel_regularizer
)(x)
a = Activation('relu')(a)
b = Convolution2D(
expand, (3, 3), padding='same',
name=module_name + '/' + 'expand3x3',
trainable=trainable,
kernel_regularizer=kernel_regularizer
)(x)
b = Activation('relu')(b)
return concatenate([a, b])
def SqueezeNet(weight_decay, image_size=224):
image = Input(shape=(image_size, image_size, 3))
x = Convolution2D(
64, (3, 3), strides=(2, 2), name='conv1',
trainable=False
)(image) # 111, 111, 64
x = Activation('relu')(x)
x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2))(x) # 55, 55, 64
x = fire_module('2', x, squeeze=16, expand=64) # 55, 55, 128
x = fire_module('3', x, squeeze=16, expand=64) # 55, 55, 128
x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2))(x) # 27, 27, 128
x = fire_module('4', x, squeeze=32, expand=128) # 27, 27, 256
x = fire_module('5', x, squeeze=32, expand=128) # 27, 27, 256
x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2))(x) # 13, 13, 256
x = fire_module('6', x, squeeze=48, expand=192) # 13, 13, 384
x = fire_module('7', x, squeeze=48, expand=192) # 13, 13, 384
x = fire_module('8', x, squeeze=64, expand=256) # 13, 13, 512
x = fire_module('9', x, squeeze=64, expand=256) # 13, 13, 512
x = Dropout(0.5)(x)
x = Convolution2D(
256, (1, 1), name='conv10',
kernel_initializer=keras.initializers.RandomNormal(stddev=0.01),
kernel_regularizer=keras.regularizers.l2(weight_decay)
)(x) # 13, 13, 256
x = Activation('relu')(x)
logits = GlobalAveragePooling2D()(x) # 256
probabilities = Activation('softmax')(logits)
model = Model(image, probabilities)
model.load_weights('squeezenet_weights.hdf5', by_name=True)
return model