我想从预训练的 Keras 模型中删除前N层。例如, an 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>]
Run Code Online (Sandbox Code Playgroud)
正如M.Innat提到的,第一层是Input Layer
,应该保留或重新附加。我想删除这些层,但是像这样的简单方法会引发错误:
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>]
Run Code Online (Sandbox Code Playgroud)
这将导致:
ValueError: Graph disconnected: cannot obtain value for tensor KerasTensor(...)
Run Code Online (Sandbox Code Playgroud)
推荐的方法是什么?
任务是确定图像属于 3 个类中的哪一个,或者不属于。
我收到了一个现成的模型。具有 ImageNet 权重的 EfficientNet B4 已应用迁移学习来识别 4 个类别:3 个目标类别和第 4 个“无”。后者接受了不包含任何目标对象的随机图像示例的训练。
问题是这是否是正确的方法——是否需要第四堂课?
我的直觉是 net 应该只在 3 个目标类上进行训练。如果输出概率保持在某个阈值(90%?)以下,图像应被视为不包含任何目标对象。我对吗?