tensorflow / tfjs

A WebGL accelerated JavaScript library for training and deploying ML models.
https://js.tensorflow.org
Apache License 2.0
18.34k stars 1.92k forks source link

tensorflowjs_converter NotImplementedError #3381

Closed fawazahmed0 closed 4 years ago

fawazahmed0 commented 4 years ago

Tensorflowjs Python Package Version

1.7.4.post1

Operating System

Windows 7

Describe the problem or feature request

Hi, I trained my model assuming I would be able to convert it into javascript, but I didn't knew that only few operations are supported in tensorflowjs. If you could help me to remove the unsupported operation from my code , that would be great. I would like to convert my model saved in SavedModel format

This is what I am getting(probability is the folder containing my SavedModel and tfjs is the output dir)

tensorflowjs_converter probability tfjs
2020-06-03 04:39:12.442563: I tensorflow/compiler/xla/service/service.cc:168] XL
A service 0x129b9a40 initialized for platform Host (this does not guarantee that
 XLA will be used). Devices:
2020-06-03 04:39:12.443563: I tensorflow/compiler/xla/service/service.cc:176]
StreamExecutor device (0): Host, Default Version
2020-06-03 04:39:17.653861: I tensorflow/core/grappler/devices.cc:60] Number of
eligible GPUs (core count >= 8, compute capability >= 0.0): 0 (Note: TensorFlow
was not compiled with CUDA support)
2020-06-03 04:39:17.654861: I tensorflow/core/grappler/clusters/single_machine.c
c:356] Starting new session
2020-06-03 04:39:17.807870: I tensorflow/core/grappler/optimizers/meta_optimizer
.cc:797] Optimization results for grappler item: graph_to_optimize
2020-06-03 04:39:17.808870: I tensorflow/core/grappler/optimizers/meta_optimizer
.cc:799]   function_optimizer: Graph size after: 120 nodes (110), 208 edges (198
), time = 6ms.
2020-06-03 04:39:17.808870: I tensorflow/core/grappler/optimizers/meta_optimizer
.cc:799]   function_optimizer: function_optimizer did nothing. time = 0ms.
WARNING:tensorflow:From c:\users\nawaz\appdata\local\programs\python\python38\li
b\site-packages\tensorflowjs\converters\tf_saved_model_conversion_v2.py:313: loa
d (from tensorflow.python.saved_model.loader_impl) is deprecated and will be rem
oved in a future version.
Instructions for updating:
This function will only be available through the v1 compatibility library as tf.
compat.v1.saved_model.loader.load or tf.compat.v1.saved_model.load. There will b
e a new function for importing SavedModels in Tensorflow 2.0.
WARNING:tensorflow:From c:\users\nawaz\appdata\local\programs\python\python38\li
b\site-packages\tensorflowjs\converters\tf_saved_model_conversion_v2.py:314: con
vert_variables_to_constants (from tensorflow.python.framework.graph_util_impl) i
s deprecated and will be removed in a future version.
Instructions for updating:
Use  tf.compat.v1.graph_util.convert_variables_to_constants 
WARNING:tensorflow:From c:\users\nawaz\appdata\local\programs\python\python38\li
b\site-packages\tensorflow\python\framework\graph_util_impl.py:359: extract_sub_
graph (from tensorflow.python.framework.graph_util_impl) is deprecated and will
be removed in a future version.
Instructions for updating:
Use  tf.compat.v1.graph_util.extract_sub_graph 
Traceback (most recent call last):
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\
tensorflowjs\converters\tf_saved_model_conversion_v2.py", line 450, in convert_t
f_saved_model
    frozen_graph = _freeze_saved_model_v2(concrete_func)
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\
tensorflowjs\converters\tf_saved_model_conversion_v2.py", line 324, in _freeze_s
aved_model_v2
    return convert_to_constants.convert_variables_to_constants_v2(
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\
tensorflow\python\framework\convert_to_constants.py", line 678, in convert_varia
bles_to_constants_v2
    output_graph_def, converted_inputs = _convert_variables_to_constants_v2_impl
(
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\
tensorflow\python\framework\convert_to_constants.py", line 457, in _convert_vari
ables_to_constants_v2_impl
    tensor_data = _get_tensor_data(func)
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\
tensorflow\python\framework\convert_to_constants.py", line 217, in _get_tensor_d
ata
    data = val_tensor.numpy()
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\
tensorflow\python\framework\ops.py", line 961, in numpy
    maybe_arr = self._numpy()  # pylint: disable=protected-access
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\
tensorflow\python\framework\ops.py", line 929, in _numpy
    six.raise_from(core._status_to_exception(e.code, e.message), None)
  File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot convert a T
ensor of dtype resource to a NumPy array.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\runpy.py", lin
e 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\runpy.py", lin
e 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\Nawaz\AppData\Local\Programs\Python\Python38\Scripts\tensorflow
js_converter.exe\__main__.py", line 7, in <module>
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\
tensorflowjs\converters\converter.py", line 671, in pip_main
    main([' '.join(sys.argv[1:])])
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\
tensorflowjs\converters\converter.py", line 675, in main
    convert(argv[0].split(' '))
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\
tensorflowjs\converters\converter.py", line 611, in convert
    tf_saved_model_conversion_v2.convert_tf_saved_model(
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\
tensorflowjs\converters\tf_saved_model_conversion_v2.py", line 452, in convert_t
f_saved_model
    frozen_graph = _freeze_saved_model_v1(saved_model_dir, saved_model_tags,
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\
tensorflowjs\converters\tf_saved_model_conversion_v2.py", line 314, in _freeze_s
aved_model_v1
    frozen_graph_def = tf.compat.v1.graph_util.convert_variables_to_constants(
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\
tensorflow\python\util\deprecation.py", line 324, in new_func
    return func(*args, **kwargs)
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\
tensorflow\python\framework\graph_util_impl.py", line 359, in convert_variables_
to_constants
    inference_graph = extract_sub_graph(input_graph_def, output_node_names)
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\
tensorflow\python\util\deprecation.py", line 324, in new_func
    return func(*args, **kwargs)
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\
tensorflow\python\framework\graph_util_impl.py", line 205, in extract_sub_graph
    _assert_nodes_are_present(name_to_node, dest_nodes)
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\
tensorflow\python\framework\graph_util_impl.py", line 160, in _assert_nodes_are_
present
    assert d in name_to_node, "%s is not in graph" % d
AssertionError: Identity is not in graph

I even tried to convert using python:

tfjs.converters.save_keras_model(probability_model, "C:\\Users\\Nawaz\\Desktop\\qadev\\js\\models\\tfjs")

and this is the error I got:

 2020-06-03 03:45:56.206749: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0xf8e5400 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-06-03 03:45:56.207749: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
Traceback (most recent call last):
  File "C:\Users\Nawaz\Desktop\qadev\js\testmodel.py", line 20, in <module>
    tfjs.converters.save_keras_model(probability_model, "C:\\Users\\Nawaz\\Desktop\\qadev\\js\\models\\tfjs")
  File "C:\Users\Nawaz\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflowjs\converters\keras_h5_conversion.py", line 338, in save_keras_model
    model.save(temp_h5_path)
  File "C:\Users\Nawaz\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\network.py", line 1051, in save
    save.save_model(self, filepath, overwrite, include_optimizer, save_format,
  File "C:\Users\Nawaz\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\saving\save.py", line 134, in save_model
    hdf5_format.save_model_to_hdf5(
  File "C:\Users\Nawaz\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\saving\hdf5_format.py", line 109, in save_model_to_hdf5
    model_metadata = saving_utils.model_metadata(model, include_optimizer)
  File "C:\Users\Nawaz\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\saving\saving_utils.py", line 155, in model_metadata
    raise e
  File "C:\Users\Nawaz\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\saving\saving_utils.py", line 152, in model_metadata
    model_config['config'] = model.get_config()
  File "C:\Users\Nawaz\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\sequential.py", line 368, in get_config
    layer_configs.append(generic_utils.serialize_keras_object(layer))
  File "C:\Users\Nawaz\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 275, in serialize_keras_object
    raise e
  File "C:\Users\Nawaz\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 270, in serialize_keras_object
    config = instance.get_config()
  File "C:\Users\Nawaz\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\sequential.py", line 368, in get_config
    layer_configs.append(generic_utils.serialize_keras_object(layer))
  File "C:\Users\Nawaz\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 275, in serialize_keras_object
    raise e
  File "C:\Users\Nawaz\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 270, in serialize_keras_object
    config = instance.get_config()
  File "C:\Users\Nawaz\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py", line 841, in get_config
    raise NotImplementedError
NotImplementedError
[Finished in 20.469s] 

Code to reproduce the bug / link to feature request

Here is the code which was used to build the model:

import pandas as pd
import tensorflow as tf
import tensorflow_hub as hub
import numpy as np
import glob
import os
from tensorflow import keras

# http://tanzil.net/trans/
translationDir = "C:/Users/Nawaz/Downloads/qdata/"

transData = []
allTransData = []
simpleTarget = []
allTransTarget = []

# https://stackoverflow.com/questions/15233340/getting-rid-of-n-when-using-readlines
# https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory
# Reading all the translations in translationDir
for i in glob.glob(translationDir+"*.txt"):
    with open(i,'r', encoding='cp1252') as f:
           transData.append(f.read().splitlines())

# Adding all translations to allTrans
for i in transData:
    count = 0
    for j in i:
        allTransData.append(j)
        allTransTarget.append(count)
        count+=1

noOfVerse = len(transData[0])
# https://stackoverflow.com/questions/18265935/python-create-list-with-numbers-between-2-values
simpleTarget = list(range(0, noOfVerse))

# https://stackoverflow.com/questions/51125266/how-do-i-split-tensorflow-datasets
DATASET_SIZE = len(allTransData)
train_size = int(0.7 * DATASET_SIZE)
val_size = int(0.15 * DATASET_SIZE)
test_size = int(0.15 * DATASET_SIZE)

full_dataset = tf.data.Dataset.from_tensor_slices((allTransData, allTransTarget))
# shuffling the data, it increases accuracy
full_dataset = full_dataset.shuffle(len(allTransData))
train_dataset = full_dataset.take(train_size)
test_dataset = full_dataset.skip(train_size)
validation_dataset = test_dataset.skip(test_size)
test_dataset = test_dataset.take(test_size)

# keeping batch size low will increase the accuracy, and keeping it high will decrease the accuracy
# batch size low will take hours to train and large batch size will take minutes to train
batch_size = 512
train_dataset = train_dataset.batch(batch_size)
test_dataset = test_dataset.batch(batch_size)
validation_dataset = validation_dataset.batch(batch_size)

# trainable means the weights in this layer will get updated during training
embedding = hub.load("C:/Users/Nawaz/Desktop/qadev/js/nlm128")

hub_layer = hub.KerasLayer(embedding, input_shape=[],
                           dtype=tf.string, trainable=True)

model = tf.keras.Sequential()
model.add(hub_layer)
model.add(tf.keras.layers.Dense(100, activation='relu'))
model.add(tf.keras.layers.Dense(6236))

model.summary()
# https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/tutorials/keras/regression.ipynb#scrollTo=LcopvQh3X-kX
# https://keras.io/api/callbacks/
# we will stop ,if the loss does not change after n epochs , patience stores the n variable
# don't remove this assuming tuner will take care of epochs, we have to stop by using earlystopping to avoid unecessary epochs
early_stop = [ tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=5) ]
# if target or label is integer values i.e 1,2,3 use SparseCategoricalCrossentropy, else if they are one hot encoded [1,0,0][0,1,0][0,0,1] use categorical_crossentropy
# https://jovianlin.io/cat-crossentropy-vs-sparse-cat-crossentropy/
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

model.fit(train_dataset, epochs=30, validation_data=validation_dataset, verbose=1, callbacks=early_stop)

model.evaluate(test_dataset, verbose=2)

model.save('saved_model/my_model')

Here is the list of python packages and their version installed in my pc:

absl-py                0.9.0
astunparse             1.6.3
backcall               0.1.0
cachetools             4.1.0
certifi                2020.4.5.1
cffi                   1.14.0
chardet                3.0.4
click                  7.1.2
colorama               0.4.3
decorator              4.4.2
enum34                 1.1.10
future                 0.18.2
gast                   0.3.3
google-auth            1.16.0
google-auth-oauthlib   0.4.1
google-pasta           0.2.0
grpcio                 1.29.0
h5py                   2.10.0
idna                   2.9
ipython                7.15.0
ipython-genutils       0.2.0
jedi                   0.17.0
joblib                 0.15.1
Keras                  2.2.4
Keras-Applications     1.0.8
Keras-Preprocessing    1.1.2
keras-tuner            1.0.1
Markdown               3.2.2
numpy                  1.18.4
oauthlib               3.1.0
opt-einsum             3.2.1
pandas                 1.0.4
parso                  0.7.0
pickleshare            0.7.5
pip                    20.1.1
plaidbench             0.7.0
plaidml                0.7.0
plaidml-keras          0.7.0
prompt-toolkit         1.0.14
protobuf               3.12.2
pyasn1                 0.4.8
pyasn1-modules         0.2.8
pycparser              2.20
Pygments               2.6.1
PyInquirer             1.0.3
python-dateutil        2.8.1
pytz                   2020.1
PyYAML                 5.3.1
regex                  2020.5.14
requests               2.23.0
requests-oauthlib      1.3.0
rsa                    4.0
scikit-learn           0.23.1
scipy                  1.4.1
setuptools             41.2.0
six                    1.15.0
sklearn                0.0
tabulate               0.8.7
tensorboard            2.2.2
tensorboard-plugin-wit 1.6.0.post3
tensorflow             2.2.0
tensorflow-cpu         2.2.0
tensorflow-estimator   2.2.0
tensorflow-hub         0.7.0
tensorflowjs           1.7.4.post1
termcolor              1.1.0
terminaltables         3.1.0
threadpoolctl          2.1.0
tqdm                   4.46.0
traitlets              4.3.3
urllib3                1.25.9
wcwidth                0.2.2
Werkzeug               1.0.1
wheel                  0.34.2
wrapt                  1.12.1
rthadur commented 4 years ago

@fawazahmed0 please follow the instructions here to convert your model : https://github.com/tensorflow/tfjs/tree/master/tfjs-converter

fawazahmed0 commented 4 years ago

@rthadur , Thanks for replying, I ran the tensorflowjs_wizard, and this is the error I got:

C:\Users\Nawaz\Desktop\qadev\js>tensorflowjs_wizard
Welcome to TensorFlow.js Converter.
? Please provide the path of model file or the directory that contains model fi
If you are converting TFHub module please provide the URL.  saved_model\my_model
? What is your input model format? (auto-detected format is marked with *)  Ten
? Do you want to compress the model? (this will decrease the model precision.)
? Which directory do you want to save the converted model in?  models\tfjs
? The output already directory exists, do you want to overwrite it?  Yes
converter command generated:
tensorflowjs_converter --input_format=keras_saved_model saved_model\my_model models\tfjs

2020-06-03 22:53:51.157330: I tensorflow/compiler/xla/service/service.cc:168] XL
A service 0x1318a340 initialized for platform Host (this does not guarantee that
 XLA will be used). Devices:
2020-06-03 22:53:51.157330: I tensorflow/compiler/xla/service/service.cc:176]
StreamExecutor device (0): Host, Default Version
WARNING:tensorflow:From c:\users\nawaz\appdata\local\programs\python\python38\li
b\site-packages\tensorflow\python\ops\resource_variable_ops.py:1659: calling Bas
eResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) wi
th constraint is deprecated and will be removed in a future version.
Instructions for updating:
If using Keras pass *_constraint arguments to layers.
Conversion failed, please check error log file C:\Users\Nawaz\AppData\Local\Temp
\converter_error.log.

C:\Users\Nawaz\Desktop\qadev\js>

And this is the log file it generated:

Traceback (most recent call last):
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\tensorflowjs\converters\wizard.py", line 562, in run
    converter.convert(arguments)
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\tensorflowjs\converters\converter.py", line 604, in convert
    dispatch_keras_saved_model_to_tensorflowjs_conversion(
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\tensorflowjs\converters\converter.py", line 182, in dispatch_keras_saved_model_to_tensorflowjs_conversion
    model.save(temp_h5_path, save_format='h5')
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\keras\engine\network.py", line 1051, in save
    save.save_model(self, filepath, overwrite, include_optimizer, save_format,
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\keras\saving\save.py", line 134, in save_model
    hdf5_format.save_model_to_hdf5(
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\keras\saving\hdf5_format.py", line 109, in save_model_to_hdf5
    model_metadata = saving_utils.model_metadata(model, include_optimizer)
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\keras\saving\saving_utils.py", line 155, in model_metadata
    raise e
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\keras\saving\saving_utils.py", line 152, in model_metadata
    model_config['config'] = model.get_config()
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\keras\engine\sequential.py", line 368, in get_config
    layer_configs.append(generic_utils.serialize_keras_object(layer))
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 275, in serialize_keras_object
    raise e
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 270, in serialize_keras_object
    config = instance.get_config()
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\keras\engine\sequential.py", line 368, in get_config
    layer_configs.append(generic_utils.serialize_keras_object(layer))
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 275, in serialize_keras_object
    raise e
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 270, in serialize_keras_object
    config = instance.get_config()
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py", line 841, in get_config
    raise NotImplementedError
NotImplementedError
Traceback (most recent call last):
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\tensorflowjs\converters\wizard.py", line 562, in run
    converter.convert(arguments)
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\tensorflowjs\converters\converter.py", line 604, in convert
    dispatch_keras_saved_model_to_tensorflowjs_conversion(
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\tensorflowjs\converters\converter.py", line 182, in dispatch_keras_saved_model_to_tensorflowjs_conversion
    model.save(temp_h5_path, save_format='h5')
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\keras\engine\network.py", line 1051, in save
    save.save_model(self, filepath, overwrite, include_optimizer, save_format,
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\keras\saving\save.py", line 134, in save_model
    hdf5_format.save_model_to_hdf5(
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\keras\saving\hdf5_format.py", line 109, in save_model_to_hdf5
    model_metadata = saving_utils.model_metadata(model, include_optimizer)
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\keras\saving\saving_utils.py", line 155, in model_metadata
    raise e
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\keras\saving\saving_utils.py", line 152, in model_metadata
    model_config['config'] = model.get_config()
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\keras\engine\sequential.py", line 368, in get_config
    layer_configs.append(generic_utils.serialize_keras_object(layer))
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 275, in serialize_keras_object
    raise e
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 270, in serialize_keras_object
    config = instance.get_config()
  File "c:\users\nawaz\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py", line 841, in get_config
    raise NotImplementedError
NotImplementedError
fawazahmed0 commented 4 years ago

@rthadur And one more thing, I am currently using this: https://tfhub.dev/google/tf2-preview/nnlm-en-dim128/1 as my word embedding layer in the code and I assume this error might be in someway related to the word embedding layer

fawazahmed0 commented 4 years ago

I am pretty much sure that this error is related to the tfhub word embedding layer, because I created a word embedding layer from scratch using GloVe dataset and the tensorflow converter script seems to convert the model into js format with no issues.

But I am not sure why when I use tfhub word embedding layers, it throws error. I even tried different embedding layer such as this: https://tfhub.dev/google/collections/universal-sentence-encoder/1 and even that doesn't seem to work

pyu10055 commented 4 years ago

@fawazahmed0 can you share a code that does not reference your local data files? Or share the saved model directory, so we can try to reproduce the problem. thanks.

fawazahmed0 commented 4 years ago

@pyu10055 ,here is the code that I made specifically made for debugging this issue, just run the code and it will save the model in models/debug directory and try converting it to js format using tensorflowjs_converter and it would throw some error.

#import json
from urllib.request import urlopen
import pandas as pd
import tensorflow as tf
import tensorflow_hub as hub
import numpy as np
import glob
import os
from tensorflow import keras

transData = []
allTransData = []
allTransTarget = []

# https://cdn.jsdelivr.net/gh/fawazahmed0/LargeTxtFile@latest/qdata/en.sahih.txt

url = urlopen('https://cdn.jsdelivr.net/gh/fawazahmed0/LargeTxtFile@latest/qdata/en.sahih.txt')

# https://stackoverflow.com/questions/15233340/getting-rid-of-n-when-using-readlines
# https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory
# Reading all the translations in translationDir
transData.append(url.read().splitlines())

# Adding all translations to allTrans
for i in transData:
    count = 0
    for j in i:
        #https://stackoverflow.com/questions/37016946/remove-b-character-do-in-front-of-a-string-literal-in-python-3
        allTransData.append(j.decode("utf-8"))
        allTransTarget.append(count)
        count+=1

# https://stackoverflow.com/questions/51125266/how-do-i-split-tensorflow-datasets
DATASET_SIZE = len(allTransData)
train_size = int(0.7 * DATASET_SIZE)
val_size = int(0.15 * DATASET_SIZE)
test_size = int(0.15 * DATASET_SIZE)

full_dataset = tf.data.Dataset.from_tensor_slices((allTransData, allTransTarget))
# shuffling the data, it increases accuracy
full_dataset = full_dataset.shuffle(len(allTransData))
train_dataset = full_dataset.take(train_size)
test_dataset = full_dataset.skip(train_size)
validation_dataset = test_dataset.skip(test_size)
test_dataset = test_dataset.take(test_size)

# we are not shuffling data, but we will try shuffling the whole data
# keeping batch size low will increase the accuracy, and keeping it high will decrease the accuracy
# batch size low will take hours to train and large batch size will take minutes to train
batch_size = 5000
train_dataset = train_dataset.batch(batch_size)
test_dataset = test_dataset.batch(batch_size)
validation_dataset = validation_dataset.batch(batch_size)

# trainable means the weights in this layer will get updated during training
#embedding = hub.load("C:/Users/Nawaz/Desktop/qadev/js/nlm128")
#https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1
embedding = hub.load("https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1")
# print(embedding(["cat is on the mat", "dog is in the fog"]))

hub_layer = hub.KerasLayer(embedding, input_shape=[],
                           dtype=tf.string, trainable=True)

model = tf.keras.Sequential()
model.add(hub_layer)
model.add(tf.keras.layers.Dense(100, activation='relu'))
model.add(tf.keras.layers.Dense(6236))
# New add softmax layer as output while training, it will effect in loss calculation, model might not train well
# model.add(tf.keras.layers.Dense(6236, activation='softmax'))

model.summary()
# https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/tutorials/keras/regression.ipynb#scrollTo=LcopvQh3X-kX
# https://keras.io/api/callbacks/
# we will stop ,if the loss does not change after n epochs , patience stores the n variable
# don't remove this assuming tuner will take care of epochs, we have to stop by using earlystopping to avoid unecessary epochs
early_stop = [ tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=5) ]
# if target or label is integer values i.e 1,2,3 use SparseCategoricalCrossentropy, else if they are one hot encoded [1,0,0][0,1,0][0,0,1] use categorical_crossentropy
# https://jovianlin.io/cat-crossentropy-vs-sparse-cat-crossentropy/
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

model.fit(train_dataset, epochs=1, validation_data=validation_dataset, verbose=1, callbacks=early_stop)

model.evaluate(test_dataset, verbose=2)

model.save('models/debug')

Just in case you still need the saved model, here it is: debug.zip

Thanks

pyu10055 commented 4 years ago

The error seems to be a known issue of TF https://github.com/tensorflow/tensorflow/issues/37441, there might a solution in the related stackoverflow link.

fawazahmed0 commented 4 years ago

The solution in the stackoverflow link seems to be using custom layer, and tensorflow.js doesn't support custom layers yet. Refer: https://www.tensorflow.org/js/tutorials/conversion/import_keras

Anyways, thanks for looking into this

pyu10055 commented 4 years ago

@fawazahmed0 If you converted the model to custom layer, you can convert the saved model to TFJS GraphModel for inference. (LayersModel does not support custom layer, but GraphModel may)

fawazahmed0 commented 4 years ago

@pyu10055 , oh wait, I assume you were looking at first error log, this the log you should look at: https://github.com/tensorflow/tfjs/issues/3381#issuecomment-638347014 My model is saved in keras saved model format, the first error log I got due the fact that I did not specify the input format as keras saved model.

And also one more thing, the problem is not in my code, but rather the word embedding layer (TF Hub Module) and specifically the word embedding layers which doesn't require preprocessing the input, we can feed variable length string into it.

To reproduce this issue, just try converting any TF Hub text embedding model (SavedModel 2.0 format) which can take variable length string as input, it would throw some signature error.

You can try converting this into js using tensorflowjs_converter: https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1