Closed fawazahmed0 closed 4 years ago
@fawazahmed0 please follow the instructions here to convert your model : https://github.com/tensorflow/tfjs/tree/master/tfjs-converter
@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
@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
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
@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.
@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
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.
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
@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)
@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
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)
I even tried to convert using python:
and this is the error I got:
Code to reproduce the bug / link to feature request
Here is the code which was used to build the model:
Here is the list of python packages and their version installed in my pc: