我正在尝试对Xception
模型(具有经过修改的顶层)使用热图:
import tensorflow as to
import cv2
import numpy as np
label = 1
IMAGE_PATH = '/home/piyush/Desktop/HV/hv_2/deep-viz-keras/images/doberman.png'
img = tf.keras.preprocessing.image.load_img(IMAGE_PATH, target_size=(299, 299))
img = tf.keras.preprocessing.image.img_to_array(img)
model_path = '/home/piyush/Desktop/temp/model.h5'
model = tf.keras.models.load_model(model_path)
inputs = tf.keras.Input(shape=(299,299,3))
#print(model.summary())
#print([layer.shape for layer in model.get_layer('xception').layers])
final = tf.keras.Model(inputs=model.inputs, outputs=model.output)
print(model.inputs)
#extractor = tf.keras.Model(inputs=inputs, outputs=[layer.output for layer in model.layers])
conv_layer = tf.keras.Model(inputs=inputs,outputs=model.get_layer('xception').get_layer('block14_sepconv2_act').output)
如果运行此命令,则会出现以下错误:
conv_layer = tf.keras.Model(inputs=inputs, outputs=model.get_layer('xception').get_layer('block14_sepconv2_act').output)
ValueError: Graph disconnected: cannot obtain value for tensor Tensor("input_1:0", shape=(None, 299, 299, 3), dtype=float32) at layer "input_1". The following previous layers were accessed without issue: []
答案 0 :(得分:0)
您定义
inputs = tf.keras.Input(shape=(299,299,3))
,但是永远不要在模型中使用此变量。您可能只是想要
inputs = model.inputs
因为要加载现有模型,而不是创建新模型。
答案 1 :(得分:0)
已解决:
conv_layer = tf.keras.Model(inputs= model.get_layer('xception').inputs,outputs=model.get_layer('xception').get_layer('block14_sepconv2_act').output)
模型断开的原因,因此输入和输出应为同一模型。