喀拉拉邦的Grad-CAM,ValueError:图形已断开连接:无法获取张量张量“ input_11_6:0”的值,shape =None,150,150,3

问题描述

如何在预训练的自定义模型上执行Grad-CAM。 如何选择last_conv_layer_nameclassifier_layer_names? 它的意义是什么?如何选择图层名称? 我是否应该将Densenet121子层或密网视为一个功能层? 如何为这个训练有素的网络执行Grad-CAM? 这些是我尝试过的步骤,

#load model and custom metrics
dependencies = {'recall_m': recall_m,'precision_m' : precision_m,'f1_m' : f1_m }
model = keras.models.load_model("model_val_acc-73.33.h5",custom_objects = dependencies)
model.summary()
Model: "sequential_9"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
densenet121 (Functional)     (None,4,1024)        7037504   
_________________________________________________________________
flatten (Flatten)            (None,16384)             0         
_________________________________________________________________
dense_encoder (Dense)        (None,1024)              16778240  
_________________________________________________________________
dropout_51 (Dropout)         (None,1024)              0         
_________________________________________________________________
dense_2 (Dense)              (None,256)               262400    
_________________________________________________________________
dropout_52 (Dropout)         (None,256)               0         
_________________________________________________________________
dense_3 (Dense)              (None,128)               32896     
_________________________________________________________________
dropout_53 (Dropout)         (None,128)               0         
_________________________________________________________________
dense_4 (Dense)              (None,64)                8256      
_________________________________________________________________
dropout_54 (Dropout)         (None,64)                0         
_________________________________________________________________
dense_5 (Dense)              (None,32)                2080      
_________________________________________________________________
dropout_55 (Dropout)         (None,32)                0         
_________________________________________________________________
Final (Dense)                (None,2)                 66        
=================================================================
Total params: 24,121,442
Trainable params: 17,083,938
Non-trainable params: 7,037,504

这是热图功能:-

###defining heat map

def make_gradcam_heatmap(img_array,model,last_conv_layer_name,classifier_layer_names):
    # First,we create a model that maps the input image to the activations
    # of the last conv layer
    last_conv_layer = model.get_layer(last_conv_layer_name)
    last_conv_layer_model = keras.Model(model.inputs,last_conv_layer.output)

    # Second,we create a model that maps the activations of the last conv
    # layer to the final class predictions
    classifier_input = keras.Input(shape=last_conv_layer.output.shape[1:])
    x = classifier_input
    for layer_name in classifier_layer_names:
        x = model.get_layer(layer_name)(x)
    classifier_model = keras.Model(classifier_input,x)

    # Then,we compute the gradient of the top predicted class for our input image
    # with respect to the activations of the last conv layer
    with tf.GradientTape() as tape:
        # Compute activations of the last conv layer and make the tape watch it
        last_conv_layer_output = last_conv_layer_model(img_array)
        tape.watch(last_conv_layer_output)
        # Compute class predictions
        preds = classifier_model(last_conv_layer_output)
        top_pred_index = tf.argmax(preds[0])
        top_class_channel = preds[:,top_pred_index]

    # This is the gradient of the top predicted class with regard to
    # the output feature map of the last conv layer
    grads = tape.gradient(top_class_channel,last_conv_layer_output)

    # This is a vector where each entry is the mean intensity of the gradient
    # over a specific feature map channel
    pooled_grads = tf.reduce_mean(grads,axis=(0,1,2))

    # We multiply each channel in the feature map array
    # by "how important this channel is" with regard to the top predicted class
    last_conv_layer_output = last_conv_layer_output.numpy()[0]
    pooled_grads = pooled_grads.numpy()
    for i in range(pooled_grads.shape[-1]):
        last_conv_layer_output[:,:,i] *= pooled_grads[i]

    # The channel-wise mean of the resulting feature map
    # is our heatmap of class activation
    heatmap = np.mean(last_conv_layer_output,axis=-1)

    # For visualization purpose,we will also normalize the heatmap between 0 & 1
    heatmap = np.maximum(heatmap,0) / np.max(heatmap)
    return heatmap

这是图像输入

img_array = X_test[10]    # 10th image sample
X_test[10].shape
#(150,150,3)

last_conv_layer_name = "densenet121"
classifier_layer_names = [ "dense_2","dense_3","dense_4","dense_5","Final" ]

# Generate class activation heatmap
heatmap = make_gradcam_heatmap(
    img_array,classifier_layer_names
)  ####===> (I'm getting error here,in this line)

那么last_conv_layer_nameclassifier_layer_names有什么问题。 谁能解释一下?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...