tensorflow / tfjs

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

np.object deprecation issue in tensorflowjs_converter #8434

Open ayushsharma-1 opened 1 week ago

ayushsharma-1 commented 1 week ago

1. URL of the file using: https://github.com/tensorflow/tfjs

2. Issue: I am encountering an issue when attempting to convert a Keras model (.h5) to TensorFlow.js format using the tensorflowjs_converter command. The issue arises due to the deprecated usage of np.object in the TensorFlow.js code.

Starting from NumPy 1.20, the np.object alias has been removed, causing compatibility issues with newer versions of NumPy. When I try running the tensorflowjs_converter tool, I receive the following error:

AttributeError: module 'numpy' has no attribute 'object'

This error occurs because np.object was removed as part of the NumPy 1.20 update, and is now considered a deprecated alias for the built-in object. The solution to this issue would be to replace the usage of np.object in TensorFlow.js with the standard object type, which is the correct usage in newer versions of NumPy.

Steps to Reproduce:

  1. Install the latest version of NumPy (1.20 or higher).
  2. Try to convert a Keras .h5 model to TensorFlow.js format using the command:
    tensorflowjs_converter --input_format keras my_dogs_vs_cats_model.h5 model
  3. Observe the error message:
    AttributeError: module 'numpy' has no attribute 'object'

3. Additional context:

4. Are you willing to contribute it? (Yes or No): No

shmishra99 commented 1 week ago

Hi @ayushsharma-1 ,

Thank you for reporting the issue. As far as I know, the tfjs-converter doesn't require NumPy to convert .h5 models. However, it's possible that your model was created with a Keras that used NumPy 1.20 or higher. To investigate further, please share your .h5 model file.

Thank You!!

Satge96 commented 5 days ago

tensorflowjs_wizard 2024-11-21 08:48:25.489059: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable TF_ENABLE_ONEDNN_OPTS=0. 2024-11-21 08:48:25.501613: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:477] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered WARNING: All log messages before absl::InitializeLog() is called are written to STDERR E0000 00:00:1732175305.516847 4053563 cuda_dnn.cc:8310] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered E0000 00:00:1732175305.521461 4053563 cuda_blas.cc:1418] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered 2024-11-21 08:48:25.536675: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations. To enable the following instructions: AVX2 AVX512F AVX512_VNNI AVX512_BF16 AVX512_FP16 AVX_VNNI AMX_TILE AMX_INT8 AMX_BF16 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags. /site-packages/tensorflowjs/read_weights.py:28: FutureWarning: In the future np.object will be defined as the corresponding NumPy scalar. np.uint8, np.uint16, np.object, np.bool] Traceback (most recent call last): File "/bin/tensorflowjs_wizard", line 5, in from tensorflowjs.converters.wizard import pip_main File "/lib/python3.12/site-packages/tensorflowjs/init.py", line 21, in from tensorflowjs import converters File "/lib/python3.12/site-packages/tensorflowjs/converters/init.py", line 21, in from tensorflowjs.converters.converter import convert File "/lib/python3.12/site-packages/tensorflowjs/converters/converter.py", line 35, in from tensorflowjs.converters import keras_h5_conversion as conversion File "/lib/python3.12/site-packages/tensorflowjs/converters/keras_h5_conversion.py", line 33, in from tensorflowjs import write_weights # pylint: disable=import-error ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/lib/python3.12/site-packages/tensorflowjs/write_weights.py", line 25, in from tensorflowjs import read_weights File "/lib/python3.12/site-packages/tensorflowjs/read_weights.py", line 28, in np.uint8, np.uint16, np.object, np.bool] ^^^^^^^^^ File "/lib/python3.12/site-packages/numpy/init.py", line 394, in getattr raise AttributeError(__former_attrs_[attr]) AttributeError: module 'numpy' has no attribute 'object'. np.object was a deprecated alias for the builtin object. To avoid this error in existing code, use object by itself. Doing this will not modify any behavior and is safe. The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations. Did you mean: 'object'?

For me, it happens directly when I invoke the wizard.

Satge96 commented 4 days ago

For me, changing to the newest tfjs already resolved the problem. As they already fixed it in the source. They replaced np.object with object.

shmishra99 commented 1 day ago

Hi @ayushsharma-1 ,

The issue has been occurring in the following line: link. The NumPy data type deprecation has been fixed in the following PR: #6625. Apologies for the confusion in my last comment. Thanks for the clarification, @Satge96.

Thank You!!

shmishra99 commented 1 day ago

@ayushsharma-1 ,If you're still encountering the issue after upgrading tfjs, please share your .h5 model for further investigation. Thank You!!