ultralytics / yolov5

YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
49.84k stars 16.13k forks source link

Trained custom set but no detection #1607

Closed waylandc closed 3 years ago

waylandc commented 3 years ago

❔Question

I labelled over 100 custom images using hyperlabel, ran train.py and the generated jpegs in my runs\train\exp17 directory look good. test_batch1_labels.jpeg and test_batch2_labels.jpeg are properly detected/labelled.

Then I run detect on one of the original images contained within the above batch image (so I know that training was able to properly detect) and the image in my runs\detect\exp17 folder shows no labels and the runs\detect\exp\labels folder is empty.

Any help would be appreciated

Additional context

note the weird img size because my images are custom snapshots 882x649 batch 4 because anything else I get CUDA OOM on my 1080ti

$ python train.py --img 882 --rect --batch 4 --epochs 100 --data training\dataset.yaml --cfg training\yolov5s.yaml --weights yolov5s.pt --device 0

$ python detect.py --weights runs\train\exp17\weights\best.pt --source inference\images\1606845911.png --save-txt
glenn-jocher commented 3 years ago

@waylandc you should detect with the same arguments as you trained with for best results. What you are doing above is training/testing and detecting with different settings.

waylandc commented 3 years ago

thanks for quick response @glenn-jocher but I don't understand which arguments you're referring to. The only common argument between train.y and detect.py is "--weights".

glenn-jocher commented 3 years ago

I would suggest you align test.py and detect.py arguments if you expect your results to align between the two.

Train.py trains, it does not produce detections.

Rahul828086 commented 3 years ago

Namaste,

I have same problem as mention above. To train: python train.py --img 640 --batch 16 --epochs 15 --data Custom.yaml --cfg yolov5s.yaml --weights ''

To detect: python detect.py --weights weights\best123.pt

I am using Windows system.

glenn-jocher commented 3 years ago

@Rahul828086 if you are not detecting any objects after training, it's likely your training acheived poor results and you are simply not able to detect anything at the default confidence threshold (0.25). You can reduce --conf to zero untill you start detecting objects, though looking at your command it's likely you may be able to achieve better results by starting from pretraind weights (as the tutorial recommends).

To get started using YOLOv5 best practices please start from the Train Custom Data tutorial:

YOLOv5 Tutorials

Rahul828086 commented 3 years ago

Thank you for the quick reply @glenn-jocher. I was confused with test_batch0_labels.jpg image. it had label so assumed it to be test data. I was able to detect after 500 epochs and through colab. previously I was trying in windows system. Anyways thanks alot for your quick reply.

glenn-jocher commented 3 years ago

@Rahul828086 great!

Rahul828086 commented 3 years ago

@glenn-jocher great work.. Thank you again.

Shashi630 commented 9 months ago

@glenn-jocher trained the model with custom data but it is not detecting anything.. showing >>>>>>>>>>> (no detections) why??? Fusing layers... Model summary: 157 layers, 7015519 parameters, 0 gradients, 15.8 GFLOPs image 1/1 /content/tools.jpg: 384x640 (no detections), 55.5ms

glenn-jocher commented 9 months ago

@Shashi630 it seems like your trained model is not detecting anything. Check your training data and annotations for correct labeling, and consider starting from pre-trained weights for better results. Good luck!

Shashi630 commented 9 months ago

@glenn-jocher \Downloads\darknet-master\darknet-master>cmake -DENABLE_CUDA=ON CMake Warning: No source or binary directory provided. Both will be assumed to be the same as the current working directory, but note that this warning will become a fatal error in future CMake releases.

-- vcpkg not found, toolchain not defined, using integrated libs on win32 Darknet_VERSION: 0.2.5.7 -- Selecting Windows SDK version 10.0.22621.0 to target Windows 10.0.22631. -- CUDA_PATH: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0 -- CUDACXX: CMake Error at CMakeLists.txt:188 (message): CUDA not found, please build explicitly with -DENABLE_CUDA=OFF if you do not want CUDA.

-- Configuring incomplete, errors occurred!

how to fix this

glenn-jocher commented 9 months ago

@Shashi630 it looks like you are encountering a CUDA configuration error while trying to build darknet with CMake. It appears that CUDA is not being found properly. You may want to ensure that your CUDA installation is correctly configured and that the paths are set up correctly in your system environment variables. You can also consider explicitly building with -DENABLE_CUDA=OFF if you do not need CUDA. Good luck!

Shashi630 commented 8 months ago

@glenn-jocher Screenshot 2024-01-01 111135

please help me to solve this error..

glenn-jocher commented 8 months ago

@Shashi630 i'm sorry for the confusion, but as the author and maintainer of the Ultralytics YOLOv5 repository, I can only provide support for YOLOv5-related issues. The screenshot and error you're referring to seem to be related to the Darknet framework, which is a separate project from YOLOv5.

For issues with Darknet, I recommend checking the Darknet GitHub repository's issues section or seeking help on forums dedicated to Darknet. Make sure to provide detailed information about the error and the steps leading up to it to get the best possible assistance from the community. Good luck!

alfiansyahhidayat commented 8 months ago

hello, I have the same problem, my image is not detected Screenshot 2024-01-17 204708

glenn-jocher commented 8 months ago

@alfiansyahhidayat hello! If your YOLOv5 model is not detecting objects in an image, here are a few steps you can take to troubleshoot:

  1. Check the Confidence Threshold: Ensure that the confidence threshold is not set too high during detection. You can lower it using the --conf-thres flag in the detect.py script.

  2. Review Training Data: Make sure your training data is correctly labeled and that the model has been trained for a sufficient number of epochs.

  3. Inspect Model Weights: Confirm that you are using the correct model weights file for detection, typically the best.pt file generated after training.

  4. Analyze Model Performance: Review the training logs and validation results to ensure your model has learned effectively. Look for signs of overfitting or underfitting.

  5. Image Preprocessing: Verify that the image preprocessing during detection matches the preprocessing used during training.

  6. Model Architecture: If you've made changes to the model architecture, ensure they are compatible with the detection process.

If you continue to face issues, please provide more details about your training process, including the command you used to run the detection, and any relevant output messages. This will help in diagnosing the problem more effectively.

alfiansyahhidayat commented 8 months ago

I just followed the tutorial on YouTube but when I run it it turns out it's not the same, here it is on YouTube https://www.youtube.com/watch?v=tFNJGim3FXw

glenn-jocher commented 8 months ago

@alfiansyahhidayat i'm sorry for any confusion, but as per the guidelines, I cannot view or comment on external content such as YouTube videos. However, if you're experiencing issues with YOLOv5 detection, I recommend closely following the official Ultralytics documentation and ensuring that all steps are executed correctly.

If you've followed a tutorial and the results are not as expected, it's possible that there might be discrepancies between the tutorial and the current state of the YOLOv5 repository, as it is actively maintained and updated.

Please ensure that:

If you continue to experience issues, you can open an issue on the YOLOv5 GitHub repository with detailed information about the steps you've taken, the commands you've run, and any error messages you've encountered. This will help the community to assist you more effectively.

Archangel31 commented 3 months ago

I am getting the similar issue while using the pretrained model(yolov5xu), model is not able to detect the objects from my input video. I added some extra custom training samples while generating the weights, which is somehow resulting to "no detections" issue.

glenn-jocher commented 3 months ago

Hello @Archangel31,

Thank you for reaching out and providing details about your issue. Let's work together to resolve this.

First, let's ensure that your environment is up-to-date. Please verify that you are using the latest versions of torch and the YOLOv5 repository. You can update them using the following commands:

pip install --upgrade torch
git pull https://github.com/ultralytics/yolov5

Next, it would be helpful to have a minimum reproducible example to better understand and diagnose the issue. This includes the exact commands you used for training and detection, as well as any relevant configuration files. You can refer to our guide on creating a minimum reproducible example here: Minimum Reproducible Example.

In the meantime, here are a few things you can check:

  1. Confidence Threshold: Ensure that the confidence threshold is not set too high. You can adjust it using the --conf-thres flag in the detect.py script. For example:

    python detect.py --weights runs/train/exp/weights/best.pt --source your_video.mp4 --conf-thres 0.25
  2. Training Data: Verify that your custom training samples are correctly labeled and that the annotations are in the correct format.

  3. Model Weights: Ensure that you are using the correct weights file generated after training. Typically, this would be the best.pt file located in the runs/train/exp/weights/ directory.

  4. Image Preprocessing: Confirm that the preprocessing steps during detection match those used during training.

  5. Training Logs: Review the training logs to check for any anomalies or indications that the model might not have learned effectively.

If you provide the commands and configurations you used, we can delve deeper into the issue. The YOLOv5 community and the Ultralytics team are here to support you, so don't hesitate to share more details.

Looking forward to your response! 😊

Archangel31 commented 3 months ago

Hello @Archangel31,

Thank you for reaching out and providing details about your issue. Let's work together to resolve this.

First, let's ensure that your environment is up-to-date. Please verify that you are using the latest versions of torch and the YOLOv5 repository. You can update them using the following commands:

pip install --upgrade torch
git pull https://github.com/ultralytics/yolov5

Next, it would be helpful to have a minimum reproducible example to better understand and diagnose the issue. This includes the exact commands you used for training and detection, as well as any relevant configuration files. You can refer to our guide on creating a minimum reproducible example here: Minimum Reproducible Example.

In the meantime, here are a few things you can check:

  1. Confidence Threshold: Ensure that the confidence threshold is not set too high. You can adjust it using the --conf-thres flag in the detect.py script. For example:
    python detect.py --weights runs/train/exp/weights/best.pt --source your_video.mp4 --conf-thres 0.25
  2. Training Data: Verify that your custom training samples are correctly labeled and that the annotations are in the correct format.
  3. Model Weights: Ensure that you are using the correct weights file generated after training. Typically, this would be the best.pt file located in the runs/train/exp/weights/ directory.
  4. Image Preprocessing: Confirm that the preprocessing steps during detection match those used during training.
  5. Training Logs: Review the training logs to check for any anomalies or indications that the model might not have learned effectively.

If you provide the commands and configurations you used, we can delve deeper into the issue. The YOLOv5 community and the Ultralytics team are here to support you, so don't hesitate to share more details.

Looking forward to your response! 😊

Thanks for your response!

This is the code I used to generate weights:

!yolo detect mode=train model=yolov5x.pt data='/content/drive/MyDrive/football-players-detection-1/data.yaml' epochs=50 imgsz=640

where data.yaml file looks like this:

names:
- ball
- goalkeeper
- player
- referee
nc: 4
# roboflow:
#   license: CC BY 4.0
#   project: football-players-detection-3zvbc
  # url: https://universe.roboflow.com/roboflow-jvuqo/football-players-detection-3zvbc/dataset/1
#   version: 1
#   workspace: roboflow-jvuqo
test: ../test/images
train: football-players-detection-1/train/images
val: football-players-detection-1/valid/images
glenn-jocher commented 3 months ago

Hello @Archangel31,

Thank you for providing the details and the code snippet! Let's dive into this together to resolve the issue.

First, let's ensure that your environment is up-to-date. Please verify that you are using the latest versions of torch and the YOLOv5 repository. You can update them using the following commands:

pip install --upgrade torch
git pull https://github.com/ultralytics/yolov5

Next, let's review your training command and data.yaml file. Everything seems to be in order, but here are a few additional checks and suggestions:

  1. Confidence Threshold: Ensure that the confidence threshold is not set too high during detection. You can adjust it using the --conf-thres flag. For example:

    python detect.py --weights runs/train/exp/weights/best.pt --source your_video.mp4 --conf-thres 0.25
  2. Training Data: Verify that your custom training samples are correctly labeled and that the annotations are in the correct format. Ensure that the paths in your data.yaml file are correct and accessible.

  3. Model Weights: Ensure that you are using the correct weights file generated after training. Typically, this would be the best.pt file located in the runs/train/exp/weights/ directory.

  4. Image Preprocessing: Confirm that the preprocessing steps during detection match those used during training. This includes image resizing and normalization.

  5. Training Logs: Review the training logs to check for any anomalies or indications that the model might not have learned effectively. Look for metrics such as loss, precision, and recall to ensure the model is performing well.

  6. Minimum Reproducible Example: If the issue persists, please provide a minimum reproducible example, including the exact commands you used for training and detection, as well as any relevant configuration files. This will help us better understand and diagnose the issue. You can refer to our guide on creating a minimum reproducible example here: Minimum Reproducible Example.

Here's an example command for running detection with a custom confidence threshold:

python detect.py --weights runs/train/exp/weights/best.pt --source your_video.mp4 --conf-thres 0.25 --save-txt

If you continue to face issues, please share the specific commands and configurations you used, along with any error messages or logs. The YOLOv5 community and the Ultralytics team are here to support you, so don't hesitate to share more details.

Looking forward to your response! 😊

AkobirNarzullaev commented 1 month ago

Hi! I trained yolov5 model to detect passport from an image using this code: python yolov5/train.py --img 320 --workers 0 --batch 32 --epochs 50 --data yolov5/dataset.yaml --weights yolov5s.pt If I do inference it works only when I set the image size to 320: python yolov5/detect.py --weights yolov5/runs/train/exp7/weights/best.pt --img 320 --source images/photo_2024-06-21_08-57-45.jpg. I am unable to use it inside my python script it just does not detect. How can I fix this? Here is my python script:

import torch
import pathlib
import platform
import cv2
import numpy as np

# Adjust path handling for different platforms
plt = platform.system()
if plt == 'Linux': pathlib.WindowsPath = pathlib.PosixPath

# Path to your trained YOLOv5 model
MODEL_PATH = 'yolov5/runs/train/exp7/weights/best.pt'

# Load the YOLOv5 model
model = torch.hub.load('ultralytics/yolov5', 'custom', path=MODEL_PATH)

# Load the image
img = cv2.imread("images/photo_2024-06-21_08-57-45.jpg")

# Resize the image to 640x640 pixels
img_resized = cv2.resize(img, (640, 640))

# Convert image to RGB (YOLOv5 expects RGB images)
img_rgb = cv2.cvtColor(img_resized, cv2.COLOR_BGR2RGB)

# Normalize the image to the range [0, 1]
img_rgb = img_rgb / 255
glenn-jocher commented 1 month ago

@AkobirNarzullaev hi! It looks like your model was trained with an image size of 320, so it performs best with that size. In your Python script, try resizing the image to 320x320 instead of 640x640 before inference. Here's the adjusted code:

# Resize the image to 320x320 pixels
img_resized = cv2.resize(img, (320, 320))

This should help your model detect objects as expected. If the issue persists, please ensure you're using the latest version of YOLOv5.

AkobirNarzullaev commented 4 weeks ago

@glenn-jocher It did not work. I did the same thing and I am using the latest version of yolov5

glenn-jocher commented 4 weeks ago

Thank you for confirming. Please ensure your image size during inference matches the training size (320x320). If the issue persists, could you share any error messages or logs for further diagnosis?