DeepLearning.AI TensorFlow Developer: Convolutional Neural Networks in TensorFlow:C2_W1_Lab1

CNN:C2_W1_LAB 1

I have a problem to visualize the images when run into my local machine it says this error:
The layer sequential_5 has never been called and thus has no defined input.
the initial code is below:
import numpy as np
import random
from tensorflow.keras.utils import img_to_array, load_img

Define a new Model that will take an image as input, and will output

intermediate representations for all layers in the previous model

successive_outputs = [layer.output for layer in model.layers]
visualization_model = tf.keras.models.Model(inputs = model.input, outputs = successive_outputs)

Prepare a random input image from the training set.

cat_img_files = [os.path.join(train_cats_dir, f) for f in train_cat_fnames]
dog_img_files = [os.path.join(train_dogs_dir, f) for f in train_dog_fnames]
img_path = random.choice(cat_img_files + dog_img_files)
img = load_img(img_path, target_size=(150, 150)) # this is a PIL image
x = img_to_array(img) # Numpy array with shape (150, 150, 3)
x = x.reshape((1,) + x.shape) # Numpy array with shape (1, 150, 150, 3)

Scale by 1/255

x /= 255.0

Run the image through the network, thus obtaining all

intermediate representations for this image.

successive_feature_maps = visualization_model.predict(x)

These are the names of the layers, so you can have them as part of our plot

layer_names = [layer.name for layer in model.layers]

Display the representations

for layer_name, feature_map in zip(layer_names, successive_feature_maps):

if len(feature_map.shape) == 4:

#-------------------------------------------
# Just do this for the conv / maxpool layers, not the fully-connected layers
#-------------------------------------------
n_features = feature_map.shape[-1]  # number of features in the feature map
size       = feature_map.shape[ 1]  # feature map shape (1, size, size, n_features)

# Tile the images in this matrix
display_grid = np.zeros((size, size * n_features))

#-------------------------------------------------
# Postprocess the feature to be visually palatable
#-------------------------------------------------
for i in range(n_features):
  x  = feature_map[0, :, :, i]
  x -= x.mean()
  x /= x.std ()
  x *=  64
  x += 128
  x  = np.clip(x, 0, 255).astype('uint8')
  display_grid[:, i * size : (i + 1) * size] = x # Tile each filter into a horizontal grid

#-----------------
# Display the grid
#-----------------
scale = 20. / n_features
plt.figure( figsize=(scale * n_features, scale) )
plt.title ( layer_name )
plt.grid  ( False )
plt.imshow( display_grid, aspect='auto', cmap='viridis' ) 

I have no problem when run into google colab so I am curious I search into and re-run the model few times but still I cant visualize
please guide

Hi,

It’s difficult to say because I don’t know the local environment you have. Windows? Mac?

But check the versions of the libraries, you can use the command:
pip show library and check that tensorflow, keras and the rest of libraries are the same version in your local environment and in colab.

LINUX yeah i think maybe because i run in local machine instead of google collab certain tensorflow need to be update for my python environment
below is my current tensorflow

Name: tensorflow
Version: 2.16.1
Summary: TensorFlow is an open source machine learning framework for everyone.
Home-page: https://www.tensorflow.org/
Author: Google Inc.
Author-email: packages@tensorflow.org
License: Apache 2.0
Location: /home/ffs223/anaconda3/lib/python3.11/site-packages
Requires: absl-py, astunparse, flatbuffers, gast, google-pasta, grpcio, h5py, keras, libclang, ml-dtypes, numpy, opt-einsum, packaging, protobuf, requests, setuptools, six, tensorboard, tensorflow-io-gcs-filesystem, termcolor, typing-extensions, wrapt
Required-by:

@fatihah

Can you share the screenshot of the error.

Also you confirmed that you didn’t encounter this issue to visualise graph while running the codes on Google Colab but on local machine? So was the Google Colab environment course provided environment or your local Google Colab.

Regards
DP


yes miss its just on my local machine on google collab all are good
so I just run everything on google collab just to grab concept I think need to update my tensorflow or something

notice the error, it is pointing towards input not being defined as one run the assignment code in the environment provided the necessary metadata provides the def input detail but because you running the codes locally it is throwing you this error as you mentioned input=model.input.

So make sure when you are running it locally you have all the necessary files uploaded in the local environment.

alright noted thank you