Need help to understanding C1_W4_Lab_1_image_generator_no_validation

Hi, I have 2 questions relating to C1W4Lab1’s “Visualizing Intermediate Representations” part, please help:

  1. To display image, why do we need these lines:
      x -= x.mean()
      x /= x.std()
      x *= 64
      x += 128

Specifically *= 64 and += 128 part. If I replace it with x *= 255 it seems to display “darker” version of the output image, but what is the rationale for *= 64 then +=128?

  1. Regarding the code for creating visualization_model:
successive_outputs = [layer.output for layer in model.layers[1:]]
visualization_model = tf.keras.models.Model(inputs = model.input, outputs = successive_outputs)

# ... irrelevant lines here

# Run the image through the network, thus obtaining all
# intermediate representations for this image.
successive_feature_maps = visualization_model.predict(x)

Why we only need to provide outputs to model to make it able to predict something? Does those output layers already contain all the calculation of Conv2D and MaxPooling inside them?

Many thanks in advance.

Hello @phuna
Welcome to our Community! Thanks for reaching out. We are here to help you.

First, it’s essential to understand why we implemented this visualization step, and the main reason is to see how an input gets transformed through the convnet neural network.
It’s an excellent way to see what type of filter is applying the convolutional neural network; remember that the convolutional layer uses filter that gets the most critical features of the images.
If you want to make an inference, you don’t have to do all these steps.

Relate to the first question, an image is a matrix of pixels, and each of the elements of this matrix has a value from 0 to 255; if you print the x (image) before multiplying for the scalar, you are going to see that you are going to have values closes to 0, you multiply each of the elements of the matrix for 64 and the sum 128 to get a better visualization of the image, now the question is why 64 and why 128, its was finding trying to show to the students better results of the image before the filter, you can change this value. If you increase it, you will see a more bright image.
0 black | 255 white

Relate to the second question; you apply this loop to get each layer’s result individually and see the results after the filter.

successive_outputs = [layer. Output for layer in the model. layers[1:]]

If you want to make the prediction, you should only use the predict function with the whole model.

# Generate predictions for samples
predictions = model.predict(samples_to_predict)

I hope this can help.
Happy New year :hugs:

I understand it now. Thanks a lot for your answer.