Convolutional Neural Networks - Exercise 3 - ResNet50

Pretty lost, I read up on averagepooling2D (link below) but it didn’t help me at all. I am thinking this is my error because everything else is explained out on what to code.

I reviewed - tf.keras.layers.AveragePooling2D  |  TensorFlow Core v2.7.0

I really don’t even know if that is where my error is. Something about Input is not defined?? TIA for the help

My Code:

UNQ_C3

GRADED FUNCTION: ResNet50

def ResNet50(input_shape = (64, 64, 3), classes = 6):
“”"
Stage-wise implementation of the architecture of the popular ResNet50:
CONV2D → BATCHNORM → RELU → MAXPOOL → CONVBLOCK → IDBLOCK2 → CONVBLOCK → IDBLOCK3
→ CONVBLOCK → IDBLOCK5 → CONVBLOCK → IDBLOCK2 → 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))(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

The error

Run the following code to build the model’s graph. If your implementation is incorrect, you’ll know it by checking your accuracy when running model.fit(…) below.

model = ResNet50(input_shape = (64, 64, 3), classes = 6)

print(model.summary())


NameError Traceback (most recent call last)
in
----> 1 model = ResNet50(input_shape = (64, 64, 3), classes = 6)
2 print(model.summary())

in ResNet50(input_shape, classes)
17
18 # Define the input as a tensor with shape input_shape
—> 19 X_input = Input(input_shape)
20
21

NameError: name ‘Input’ is not defined

Try removing the “pool_size” argument.

Why would you conclude from that error message that the problem is with AveragePooling? The message is talking about “Input” which is the first primitive referenced within that function. The message tells you that Input is undefined. So how could that happen? Search the notebook to figure out where Input is defined and you’ll find that it was “imported” by the import cell, which is the first executable cell in the notebook. So what this probably means is simply that you have closed and reopened the notebook, but have not run the earlier cells. Or you did “Kernel → Restart” and then didn’t rerun the previous cells. Try “Cell → Run All Above” and then run the resnet50 cell again.

Anytime you reopen the notebook, all the “runtime state” is lost and you must recreate it by running the cells again. One other thing to note along this same line is that just typing some new code into a function cell and then calling the function again does nothing: it reruns the old code. You need to actually execute the modified cell by clicking “Shift-Enter” in order for the new code you just typed to get actually loaded into the runtime state of the notebook.

Wait, I thought you were taking Course 1 of DLS, not Course 4 (ConvNets). You were just asking a question about the softmax exercise in the very first assignment of Course 1. How did you get all the way from the very first introduction to numpy in Week 2 of Course 1 to Week 2 of Course 4 in one day?

The problem was reopening the notebook. Cell> run all above fixed everything.