neural-networktransfer-learningefficientnet

After transfer learning how to use original output layer and get both models predictions?


I trained a new network using EfficientNet for object detection, instead of using the original output layer of EfficientNet i change it to 5 output dense (5 labels from the original layer). in the training procedure, i locked all the layers and only train my custom output layer i managed to achieve a better result in my custom output layer labels (in EfficientNet they achieve 84% i managed to get an average of 92% accuracy ) i would like to get the original 1000 label and also my own 5 output layer

TLDR

what I am trying to achieve is this :

let's say I have 2 neural networks: A, B they both contains N-1 identical layers and different output layer

because they both identical up until the output layer they both will have the same output before the output layer

I would like to run model B then get the output of the dropout layer, run this output against model A output layer instead of running both of the models again is it possible or I am facing an XY problem ? what is the right way to create a neural network with 2 output layers?

I illustrated the problem :

enter image description here


Solution

  • I found how to do it just need to find the point of the split copy the first layer and direct its input to the other model and then concatenate all layers together

    efficientnet = efn.EfficientNetB0()
    retrained_model = load_model(model_path, custom_objects=get_custom_objects())
    top_conv = efficientnet.get_layer('top_conv')
    top_conv._name = top_conv._name + str("ef0")
    
    top_bn = efficientnet.get_layer("top_bn")
    top_bn._name = top_bn._name + str("ef0")
    
    top_activation = efficientnet.get_layer('top_activation')
    top_activation._name = top_activation._name + str("ef0")
    
    avg_pool = efficientnet.get_layer('avg_pool')
    avg_pool._name = avg_pool._name + str("ef0")
    
    top_dropout = efficientnet.get_layer('top_dropout')
    top_dropout._name = top_dropout._name + str("ef0")
    
    probs = efficientnet.get_layer('probs')
    
    probs(top_dropout(
        avg_pool(top_activation(top_bn(top_conv(retrained_model.get_layer('block7a_project_bn').output))))))
    
    model = Model(inputs=retrained_model.input,
                       outputs=[retrained_model.output, probs.get_output_at(0)])
    model.summary()
    model.save("/home/naor/projects/efficientnetretrainedmodel/bin/model-2-pred.h5")
    

    the new model created as model A input and as output an [A.output ,B.output]