Home > Back-end >  How to add layers into keras functional object (e.g InceptionResNetV2)
How to add layers into keras functional object (e.g InceptionResNetV2)

Time:09-16

I am trying to add layers into the InceptionResNetV2 (or any other pretrained network that can be imported via tf.keras.applications). I do know that I can add the object to a sequential model or functional model. However, when I do that, I won't be able to access individual outputs from the layers to use them in Grad-CAM or similar applications.

I am using the following model structure right now. It works, it can be trained. However, it does NOT allow me to access output of the last convolutional layer of InceptionResNetV2 in regards to a specific input and specific output.

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())

CodePudding user response:

Expanding the nested models are difficult after they're created. Passing input_tensor argument to the pretrained model gives the expected results.

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)

Model summary:

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

                                                           
  • Related