2

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?

figs_and_nuts
  • 4,870
  • 2
  • 31
  • 56
  • How you define `x_in`? Do you want to concatenate two inputs or just add layers before model? – Sharky May 30 '19 at 06:56
  • just add layers before the model. Basically i want my architecture to be . layer1, layer2 ... layern --> modelinput-->modeloutput-->layerk,layersk+1 ... – figs_and_nuts May 30 '19 at 08:26

1 Answers1

3

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)
Sharky
  • 4,473
  • 2
  • 19
  • 27