Tensorflow keras中的梯度凸轮,值错误:图形断开连接:无法获取张量的值;输入“U 11”和“U 6:0”;,形状=(无、150、150、3)
如何在预训练的自定义模型上执行梯度CAM。 如何选择Tensorflow keras中的梯度凸轮,值错误:图形断开连接:无法获取张量的值;输入“U 11”和“U 6:0”;,形状=(无、150、150、3),tensorflow,keras,computer-vision,heatmap,image-recognition,Tensorflow,Keras,Computer Vision,Heatmap,Image Recognition,如何在预训练的自定义模型上执行梯度CAM。 如何选择last\u conv\u layer\u name和classifier\u layer\u name? 它的意义是什么?如何选择图层名称? 我是否应该考虑密集层121子层或密集层作为一个功能层? 如何为这个经过培训的网络执行梯度CAM? 这些是我尝试过的步骤 #load model and custom metrics dependencies = {'recall_m': recall_m, 'precision_m' : precisi
last\u conv\u layer\u name
和classifier\u layer\u name
?
它的意义是什么?如何选择图层名称?
我是否应该考虑密集层121子层或密集层作为一个功能层?
如何为这个经过培训的网络执行梯度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()
这是热图功能:-
###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, model, last_conv_layer_name, classifier_layer_names
) ####===> (I'm getting error here, in this line)
那么last\u conv\u layer\u name
和classifier\u layer\u name
有什么问题呢。
有人能解释一下吗
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, model, last_conv_layer_name, classifier_layer_names
) ####===> (I'm getting error here, in this line)