我正在尝试将层添加到InceptionResNetV 2(或任何其他可以通过tf.keras.applications导入的预先训练的网络)。我知道我可以将对象添加到顺序模型或功能模型。但是,当我这样做时,我将无法访问层的单个输出以将其用于Grad-CAM或类似的应用程序。
我现在正在使用下面的模型结构。它可以工作,它可以训练。但是,它不允许我访问InceptionResNetV 2的最后一个卷积层的输出,关于特定的输入和特定的输出。
from tensorflow.keras import layers, models
InceptionResNetV2 = tf.keras.applications.inception_resnet_v2.InceptionResNetV2
def get_base():
conv_base = InceptionResNetV2(weights=None, include_top=False, input_shape=(224, 224, 3))
conv_base.trainable = False
return(conv_base)
def get_model():
base = get_base()
inputs = tf.keras.Input(shape=(224, 224, 3))
x = base(inputs, training=False)
x = layers.Flatten()(x)
x = layers.Dense(512, "relu")(x)
x = layers.Dropout(0.25)(x)
x = layers.Dense(256, "relu")(x)
x = layers.Dropout(0.25)(x)
dims = layers.Dense(2, name="Valence_Arousal")(x)
expression = layers.Dense(2, name="Emotion_Category")(x)
model = models.Model(inputs=[inputs], outputs=[expression, dims])
return(model)
print(get_model().summary())
1条答案
按热度按时间qv7cva1a1#
在创建嵌套模型之后,扩展它们是困难的。将
input_tensor
参数传递给预先训练的模型可以得到预期的结果。模型摘要: