1

I am new in Tensorflow and Python. I have built a multi layer network and I have pretrained it. Now I want to build another multilayer network next to the first. The weights of the first network are frozen and I want to concatenate features I got from the first network with the normal input of the layer of the new network. How can I concatenate the output of a specific layer in the first network with the input to this network and feed the layer of the new network?

1
  • Can you please clarify the requirement? Can you add a pseudo code for better understanding. Commented May 5, 2020 at 10:49

1 Answer 1

3

Below is the simple example of concatenating 2 input layers of different input shape and feeding to next layer.

import tensorflow.keras.backend as K
import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, concatenate, Conv2D, ZeroPadding2D, Dense
from tensorflow.keras.optimizers import Adagrad

input_img1 = Input(shape=(44,44,3))
x1 = Conv2D(3, (3, 3), activation='relu', padding='same')(input_img1)

input_img2 = Input(shape=(34,34,3))
x2 = Conv2D(3, (3, 3), activation='relu', padding='same')(input_img2)
# Zero Padding of 5 at the top, bottom, left and right side of an image tensor
x3 = ZeroPadding2D(padding = (5,5))(x2)

# Concatenate works as layers have same size output
x4 = concatenate([x1,x3])

output = Dense(18, activation='relu')(x4)

model = Model(inputs=[input_img1,input_img2], outputs=output)

model.summary()

Output -

Model: "model"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_4 (InputLayer)            [(None, 34, 34, 3)]  0                                            
__________________________________________________________________________________________________
input_3 (InputLayer)            [(None, 44, 44, 3)]  0                                            
__________________________________________________________________________________________________
conv2d_3 (Conv2D)               (None, 34, 34, 3)    84          input_4[0][0]                    
__________________________________________________________________________________________________
conv2d_2 (Conv2D)               (None, 44, 44, 3)    84          input_3[0][0]                    
__________________________________________________________________________________________________
zero_padding2d_1 (ZeroPadding2D (None, 44, 44, 3)    0           conv2d_3[0][0]                   
__________________________________________________________________________________________________
concatenate_1 (Concatenate)     (None, 44, 44, 6)    0           conv2d_2[0][0]                   
                                                                 zero_padding2d_1[0][0]           
__________________________________________________________________________________________________
dense (Dense)                   (None, 44, 44, 18)   126         concatenate_1[0][0]              
==================================================================================================
Total params: 294
Trainable params: 294
Non-trainable params: 0
__________________________________________________________________________________________________

If the above answer is not what you are looking for, then would request you to share the pseudo code or flow diagram of the models to answer better.

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

1 Comment

Yes, I have also found this way to implement it and it worked. However, thanks a lot for the reply!!!

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.