TensorFlow.js Converter Issue
Hey!
I'm able to use tensorflowjs converter properly as used in this notebook from @shmishra99. But when I try and use the converter in my docker container running on linux arm64, it fails and gives me this error:
# tensorflowjs_converter --input_format=tfjs_layers_model --output_format=keras models/tfjs/model.json models/keras/fla_model.h5
WARNING:root:Failure to load the inference.so custom c++ tensorflow ops. This error is likely caused the version of TensorFlow and TensorFlow Decision Forests are not compatible. Full error:/usr/local/lib/python3.10/site-packages/tensorflow_decision_forests/tensorflow/ops/inference/inference.so: cannot open shared object file: No such file or directory
Traceback (most recent call last):
File "/usr/local/bin/tensorflowjs_converter", line 5, in <module>
from tensorflowjs.converters.converter import pip_main
File "/usr/local/lib/python3.10/site-packages/tensorflowjs/__init__.py", line 21, in <module>
from tensorflowjs import converters
File "/usr/local/lib/python3.10/site-packages/tensorflowjs/converters/__init__.py", line 21, in <module>
from tensorflowjs.converters.converter import convert
File "/usr/local/lib/python3.10/site-packages/tensorflowjs/converters/converter.py", line 38, in <module>
from tensorflowjs.converters import tf_saved_model_conversion_v2
File "/usr/local/lib/python3.10/site-packages/tensorflowjs/converters/tf_saved_model_conversion_v2.py", line 28, in <module>
import tensorflow_decision_forests
File "/usr/local/lib/python3.10/site-packages/tensorflow_decision_forests/__init__.py", line 64, in <module>
from tensorflow_decision_forests import keras
File "/usr/local/lib/python3.10/site-packages/tensorflow_decision_forests/keras/__init__.py", line 53, in <module>
from tensorflow_decision_forests.keras import core
File "/usr/local/lib/python3.10/site-packages/tensorflow_decision_forests/keras/core.py", line 62, in <module>
from tensorflow_decision_forests.keras import core_inference
File "/usr/local/lib/python3.10/site-packages/tensorflow_decision_forests/keras/core_inference.py", line 36, in <module>
from tensorflow_decision_forests.tensorflow.ops.inference import api as tf_op
File "/usr/local/lib/python3.10/site-packages/tensorflow_decision_forests/tensorflow/ops/inference/api.py", line 179, in <module>
from tensorflow_decision_forests.tensorflow.ops.inference import op
File "/usr/local/lib/python3.10/site-packages/tensorflow_decision_forests/tensorflow/ops/inference/op.py", line 15, in <module>
from tensorflow_decision_forests.tensorflow.ops.inference.op_dynamic import *
File "/usr/local/lib/python3.10/site-packages/tensorflow_decision_forests/tensorflow/ops/inference/op_dynamic.py", line 24, in <module>
raise e
File "/usr/local/lib/python3.10/site-packages/tensorflow_decision_forests/tensorflow/ops/inference/op_dynamic.py", line 21, in <module>
ops = tf.load_op_library(resource_loader.get_path_to_datafile("inference.so"))
File "/usr/local/lib/python3.10/site-packages/tensorflow/python/framework/load_library.py", line 54, in load_op_library
lib_handle = py_tf.TF_LoadLibrary(library_filename)
tensorflow.python.framework.errors_impl.NotFoundError: /usr/local/lib/python3.10/site-packages/tensorflow_decision_forests/tensorflow/ops/inference/inference.so: cannot open shared object file: No such file or directory
This is the docker file im running off:
FROM python:3.10.12-slim
WORKDIR /backend
COPY . /backend
RUN apt-get update && apt-get install -y pkg-config gcc g++ libhdf5-dev
RUN pip install --upgrade pip setuptools wheel
RUN pip install --no-cache-dir flask flask-sqlalchemy psycopg2-binary python-dotenv flask-cors pillow tensorflowjs tensorflow-aarch64==2.15.0 tensorflow_decision_forests==1.8.1
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
EXPOSE 5000
ENV FLASK_APP=app.py
ENV FLASK_ENV=development
CMD ["flask", "run", "--host=0.0.0.0", "--debug"]
Thanks for any help!
Hi @TheRealCasmat ,
I have created a Dockerfile image using the same base image, python:3.10.12-slim (linux arm64). I am able to convert the Keras model to a TFJS model using tfjs-converter, using the same Colab snippet file that I shared previously.
In your Dockerfile, you can use pip install tensorflow==2.15.0 instead of pip install tensorflow-aarch64==2.15.0. Let me know if you have any specific reason to use tensorflow-aarch64.
Thank You!
Hey @shmishra99!
I forgot to mention I wasn't exactly following the notebook you shared. I ran tensorflowjs_converter --input_format=tfjs_layers_model --output_format=keras models/tfjs/model.json models/keras/fla_model.h5 inside the container and got the error. Could you try that using this model on your end:
backend.zip
If that does work, could you share your docker image? I don't know really why i used tensorflow-aarch64... used tensorflow and built again but still getting error. The Dockerfile is the same as I used before but with the '-aarch64' removed. I'm not sure why the inference.so file cannot be opened, It does exist on my end.
Also, just a quick question, when using the converter in python, if I wanted to convert keras h5 model that is created using the command above back to a tfjs layers model, would I use tfjs.converters.dispatch_keras_h5_to_tfjs_layers_model_conversion('models/keras/fla-model.h5', 'models/tfjs/')?
Thanks again for your help!
Hi @TheRealCasmat ,
I'm building a Docker image using your provided Dockerfile. You could follow the steps I've tried, they might be helpful for you.
Here are the steps I've taken:
- Created a Dockerfile using your provided file, making sure to include TensorFlow 2.15.0.
- Ran the build command
docker build -t my-container-image. with my app.py file as the dummy server. Output logs: - Run the container using the command:
docker run -it my-container-image - Enter the container by executing:
docker exec -it <container-id> bash - Run the
converter.pyscript (as described in the Colab notebook). Output:
Let me know if these steps are help for you or not.
The command tfjs.converters.dispatch_keras_h5_to_tfjs_layers_model_conversion('models/keras/fla-model.h5', 'models/tfjs/') is the correct approach to convert your Keras H5 model to a TensorFlow.js model.
Thank You!!
Hey @shmishra99!
Hmm still doesn’t work for me for some reason. It seems that on my end I’m not able to import tensorflowjs as tfjs and that gives the inference.so error. I created new script in the backend directory that is the same as the last code block in collab
Used:
FROM python:3.10.12-slim
WORKDIR /backend
COPY . /backend
RUN apt-get update && apt-get install -y pkg-config gcc g++ libhdf5-dev
RUN pip install --upgrade pip setuptools wheel
RUN pip install --no-cache-dir flask flask-sqlalchemy psycopg2-binary python-dotenv flask-cors pillow tensorflowjs tensorflow==2.15.0 tensorflow_decision_forests==1.8.1
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
EXPOSE 5000
ENV FLASK_APP=app.py
ENV FLASK_ENV=development
CMD ["flask", "run", "--host=0.0.0.0", "--debug"]
Could you share the most recent error messages from the converter script, along with the command you're using to run it?
WARNING:root:Failure to load the inference.so custom c++ tensorflow ops. This error is likely caused the version of TensorFlow and TensorFlow Decision Forests are not compatible. Full error:/usr/local/lib/python3.10/site-packages/tensorflow_decision_forests/tensorflow/ops/inference/inference.so: cannot open shared object file: No such file or directory
Traceback (most recent call last):
File "/backend/r.py", line 2, in <module>
import tensorflowjs as tfjs
File "/usr/local/lib/python3.10/site-packages/tensorflowjs/__init__.py", line 21, in <module>
from tensorflowjs import converters
File "/usr/local/lib/python3.10/site-packages/tensorflowjs/converters/__init__.py", line 21, in <module>
from tensorflowjs.converters.converter import convert
File "/usr/local/lib/python3.10/site-packages/tensorflowjs/converters/converter.py", line 38, in <module>
from tensorflowjs.converters import tf_saved_model_conversion_v2
File "/usr/local/lib/python3.10/site-packages/tensorflowjs/converters/tf_saved_model_conversion_v2.py", line 28, in <module>
import tensorflow_decision_forests
File "/usr/local/lib/python3.10/site-packages/tensorflow_decision_forests/__init__.py", line 64, in <module>
from tensorflow_decision_forests import keras
File "/usr/local/lib/python3.10/site-packages/tensorflow_decision_forests/keras/__init__.py", line 53, in <module>
from tensorflow_decision_forests.keras import core
File "/usr/local/lib/python3.10/site-packages/tensorflow_decision_forests/keras/core.py", line 62, in <module>
from tensorflow_decision_forests.keras import core_inference
File "/usr/local/lib/python3.10/site-packages/tensorflow_decision_forests/keras/core_inference.py", line 36, in <module>
from tensorflow_decision_forests.tensorflow.ops.inference import api as tf_op
File "/usr/local/lib/python3.10/site-packages/tensorflow_decision_forests/tensorflow/ops/inference/api.py", line 179, in <module>
from tensorflow_decision_forests.tensorflow.ops.inference import op
File "/usr/local/lib/python3.10/site-packages/tensorflow_decision_forests/tensorflow/ops/inference/op.py", line 15, in <module>
from tensorflow_decision_forests.tensorflow.ops.inference.op_dynamic import *
File "/usr/local/lib/python3.10/site-packages/tensorflow_decision_forests/tensorflow/ops/inference/op_dynamic.py", line 24, in <module>
raise e
File "/usr/local/lib/python3.10/site-packages/tensorflow_decision_forests/tensorflow/ops/inference/op_dynamic.py", line 21, in <module>
ops = tf.load_op_library(resource_loader.get_path_to_datafile("inference.so"))
File "/usr/local/lib/python3.10/site-packages/tensorflow/python/framework/load_library.py", line 54, in load_op_library
lib_handle = py_tf.TF_LoadLibrary(library_filename)
tensorflow.python.framework.errors_impl.NotFoundError: /usr/local/lib/python3.10/site-packages/tensorflow_decision_forests/tensorflow/ops/inference/inference.so: cannot open shared object file: No such file or directory
and just python r.py on the python file that has
import tensorflow as tf
import tensorflowjs as tfjs
# tf.keras.applications.MobileNetV3Large(
# input_shape=None,
# alpha=1.0,
# minimalistic=False,
# include_top=True,
# weights='imagenet',
# input_tensor=None,
# classes=1000,
# pooling=None,
# dropout_rate=0.2,
# classifier_activation='softmax',
# include_preprocessing=True
# )
model = tf.keras.applications.MobileNetV3Large(
input_shape=(224, 224, 3), weights='imagenet', classifier_activation='softmax'
)
tfjs.converters.save_keras_model(model, "sample_data/tfjs_model_keras_MobileNetV3Large")
You can try my image docker pull dhanwanth/fla-backend:latest
Hli @TheRealCasmat ,
I am encountering the following error while running your Docker image: 'exec /usr/local/bin/flask: exec format error.'
From the error logs, I can see that there seems to be a compatibility issue between TensorFlow and TensorFlow-Decision-Forest dependencies.
Could you please run the following command and check the versions of the installed dependencies to ensure they are compatible?
pip show tensorflow
pip show tensorflowjs
pip show tensorflow-decision-forest
Thank You!!
Name: tensorflow
Version: 2.15.0
Summary: TensorFlow is an open source machine learning framework for everyone.
Home-page: https://www.tensorflow.org/
Author: Google Inc.
Author-email: [email protected]
License: Apache 2.0
Location: /usr/local/lib/python3.10/site-packages
Requires: tensorflow-cpu-aws
Required-by: tensorflow_decision_forests, tensorflowjs, tf_keras
Name: tensorflowjs
Version: 4.20.0
Summary:
Home-page: https://js.tensorflow.org/
Author: Google LLC
Author-email: [email protected]
License: Apache 2.0
Location: /usr/local/lib/python3.10/site-packages
Requires: flax, importlib_resources, jax, jaxlib, packaging, six, tensorflow, tensorflow-decision-forests, tensorflow-hub, tf-keras
Required-by:
WARNING: Package(s) not found: tensorflow-decision-forest
pip show tensorflow-decision-forests with a ‘s’ returns
Name: tensorflow_decision_forests
Version: 1.8.1
Summary: Collection of training and inference decision forest algorithms.
Home-page: https://github.com/tensorflow/decision-forests
Author: Google Inc.
Author-email: [email protected]
License: Apache 2.0
Location: /usr/local/lib/python3.10/site-packages
Requires: absl-py, numpy, pandas, six, tensorflow, wheel, wurlitzer
Required-by: tensorflowjs
It seems like the installed dependencies appear compatible, but there could be various reasons why the system isn't finding the correct dependencies. At this point, it's unclear exactly what is causing the error.
Could you try deleting your existing Docker image and rebuild it using the same Dockerfile but with a different image name? You can use the following commands:
docker rmi <existing-image>
docker build -t <image-name> .
docker run -it <image-name>
Thank You!!
Still shows same error.. deleted old image, created new build and ran but still got the earlier inference.so error after running the same r.py script
You can try my image
docker pull dhanwanth/fla-backend:latest
HI @TheRealCasmat ,
I’m having trouble running your image and encountered the following error after pulling the image:
CMD:
docker run -d --name tf-converter 28bbce6bb66e
Error:
WARNING: The requested image's platform (linux/arm64) does not match the detected host platform (linux/amd64/v3) and no specific platform was requested
9cd31c51ed110f24e0ce9d31589869d87f3355245cfa34e2f8c9dccb2f77dafa
Could you please assist me in setting up the container for this image?
Thank you!
Hey @shmishra99!
You seem to be running on an amd host instead of arm, which is what I’m getting the error using?
Thanks!
I'm getting the same error on windows 10. I guess the error is that TF_LoadLibrary is trying to load inference.so. Which I guess is a linux library. That's why I get the Bad format message. Seems the linux sub system needs to be installed