Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help: Keras with feed_dict mechanism of Session #9

Open
caocuong0306 opened this issue Jul 3, 2019 · 2 comments
Open

Help: Keras with feed_dict mechanism of Session #9

caocuong0306 opened this issue Jul 3, 2019 · 2 comments
Assignees

Comments

@caocuong0306
Copy link

caocuong0306 commented Jul 3, 2019

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

@martinkersner martinkersner self-assigned this Jul 3, 2019
@martinkersner
Copy link
Member

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

@caocuong0306
Copy link
Author

caocuong0306 commented Jul 9, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants