如何将图层添加到keras功能对象(例如InceptionResNetV2)

093gszye  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(124)

我正在尝试将层添加到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())
qv7cva1a

qv7cva1a1#

在创建嵌套模型之后,扩展它们是困难的。将input_tensor参数传递给预先训练的模型可以得到预期的结果。

def get_model():

    inputs = tf.keras.Input(shape=(224, 224, 3))
    
    conv_base = InceptionResNetV2(weights=None, include_top=False, input_tensor = inputs)
    conv_base.trainable = False
    
    x = layers.Flatten()(conv_base.output)
    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)

模型摘要:

input_1 (InputLayer)           [(None, 224, 224, 3  0           []                               
                                )]                                                                
                                                                                                  
conv2d (Conv2D)                (None, 111, 111, 32  864         ['input_1[0][0]']                
                                )  
...

相关问题