Gagan Ganapathy
Gagan Ganapathy

Reputation: 75

Not able to convert keras model to tfjs layers using tensorflowjs_converter

I am trying to use tensorflowjs for the first time alongside react to build a web application, I have trained my model using keras and have saved my weights into a hdf5 file format and am referring to this link here - https://www.tensorflow.org/js/guide/conversion

$ tensorflowjs_converter --input_format=keras /tmp/model.h5 /tmp/tfjs_model

This ^ is what I tried but then I am getting errors.

  1. Initially got this error given here (no add_to_collection was found when using tensorflowjs_converter) I solved it by installing a different version of tensorflowjs v0.6.4

  2. Now that I have installed tensorflowjs v0.6.4 I am getting another error described below :

gaganganapathyas:~ codhek$ tensorflowjs_converter --input_format=keras /tmp/model.h5 /tmp/tfjs_model
Using TensorFlow backend.
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/bin/tensorflowjs_converter", line 6, in <module>
    from tensorflowjs.converters.converter import main
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflowjs/__init__.py", line 21, in <module>
    from tensorflowjs import converters
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflowjs/converters/__init__.py", line 24, in <module>
    from tensorflowjs.converters.tf_saved_model_conversion import convert_tf_saved_model
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflowjs/converters/tf_saved_model_conversion.py", line 34, in <module>
    import tensorflow_hub as hub
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow_hub/__init__.py", line 25, in <module>
    from tensorflow_hub.feature_column import image_embedding_column
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow_hub/feature_column.py", line 25, in <module>
    from tensorflow_hub import module
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow_hub/module.py", line 23, in <module>
    from tensorflow_hub import native_module
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow_hub/native_module.py", line 26, in <module>
    from tensorflow_hub import compressed_module_resolver
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow_hub/compressed_module_resolver.py", line 35, in <module>
    from tensorflow_hub import resolver
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow_hub/resolver.py", line 34, in <module>
    FLAGS = tf.flags.FLAGS
AttributeError: 'module' object has no attribute 'flags'

Even tried to add the absolute path along with the above command, but didn't work :

/Library/Frameworks/Python.framework/Versions/2.7/bin/tensorflowjs_converter --input_format=keras /tmp/weights.model /tmp/tfjs_model

Here is the link to my keras cnn model : https://www.kaggle.com/codhek/cnn-using-keras-using-csv-accuracy-99-82

Does model.save_weights() and model.save() make any difference?

Also, if I save a .json version of my model can I directly load it into my js app ?

Upvotes: 0

Views: 743

Answers (1)

Gagan Ganapathy
Gagan Ganapathy

Reputation: 75

It worked once you use tensorflow v0.6.4 with keras v2.1.6 although keras install might give you a warning that

ERROR: tensorflowjs 0.6.4 has requirement keras==2.2.2, but you'll have keras 2.1.6 which is incompatible.

This is because tensorflow v0.6.4 needs keras==2.2.2 but then keras-preprocessing version don't match!

Upvotes: 1

Related Questions