Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting Keras models / replacing input but keeping layers

This questions is similar to Keras replacing input layer.

I have a classifier network and an autoencoder network and I want to use the output of the autoencoder (i.e. encoding + decoding, as a preprocessing step) as the input to the classifier - but after the classifier was already trained on the regular data.

The classification network was built with the functional API like this (based on this example):

clf_input = Input(shape=(28,28,1))
clf_layer = Conv2D(...)(clf_input)
clf_layer = MaxPooling2D(...)(clf_layer)
...
clf_output = Dense(num_classes, activation='softmax')(clf_layer)
model = Model(clf_input, clf_output)
model.compile(...)
model.fit(...)

And the autoencoder like this (based on this example):

ae_input = Input(shape=(28,28,1))
x = Conv2D(...)(ae_input)
x = MaxPooling2D(...)(x)
...
encoded = MaxPooling2D(...)(x)
x = Conv2d(...)(encoded)
x = UpSampling2D(...)(x)
...
decoded = Conv2D(...)(x)
autoencoder = Model(ae_input, decoded)
autoencoder.compile(...)
autoencoder.fit(...)

I can concatenate the two models like this (I still need the original models, hence the copying):

model_copy = keras.models.clone_model(model)
model_copy.set_weights(model.get_weights())
# remove original input layer
model_copy.layers.pop(0)
# set the new input
new_clf_output = model_copy(decoded)
# get the stacked model
stacked_model = Model(ae_input, new_clf_output)
stacked_model.compile(...)

And this works great when all I want to do is apply the model to new test data, but it gives an error on something like this:

for layer in stacked_model.layers:
    print layer.get_config()

where it gets to the end of the autoencoder but then fails with a KeyError at the point where the classifier model gets its input. Also when plotting the model with keras.utils.plot_model I get this:

stacked_model

where you can see the autoencoder layers but then at the end, instead of the individual layers from the classifier model, there is only the complete model in one block.

Is there a way to connect two models such the new stacked model is actually made up of all the individual layers?

like image 484
cod3licious Avatar asked May 04 '18 22:05

cod3licious


People also ask

Do we need to specify the input shape in keras?

As the last two Dense layers do not need new input, we do not need to specify the input shape. Keras focuses on the idea of models. To build models, layers are their primary block. There is a wide variety of layers present in Keras. Each layer has its specific tasks.

What are the layers of a keras model?

Keras - Layers. Advertisements. Previous Page. Next Page. As learned earlier, Keras layers are the primary building block of Keras models. Each layer receives input information, do some computation and finally output the transformed information. The output of one layer will flow into the next layer as its input.

Does the Keras input work with TensorFlow?

However, the resulting model will not track any variables that were used as inputs to TensorFlow ops. All variable usages must happen within Keras layers to make sure they will be tracked by the model's weights. The Keras Input can also create a placeholder from an arbitrary tf.TypeSpec , e.g:

What is the difference between constraints and regularizer in keras?

In between, constraints restricts and specify the range in which the weight of input data to be generated and regularizer will try to optimize the layer (and the model) by dynamically applying the penalties on the weights during optimization process. To summarise, Keras layer requires below minimum details to create a complete layer.


Video Answer


1 Answers

Ok, what I could come up with is to really manually go through each layer of the model and reconnect them one by one again like this:

l = model.layers[1](decoded)  # layer 0 is the input layer, which we're replacing
for i in range(2, len(model.layers)):
    l = model.layers[i](l)
stacked_model = Model(ae_input, l)
stacked_model.compile(...)

while this works and produces the correct plot and no errors, this does not seem like the most elegant solution...

(btw, the copying of the model actually seems to be unnecessary as I'm not retraining anything.)

like image 163
cod3licious Avatar answered Oct 13 '22 00:10

cod3licious