-
Notifications
You must be signed in to change notification settings - Fork 2
/
GoogLeNet.py
89 lines (64 loc) · 3.4 KB
/
GoogLeNet.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
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
# GoogLeNet
from keras.optimizers import Adam
from keras.layers import Dense, Activation, Conv2D, MaxPooling2D, Flatten,Dropout
from keras.layers import BatchNormalization,AveragePooling2D,concatenate
from keras.layers import Input, concatenate
from keras.layers import Add
from keras.layers import GlobalAveragePooling2D
from keras.models import Model
#Define convolution with batchnromalization
def Conv2d_BN(x, nb_filter,kernel_size, padding='same',strides=(1,1),name=None):
if name is not None:
bn_name = name + '_bn'
conv_name = name + '_conv'
else:
bn_name = None
conv_name = None
x = Conv2D(nb_filter,kernel_size,padding=padding,strides=strides,activation='relu',name=conv_name)(x)
x = BatchNormalization(axis=3,name=bn_name)(x)
return x
#Define Inception structure
def Inception(x,nb_filter_para):
(branch1,branch2,branch3,branch4)= nb_filter_para
branch1x1 = Conv2D(branch1[0],(1,1), padding='same',strides=(1,1),name=None)(x)
branch3x3 = Conv2D(branch2[0],(1,1), padding='same',strides=(1,1),name=None)(x)
branch3x3 = Conv2D(branch2[1],(3,3), padding='same',strides=(1,1),name=None)(branch3x3)
branch5x5 = Conv2D(branch3[0],(1,1), padding='same',strides=(1,1),name=None)(x)
branch5x5 = Conv2D(branch3[1],(1,1), padding='same',strides=(1,1),name=None)(branch5x5)
branchpool = MaxPooling2D(pool_size=(3,3),strides=(1,1),padding='same')(x)
branchpool = Conv2D(branch4[0],(1,1),padding='same',strides=(1,1),name=None)(branchpool)
x = concatenate([branch1x1,branch3x3,branch5x5,branchpool],axis=3)
return x
#Build InceptionV1 model
def InceptionV1(width, height, depth, classes):
inpt = Input(shape=(width,height,depth))
x = Conv2d_BN(inpt,64,(7,7),strides=(2,2),padding='same')
x = MaxPooling2D(pool_size=(3,3),strides=(2,2),padding='same')(x)
x = Conv2d_BN(x,192,(3,3),strides=(1,1),padding='same')
x = MaxPooling2D(pool_size=(3,3),strides=(2,2),padding='same')(x)
x = Inception(x,[(64,),(96,128),(16,32),(32,)]) #Inception 3a 28x28x256
x = Inception(x,[(128,),(128,192),(32,96),(64,)]) #Inception 3b 28x28x480
x = MaxPooling2D(pool_size=(3,3),strides=(2,2),padding='same')(x) #14x14x480
x = Inception(x,[(192,),(96,208),(16,48),(64,)]) #Inception 4a 14x14x512
x = Inception(x,[(160,),(112,224),(24,64),(64,)]) #Inception 4a 14x14x512
x = Inception(x,[(128,),(128,256),(24,64),(64,)]) #Inception 4a 14x14x512
x = Inception(x,[(112,),(144,288),(32,64),(64,)]) #Inception 4a 14x14x528
x = Inception(x,[(256,),(160,320),(32,128),(128,)]) #Inception 4a 14x14x832
x = MaxPooling2D(pool_size=(3,3),strides=(2,2),padding='same')(x) #7x7x832
x = Inception(x,[(256,),(160,320),(32,128),(128,)]) #Inception 5a 7x7x832
x = Inception(x,[(384,),(192,384),(48,128),(128,)]) #Inception 5b 7x7x1024
#Using AveragePooling replace flatten
x = AveragePooling2D(pool_size=(7,7),strides=(7,7),padding='same')(x)
x =Flatten()(x)
x = Dropout(0.4)(x)
x = Dense(1000,activation='relu')(x)
x = Dense(classes,activation='softmax')(x)
model=Model(input=inpt,output=x)
return model
InceptionV1_model = InceptionV1(224,224,3,2)
InceptionV1_model.summary()
InceptionV1_model.compile(optimizer=Adam(lr=0.00001, beta_1=0.9, beta_2=0.999, epsilon=1e-08),loss = 'categorical_crossentropy',metrics=['accuracy'])
model = InceptionV1_model