ultralytics / yolov5

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

Exporting trained yolov5 model (trained on custom dataset) to 'saved model' format changes the no. of classes and the name of classes to default coco128 values #13243

Open ssingh17j opened 1 month ago

ssingh17j commented 1 month ago

Search before asking

YOLOv5 Component

Export

Bug

I trained yolov5s model to detect various logos (amazon, ups, fedex etc). The model detects the logos well. The command used for training is: python train.py --weights yolov5s.pt --epoch 100 --data C:\projects\logo_detector\yolov5\datasetv3\data.yaml

The command used for detecting logos is: python detect.py --weights best.pt --source 0

Screenshot of trained yolov5s model detecting the logos: yolov5s model

When I use export.py to convert the above model to saved model format, the model starts giving wrong output. The command used for exporting the model is: python export.py --weights best.pt --data C:\projects\logo_detector\yolov5\datasetv3\data.yaml --include saved_model

The command used for detection of logos using this saved model is: python detect.py --weights best_saved_model --source 0

Screenshot of yolov5s saved model giving wrong output is: yolov5s saved model

As far as I can understand, the model starts giving output according to the default coco128.yaml file. But I have not specified this file in my commands, so I cannot understand the reason behind this behaviour. Please let me know how to get correct output.

Environment

Minimal Reproducible Example

No response

Additional

No response

Are you willing to submit a PR?

github-actions[bot] commented 1 month ago

πŸ‘‹ Hello @ssingh17j, 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 a minimum reproducible example to help us debug it.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results.

Requirements

Python>=3.8.0 with all requirements.txt installed including PyTorch>=1.8. 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

YOLOv5 CI

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

Introducing YOLOv8 πŸš€

We're excited to announce the launch of our latest state-of-the-art (SOTA) object detection model for 2023 - YOLOv8 πŸš€!

Designed to be fast, accurate, and easy to use, YOLOv8 is an ideal choice for a wide range of object detection, image segmentation and image classification tasks. With YOLOv8, you'll be able to quickly and accurately detect objects in real-time, streamline your workflows, and achieve new levels of accuracy in your projects.

Check out our YOLOv8 Docs for details and get started with:

pip install ultralytics
glenn-jocher commented 1 month ago

@ssingh17j hello,

Thank you for reaching out and providing detailed information about the issue you're encountering. It appears that the problem might be related to the class names and number of classes not being correctly transferred during the export process.

To address this, please ensure the following steps:

  1. Verify Data Configuration: Make sure that the data.yaml file used during training is the same one used during export. This file should correctly define the number of classes and their names.

  2. Check Export Command: Your export command looks correct, but let's ensure that the data.yaml is correctly referenced:

    python export.py --weights best.pt --data C:\projects\logo_detector\yolov5\datasetv3\data.yaml --include saved_model
  3. Update to Latest Version: Ensure you are using the latest version of YOLOv5 and its dependencies. Sometimes, issues are resolved in newer releases. You can update your repository and dependencies with:

    git pull
    pip install -r requirements.txt
  4. Validate Exported Model: After exporting, you can validate the exported model to ensure it retains the correct class information. Use the val.py script to check the model's performance:

    python val.py --weights best_saved_model --data C:\projects\logo_detector\yolov5\datasetv3\data.yaml
  5. Inspect the Exported Model: You can use tools like Netron to visualize the exported model and verify that the class names and number of classes are correctly set.

If the issue persists, it might be helpful to debug the exported model. You can load the exported model in TensorFlow and inspect the class names and number of classes to ensure they match your custom dataset.

Here's a small snippet to load and inspect the TensorFlow SavedModel:

import tensorflow as tf

# Load the SavedModel
model = tf.saved_model.load('path/to/best_saved_model')

# Inspect the model's signature
print(model.signatures)

If you find any discrepancies or need further assistance, please feel free to share additional details, and we'll be happy to help you troubleshoot further.