lucid icon indicating copy to clipboard operation
lucid copied to clipboard

Support for tf.keras

Open AakashKumarNain opened this issue 5 years ago • 10 comments

As tf.keras is now the official TF high level API, are there any plans to support models trained in tf.keras?

AakashKumarNain avatar Mar 08 '19 09:03 AakashKumarNain

Only to note, this seems to be related to the issue i posted #162 . Seems to be that there is no compatibility although the both inherit from TF metaclass.

Uiuran avatar May 08 '19 04:05 Uiuran

Hello! Like any TensorFlow model, you can export keras models for visualization using Model.save().

(1) Construct the model in your default graph. (2) Run Model.suggest_save_args(). (3) Fill in any arguments that couldn't be inferred, sanity check the others, and call Model.save(...)

colah avatar May 18 '19 19:05 colah

Hey @colah . Thank you for the information. I will try it out and will let you know if it works the way as expected

AakashKumarNain avatar May 19 '19 12:05 AakashKumarNain

Wonderful! Excited to hear about your experiences!

colah avatar May 19 '19 16:05 colah

We just tried this workflow with @ncammarata successfully. One impediment to it working out of the box is that keras doesn't register its session as a default session, which our Model saving code expects.

Temporary(?) workaround:

with K.get_session().as_default():
…
  Model.save(…)
…

An additional issue (which may just be me not knowing Keras, like, at all) is that lucid can only deal with an "inference mode" graph, so e.g. no stochastic Dropout at eval time. I'm not sure how/whether one defines different graphs for training and evaluation time in Keras.

ludwigschubert avatar May 21 '19 00:05 ludwigschubert

@ludwigschubert

Can you please elaborate exactly what I need to do to export my keras model? Do I need to follow the modelzoo tutorial and convert it to a graph def?

parthnatekar avatar Jul 10 '19 07:07 parthnatekar

Can someone clarify what function is being referred to by Model.save()

Is it tf.keras.Model.save()?

https://www.tensorflow.org/api_docs/python/tf/keras/Model#save

andrewt3000 avatar Sep 19 '19 16:09 andrewt3000

This is lucid's Model.save. I've written up instructions on using it here:

https://github.com/tensorflow/lucid/wiki/Importing-Models-into-Lucid

colah avatar Sep 19 '19 18:09 colah

I am sorry but I am using lucid version 0.3.8 and the Model class imported from lucid.modelzoo.vision_models doesn't have the save or suggest_save_args methods. I used pip to install the lucid package. Do you have any idea what's it can be caused by ?

ngonthier avatar Mar 24 '20 14:03 ngonthier

@colah I have been trying to import my Keras model into Lucid from the "weights.h5" , I have tried to export my model in a compatible format but so far no success.

Do you think you could please provide an example of this part from your instructions:

# <Code to construct & load your model inference graph goes here> # ...

Specifically how to pass from the h5 keras weights to inference graph format compatible with lucid. thank you very much.

mcamila777 avatar Dec 01 '20 14:12 mcamila777