1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] AttributeError: "The layer sequential_2 has never been called" when trying to...

Discussão em 'Python' iniciado por Stack, Outubro 25, 2024 às 11:52.

  1. Stack

    Stack Membro Participativo

    I'm working on generating a Grad-CAM heatmap from a trained ResNet50 model, but I've been encountering a persistent error related to sequential_2. Despite feeding the model with input images, I keep getting an AttributeError saying that sequential_2 has never been called. I'm hoping to get help resolving this issue.

    I have trained a ResNet50 model for skin lesion classification using Keras, and saved it as ResNet50_skin_lesion_final_v2.keras. The model structure includes a resnet50 base, followed by global average pooling, a dense layer, dropout, and a final output dense layer with 8 classes. I want to generate a Grad-CAM heatmap using the conv5_block3_out layer of ResNet50 to visualize where the model is focusing on an input image.

    When I try to create the Grad-CAM heatmap, I get the following error: AttributeError: The layer sequential_2 has never been called and thus has no defined input.

    First, I confirmed that the model layer exists resnet50 <KerasTensor shape=(None, 7, 7, 2048), dtype=float32, sparse=False, name=keras_tensor_21197> global_average_pooling2d_3 <KerasTensor shape=(None, 2048), dtype=float32, sparse=False, name=keras_tensor_21200> dense_5 <KerasTensor shape=(None, 1024), dtype=float32, sparse=False, name=keras_tensor_21203> dropout_2 <KerasTensor shape=(None, 1024), dtype=float32, sparse=False, name=keras_tensor_21207> dense_6 <KerasTensor shape=(None, 8), dtype=float32, sparse=False, name=keras_tensor_21212>

    The code for generating the Grad-CAM heatmap is here import numpy as np import tensorflow as tf import matplotlib.pyplot as plt from tensorflow.keras.preprocessing import image from tensorflow.keras.models import load_model

    # Load trained model (ResNet50_skin_lesion_final_v2)
    model = load_model('ResNet50_skin_lesion_final_v2.keras')

    # Function to preprocess the input image
    def preprocess_input_image(img_path, target_size=(224, 224)):
    img = image.load_img(img_path, target_size=target_size)
    img_array = image.img_to_array(img)
    img_array = np.expand_dims(img_array, axis=0)
    img_array /= 255.0 # Rescale the image like the training data
    return img_array

    # Function to generate Grad-CAM heatmap
    def generate_gradcam_heatmap(model, img_array, last_conv_layer_name, pred_index=None):
    # Forward pass to ensure the model input is called
    predictions = model.predict(img_array)
    if pred_index is None:
    pred_index = tf.argmax(predictions[0])

    # Get the ResNet50 base model
    base_model = model.get_layer('resnet50')

    # Build a model that outputs the activations of the last conv layer and the model output
    grad_model = tf.keras.models.Model(
    inputs=model.input,
    outputs=[base_model.get_layer(last_conv_layer_name).output, model.output]
    )

    with tf.GradientTape() as tape:
    # Watch the gradients for the convolutional layer
    conv_outputs, predictions = grad_model(img_array)
    class_output = predictions[:, pred_index]

    # Compute the gradient of the class output with respect to the feature map
    grads = tape.gradient(class_output, conv_outputs)

    # Take the mean of the gradients across the channels
    pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2))

    # Multiply the output feature map by the pooled gradients
    conv_outputs = conv_outputs[0]
    conv_outputs *= pooled_grads

    # Generate the heatmap
    heatmap = tf.reduce_mean(conv_outputs, axis=-1)
    heatmap = np.maximum(heatmap, 0) / np.max(heatmap) # Normalize between 0 and 1
    return heatmap.numpy()

    # Function to display the Grad-CAM heatmap on top of the original image
    def display_gradcam(img_path, heatmap, alpha=0.4):
    # Load the original image
    img = image.load_img(img_path)
    img = image.img_to_array(img)

    # Resize the heatmap to match the original image size
    heatmap = np.uint8(255 * heatmap)
    heatmap = np.expand_dims(heatmap, axis=-1)
    heatmap = tf.image.resize(heatmap, (img.shape[0], img.shape[1])).numpy()

    # Create an RGB heatmap
    heatmap = np.uint8(plt.cm.jet(heatmap[..., 0]) * 255)

    # Superimpose the heatmap on the original image
    superimposed_img = heatmap * alpha + img

    # Display the image
    plt.figure(figsize=(8, 8))
    plt.imshow(superimposed_img.astype('uint8'))
    plt.axis('off')
    plt.show()

    # Example usage
    img_path = '/mnt/c/Users/arjay/Downloads/Ubuntu/ISIC_2019_Skin_Lesion_Data/ISIC_2019_Training_Input/m anual_test/ISIC_0027665.jpg'
    img_array = preprocess_input_image(img_path)

    # Generate the Grad-CAM heatmap from the last conv layer of ResNet50
    heatmap = generate_gradcam_heatmap(model, img_array, last_conv_layer_name='conv5_block3_out')

    # Display the Grad-CAM heatmap on the original image
    display_gradcam(img_path, heatmap)


    The error would be like this AttributeError Traceback (most recent call last) Cell In[128], line 82 79 img_array = preprocess_input_image(img_path) 81 # Generate the Grad-CAM heatmap from the last conv layer of ResNet50 ---> 82 heatmap = generate_gradcam_heatmap(model, img_array, last_conv_layer_name='conv5_block3_out') 84 # Display the Grad-CAM heatmap on the original image 85 display_gradcam(img_path, heatmap)

    Cell In[128], line 30 26 base_model = model.get_layer('resnet50') 28 # Build a model that outputs the activations of the last conv layer and the model output 29 grad_model = tf.keras.models.Model( ---> 30 inputs=model.input, 31 outputs=[base_model.get_layer(last_conv_layer_name).output, model.output] 32 ) 34 with tf.GradientTape() as tape: 35 # Watch the gradients for the convolutional layer 36 conv_outputs, predictions = grad_model(img_array)

    File ~/projects/tf217/tf217/lib/python3.12/site-packages/keras/src/ops/operation.py:254, in Operation.input(self) 244 @property 245 def input(self): 246 """Retrieves the input tensor(s) of a symbolic operation. 247 ... 292 f"{node_index}, but the operation has only " 293 f"{len(self._inbound_nodes)} inbound nodes." 294 )

    AttributeError: The layer sequential_2 has never been called and thus has no defined input.

    My questions are

    1. Why does the error mention sequential_2 even though I'm loading and referring to the trained ResNet50 model?
    2. How can I properly feed the model with input data so that I can generate the Grad-CAM heatmap?

    Any help or suggestions would be greatly appreciated!

    Continue reading...

Compartilhe esta Página