tensorflow / lucid

A collection of infrastructure and tools for research in neural network interpretability.
Apache License 2.0
4.65k stars 655 forks source link

Support for tf.keras #150

Open AakashKumarNain opened 5 years ago

AakashKumarNain commented 5 years ago

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

Uiuran commented 5 years ago

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.

colah commented 5 years ago

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(...)

AakashKumarNain commented 5 years ago

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

colah commented 5 years ago

Wonderful! Excited to hear about your experiences!

ludwigschubert commented 5 years ago

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.

parthnatekar commented 5 years ago

@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?

andrewt3000 commented 5 years ago

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

colah commented 5 years ago

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

ngonthier commented 4 years ago

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 ?

mcamila777 commented 3 years ago

@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.