Search code examples
tensorflowkerastf.kerastransfer-learningmobilenet

How to add a few layers before the model in transfer learning with tensorflow


I am trying to use transfer learning in tensorflow. I know the high level paradigm

base_model=MobileNet(weights='imagenet',include_top=False) #imports the 

mobilenet model and discards the last 1000 neuron layer.

x=base_model.output
x=GlobalAveragePooling2D()(x)
x=Dense(1024,activation='relu')(x) #we add dense layers so that the model can learn more complex functions and classify for better results.
x=Dense(1024,activation='relu')(x) #dense layer 2
x=Dense(512,activation='relu')(x) #dense layer 3
preds=Dense(120,activation='softmax')(x) #final layer with softmax activation

and then one compiles it by

model=Model(inputs=base_model.input,outputs=preds)

However i want the there to be a few other layers before the base_model.input. I want to add adversarial noise to the images that come in and a few other things. So effectively i want to know how to :

base_model=MobileNet(weights='imagenet',include_top=False) #imports the 

mobilenet model and discards the last 1000 neuron layer

x = somerandomelayers(x_in)
base_model.input = x_in
x=base_model.output
x=GlobalAveragePooling2D()(x)
x=Dense(1024,activation='relu')(x) #we add dense layers so that the model can learn more complex functions and classify for better results.
x=Dense(1024,activation='relu')(x) #dense layer 2
x=Dense(512,activation='relu')(x) #dense layer 3
preds=Dense(120,activation='softmax')(x) #final layer with softmax activation
model=Model(inputs=x_in,outputs=preds)

but the line base_model.input = x_in is apparently not the way to do it as it throws can't set attribute error. How do i go about achieving the desired behavior?


Solution

  • You need to define input layer. It's rather straightforward, just be sure to set right shapes. For example, you can use any predefined model from Keras.

    base_model = keras.applications.any_model(...)
    input_layer = keras.layers.Input(shape)
    x = keras.layers.Layer(...)(input_layer)
    ...
    x = base_model(x)
    ...
    output = layers.Dense(num_classes, activation)(x)
    model = keras.Model(inputs=input_layer, outputs=output)