import tensorflow.keras as keras
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
if __name__ == '__main__':
model = keras.models.load_model('model/model_test_0.99408.h5', custom_objects={'leaky_relu': tf.nn.leaky_relu})
model.summary()
inputs = keras.layers.Input(shape=(28, 28, 1))
y = model(inputs)
feature = model.get_layer('conv2d_4').output
model = keras.Model(inputs=inputs, outputs=[y, feature])
model.summary()
为什么我不能得到'conv2d_4‘的输出,它是模型的内层?我得到了下面的错误。
Graph disconnected: cannot obtain value for tensor Tensor("input_1:0", shape=(None, 28, 28, 1), dtype=float32) at layer "conv2d". The following previous layers were accessed without issue: []
发布于 2021-05-19 08:20:17
我们可以尝试重新堆叠模型,将feature
分配给所需的层,
import tensorflow.keras as keras
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
if __name__ == '__main__':
model = keras.models.load_model('model/model_test_0.99408.h5', custom_objects={'leaky_relu': tf.nn.leaky_relu})
model.summary()
inputs = keras.layers.Input(shape=(28, 28, 1))
y = inputs
for layer in vgg.layers:
if layer.name == 'conv2d_4':
feature = y
y = layer( y )
model = keras.Model(inputs=inputs, outputs=[y, feature])
model.summary()
https://stackoverflow.com/questions/67599150
复制