ultralytics / ultralytics

Ultralytics YOLO11 🚀
https://docs.ultralytics.com
GNU Affero General Public License v3.0
29.86k stars 5.82k forks source link

Export to tfjs error #869

Closed hoseinhamzei closed 1 year ago

hoseinhamzei commented 1 year ago

Search before asking

YOLOv8 Component

No response

Bug

I need to export yolov8n to tfjs json model but when I run the following CLI command in colab it throws multiple errors: !yolo export model=yolov8n.pt format=tfjs

this is because export to pb format fails, so I tried to export to pb first but it throws "_UserObject' object has no attribute 'inputs'" error. after running the export command yolov8n_web_model/model.json is empty and no tfjs bin file is generated.

error:


TensorFlow GraphDef: starting export with tensorflow 2.11.0...
TensorFlow GraphDef: export failure ❌ 0.0s: '_UserObject' object has no attribute 'inputs'
[Errno 2] No such file or directory: '/usr/local/lib/python3.8/dist-packages/protobuf-3.19.6.dist-info/METADATA'

TensorFlow.js: starting export with tensorflowjs 4.2.0...
2023-02-08 12:23:58.926645: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/local/lib/python3.8/dist-packages/cv2/../../lib64:/usr/local/lib/python3.8/dist-packages/cv2/../../lib64:/usr/lib64-nvidia
2023-02-08 12:23:58.926753: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/local/lib/python3.8/dist-packages/cv2/../../lib64:/usr/local/lib/python3.8/dist-packages/cv2/../../lib64:/usr/lib64-nvidia
2023-02-08 12:23:58.926773: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
Traceback (most recent call last):
  File "/usr/local/bin/tensorflowjs_converter", line 8, in <module>
    sys.exit(pip_main())
  File "/usr/local/lib/python3.8/dist-packages/tensorflowjs/converters/converter.py", line 827, in pip_main
    main([' '.join(sys.argv[1:])])
  File "/usr/local/lib/python3.8/dist-packages/tensorflowjs/converters/converter.py", line 831, in main
    convert(argv[0].split(' '))
  File "/usr/local/lib/python3.8/dist-packages/tensorflowjs/converters/converter.py", line 817, in convert
    _dispatch_converter(input_format, output_format, args, quantization_dtype_map,
  File "/usr/local/lib/python3.8/dist-packages/tensorflowjs/converters/converter.py", line 581, in _dispatch_converter
    tf_saved_model_conversion_v2.convert_tf_frozen_model(
  File "/usr/local/lib/python3.8/dist-packages/tensorflowjs/converters/tf_saved_model_conversion_v2.py", line 617, in convert_tf_frozen_model
    graph = load_graph(frozen_model_path)
  File "/usr/local/lib/python3.8/dist-packages/tensorflowjs/converters/tf_saved_model_conversion_v2.py", line 76, in load_graph
    graph_def.ParseFromString(f.read())
  File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/lib/io/file_io.py", line 116, in read
    self._preread_check()
  File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/lib/io/file_io.py", line 77, in _preread_check
    self._read_buf = _pywrap_file_io.BufferedInputStream(
tensorflow.python.framework.errors_impl.NotFoundError: yolov8n.pb; No such file or directory
TensorFlow.js: export success ✅ 3.2s, saved as yolov8n_web_model (0.0 MB)

Export complete (57.2s)
Results saved to /content
Predict:         yolo task=detect mode=predict model=yolov8n_web_model -WARNING ⚠️ not yet supported for YOLOv8 exported models
Validate:        yolo task=detect mode=val model=yolov8n_web_model -WARNING ⚠️ not yet supported for YOLOv8 exported models
Visualize:       https://netron.app/
`

### Environment

Google Colab, YOLOv8.0.31 🚀 Python-3.8.10 torch-1.13.1+cu116 CUDA:0 (Tesla T4, 15110MiB)
knoppmyth commented 1 year ago

Export to Tensorflow is a work in progress. See #209 for progress.

glenn-jocher commented 1 year ago

@hoseinhamzei @knoppmyth we don't have tf.js export currently, but we do have TF and TFLite FP32 and FP16 export working correctly, i.e.

yolo export model=yolov8n.pt format=tflite

If you have TF expertise please consider helping out with TF.js exports. Thank you!

github-actions[bot] commented 1 year ago

👋 Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help.

For additional resources and information, please see the links below:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLO 🚀 and Vision AI ⭐

mbenami commented 1 year ago

hi @glenn-jocher I can see tfjs is now supported. any change the output will be the same as yolov5? with nms + agnostic , as [bbox, scores, labels, numDetections] ) https://github.com/ultralytics/yolov5/blob/5ca8e822c8e75cde1d613dea8bfa49009fdc3618/export.py#L457

for now, its looks like there are no nms and the output is Identity:0 here https://github.com/ultralytics/ultralytics/blob/701fba47700dee7a6bd8ff4d219eca91adc9d3be/ultralytics/yolo/engine/exporter.py#L652

if not is it possible to get some clarification on the model output? the output shape looks like bbox[4] + score[1] + one hot encoder [len(classes)] is the 4bbox in format xyxy or xywh? is the score right after the bbox?

Thanks

glenn-jocher commented 1 year ago

@mbenami we're using onnx2tf for all TF exports now, so we have a more scalable export solution that doesn't require us to maintain and build TF modules/models for exports, but it also means that we haven't reproduced all of the added export features in YOLOv5 either. If you'd like to help contribute please submit a PR! Thank you!

mbenami commented 1 year ago

@glenn-jocher thanks for now, I convert the output on my side

for everyone else that needs some clarification on the tfjs output

batch, [x_center, y_center, width, height, prob_class0, prob_class1, ..., prob_class_N], number of results per model type (anchors)

github-actions[bot] commented 1 year ago

👋 Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help.

For additional resources and information, please see the links below:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLO 🚀 and Vision AI ⭐

FatemeZamanian commented 1 year ago

Hi i got onnx model from export code then i run this code: https://colab.research.google.com/drive/1ek5gT3wXdXVs95Bs0_EIyOf-gwa0_a3c?usp=sharing and got tfjs files :)

glenn-jocher commented 1 year ago

@FatemeZamanian hi there,

I'm glad to hear that you were able to successfully convert your YOLOv8 model to tfjs using the provided code in the Colab notebook. This indicates that the export process was successful and you now have the necessary tfjs files.

If you have any further questions or need assistance with anything else related to YOLOv8, please feel free to ask.

VictorBarbosa commented 1 year ago

I have created an object detection model, where my input is (1, 3, 224, 224) and the output is (1, 7, 1029). It works perfectly when using YOLO. However, when I export it to TensorFlow.js, I have no idea how to transform the output (1, 7, 1029) into bounding boxes. Can someone help me?"

In this request, the user explains that they have created an object detection model that takes input of shape (1, 3, 224, 224) and produces output of shape (1, 7, 1029). The model performs well when using the YOLO framework. However, they are facing difficulties in transforming the output tensor into bounding boxes when exporting the model to TensorFlow.js. They are seeking assistance to handle this issue.

glenn-jocher commented 1 year ago

@VictorBarbosa the output tensor shape of (1, 7, 1029) in your TensorFlow.js model represents the model's prediction outputs grid with the size of 7x7. Each cell in the grid is responsible for predicting an object within that cell. The tensor contains the following parameters for each cell:

  1. Bounding box details: The first four elements in the tensor are the x, y, width, and height of the bounding box, respectively.
  2. Objectness score: The fifth element is the objectness score, which indicates the likelihood of an object present within the bounding box.
  3. Class probabilities: The rest of the tensor elements (i.e., from the sixth element to the end) hold the predicted probabilities for each object class the model is trained on.

To extract the bounding boxes from the output tensor, you would need to apply some post-processing steps, including applying an objectness score threshold and non-maximum suppression (NMS) for handling overlapping boxes.

The exact implementation can vary across different projects and use-cases, so you may need to adjust these steps according to your needs.

VictorBarbosa commented 1 year ago

@glenn-jocher Thank you ,

glenn-jocher commented 1 year ago

@VictorBarbosa you're welcome! If you have any more questions or need further assistance, don't hesitate to ask. Happy coding!

Smith-S-S commented 7 months ago

When I am trying to convert the trained yolov8 model to ( !yolo export model="/content/drive/MyDrive/PPE_Detection_YOLOV8/dataset/runs/detect/train/weights/best.pt" format= tfjs) its only giving this "best_saved_model" but I want "best_web_model" in the tutorial iam following one they run the code they got best_web_model. can you help me with this guys

Screenshot 2024-02-16 at 1 06 21 AM Screenshot 2024-02-16 at 1 07 56 AM Screenshot 2024-02-16 at 1 08 15 AM
glenn-jocher commented 7 months ago

@Smith-S-S it appears that you are encountering an issue with exporting a YOLOv8 model to TensorFlow.js (tfjs) format. The error message indicates that the export to the intermediate .pb (Protocol Buffers) format is failing, which is a necessary step before converting the model to TensorFlow.js format.

The error '_UserObject' object has no attribute 'inputs' suggests that there might be an issue with the model object or the export process that is not correctly handling the model's input layer.

To troubleshoot this issue, please follow these steps:

  1. Ensure that you have the latest version of the required libraries installed. In Google Colab, you can install or upgrade libraries using !pip install or !pip install --upgrade.

  2. Make sure that the model you are trying to export is compatible with TensorFlow.js. Some model architectures or custom layers might not be directly convertible.

  3. Try exporting the model to a different format first, such as ONNX or TensorFlow SavedModel, to see if the issue is specific to TensorFlow.js. If the export to these formats is successful, it might indicate a problem with the TensorFlow.js conversion process.

  4. If the issue persists, consider opening an issue on the Ultralytics YOLOv8 GitHub repository with detailed information about the error and the steps you've taken. The maintainers or the community might be able to provide more specific guidance or a fix.

  5. As a workaround, you can try using a different model that has been successfully exported to TensorFlow.js by others, or you can attempt to modify the model architecture to remove any unsupported layers or operations.

Remember that exporting models to different formats can sometimes be challenging due to compatibility issues between different frameworks and libraries. It often requires trial and error to resolve these issues.

Smith-S-S commented 7 months ago

Sure sir I try now this thing and come to you sir

Smith-S-S commented 7 months ago

this was my full code, sir drive.mount("/content/drive") %cd "/content/drive/MyDrive/PPE_Detection_YOLOV8"

Installing the dataset from the roboflow

!pip install roboflow from roboflow import Roboflow rf = Roboflow(api_key="R38i0N5gBDDkzbmApwoO") project = rf.workspace("aabbcceeffgg").project("brain-tumor-detection-69d9s") dataset = project.version(2).download("yolov5")

Train the model

!yolo task=detect model=yolov8n.pt data="/content/drive/MyDrive/PPE_Detection_YOLOV8/dataset/Brain-Tumor-Detection-2/data.yaml" epochs=170 imgsz=640

Converting the model

!pip install ultralytics !pip install tensorflow==2.13.0 !yolo export model="/content/drive/MyDrive/PPE_Detection_YOLOV8/dataset/runs/detect/train/weights/best.pt" format= tfjs

After this in that video they can get 2 files downloaded One is "best_saved_model" and "best_web_model" so they are using the "best_web_model" inside that they have the "model.json" file they are using that in the tutorial

Screenshot 2024-02-16 at 1 07 56 AM

But for me getting the only one file it is "best_saved_model" and in side I have

Screenshot 2024-02-16 at 1 08 15 AM

so now what can I do to get that "best_web_model" sir?

Smith-S-S commented 7 months ago

I got it sir thank u

glenn-jocher commented 7 months ago

@Smith-S-S it appears that you are encountering an issue with exporting a YOLOv8 model to TensorFlow.js (tfjs) format. The error message indicates that the export to the .pb (Protocol Buffers) format is failing, which is a prerequisite step before converting to TensorFlow.js format.

The error '_UserObject' object has no attribute 'inputs' suggests that there might be an issue with the model object or the export process that is not correctly handling the model's inputs.

To resolve this issue, please ensure that you are using the latest version of YOLOv8 and all its dependencies. If you are already using the latest version, this might be a bug that needs to be reported to the Ultralytics YOLOv8 GitHub repository.

In the meantime, you can try the following steps to troubleshoot the problem:

  1. Make sure that you have the correct versions of TensorFlow and TensorFlow.js installed in your Colab environment. You can install or upgrade them using the following commands:

    !pip install --upgrade tensorflow
    !pip install --upgrade tensorflowjs
  2. Before attempting to export to TensorFlow.js, try exporting the model to other formats such as ONNX or TensorFlow SavedModel to see if those exports are successful. This can help isolate whether the issue is specific to TensorFlow.js or a more general export problem.

  3. If the export to other formats works, but TensorFlow.js still fails, you can try manually converting the exported model to TensorFlow.js using the tensorflowjs_converter tool. First, export the model to the TensorFlow SavedModel format:

    !yolo export model=yolov8n.pt format=saved_model

    Then, use the tensorflowjs_converter to convert the SavedModel to TensorFlow.js format:

    !tensorflowjs_converter --input_format=tf_saved_model --output_format=tfjs_graph_model /path/to/saved_model /path/to/tfjs_model
  4. If none of the above steps work, consider opening an issue on the Ultralytics YOLOv8 GitHub repository with detailed information about the error and the steps you've taken to reproduce it. The maintainers and community may be able to provide further assistance.

Remember to replace /path/to/saved_model and /path/to/tfjs_model with the actual paths to your exported SavedModel and the desired output directory for the TensorFlow.js model.

Smith-S-S commented 7 months ago

Thank u sir

glenn-jocher commented 7 months ago

@Smith-S-S it appears that you are encountering an issue with exporting a YOLOv8 model to TensorFlow.js (tfjs) format. The error message indicates that the export to the intermediate .pb (Protocol Buffers) format is failing, which is a necessary step before converting the model to tfjs format.

The error '_UserObject' object has no attribute 'inputs' suggests that there might be an issue with the TensorFlow or TensorFlow.js converter version you are using, or with the way the model is being loaded or processed during the conversion.

Here are a few steps you can take to troubleshoot and potentially resolve the issue:

  1. Check TensorFlow and TensorFlow.js Versions: Ensure that you have the correct versions of TensorFlow and TensorFlow.js installed. Compatibility issues between different versions can cause errors during export.

  2. Update TensorFlow.js Converter: If you are using an older version of the TensorFlow.js converter, try updating it to the latest version, as it may contain fixes for issues like the one you are experiencing.

  3. Use a Different Export Format: As a workaround, you can try exporting the model to a different format that is supported by TensorFlow.js, such as the SavedModel format, and then use the TensorFlow.js converter to convert the SavedModel to tfjs format.

  4. Check for Known Issues: Review the Ultralytics YOLOv8 GitHub issues to see if others have reported similar problems and if there are any suggested solutions or workarounds.

  5. Report the Issue: If none of the above steps resolve the issue, consider reporting it on the Ultralytics YOLOv8 GitHub repository. Provide detailed information about the error, the steps you've taken, and the environment in which you are running the export command.

  6. Use a Stable Environment: Sometimes, running the export in a different environment (e.g., a local machine with a stable setup) can help identify if the issue is specific to the Colab environment.

  7. Manual Conversion: If the CLI command continues to fail, you can attempt to manually convert the model to tfjs format by following the TensorFlow.js conversion instructions. This may involve exporting the model to a TensorFlow SavedModel first and then using the TensorFlow.js converter.

Remember to keep your Colab environment updated and to restart the runtime if you make any changes to the installed packages. If you continue to face issues, reaching out to the Ultralytics support or community may provide additional assistance.