ultralytics / yolov5

YOLOv5 πŸš€ in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
50.39k stars 16.26k forks source link

Export .tflite #5133

Closed alexiej closed 2 years ago

alexiej commented 3 years ago

❔Question

How to export the trained model to .tflite ?

Additional context

I've tried this instruction:

python export.py --weights yolo5/best_yolo5s.pt --include tflite

But I've got error:

export: data=yolo5/data/coco128.yaml, weights=yolo5/best_yolo5s.pt, imgsz=[640, 640], batch_size=1, device=cpu, half=False, inplace=False, train=False, optimize=False, int8=False, dynamic=False, simplify=False, opset=13, topk_per_class=100, topk_all=100, iou_thres=0.45, conf_thres=0.25, include=['tflite']
YOLOv5 πŸš€ ca30934 torch 1.7.1 CPU

[2021-10-11 14:38:48.066 MacBook-Pro-Arkadiusz.local:96894 INFO utils.py:27] RULE_JOB_STOP_SIGNAL_FILENAME: None
[2021-10-11 14:38:48.162 MacBook-Pro-Arkadiusz.local:96894 INFO profiler_config_parser.py:111] Unable to find config at /opt/ml/input/config/profilerconfig.json. Profiler is disabled.
Fusing layers... 
Model Summary: 275 layers, 7064511 parameters, 0 gradients

TensorFlow saved_model: export failure: no matching TensorFlow activation found for SiLU()

TensorFlow Lite: starting export with tensorflow 2.6.0...

TensorFlow Lite: export failure: 'NoneType' object has no attribute 'call'

I tried found what SiLU() is, and it should work properly, but I've got errror.

github-actions[bot] commented 3 years ago

πŸ‘‹ Hello @alexiej, thank you for your interest in YOLOv5 πŸš€! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a πŸ› Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://ultralytics.com or email Glenn Jocher at glenn.jocher@ultralytics.com.

Requirements

Python>=3.6.0 with all requirements.txt installed including PyTorch>=1.7. To get started:

$ git clone https://github.com/ultralytics/yolov5
$ cd yolov5
$ pip install -r requirements.txt

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), validation (val.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

glenn-jocher commented 3 years ago

@alexiej it seems like your TensorFlow version may be out of date. You should update TensorFlow, or you can also run YOLOv5 exports in a verified environment like Colab where everything works well.

alexiej commented 3 years ago

@glenn-jocher I've got the same error on Colab (GPU and CPU version). :/. In MAC, and COLAB I use the same version 2.6.0 of tensorflow.

image

/usr/local/lib/python3.7/dist-packages/requests/__init__.py:91: RequestsDependencyWarning: urllib3 (1.26.7) or chardet (3.0.4) doesn't match a supported version!
  RequestsDependencyWarning)
export: data=yolo5/data/coco128.yaml, weights=yolo5/best_yolo5s.pt, imgsz=[640, 640], batch_size=1, device=cpu, half=False, inplace=False, train=False, optimize=False, int8=False, dynamic=False, simplify=False, opset=13, topk_per_class=100, topk_all=100, iou_thres=0.45, conf_thres=0.25, include=['tflite']
YOLOv5 πŸš€ 732439b torch 1.9.0+cu111 CPU

[2021-10-12 12:12:25.539 5e560cd0409f:190 INFO utils.py:27] RULE_JOB_STOP_SIGNAL_FILENAME: None
[2021-10-12 12:12:25.590 5e560cd0409f:190 INFO profiler_config_parser.py:111] Unable to find config at /opt/ml/input/config/profilerconfig.json. Profiler is disabled.
Fusing layers... 
Model Summary: 275 layers, 7064511 parameters, 0 gradients

PyTorch: starting from yolo5/best_yolo5s.pt (14.4 MB)

TensorFlow saved_model: starting export with tensorflow 2.6.0...

                 from  n    params  module                                  arguments                     
  0                -1  1      3520  models.common.Focus                     [3, 32, 3]                    
2021-10-12 12:12:29.578868: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected
2021-10-12 12:12:29.578937: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (5e560cd0409f): /proc/driver/nvidia/version does not exist

TensorFlow saved_model: export failure: no matching TensorFlow activation found for SiLU()

TensorFlow Lite: starting export with tensorflow 2.6.0...

TensorFlow Lite: export failure: 'NoneType' object has no attribute 'call'

Export complete (4.21s)
Results saved to /content/model-training/src/yolo5
Visualize with https://netron.app

The SiLU layer I found is: Maybe it should be torch.nn.SiLU

# SiLU https://arxiv.org/pdf/1606.08415.pdf ---
class SiLU(nn.Module):  # export-friendly version of nn.SiLU()
    @staticmethod
    def forward(x):
        return x * torch.sigmoid(x)
alexiej commented 3 years ago

When I comment in export.py line: 385

            if isinstance(m.act, nn.SiLU):
                # m.act = SiLU()
                print("ACTIVATION", m.act)

The model was exported without issues. I need to verify if it works as original, but I think this was the problem of the export.

glenn-jocher commented 3 years ago

@alexiej Colab export to TFLite works correctly for me, I'm not able to reproduce your issue:

Screen Shot 2021-10-12 at 12 25 59 PM

We've created a few short guidelines below to help users provide what we need in order to get started investigating a possible problem.

How to create a Minimal, Reproducible Example

When asking a question, people will be better able to provide help if you provide code that they can easily understand and use to reproduce the problem. This is referred to by community members as creating a minimum reproducible example. Your code that reproduces the problem should be:

In addition to the above requirements, for Ultralytics to provide assistance your code should be:

If you believe your problem meets all of the above criteria, please close this issue and raise a new one using the πŸ› Bug Report template and providing a minimum reproducible example to help us better understand and diagnose your problem.

Thank you! πŸ˜ƒ

github-actions[bot] commented 2 years ago

πŸ‘‹ Hello, this issue has been automatically marked as stale because it has not had recent activity. Please note it will be closed if no further activity occurs.

Access additional YOLOv5 πŸš€ resources:

Access additional Ultralytics ⚑ resources:

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 YOLOv5 πŸš€ and Vision AI ⭐!