mobilenetv3-tensorflow icon indicating copy to clipboard operation
mobilenetv3-tensorflow copied to clipboard

Help: Keras with feed_dict mechanism of Session

Open caocuong0306 opened this issue 5 years ago • 2 comments

Dear @martinkersner ,

This is a question rather than an issue. I'd be grateful if you could give your thought.

I'm using your code to test a small (but old) project. The project was developed based on Queue-based and feed_dict mechanism of Session. That means I can only use your Model (build_mobilenetv3 function) without other Keras-based functions (model.compile, model.fit, model.evaluate, etc.)

The problem is that I need to set Keras's learning_phase to true during training, and false during evaluation or inference. I tried several ways like:

  1. tf.keras.backend.set_learning_phase(True) / tf.keras.backend.set_learning_phase(False)
  2. feed_dict = {tf.keras.backend.learning_phase() : 1} / feed_dict = {tf.keras.backend.learning_phase() : 0}
  3. I also modified your code a bit such as:
model = BuildMobileNetV3(
        num_classes=5,
        width_multiplier=1.0,
        l2_reg=1e-5,
    )
input_shape=(150, 150, 3)
input_tensor = tf.keras.layers.Input(shape=input_shape)

And use, model(input_tensor, training=True) / model(input_tensor, training=False)

However, none of the aforementioned methods works. The training cost and accuracy look good, but the validation accuracy's never been improved. I guess this is because of BN and dropout layers, but what else should I do beyond the above 3 approaches.

Thank you for your great work. I'm looking forward to discussing with you about the question.

Thanks. Cuong

caocuong0306 avatar Jul 03 '19 09:07 caocuong0306

Hi @caocuong0306 !

When do you set the learning phase? Before or after the model is created? We use tf.keras.backend.set_learning_phase at the beginning of script in our other project and don't have any problem with it. Also, be aware that set_learning_phase expect integers as inputs https://www.tensorflow.org/api_docs/python/tf/keras/backend/set_learning_phase. I hope it helps. Let us know if you solved your problem.

Cheers, Martin

martinkersner avatar Jul 06 '19 07:07 martinkersner

Hi @martinkersner ,

Thank you very much for your answer. I've read your reply yesterday, but I've been working on a small example using Colab so I can explain in detail.

I've been using TF so far, and I'm new to Keras. It seems to me that we cannot perform training & validation at the same time (within a session) when using Keras as model definition similar to what we usually do with TF, because we need to set_learning_phase at the beginning of scripts.

caocuong0306 avatar Jul 09 '19 00:07 caocuong0306