# UNQ_C1# GRADED FUNCTION: identity_blockdefidentity_block(X, f, filters, training=True, initializer=random_uniform):"""
Implementation of the identity block as defined in Figure 4
Arguments:
X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)
f -- integer, specifying the shape of the middle CONV's window for the main path
filters -- python list of integers, defining the number of filters in the CONV layers of the main path
training -- True: Behave in training mode
False: Behave in inference mode
initializer -- to set up the initial weights of a layer. Equals to random uniform initializer
Returns:
X -- output of the identity block, tensor of shape (m, n_H, n_W, n_C)
"""# Retrieve Filters
F1, F2, F3 = filters
# Save the input value. You'll need this later to add back to the main path.
X_shortcut = X
# First component of main path
X = Conv2D(filters = F1, kernel_size =1, strides =(1,1), padding ='valid', kernel_initializer = initializer(seed=0))(X)
X = BatchNormalization(axis =3)(X, training = training)# Default axis
X = Activation('relu')(X)### START CODE HERE## Second component of main path (≈3 lines)## Set the padding = 'same'
X = Conv2D(filters = F2, kernel_size =(f, f), strides =(1,1), padding ='same', kernel_initializer = initializer(seed=0))(X)
X = BatchNormalization(axis =3)(X, training = training)
X = Activation('relu')(X)## Third component of main path (≈2 lines)## Set the padding = 'valid'
X = Conv2D(filters = F3, kernel_size =(1,1), strides =(1,1), padding ='valid', kernel_initializer = initializer(seed=0))(X)
X = BatchNormalization(axis =3)(X, training = training)## Final step: Add shortcut value to main path, and pass it through a RELU activation (≈2 lines)
X = Add()([X, X_shortcut])
X = Activation('relu')(X)### END CODE HEREreturn X
# UNQ_C2# GRADED FUNCTION: convolutional_blockdefconvolutional_block(X, f, filters, s =2, training=True, initializer=glorot_uniform):"""
Implementation of the convolutional block as defined in Figure 4
Arguments:
X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)
f -- integer, specifying the shape of the middle CONV's window for the main path
filters -- python list of integers, defining the number of filters in the CONV layers of the main path
s -- Integer, specifying the stride to be used
training -- True: Behave in training mode
False: Behave in inference mode
initializer -- to set up the initial weights of a layer. Equals to Glorot uniform initializer,
also called Xavier uniform initializer.
Returns:
X -- output of the convolutional block, tensor of shape (m, n_H, n_W, n_C)
"""# Retrieve Filters
F1, F2, F3 = filters
# Save the input value
X_shortcut = X
##### MAIN PATH ###### First component of main path glorot_uniform(seed=0)
X = Conv2D(filters = F1, kernel_size =1, strides =(s, s), padding='valid', kernel_initializer = initializer(seed=0))(X)
X = BatchNormalization(axis =3)(X, training=training)
X = Activation('relu')(X)### START CODE HERE## Second component of main path (≈3 lines)
X = Conv2D(F2,(f, f), strides =(1,1), padding='same', kernel_initializer = initializer(seed=0))(X)
X = BatchNormalization(axis =3)(X, training=training)
X = Activation('relu')(X)## Third component of main path (≈2 lines)
X = Conv2D(F3,(1,1), strides =(1,1), padding='valid', kernel_initializer = initializer(seed=0))(X)
X = BatchNormalization(axis =3)(X, training=training)##### SHORTCUT PATH ##### (≈2 lines)
X_shortcut = Conv2D(F3,(1,1), strides =(s,s), padding='valid', kernel_initializer = initializer(seed=0))(X_shortcut)
X_shortcut = BatchNormalization(axis =3)(X_shortcut, training=training)### END CODE HERE# Final step: Add shortcut value to main path (Use this order [X, X_shortcut]), and pass it through a RELU activation
X = Add()([X, X_shortcut])
X = Activation('relu')(X)return X
# UNQ_C3# GRADED FUNCTION: ResNet50defResNet50(input_shape =(64,64,3), classes =6):"""
Stage-wise implementation of the architecture of the popular ResNet50:
CONV2D -> BATCHNORM -> RELU -> MAXPOOL -> CONVBLOCK -> IDBLOCK*2 -> CONVBLOCK -> IDBLOCK*3
-> CONVBLOCK -> IDBLOCK*5 -> CONVBLOCK -> IDBLOCK*2 -> AVGPOOL -> FLATTEN -> DENSE
Arguments:
input_shape -- shape of the images of the dataset
classes -- integer, number of classes
Returns:
model -- a Model() instance in Keras
"""# Define the input as a tensor with shape input_shape
X_input = Input(input_shape)# Zero-Padding
X = ZeroPadding2D((3,3))(X_input)# Stage 1
X = Conv2D(64,(7,7), strides =(2,2), kernel_initializer = glorot_uniform(seed=0))(X)
X = BatchNormalization(axis =3)(X)
X = Activation('relu')(X)
X = MaxPooling2D((3,3), strides=(2,2))(X)# Stage 2
X = convolutional_block(X, f =3, filters =[64,64,256], s =1)
X = identity_block(X,3,[64,64,256])
X = identity_block(X,3,[64,64,256])### START CODE HERE## Stage 3 (≈4 lines)
X = convolutional_block(X, f=3, filters=[128,128,512], s=2)
X = identity_block(X,3,[128,128,512])
X = identity_block(X,3,[128,128,512])
X = identity_block(X,3,[128,128,512])## Stage 4 (≈6 lines)
X = convolutional_block(X, f=3, filters=[256,256,1024],s=2)
X = identity_block(X,3,[256,256,1024])
X = identity_block(X,3,[256,256,1024])
X = identity_block(X,3,[256,256,1024])
X = identity_block(X,3,[256,256,1024])
X = identity_block(X,3,[256,256,1024])## Stage 5 (≈3 lines)
X = convolutional_block(X, f=3, filters=[512,512,2048],s=2)
X = identity_block(X,3,[512,512,2048])
X = identity_block(X,3,[512,512,2048])## AVGPOOL (≈1 line). Use "X = AveragePooling2D(...)(X)"
X = AveragePooling2D(pool_size=(2,2), padding='same')(X)### END CODE HERE# output layer
X = Flatten()(X)
X = Dense(classes, activation='softmax', kernel_initializer = glorot_uniform(seed=0))(X)# Create model
model = Model(inputs = X_input, outputs = X)return model