Home > Software design >  Using pre-trained models in transfer learning - how do I do this?
Using pre-trained models in transfer learning - how do I do this?

Time:10-20

I'm trying to apply transfer learning using VGG16 but I'm struggling to find an example with multiple outputs.

I've written my own architecture as below which I used to manually train my model - though I don't understand how I can compile the same metrics using a pre-trained model.

How do I do this?

from keras.applications.vgg16 import VGG16

vgg16_model = VGG16(weights='imagenet',
                  include_top=False,
                  input_shape=(224,224,3))

vgg16_model.summary()

def modelG():
    input=tf.keras.layers.Input(shape=(224,224,3))
    x=input
    x=layers.Conv2D(8,(3,3),activation='relu')(x)
    x=layers.Conv2D(8,(3,3),activation='relu')(x)
    x=layers.MaxPooling2D(2)(x)
    x=layers.Dropout(0.1)(x)
    x=layers.Conv2D(16,(3,3),activation='relu')(x)
    x=layers.Conv2D(16,(3,3),activation='relu')(x)
    x=layers.MaxPooling2D(2)(x)
    x=layers.Dropout(0.1)(x)
    x=layers.Conv2D(32,(3,3),activation='relu')(x)
    x=layers.Conv2D(32,(3,3),activation='relu')(x)
    x=layers.MaxPooling2D(2)(x)
    x=layers.Dropout(0.1)(x)
    x=layers.Conv2D(84,(3,3),activation='relu')(x)
    x=layers.Dropout(0.1)(x)
    x=layers.Flatten()(x)
    out_col=layers.Dense(512,activation='relu')(x)
    out_ren=layers.Dense(512,activation='relu')(x)
    out_col= layers.Dense(1,activation='sigmoid',name='col_out')(out_col)
    out_ren=layers.Dense(1,activation='relu',name='ren_out')(out_ren)
    multiOutputModel=tf.keras.models.Model(inputs=input, outputs=[out_col, out_ren])

# Compile
    multiOutputModel.compile(
              optimizer='adam', 
              loss={
                  'ren_out': 'mean_squared_error',  
                  'col_out': 'binary_crossentropy'},
              loss_weights={
                  'ren_out': 4.0, 
                  'col_out': 0.1},
              metrics={
                  'ren_out': 'mean_absolute_error', 
                  'col_out': 'accuracy'})

    tf.keras.utils.plot_model(modelB, 'modelB.png',show_shapes=True)
    return multiOutputModel

multiOutputModel.summary()

CodePudding user response:

you would do it as you would for the single output case

base_model=tf.keras.applications.VGG19(include_top=False, weights="imagenet",input_shape=img_shape, pooling='max') 
x=base_model.output
out_col=layers.Dense(512,activation='relu')(x)
out_ren=layers.Dense(512,activation='relu')(x)
out_col= layers.Dense(1,activation='sigmoid',name='col_out')(out_col)
out_ren=layers.Dense(1,activation='relu',name='ren_out')(out_ren)
multiOutputModel=tf.keras.models.Model(inputs=base_model.input, outputs=[out_col, out_ren])

Note in the VGG model I set pooling='max' so the output of VGG is a 1 dimentionsal tensor so you do not need a flatten layer.

  • Related