C4 W2 A2: Doubt about Transfer Learning exercise

Hi,

I’m having a doubt about the W2 A2. In the following given code:


What’s the difference between the line:

  • feature_batch = base_model(image_batch)

and this other line:

  • pred = base_model(image_var)

?

I’m afraid I’m not following what we are doing on each of those two chunks of code.

Can someone explain what we are doing there?

Thanks a lot.

PS: I already reported that line “base_model = tf.keras.applications.MobileNetV2(…)” is giving error of type ([Errno 99] Cannot assign requested address). It seems to be solved by trying the loading several times. However, the line “tf.keras.applications.mobilenet_v2.decode_predictions(pred.numpy(), top=2)” is also giving that same error.

Hi @Fausto_Blasco

This uses the trainable parameters and basically training the image batches based on the features

where as

for pred, when we set base_model. trainable=False, we only freeze trainable parameters . BatchNormalization will still record its moving average if we don’t set training=False for the BatchNormalization.

So basically pred has is the output of variable of the image batch where the parameters have been used as you know var = tf.variable() defines a variable that is trainable.

Can you share a screenshot of the error you are mentioning here, do not share any codes just the error output.

Regards
DP

The “Cannot assign requested address” error is being tracked via this thread:

hi @Fausto_Blasco

for this error resolution, can you try the below steps.

remove all the files from the file ==> open options, and then get a fresh copy, re-do the assignment, then refer the saved copy to write your codes again. let me know if the error with http works.

To get a fresh copy,

Click file, then select open. You will find all the files related to the assignment. Here if you have saved a copy of your notebook with renaming other than what was mentioned. Then go ahead and delete the assignment notebook by selecting the particular notebook.

Once deleted, you will find 404 not found image on your browser. Then close the browser.

Open the assignment page again, when you open you will find 404 not found.

At this time, click :question: on the right hand top corner Before Grades, where you will find Reboot. Click reboot.

Then click the same :question:on the right hand top corner, then click Get latest version and then Update lab.

Regards
DP

Invoking __call__ outside the scope of model.fit and gradient tape (where weights can be changed by calling optimizer.apply_gradients explicitly) will not update model weights. Here’s some code to prove that:

def check(input_batch, use_vars = False):
    from copy import deepcopy
    print(f"base_model.trainable = {base_model.trainable}")
    befores = deepcopy(base_model.weights)
    if use_vars:
        pred = base_model(tf.Variable(preprocess_input(input_batch)))
    else:
        pred = base_model(preprocess_input(input_batch))
    print("are predictions the same as features?", tf.equal(pred, feature_batch).numpy().all())
    for before, after in zip(befores, base_model.weights):
        assert np.array_equal(before.numpy(), after.numpy())
        assert before is not after

check(image_batch)
check(image_batch, use_vars=True)

Output:

base_model.trainable = True
are predictions the same as features? True
base_model.trainable = True
are predictions the same as features? True

Hi @Deepti_Prasad,

thanks for your answer and the proposed solution for the problem. I want to let you know that your suggested workaround does not work.

I’ve tried it as per your instructions, but still got the same error.

With kind regards,
/Fausto.

A new version of the notebook has been published, and the grader has been updated.

You will need to move your code from the auto-saved file into the new notebook.