Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export to saved_model with --nms flag yields incorrect/incomplete results. #7205

Closed
1 of 2 tasks
tylertroy opened this issue Mar 29, 2022 · 4 comments · Fixed by #7228
Closed
1 of 2 tasks

export to saved_model with --nms flag yields incorrect/incomplete results. #7205

tylertroy opened this issue Mar 29, 2022 · 4 comments · Fixed by #7228
Labels
bug Something isn't working

Comments

@tylertroy
Copy link

Search before asking

  • I have searched the YOLOv5 issues and found no similar bug report.

YOLOv5 Component

Export

Bug

Relates to two issues resulting from exporting exporting to saved_model with the --nms flag set.

  1. Output from model has no data for classes and scores
  2. 100 boxes are returned with --nms flag compared to 4 boxes (2 persons, 2 ties) without --nms flag

After exporting with the command

python export.py --weights yolov5s.pt --include saved_model --nms

The output from saved_model_cli shows the following inputs/outputs

signature_def['serving_default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['x'] tensor_info:
        dtype: DT_FLOAT
        shape: (1, 640, 640, 3)
        name: serving_default_x:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['output_0'] tensor_info:
        dtype: DT_FLOAT
        shape: (1, 100, 4)
        name: PartitionedCall:0
  Method name is: tensorflow/serving/predict
WARNING:absl:Importing a function (__inference_pruned_7064) with ops with unsaved custom gradients. Will likely fail if a gradient is requested.

This indicates that the output is a tensor of 100 rows of bbox data. Visualization of these output boxes shows:

python export.py --weights yolov5s.pt --include saved_model --nms
python test_serve.py  # Here test_serve uses the tensroflow serve restful API

image

I have confirmed that the export function works perfectly well with no nms when doing inference with detect.py

Output from model with no nms using:

python export.py --weights yolov5s.pt --include saved_model
python detect.py --weights yolov5s_saved_model/ --source data/images/zidane.jpg

image

Environment

  • YOLOv5 🚀 v6.1-74-gcf4f3c3 torch 1.11.0+cu113 CPU
  • Ubuntu 18.04 LTS
  • Python 3.8

Minimal Reproducible Example

No response

Additional

No response

Are you willing to submit a PR?

  • Yes I'd like to help by submitting a PR!
@tylertroy tylertroy added the bug Something isn't working label Mar 29, 2022
@tylertroy tylertroy changed the title export to saved_model with --nms flag yields incorrect results. export to saved_model with --nms flag yields incorrect/incomplete results. Mar 29, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Mar 29, 2022

👋 Hello @tylertroy, 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 support@ultralytics.com.

Requirements

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

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

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.

@tylertroy
Copy link
Author

tylertroy commented Mar 29, 2022

I have found part 2 of my issue was due to operator error whereby input was not normalized to 255. I am now getting sensible output (See rectified output image below) but part 1 whereby no classes or scores are returned still remains an issue.

image

Edit: I will note that the ties have not been detected but it's possible that there is a difference in thresholds between detect.py and what is exported.

@tylertroy
Copy link
Author

I managed to solve the part 1 issue with the following edit to export.py

diff --git a/export.py b/export.py
index 7517dc4..0404b25 100644
--- a/export.py
+++ b/export.py
@@ -276,7 +276,7 @@ def export_saved_model(model, im, file, dynamic,
             m = m.get_concrete_function(spec)
             frozen_func = convert_variables_to_constants_v2(m)
             tfm = tf.Module()
-            tfm.__call__ = tf.function(lambda x: frozen_func(x)[0], [spec])
+            tfm.__call__ = tf.function(lambda x: frozen_func(x)[:4], [spec])
             tfm.__call__(im)
             tf.saved_model.save(
                 tfm,

And this results in the following inputs/outputs as visualized with

$ saved_model_cli show --dir yolov5s_saved_model/ --signature_def serving_default --tag serve
The given SavedModel SignatureDef contains the following input(s):
  inputs['x'] tensor_info:
      dtype: DT_FLOAT
      shape: (1, 640, 640, 3)
      name: serving_default_x:0
The given SavedModel SignatureDef contains the following output(s):
  outputs['output_0'] tensor_info:
      dtype: DT_FLOAT
      shape: (1, 100, 4)
      name: PartitionedCall:0
  outputs['output_1'] tensor_info:
      dtype: DT_FLOAT
      shape: (1, 100)
      name: PartitionedCall:1
  outputs['output_2'] tensor_info:
      dtype: DT_FLOAT
      shape: (1, 100)
      name: PartitionedCall:2
  outputs['output_3'] tensor_info:
      dtype: DT_INT32
      shape: (1)
      name: PartitionedCall:3
Method name is: tensorflow/serving/predict

I'm still having an issue whereby output is different between detect.py and results from tfserve but I will raise this in a separate issue if I can't find a solution in the existing issues.

I will leave closing of this issue to the authors in case they would like to incorporate the fix in export.py.

@glenn-jocher
Copy link
Member

glenn-jocher commented Mar 30, 2022

@tylertroy hi, thank you for your feature suggestion on how to improve YOLOv5 🚀! It looks like you've uncovered a bug and implemented and tested a fix. Would you like to submit a PR to receive credit for this?

Please see our ✅ Contributing Guide to get started.

@glenn-jocher glenn-jocher linked a pull request Mar 31, 2022 that will close this issue
glenn-jocher added a commit that referenced this issue Mar 31, 2022
@glenn-jocher glenn-jocher removed the TODO label Mar 31, 2022
glenn-jocher added a commit that referenced this issue Mar 31, 2022
* SavedModel TF Serve Fix

Fix for #7205 proposed by @tylertroy

* Update export.py
BjarneKuehl pushed a commit to fhkiel-mlaip/yolov5 that referenced this issue Aug 26, 2022
* SavedModel TF Serve Fix

Fix for ultralytics#7205 proposed by @tylertroy

* Update export.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants