0

So I am building a keras sequential model in which the last output layer is an Upsampling2D layer & I need to feed the input image to that output layer to do a simple operation and return the output, any ideas?

EDIT :

The model mentioned before is the generator of a GAN model in which I need to add the input image to the output of the generator before feeding it to the discriminator

2
  • What kind of simple operation you want do? Did you want using skip connection? Commented Jan 22, 2021 at 2:00
  • Yes its like a skip connection , but the model is pretrained so I d rather avoid recreating the architecture and just replace the last upsampling layer ( Note : this model is going to be a generator on a GAN architecture ) Commented Jan 22, 2021 at 7:47

2 Answers 2

1

1.You can define a backbone model using inputs of pre-trained model and the outputs of the last layer before the output layer of pre-trained model

2.Base on that backbone model, defined new model have that new skip connection and the output layer as same as pre-trained model

3.Set the weights of output layer in new model to equal to weights of output layer in pre-trained model, using: new_model.layers[-1].set_weights(pre_model.layers[-1].get_weights())

Here is one good article about Adding Layers to the middle of a pre-trained network whithout invalidating the weights

Sign up to request clarification or add additional context in comments.

Comments

0

So for the future reference, I solved it by using lambda layers as follow :

# z is the input I needed to use later on with the generator output to perform a certain function

generated_image = self.generator(z)        
generated_image_modified=tf.keras.layers.Concatenate()([generated_image,z])

# with x[...,variable_you_need_range] you can access the input we just concatenated in your train loop

lambd = tf.keras.layers.Lambda(lambda x: your_function(x[...,2:5],x[...,:2]))(generated_image_modified)

full_model = self.discriminator(lambd)
self.combined = Model(z,outputs = full_model)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.