CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
y33-j3T

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: y33-j3T/Coursera-Deep-Learning
Path: blob/master/Convolutional Neural Networks/week4/Face Recognition/inception_blocks_v2.py
Views: 13377
1
import tensorflow as tf
2
import numpy as np
3
import os
4
from numpy import genfromtxt
5
from keras import backend as K
6
from keras.layers import Conv2D, ZeroPadding2D, Activation, Input, concatenate
7
from keras.models import Model
8
from keras.layers.normalization import BatchNormalization
9
from keras.layers.pooling import MaxPooling2D, AveragePooling2D
10
import fr_utils
11
from keras.layers.core import Lambda, Flatten, Dense
12
13
def inception_block_1a(X):
14
"""
15
Implementation of an inception block
16
"""
17
18
X_3x3 = Conv2D(96, (1, 1), data_format='channels_first', name ='inception_3a_3x3_conv1')(X)
19
X_3x3 = BatchNormalization(axis=1, epsilon=0.00001, name = 'inception_3a_3x3_bn1')(X_3x3)
20
X_3x3 = Activation('relu')(X_3x3)
21
X_3x3 = ZeroPadding2D(padding=(1, 1), data_format='channels_first')(X_3x3)
22
X_3x3 = Conv2D(128, (3, 3), data_format='channels_first', name='inception_3a_3x3_conv2')(X_3x3)
23
X_3x3 = BatchNormalization(axis=1, epsilon=0.00001, name='inception_3a_3x3_bn2')(X_3x3)
24
X_3x3 = Activation('relu')(X_3x3)
25
26
X_5x5 = Conv2D(16, (1, 1), data_format='channels_first', name='inception_3a_5x5_conv1')(X)
27
X_5x5 = BatchNormalization(axis=1, epsilon=0.00001, name='inception_3a_5x5_bn1')(X_5x5)
28
X_5x5 = Activation('relu')(X_5x5)
29
X_5x5 = ZeroPadding2D(padding=(2, 2), data_format='channels_first')(X_5x5)
30
X_5x5 = Conv2D(32, (5, 5), data_format='channels_first', name='inception_3a_5x5_conv2')(X_5x5)
31
X_5x5 = BatchNormalization(axis=1, epsilon=0.00001, name='inception_3a_5x5_bn2')(X_5x5)
32
X_5x5 = Activation('relu')(X_5x5)
33
34
X_pool = MaxPooling2D(pool_size=3, strides=2, data_format='channels_first')(X)
35
X_pool = Conv2D(32, (1, 1), data_format='channels_first', name='inception_3a_pool_conv')(X_pool)
36
X_pool = BatchNormalization(axis=1, epsilon=0.00001, name='inception_3a_pool_bn')(X_pool)
37
X_pool = Activation('relu')(X_pool)
38
X_pool = ZeroPadding2D(padding=((3, 4), (3, 4)), data_format='channels_first')(X_pool)
39
40
X_1x1 = Conv2D(64, (1, 1), data_format='channels_first', name='inception_3a_1x1_conv')(X)
41
X_1x1 = BatchNormalization(axis=1, epsilon=0.00001, name='inception_3a_1x1_bn')(X_1x1)
42
X_1x1 = Activation('relu')(X_1x1)
43
44
# CONCAT
45
inception = concatenate([X_3x3, X_5x5, X_pool, X_1x1], axis=1)
46
47
return inception
48
49
def inception_block_1b(X):
50
X_3x3 = Conv2D(96, (1, 1), data_format='channels_first', name='inception_3b_3x3_conv1')(X)
51
X_3x3 = BatchNormalization(axis=1, epsilon=0.00001, name='inception_3b_3x3_bn1')(X_3x3)
52
X_3x3 = Activation('relu')(X_3x3)
53
X_3x3 = ZeroPadding2D(padding=(1, 1), data_format='channels_first')(X_3x3)
54
X_3x3 = Conv2D(128, (3, 3), data_format='channels_first', name='inception_3b_3x3_conv2')(X_3x3)
55
X_3x3 = BatchNormalization(axis=1, epsilon=0.00001, name='inception_3b_3x3_bn2')(X_3x3)
56
X_3x3 = Activation('relu')(X_3x3)
57
58
X_5x5 = Conv2D(32, (1, 1), data_format='channels_first', name='inception_3b_5x5_conv1')(X)
59
X_5x5 = BatchNormalization(axis=1, epsilon=0.00001, name='inception_3b_5x5_bn1')(X_5x5)
60
X_5x5 = Activation('relu')(X_5x5)
61
X_5x5 = ZeroPadding2D(padding=(2, 2), data_format='channels_first')(X_5x5)
62
X_5x5 = Conv2D(64, (5, 5), data_format='channels_first', name='inception_3b_5x5_conv2')(X_5x5)
63
X_5x5 = BatchNormalization(axis=1, epsilon=0.00001, name='inception_3b_5x5_bn2')(X_5x5)
64
X_5x5 = Activation('relu')(X_5x5)
65
66
X_pool = AveragePooling2D(pool_size=(3, 3), strides=(3, 3), data_format='channels_first')(X)
67
X_pool = Conv2D(64, (1, 1), data_format='channels_first', name='inception_3b_pool_conv')(X_pool)
68
X_pool = BatchNormalization(axis=1, epsilon=0.00001, name='inception_3b_pool_bn')(X_pool)
69
X_pool = Activation('relu')(X_pool)
70
X_pool = ZeroPadding2D(padding=(4, 4), data_format='channels_first')(X_pool)
71
72
X_1x1 = Conv2D(64, (1, 1), data_format='channels_first', name='inception_3b_1x1_conv')(X)
73
X_1x1 = BatchNormalization(axis=1, epsilon=0.00001, name='inception_3b_1x1_bn')(X_1x1)
74
X_1x1 = Activation('relu')(X_1x1)
75
76
inception = concatenate([X_3x3, X_5x5, X_pool, X_1x1], axis=1)
77
78
return inception
79
80
def inception_block_1c(X):
81
X_3x3 = fr_utils.conv2d_bn(X,
82
layer='inception_3c_3x3',
83
cv1_out=128,
84
cv1_filter=(1, 1),
85
cv2_out=256,
86
cv2_filter=(3, 3),
87
cv2_strides=(2, 2),
88
padding=(1, 1))
89
90
X_5x5 = fr_utils.conv2d_bn(X,
91
layer='inception_3c_5x5',
92
cv1_out=32,
93
cv1_filter=(1, 1),
94
cv2_out=64,
95
cv2_filter=(5, 5),
96
cv2_strides=(2, 2),
97
padding=(2, 2))
98
99
X_pool = MaxPooling2D(pool_size=3, strides=2, data_format='channels_first')(X)
100
X_pool = ZeroPadding2D(padding=((0, 1), (0, 1)), data_format='channels_first')(X_pool)
101
102
inception = concatenate([X_3x3, X_5x5, X_pool], axis=1)
103
104
return inception
105
106
def inception_block_2a(X):
107
X_3x3 = fr_utils.conv2d_bn(X,
108
layer='inception_4a_3x3',
109
cv1_out=96,
110
cv1_filter=(1, 1),
111
cv2_out=192,
112
cv2_filter=(3, 3),
113
cv2_strides=(1, 1),
114
padding=(1, 1))
115
X_5x5 = fr_utils.conv2d_bn(X,
116
layer='inception_4a_5x5',
117
cv1_out=32,
118
cv1_filter=(1, 1),
119
cv2_out=64,
120
cv2_filter=(5, 5),
121
cv2_strides=(1, 1),
122
padding=(2, 2))
123
124
X_pool = AveragePooling2D(pool_size=(3, 3), strides=(3, 3), data_format='channels_first')(X)
125
X_pool = fr_utils.conv2d_bn(X_pool,
126
layer='inception_4a_pool',
127
cv1_out=128,
128
cv1_filter=(1, 1),
129
padding=(2, 2))
130
X_1x1 = fr_utils.conv2d_bn(X,
131
layer='inception_4a_1x1',
132
cv1_out=256,
133
cv1_filter=(1, 1))
134
inception = concatenate([X_3x3, X_5x5, X_pool, X_1x1], axis=1)
135
136
return inception
137
138
def inception_block_2b(X):
139
#inception4e
140
X_3x3 = fr_utils.conv2d_bn(X,
141
layer='inception_4e_3x3',
142
cv1_out=160,
143
cv1_filter=(1, 1),
144
cv2_out=256,
145
cv2_filter=(3, 3),
146
cv2_strides=(2, 2),
147
padding=(1, 1))
148
X_5x5 = fr_utils.conv2d_bn(X,
149
layer='inception_4e_5x5',
150
cv1_out=64,
151
cv1_filter=(1, 1),
152
cv2_out=128,
153
cv2_filter=(5, 5),
154
cv2_strides=(2, 2),
155
padding=(2, 2))
156
157
X_pool = MaxPooling2D(pool_size=3, strides=2, data_format='channels_first')(X)
158
X_pool = ZeroPadding2D(padding=((0, 1), (0, 1)), data_format='channels_first')(X_pool)
159
160
inception = concatenate([X_3x3, X_5x5, X_pool], axis=1)
161
162
return inception
163
164
def inception_block_3a(X):
165
X_3x3 = fr_utils.conv2d_bn(X,
166
layer='inception_5a_3x3',
167
cv1_out=96,
168
cv1_filter=(1, 1),
169
cv2_out=384,
170
cv2_filter=(3, 3),
171
cv2_strides=(1, 1),
172
padding=(1, 1))
173
X_pool = AveragePooling2D(pool_size=(3, 3), strides=(3, 3), data_format='channels_first')(X)
174
X_pool = fr_utils.conv2d_bn(X_pool,
175
layer='inception_5a_pool',
176
cv1_out=96,
177
cv1_filter=(1, 1),
178
padding=(1, 1))
179
X_1x1 = fr_utils.conv2d_bn(X,
180
layer='inception_5a_1x1',
181
cv1_out=256,
182
cv1_filter=(1, 1))
183
184
inception = concatenate([X_3x3, X_pool, X_1x1], axis=1)
185
186
return inception
187
188
def inception_block_3b(X):
189
X_3x3 = fr_utils.conv2d_bn(X,
190
layer='inception_5b_3x3',
191
cv1_out=96,
192
cv1_filter=(1, 1),
193
cv2_out=384,
194
cv2_filter=(3, 3),
195
cv2_strides=(1, 1),
196
padding=(1, 1))
197
X_pool = MaxPooling2D(pool_size=3, strides=2, data_format='channels_first')(X)
198
X_pool = fr_utils.conv2d_bn(X_pool,
199
layer='inception_5b_pool',
200
cv1_out=96,
201
cv1_filter=(1, 1))
202
X_pool = ZeroPadding2D(padding=(1, 1), data_format='channels_first')(X_pool)
203
204
X_1x1 = fr_utils.conv2d_bn(X,
205
layer='inception_5b_1x1',
206
cv1_out=256,
207
cv1_filter=(1, 1))
208
inception = concatenate([X_3x3, X_pool, X_1x1], axis=1)
209
210
return inception
211
212
def faceRecoModel(input_shape):
213
"""
214
Implementation of the Inception model used for FaceNet
215
216
Arguments:
217
input_shape -- shape of the images of the dataset
218
219
Returns:
220
model -- a Model() instance in Keras
221
"""
222
223
# Define the input as a tensor with shape input_shape
224
X_input = Input(input_shape)
225
226
# Zero-Padding
227
X = ZeroPadding2D((3, 3))(X_input)
228
229
# First Block
230
X = Conv2D(64, (7, 7), strides = (2, 2), name = 'conv1')(X)
231
X = BatchNormalization(axis = 1, name = 'bn1')(X)
232
X = Activation('relu')(X)
233
234
# Zero-Padding + MAXPOOL
235
X = ZeroPadding2D((1, 1))(X)
236
X = MaxPooling2D((3, 3), strides = 2)(X)
237
238
# Second Block
239
X = Conv2D(64, (1, 1), strides = (1, 1), name = 'conv2')(X)
240
X = BatchNormalization(axis = 1, epsilon=0.00001, name = 'bn2')(X)
241
X = Activation('relu')(X)
242
243
# Zero-Padding + MAXPOOL
244
X = ZeroPadding2D((1, 1))(X)
245
246
# Second Block
247
X = Conv2D(192, (3, 3), strides = (1, 1), name = 'conv3')(X)
248
X = BatchNormalization(axis = 1, epsilon=0.00001, name = 'bn3')(X)
249
X = Activation('relu')(X)
250
251
# Zero-Padding + MAXPOOL
252
X = ZeroPadding2D((1, 1))(X)
253
X = MaxPooling2D(pool_size = 3, strides = 2)(X)
254
255
# Inception 1: a/b/c
256
X = inception_block_1a(X)
257
X = inception_block_1b(X)
258
X = inception_block_1c(X)
259
260
# Inception 2: a/b
261
X = inception_block_2a(X)
262
X = inception_block_2b(X)
263
264
# Inception 3: a/b
265
X = inception_block_3a(X)
266
X = inception_block_3b(X)
267
268
# Top layer
269
X = AveragePooling2D(pool_size=(3, 3), strides=(1, 1), data_format='channels_first')(X)
270
X = Flatten()(X)
271
X = Dense(128, name='dense_layer')(X)
272
273
# L2 normalization
274
X = Lambda(lambda x: K.l2_normalize(x,axis=1))(X)
275
276
# Create model instance
277
model = Model(inputs = X_input, outputs = X, name='FaceRecoModel')
278
279
return model
280