Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm working on GradCam from my previous version of the code that works. However, after i updated tensorflow to version 2, the code is no longer work. My previous code is the following

def find_target_layer(model):
    """
    This function finds the final convolutional layer in the network by 
    looping over the layers of the network in reverse order

    Raises
    ------
    ValueError
        Report error if 4D layer is not found.

    Returns
    -------
    TYPE
        DESCRIPTION.

    """
    for layer in reversed(model.layers):
        if len(layer.output_shape) == 4:
            return layer.name
    
    raise ValueError("Could not find 4D layer.  Cannot apply GradCAM")

def grad_cam(input_model, image, class_idx, layer_name, H=224, W=224):
    """
    This function apply GradCAM method for visualizing input saliency.

    Parameters
    ----------
    input_model : Keras Model Object
        Trained Model
    image : 4D Numpy Array (1, H, W, 3)
        Preprocessed input for a given model
    class_idx : Integer
        Class index
    layer_name : String
        Layer name of keras model to apply GradCAM
    H : Integer, optional
        Image height. The default is 224.
    W : Integer, optional
        Image width. The default is 224.

    Returns
    -------
    cam : 2D Numpy Array (H, W)
        Heatmap.


    """
    
    target_layer = find_target_layer(input_model)

    y_c = input_model.output[0, class_idx]
    conv_output = input_model.get_layer(target_layer).output
    grads = K.gradients(y_c, conv_output)[0]

    gradient_function = K.function([input_model.input], [conv_output, grads])

    output, grads_val = gradient_function([image])
    output, grads_val = output[0, :], grads_val[0, :, :, :]

    weights = np.mean(grads_val, axis=(0, 1))
    cam = np.dot(output, weights)

    # Process CAM
    cam = cv2.resize(cam, (W, H), cv2.INTER_LINEAR)
    cam = np.maximum(cam, 0)
    cam = cam / cam.max()
    return cam

I debugged line by line using densenet121 but it failed at the line grads = K.gradients(y_c, conv_output)[0]. The error message is

    TypeError: Cannot convert a symbolic Keras input/output to a numpy array. 
This error may indicate that you're trying to pass a symbolic value to a NumPy call, 
which is not supported. Or, you may be trying to pass Keras symbolic inputs/outputs 
to a TF API that does not register dispatching, preventing Keras from automatically 
converting the API call to a lambda layer in the Functional Model.

May I have your suggestions how to fix this? I'm aware there are several tutorials work on GradCam, however, I still would like to learn more how to fix this code to gain more understanding.

question from:https://stackoverflow.com/questions/65921392/gradcam-for-keras-tensorflow-2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
993 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...