nirvair
nirvair

Reputation: 4180

fine tune a model using Keras Functional API

I am using VGG16 to finetune it on my dataset.

Here's the model:

def finetune(self, aux_input):
        model = applications.VGG16(weights='imagenet', include_top=False)
        # return model

        drop_5 = Input(shape=(7, 7, 512))
        flatten = Flatten()(drop_5)
        # aux_input = Input(shape=(1,))
        concat = Concatenate(axis=1)([flatten, aux_input])

        fc1 = Dense(512, kernel_regularizer=regularizers.l2(self.weight_decay))(concat)
        fc1 = Activation('relu')(fc1)
        fc1 = BatchNormalization()(fc1)

        fc1_drop = Dropout(0.5)(fc1)
        fc2 = Dense(self.num_classes)(fc1_drop)
        top_model_out = Activation('softmax')(fc2)

        top_model = Model(inputs=drop_5, outputs=top_model_out)

        output = top_model(model.output)

        complete_model = Model(inputs=[model.input, aux_input], outputs=output)

        return complete_model

I have two inputs to the model. In the above function, I'm using Concatenate for the flattened array and my aux_input. I'm not sure if this would work with imagenet weights.

When I run this, I get an error:

ValueError: Graph disconnected: cannot obtain value for tensor Tensor("aux_input:0", shape=(?, 1), dtype=float32) at layer "aux_input". The following previous layers were accessed without issue: ['input_2', 'flatten_1']

Not sure where am I going wrong.

If it matters, this is fit function:

model.fit(x={'input_1': x_train, 'aux_input': y_aux_train}, y=y_train, batch_size=batch_size,
                    epochs=maxepoches, validation_data=([x_test, y_aux_test], y_test),
                    callbacks=[reduce_lr, tensorboard], verbose=2)

But, I get an error before this fit function when I call model.summary().

Upvotes: 1

Views: 574

Answers (1)

Anna Krogager
Anna Krogager

Reputation: 3588

The problem is that you are using aux_input in your top_model but you don't specify it as an input in your definition of top_model. Try replacing your definition of top_model and output with the following:

top_model = Model(inputs=[drop_5, aux_input], outputs=top_model_out)
output = top_model([model.output, aux_input])

Upvotes: 1

Related Questions