forked from ZFTurbo/KAGGLE_CERVICAL_CANCER_2017
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha01_squeezenet.py
82 lines (64 loc) · 3.25 KB
/
a01_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
# coding: utf-8
# Based on: https://github.com/rcmalli/keras-squeezenet
__author__ = 'ZFTurbo: https://kaggle.com/zfturbo'
from keras.layers import Input, merge
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.layers.core import Dropout, Activation
from keras.layers.pooling import GlobalAveragePooling2D
from keras.models import Model
sq1x1 = "squeeze1x1"
exp1x1 = "expand1x1"
exp3x3 = "expand3x3"
relu = "relu_"
# Modular function for Fire Node
def fire_module(x, fire_id, squeeze=16, expand=64, dim_ordering='th'):
s_id = 'fire' + str(fire_id) + '/'
if dim_ordering is 'tf':
c_axis = 3
else:
c_axis = 1
x = Convolution2D(squeeze, 1, 1, border_mode='valid', name=s_id + sq1x1)(x)
x = Activation('relu', name=s_id + relu + sq1x1)(x)
left = Convolution2D(expand, 1, 1, border_mode='valid', name=s_id + exp1x1)(x)
left = Activation('relu', name=s_id + relu + exp1x1)(left)
right = Convolution2D(expand, 3, 3, border_mode='same', name=s_id + exp3x3)(x)
right = Activation('relu', name=s_id + relu + exp3x3)(right)
x = merge([left, right], mode='concat', concat_axis=c_axis, name=s_id + 'concat')
return x
# Original SqueezeNet from paper.
def get_squeezenet_top(dim_ordering='th'):
if dim_ordering is 'th':
input_img = Input(shape=(3, 227, 227))
elif dim_ordering is 'tf':
input_img = Input(shape=(227, 227, 3))
else:
raise NotImplementedError("Theano and Tensorflow are only available")
x = Convolution2D(64, 3, 3, subsample=(2, 2), border_mode='valid', name='conv1')(input_img)
x = Activation('relu', name='relu_conv1')(x)
x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool1')(x)
x = fire_module(x, fire_id=2, squeeze=16, expand=64, dim_ordering=dim_ordering)
x = fire_module(x, fire_id=3, squeeze=16, expand=64, dim_ordering=dim_ordering)
x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool3')(x)
x = fire_module(x, fire_id=4, squeeze=32, expand=128, dim_ordering=dim_ordering)
x = fire_module(x, fire_id=5, squeeze=32, expand=128, dim_ordering=dim_ordering)
x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool5')(x)
x = fire_module(x, fire_id=6, squeeze=48, expand=192, dim_ordering=dim_ordering)
x = fire_module(x, fire_id=7, squeeze=48, expand=192, dim_ordering=dim_ordering)
x = fire_module(x, fire_id=8, squeeze=64, expand=256, dim_ordering=dim_ordering)
x = fire_module(x, fire_id=9, squeeze=64, expand=256, dim_ordering=dim_ordering)
x = Dropout(0.5, name='drop9')(x)
model = Model(input=input_img, output=[x])
model.load_weights('../weights/squeezenet_weights_th_dim_ordering_th_kernels.h5', by_name=True)
return model
def get_squeezenet(nb_classes, dim_ordering='th'):
base_model = get_squeezenet_top()
x = base_model.layers[-1].output
x = Convolution2D(nb_classes, 1, 1, border_mode='valid', name='conv10')(x)
x = Activation('relu', name='relu_conv10')(x)
x = GlobalAveragePooling2D()(x)
out = Activation('softmax', name='loss')(x)
model = Model(input=base_model.input, output=[out])
return model
if __name__ == '__main__':
model = get_squeezenet(1000, dim_ordering='th')
model.compile(loss="categorical_crossentropy", optimizer="adam")