CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
amanchadha

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