serving icon indicating copy to clipboard operation
serving copied to clipboard

how to serve a keras model with dropout layer

Open DianaYu0601 opened this issue 9 years ago • 7 comments

my trained model has dropout layer ,and it gives out different servables every time.

how can I solve the problem?

DianaYu0601 avatar Sep 20 '17 10:09 DianaYu0601

should not export dropout layer into servable

DianaYu0601 avatar Sep 20 '17 10:09 DianaYu0601

@DianaYu0601 , how did you avoid of exporting dropout layer into servable?

OleksandrMalinin avatar Sep 22 '17 07:09 OleksandrMalinin

@OleksandrMalinin actually, dropout layer is meant to avoid over-fitting in the training process, not in predict process. So that in servable, if you only mean to use it for predict, dropout layer should be deleted. I simply didn't put dropout layer in the model I exported, and it won't be a problem when load weights.

    img_input = Input(shape=(224,224,3))

    x = Conv2D(32, kernel_size=(3, 3), activation='relu', name='conv2d_1')(img_input)
    x = MaxPooling2D(pool_size=(2, 2), name='max_pool_1')(x)

    x = Conv2D(32, kernel_size=(3, 3), activation='relu', name='conv2d_2')(x)
    x = MaxPooling2D(pool_size=(2, 2), name='max_pool_2')(x)

    x = Conv2D(64, kernel_size=(3, 3), activation='relu', name='conv2d_3')(x)
    x = MaxPooling2D(pool_size=(2, 2), name='max_pool_3')(x)

    # Classification block
    x = Flatten(name='Flatten')(x)
    x = Dense(64, activation='relu', name='fc_1')(x)
    # x = Dropout(0.5)(x)
    output = Dense(2, activation='sigmoid', name='pred')(x)

    model = Model(img_input, output, name='ifeye_model')
    model.load_weights(FLAGS.weights_file)

simple as that. Hope it help you!

DianaYu0601 avatar Sep 25 '17 01:09 DianaYu0601

@DianaYu0601 , I've asked you because I didn't understand... For me it seems like you're wrong. You don't need to remove dropout. Dropout removes neurons only during training, but during inference/prediction (without training step/backward propagation) all neurons are used (and scaled appropriately depending on used probability value).

To check that, pass few times batch of zeros as input. You'll be surprised - all of them will give you same result :)

Please also check https://stackoverflow.com/questions/34597316/why-input-is-scaled-in-tf-nn-dropout-in-tensorflow

OleksandrMalinin avatar Sep 25 '17 20:09 OleksandrMalinin

@OleksandrMalinin your understanding is quite right, but that seems not how tf-serving works.

When you use predict in non-serving situation, the dropout layer is automatically ignored, which is, all neurons are used. BUT when you export your model to servable and serve it with tensorflow_model_server,it is not a regular predicting operation. It's more like a frozen serial of ops that the tensor follows. Thus if the dropout layer is "frozen" in your servable, it will randomly drop half of your neurons and cause problem. (My English is poor so if you don't understand please feel free to ask!)

I didn't know this until I left the dropout layer in servable, and the predict result is widely different servable from servable, yet stable in one servable. Which indicate when I export the model, it some how freeze which neurons to drop in the serving process.

And that's why I remove the dropout layer, not in training process, but in model export process using tf.saved_model.builder.SavedModelBuilder()

DianaYu0601 avatar Sep 26 '17 02:09 DianaYu0601

When we want to do prediction/inference in servable model, dropout layer should be ignored. In Keras, by applying K.set_learning_phase(Boolean value), we can distinguish between training and inference phase when we have dropout layers.
When the Boolean value is True, it means it is in learning phase, and when the Boolean is false, it means it is in prediction/inference phase and it automatically ignores the dropout layers.

NoushNabi avatar Mar 28 '18 18:03 NoushNabi

There is still one thing I don't understand-- If I train a model with a dropout layer, what actually happens to its weights when I run K.set_learning_phase(0)? I would imagine all values set to r, the dropout rate for the layer, but I don't see it in the documentation anywhere.

mckeown12 avatar Dec 06 '18 19:12 mckeown12