İhsan Dağ
İhsan Dağ

Reputation: 29

How to concatenate different layer outputs to feed as input to a new layer in Tensorflow?

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?

Upvotes: 1

Views: 3116

Answers (1)

user11530462
user11530462

Reputation:

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.

Upvotes: 3

Related Questions