Categories
Misc

using Resnet (pretrained) with keras functional API (Transfer learning)

I am using keras Functional API for multiple stream of inputs.

I want to use pretrained Resnet50 inbetween my layers. I would like to freeze the layers of Resnet to train i.e, Transfer learning.

How can I do it

“` CNN_Input = Input(shape=INPUT_SHAPE)

resnet = tf.keras.applications.ResNet50( include_top=False, input_shape=None, pooling=’avg’,

classes=NUM_CLASSES,

 weights='imagenet')(CNN_Input) 

————–BELOW TWO LINES ARE CAUSING ERRORS—————– I assumed we do the same way we did for Sequential Model

for layer in resnet.layers: layer.trainable=False

CNN=Flatten()(resnet) CNN=BatchNormalization()(CNN) CNN=Dropout(0.2)(CNN) CNN=Dense(1024, activation=’relu’)(CNN) CNN=Dense(512, activation=’relu’)(CNN) CNN=Dense(256, activation=’relu’)(CNN) CNN=BatchNormalization()(CNN) CNN=Dropout(0.2)(CNN) CNN=Dense(64,activation=’relu’)(CNN)

CAT_Input = Input(shape=(3,))

CAT=Dense(32, activation=’relu’)(CAT_Input) CAT=Dropout(0.2)(CAT) CAT=Dense(64, activation=’relu’)(CAT)

merge=concatenate([CNN,CAT]) hidden=Dense(64, activation=’relu’)(merge) hidden=Dense(64, activation=’relu’)(hidden) hidden=Dense(32,activation=’relu’)(hidden) output=Dense(NUM_CLASSES,activation=’softmax’)(hidden)

model = Model(inputs=[CNN_Input, CAT_Input], outputs=output) print(model.summary())

“`

submitted by /u/im-AMS
[visit reddit] [comments]

Leave a Reply

Your email address will not be published. Required fields are marked *