In [26]:
from keras import backend as K
from keras.models import Sequential
from keras.layers.core import Flatten, Dense, Dropout
from keras.layers.convolutional import Conv2D, MaxPooling2D, ZeroPadding2D
from keras.optimizers import SGD
import cv2
import numpy as np
In [2]:
model = Sequential()
model.add(ZeroPadding2D((1,1),input_shape=(224,224,3)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))

model.add(ZeroPadding2D((1,1)))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))

model.add(ZeroPadding2D((1,1)))
model.add(Conv2D(256, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Conv2D(256, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Conv2D(256, (3, 3), activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))

model.add(ZeroPadding2D((1,1)))
model.add(Conv2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Conv2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Conv2D(512, (3, 3), activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))

model.add(ZeroPadding2D((1,1)))
model.add(Conv2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Conv2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Conv2D(512, (3, 3), activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))

model.add(Flatten())

#top layer of the VGG net
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1000, activation='softmax'))

model.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
zero_padding2d_1 (ZeroPaddin (None, 226, 226, 3)       0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 224, 224, 64)      1792      
_________________________________________________________________
zero_padding2d_2 (ZeroPaddin (None, 226, 226, 64)      0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 224, 224, 64)      36928     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 112, 112, 64)      0         
_________________________________________________________________
zero_padding2d_3 (ZeroPaddin (None, 114, 114, 64)      0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 112, 112, 128)     73856     
_________________________________________________________________
zero_padding2d_4 (ZeroPaddin (None, 114, 114, 128)     0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 112, 112, 128)     147584    
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 56, 56, 128)       0         
_________________________________________________________________
zero_padding2d_5 (ZeroPaddin (None, 58, 58, 128)       0         
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 56, 56, 256)       295168    
_________________________________________________________________
zero_padding2d_6 (ZeroPaddin (None, 58, 58, 256)       0         
_________________________________________________________________
conv2d_6 (Conv2D)            (None, 56, 56, 256)       590080    
_________________________________________________________________
zero_padding2d_7 (ZeroPaddin (None, 58, 58, 256)       0         
_________________________________________________________________
conv2d_7 (Conv2D)            (None, 56, 56, 256)       590080    
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 28, 28, 256)       0         
_________________________________________________________________
zero_padding2d_8 (ZeroPaddin (None, 30, 30, 256)       0         
_________________________________________________________________
conv2d_8 (Conv2D)            (None, 28, 28, 512)       1180160   
_________________________________________________________________
zero_padding2d_9 (ZeroPaddin (None, 30, 30, 512)       0         
_________________________________________________________________
conv2d_9 (Conv2D)            (None, 28, 28, 512)       2359808   
_________________________________________________________________
zero_padding2d_10 (ZeroPaddi (None, 30, 30, 512)       0         
_________________________________________________________________
conv2d_10 (Conv2D)           (None, 28, 28, 512)       2359808   
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 14, 14, 512)       0         
_________________________________________________________________
zero_padding2d_11 (ZeroPaddi (None, 16, 16, 512)       0         
_________________________________________________________________
conv2d_11 (Conv2D)           (None, 14, 14, 512)       2359808   
_________________________________________________________________
zero_padding2d_12 (ZeroPaddi (None, 16, 16, 512)       0         
_________________________________________________________________
conv2d_12 (Conv2D)           (None, 14, 14, 512)       2359808   
_________________________________________________________________
zero_padding2d_13 (ZeroPaddi (None, 16, 16, 512)       0         
_________________________________________________________________
conv2d_13 (Conv2D)           (None, 14, 14, 512)       2359808   
_________________________________________________________________
max_pooling2d_5 (MaxPooling2 (None, 7, 7, 512)         0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 25088)             0         
_________________________________________________________________
dense_1 (Dense)              (None, 4096)              102764544 
_________________________________________________________________
dropout_1 (Dropout)          (None, 4096)              0         
_________________________________________________________________
dense_2 (Dense)              (None, 4096)              16781312  
_________________________________________________________________
dropout_2 (Dropout)          (None, 4096)              0         
_________________________________________________________________
dense_3 (Dense)              (None, 1000)              4097000   
=================================================================
Total params: 138,357,544
Trainable params: 138,357,544
Non-trainable params: 0
_________________________________________________________________
In [3]:
model.load_weights("model/vgg16_weights_tf_dim_ordering_tf_kernels.h5")
In [4]:
optimizer = SGD()
model.compile(optimizer=optimizer, loss='categorical_crossentropy')
In [14]:
im = cv2.resize(cv2.imread('cat.jpg'), (224, 224)).astype(np.float32)
# im = im.transpose((2,0,1))
im = np.expand_dims(im, axis=0)
In [6]:
out = model.predict(im)
print(np.argmax(out))
# 285: 'Egyptian cat'
# https://gist.github.com/yrevar/942d3a0ac09ec9e5eb3a  
285

Keras Build-In VGG16

In [8]:
from keras.applications.vgg16 import VGG16
import matplotlib.pyplot as plt
In [9]:
# prebuild model with pre-trained weights on imagenet
model = VGG16(weights='imagenet', include_top=True)
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(optimizer=sgd, loss='categorical_crossentropy')
In [39]:
im = cv2.resize(cv2.imread('locomotive.jpg'), (224, 224)).astype(np.float32)
im = np.expand_dims(im, axis=0)
In [40]:
# predict
out = model.predict(im)
plt.plot(out.ravel())
plt.show()
print( np.argmax(out) )  # 820: 'steam locomotive'
820

Display Images

(1) IPython.display.Image

In [30]:
from IPython.display import Image
In [32]:
Image('cat.jpg')
Out[32]:
In [33]:
Image('dog.jpg')
Out[33]:
In [38]:
Image('locomotive.jpg')
Out[38]:

(2) matplotlib.pyplot

In [34]:
%matplotlib inline
from matplotlib import pyplot as plt
In [36]:
image = cv2.imread('cat.jpg')
plt.imshow(image)
plt.show()

Feature Extraction

In [43]:
from keras.models import Model
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
In [42]:
# pre-built and pre-trained deep learning VGG16 model
base_model = VGG16(weights='imagenet', include_top=True)
for i, layer in enumerate(base_model.layers):
    print (i, layer.name, layer.output_shape)
0 input_2 (None, 224, 224, 3)
1 block1_conv1 (None, 224, 224, 64)
2 block1_conv2 (None, 224, 224, 64)
3 block1_pool (None, 112, 112, 64)
4 block2_conv1 (None, 112, 112, 128)
5 block2_conv2 (None, 112, 112, 128)
6 block2_pool (None, 56, 56, 128)
7 block3_conv1 (None, 56, 56, 256)
8 block3_conv2 (None, 56, 56, 256)
9 block3_conv3 (None, 56, 56, 256)
10 block3_pool (None, 28, 28, 256)
11 block4_conv1 (None, 28, 28, 512)
12 block4_conv2 (None, 28, 28, 512)
13 block4_conv3 (None, 28, 28, 512)
14 block4_pool (None, 14, 14, 512)
15 block5_conv1 (None, 14, 14, 512)
16 block5_conv2 (None, 14, 14, 512)
17 block5_conv3 (None, 14, 14, 512)
18 block5_pool (None, 7, 7, 512)
19 flatten (None, 25088)
20 fc1 (None, 4096)
21 fc2 (None, 4096)
22 predictions (None, 1000)
In [44]:
# extract features from block4_pool block
model = Model(input=base_model.input, output=base_model.get_layer('block4_pool').output)
img = image.load_img('cat.jpg', target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
C:\etc\Anaconda3\envs\r-tensorflow\lib\site-packages\ipykernel\__main__.py:2: UserWarning: Update your `Model` call to the Keras 2 API: `Model(outputs=Tensor("bl..., inputs=Tensor("in...)`
  from ipykernel import kernelapp as app
In [50]:
# get the features from this block
features = model.predict(x)
features.shape
Out[50]:
(1, 14, 14, 512)

Transfer Learning

(1) Load base model. Exclude the top layers !!

In [51]:
from keras.applications.inception_v3 import InceptionV3
from keras.preprocessing import image
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
from keras import backend as K

# create the base pre-trained model. Exclude the top !!
base_model = InceptionV3(weights='imagenet', include_top=False) 

for i, layer in enumerate(base_model.layers):
    print (i, layer.name, layer.output_shape)
Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.5/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5
87900160/87910968 [============================>.] - ETA: 0s0 input_3 (None, None, None, 3)
1 conv2d_14 (None, None, None, 32)
2 batch_normalization_1 (None, None, None, 32)
3 activation_1 (None, None, None, 32)
4 conv2d_15 (None, None, None, 32)
5 batch_normalization_2 (None, None, None, 32)
6 activation_2 (None, None, None, 32)
7 conv2d_16 (None, None, None, 64)
8 batch_normalization_3 (None, None, None, 64)
9 activation_3 (None, None, None, 64)
10 max_pooling2d_6 (None, None, None, 64)
11 conv2d_17 (None, None, None, 80)
12 batch_normalization_4 (None, None, None, 80)
13 activation_4 (None, None, None, 80)
14 conv2d_18 (None, None, None, 192)
15 batch_normalization_5 (None, None, None, 192)
16 activation_5 (None, None, None, 192)
17 max_pooling2d_7 (None, None, None, 192)
18 conv2d_22 (None, None, None, 64)
19 batch_normalization_9 (None, None, None, 64)
20 activation_9 (None, None, None, 64)
21 conv2d_20 (None, None, None, 48)
22 conv2d_23 (None, None, None, 96)
23 batch_normalization_7 (None, None, None, 48)
24 batch_normalization_10 (None, None, None, 96)
25 activation_7 (None, None, None, 48)
26 activation_10 (None, None, None, 96)
27 average_pooling2d_1 (None, None, None, 192)
28 conv2d_19 (None, None, None, 64)
29 conv2d_21 (None, None, None, 64)
30 conv2d_24 (None, None, None, 96)
31 conv2d_25 (None, None, None, 32)
32 batch_normalization_6 (None, None, None, 64)
33 batch_normalization_8 (None, None, None, 64)
34 batch_normalization_11 (None, None, None, 96)
35 batch_normalization_12 (None, None, None, 32)
36 activation_6 (None, None, None, 64)
37 activation_8 (None, None, None, 64)
38 activation_11 (None, None, None, 96)
39 activation_12 (None, None, None, 32)
40 mixed0 (None, None, None, 256)
41 conv2d_29 (None, None, None, 64)
42 batch_normalization_16 (None, None, None, 64)
43 activation_16 (None, None, None, 64)
44 conv2d_27 (None, None, None, 48)
45 conv2d_30 (None, None, None, 96)
46 batch_normalization_14 (None, None, None, 48)
47 batch_normalization_17 (None, None, None, 96)
48 activation_14 (None, None, None, 48)
49 activation_17 (None, None, None, 96)
50 average_pooling2d_2 (None, None, None, 256)
51 conv2d_26 (None, None, None, 64)
52 conv2d_28 (None, None, None, 64)
53 conv2d_31 (None, None, None, 96)
54 conv2d_32 (None, None, None, 64)
55 batch_normalization_13 (None, None, None, 64)
56 batch_normalization_15 (None, None, None, 64)
57 batch_normalization_18 (None, None, None, 96)
58 batch_normalization_19 (None, None, None, 64)
59 activation_13 (None, None, None, 64)
60 activation_15 (None, None, None, 64)
61 activation_18 (None, None, None, 96)
62 activation_19 (None, None, None, 64)
63 mixed1 (None, None, None, 288)
64 conv2d_36 (None, None, None, 64)
65 batch_normalization_23 (None, None, None, 64)
66 activation_23 (None, None, None, 64)
67 conv2d_34 (None, None, None, 48)
68 conv2d_37 (None, None, None, 96)
69 batch_normalization_21 (None, None, None, 48)
70 batch_normalization_24 (None, None, None, 96)
71 activation_21 (None, None, None, 48)
72 activation_24 (None, None, None, 96)
73 average_pooling2d_3 (None, None, None, 288)
74 conv2d_33 (None, None, None, 64)
75 conv2d_35 (None, None, None, 64)
76 conv2d_38 (None, None, None, 96)
77 conv2d_39 (None, None, None, 64)
78 batch_normalization_20 (None, None, None, 64)
79 batch_normalization_22 (None, None, None, 64)
80 batch_normalization_25 (None, None, None, 96)
81 batch_normalization_26 (None, None, None, 64)
82 activation_20 (None, None, None, 64)
83 activation_22 (None, None, None, 64)
84 activation_25 (None, None, None, 96)
85 activation_26 (None, None, None, 64)
86 mixed2 (None, None, None, 288)
87 conv2d_41 (None, None, None, 64)
88 batch_normalization_28 (None, None, None, 64)
89 activation_28 (None, None, None, 64)
90 conv2d_42 (None, None, None, 96)
91 batch_normalization_29 (None, None, None, 96)
92 activation_29 (None, None, None, 96)
93 conv2d_40 (None, None, None, 384)
94 conv2d_43 (None, None, None, 96)
95 batch_normalization_27 (None, None, None, 384)
96 batch_normalization_30 (None, None, None, 96)
97 activation_27 (None, None, None, 384)
98 activation_30 (None, None, None, 96)
99 max_pooling2d_8 (None, None, None, 288)
100 mixed3 (None, None, None, 768)
101 conv2d_48 (None, None, None, 128)
102 batch_normalization_35 (None, None, None, 128)
103 activation_35 (None, None, None, 128)
104 conv2d_49 (None, None, None, 128)
105 batch_normalization_36 (None, None, None, 128)
106 activation_36 (None, None, None, 128)
107 conv2d_45 (None, None, None, 128)
108 conv2d_50 (None, None, None, 128)
109 batch_normalization_32 (None, None, None, 128)
110 batch_normalization_37 (None, None, None, 128)
111 activation_32 (None, None, None, 128)
112 activation_37 (None, None, None, 128)
113 conv2d_46 (None, None, None, 128)
114 conv2d_51 (None, None, None, 128)
115 batch_normalization_33 (None, None, None, 128)
116 batch_normalization_38 (None, None, None, 128)
117 activation_33 (None, None, None, 128)
118 activation_38 (None, None, None, 128)
119 average_pooling2d_4 (None, None, None, 768)
120 conv2d_44 (None, None, None, 192)
121 conv2d_47 (None, None, None, 192)
122 conv2d_52 (None, None, None, 192)
123 conv2d_53 (None, None, None, 192)
124 batch_normalization_31 (None, None, None, 192)
125 batch_normalization_34 (None, None, None, 192)
126 batch_normalization_39 (None, None, None, 192)
127 batch_normalization_40 (None, None, None, 192)
128 activation_31 (None, None, None, 192)
129 activation_34 (None, None, None, 192)
130 activation_39 (None, None, None, 192)
131 activation_40 (None, None, None, 192)
132 mixed4 (None, None, None, 768)
133 conv2d_58 (None, None, None, 160)
134 batch_normalization_45 (None, None, None, 160)
135 activation_45 (None, None, None, 160)
136 conv2d_59 (None, None, None, 160)
137 batch_normalization_46 (None, None, None, 160)
138 activation_46 (None, None, None, 160)
139 conv2d_55 (None, None, None, 160)
140 conv2d_60 (None, None, None, 160)
141 batch_normalization_42 (None, None, None, 160)
142 batch_normalization_47 (None, None, None, 160)
143 activation_42 (None, None, None, 160)
144 activation_47 (None, None, None, 160)
145 conv2d_56 (None, None, None, 160)
146 conv2d_61 (None, None, None, 160)
147 batch_normalization_43 (None, None, None, 160)
148 batch_normalization_48 (None, None, None, 160)
149 activation_43 (None, None, None, 160)
150 activation_48 (None, None, None, 160)
151 average_pooling2d_5 (None, None, None, 768)
152 conv2d_54 (None, None, None, 192)
153 conv2d_57 (None, None, None, 192)
154 conv2d_62 (None, None, None, 192)
155 conv2d_63 (None, None, None, 192)
156 batch_normalization_41 (None, None, None, 192)
157 batch_normalization_44 (None, None, None, 192)
158 batch_normalization_49 (None, None, None, 192)
159 batch_normalization_50 (None, None, None, 192)
160 activation_41 (None, None, None, 192)
161 activation_44 (None, None, None, 192)
162 activation_49 (None, None, None, 192)
163 activation_50 (None, None, None, 192)
164 mixed5 (None, None, None, 768)
165 conv2d_68 (None, None, None, 160)
166 batch_normalization_55 (None, None, None, 160)
167 activation_55 (None, None, None, 160)
168 conv2d_69 (None, None, None, 160)
169 batch_normalization_56 (None, None, None, 160)
170 activation_56 (None, None, None, 160)
171 conv2d_65 (None, None, None, 160)
172 conv2d_70 (None, None, None, 160)
173 batch_normalization_52 (None, None, None, 160)
174 batch_normalization_57 (None, None, None, 160)
175 activation_52 (None, None, None, 160)
176 activation_57 (None, None, None, 160)
177 conv2d_66 (None, None, None, 160)
178 conv2d_71 (None, None, None, 160)
179 batch_normalization_53 (None, None, None, 160)
180 batch_normalization_58 (None, None, None, 160)
181 activation_53 (None, None, None, 160)
182 activation_58 (None, None, None, 160)
183 average_pooling2d_6 (None, None, None, 768)
184 conv2d_64 (None, None, None, 192)
185 conv2d_67 (None, None, None, 192)
186 conv2d_72 (None, None, None, 192)
187 conv2d_73 (None, None, None, 192)
188 batch_normalization_51 (None, None, None, 192)
189 batch_normalization_54 (None, None, None, 192)
190 batch_normalization_59 (None, None, None, 192)
191 batch_normalization_60 (None, None, None, 192)
192 activation_51 (None, None, None, 192)
193 activation_54 (None, None, None, 192)
194 activation_59 (None, None, None, 192)
195 activation_60 (None, None, None, 192)
196 mixed6 (None, None, None, 768)
197 conv2d_78 (None, None, None, 192)
198 batch_normalization_65 (None, None, None, 192)
199 activation_65 (None, None, None, 192)
200 conv2d_79 (None, None, None, 192)
201 batch_normalization_66 (None, None, None, 192)
202 activation_66 (None, None, None, 192)
203 conv2d_75 (None, None, None, 192)
204 conv2d_80 (None, None, None, 192)
205 batch_normalization_62 (None, None, None, 192)
206 batch_normalization_67 (None, None, None, 192)
207 activation_62 (None, None, None, 192)
208 activation_67 (None, None, None, 192)
209 conv2d_76 (None, None, None, 192)
210 conv2d_81 (None, None, None, 192)
211 batch_normalization_63 (None, None, None, 192)
212 batch_normalization_68 (None, None, None, 192)
213 activation_63 (None, None, None, 192)
214 activation_68 (None, None, None, 192)
215 average_pooling2d_7 (None, None, None, 768)
216 conv2d_74 (None, None, None, 192)
217 conv2d_77 (None, None, None, 192)
218 conv2d_82 (None, None, None, 192)
219 conv2d_83 (None, None, None, 192)
220 batch_normalization_61 (None, None, None, 192)
221 batch_normalization_64 (None, None, None, 192)
222 batch_normalization_69 (None, None, None, 192)
223 batch_normalization_70 (None, None, None, 192)
224 activation_61 (None, None, None, 192)
225 activation_64 (None, None, None, 192)
226 activation_69 (None, None, None, 192)
227 activation_70 (None, None, None, 192)
228 mixed7 (None, None, None, 768)
229 conv2d_86 (None, None, None, 192)
230 batch_normalization_73 (None, None, None, 192)
231 activation_73 (None, None, None, 192)
232 conv2d_87 (None, None, None, 192)
233 batch_normalization_74 (None, None, None, 192)
234 activation_74 (None, None, None, 192)
235 conv2d_84 (None, None, None, 192)
236 conv2d_88 (None, None, None, 192)
237 batch_normalization_71 (None, None, None, 192)
238 batch_normalization_75 (None, None, None, 192)
239 activation_71 (None, None, None, 192)
240 activation_75 (None, None, None, 192)
241 conv2d_85 (None, None, None, 320)
242 conv2d_89 (None, None, None, 192)
243 batch_normalization_72 (None, None, None, 320)
244 batch_normalization_76 (None, None, None, 192)
245 activation_72 (None, None, None, 320)
246 activation_76 (None, None, None, 192)
247 max_pooling2d_9 (None, None, None, 768)
248 mixed8 (None, None, None, 1280)
249 conv2d_94 (None, None, None, 448)
250 batch_normalization_81 (None, None, None, 448)
251 activation_81 (None, None, None, 448)
252 conv2d_91 (None, None, None, 384)
253 conv2d_95 (None, None, None, 384)
254 batch_normalization_78 (None, None, None, 384)
255 batch_normalization_82 (None, None, None, 384)
256 activation_78 (None, None, None, 384)
257 activation_82 (None, None, None, 384)
258 conv2d_92 (None, None, None, 384)
259 conv2d_93 (None, None, None, 384)
260 conv2d_96 (None, None, None, 384)
261 conv2d_97 (None, None, None, 384)
262 average_pooling2d_8 (None, None, None, 1280)
263 conv2d_90 (None, None, None, 320)
264 batch_normalization_79 (None, None, None, 384)
265 batch_normalization_80 (None, None, None, 384)
266 batch_normalization_83 (None, None, None, 384)
267 batch_normalization_84 (None, None, None, 384)
268 conv2d_98 (None, None, None, 192)
269 batch_normalization_77 (None, None, None, 320)
270 activation_79 (None, None, None, 384)
271 activation_80 (None, None, None, 384)
272 activation_83 (None, None, None, 384)
273 activation_84 (None, None, None, 384)
274 batch_normalization_85 (None, None, None, 192)
275 activation_77 (None, None, None, 320)
276 mixed9_0 (None, None, None, 768)
277 concatenate_1 (None, None, None, 768)
278 activation_85 (None, None, None, 192)
279 mixed9 (None, None, None, 2048)
280 conv2d_103 (None, None, None, 448)
281 batch_normalization_90 (None, None, None, 448)
282 activation_90 (None, None, None, 448)
283 conv2d_100 (None, None, None, 384)
284 conv2d_104 (None, None, None, 384)
285 batch_normalization_87 (None, None, None, 384)
286 batch_normalization_91 (None, None, None, 384)
287 activation_87 (None, None, None, 384)
288 activation_91 (None, None, None, 384)
289 conv2d_101 (None, None, None, 384)
290 conv2d_102 (None, None, None, 384)
291 conv2d_105 (None, None, None, 384)
292 conv2d_106 (None, None, None, 384)
293 average_pooling2d_9 (None, None, None, 2048)
294 conv2d_99 (None, None, None, 320)
295 batch_normalization_88 (None, None, None, 384)
296 batch_normalization_89 (None, None, None, 384)
297 batch_normalization_92 (None, None, None, 384)
298 batch_normalization_93 (None, None, None, 384)
299 conv2d_107 (None, None, None, 192)
300 batch_normalization_86 (None, None, None, 320)
301 activation_88 (None, None, None, 384)
302 activation_89 (None, None, None, 384)
303 activation_92 (None, None, None, 384)
304 activation_93 (None, None, None, 384)
305 batch_normalization_94 (None, None, None, 192)
306 activation_86 (None, None, None, 320)
307 mixed9_1 (None, None, None, 768)
308 concatenate_2 (None, None, None, 768)
309 activation_94 (None, None, None, 192)
310 mixed10 (None, None, None, 2048)

(2) Replace the top with Global Pooling, Dense & Softmax

In [ ]:
x = base_model.output
x = GlobalAveragePooling2D()(x)        # Global Pooling
x = Dense(1024, activation='relu')(x)  # Fully Connected, Dense(1024, Relu) 
predictions = Dense(200, activation='softmax')(x)  # Fully Connected, Dense(200, Softmax)
model = Model(input=base_model.input, output=predictions)
model.summary()

(3) Freeze the Convolution Layers; Train the Top

In [ ]:
# that is, freeze all convolutional InceptionV3 layers
for layer in base_model.layers: 
    layer.trainable = False

# compile the model (should be done *after* setting layers to non-trainable)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')

# train the model on the new data for a few epochs 
model.fit_generator(...)

(4) Free the Top; Fine tune the Convolution Layers

In [ ]:
# we chose to train the top 2 inception blocks, that is, we will freeze
# the first 172 layers and unfreeze the rest:
for layer in model.layers[:172]: 
    layer.trainable = False

for layer in model.layers[172:]: 
    layer.trainable = True
    
# we use SGD with a low learning rate
from keras.optimizers
import SGD
model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy')

# we train our model again (this time fine-tuning the top 2 inception blocks)
# alongside the top Dense layers
model.fit_generator(...)
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]: