我希望能够从预训练的Keras模型中删除前N层。例如,EfficientNetB0
的前 3 层仅用于预处理:
import tensorflow as tf
efinet = tf.keras.applications.EfficientNetB0(weights=None, include_top=True)
print(efinet.layers[:3])
# [<tensorflow.python.keras.engine.input_layer.InputLayer at 0x7fa9a870e4d0>,
# <tensorflow.python.keras.layers.preprocessing.image_preprocessing.Rescaling at 0x7fa9a61343d0>,
# <tensorflow.python.keras.layers.preprocessing.normalization.Normalization at 0x7fa9a60d21d0>]
正如M.Innat所提到的,第一层是一个输入层(Input Layer)
,应该被保留或重新附加。我想要移除这些层,但像这样简单的方法会报错:
cut_input_model = return tf.keras.Model(
inputs=[efinet.layers[3].input],
outputs=efinet.outputs
)
这将导致:
ValueError: Graph disconnected: cannot obtain value for tensor KerasTensor(...)
你建议采用什么方法来实现这个?