yolov5
yolov5 copied to clipboard
Training on non-square images
Search before asking
- [X] I have searched the YOLOv5 issues and discussions and found no similar questions.
Question
I have images of size 1280x720. I want to use these non-square images for training, my command to train,
python3 segment/train.py --img-size 1280,720 --rect --epochs 20 --data custom_dataset.yaml --weights yolov5s-seg.p t --cfg models/segment/yolov5s-seg.yaml --batch 16 --workers 16 --name yolov5_instance_segmentation_run1
However, I get the error as
train.py: error: argument --imgsz/--img/--img-size: invalid int value: '1280,720'
Additional
No response
👋 Hello @bharathsanthanam94, 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):
-
Notebooks with free GPU:
- Google Cloud Deep Learning VM. See GCP Quickstart Guide
- Amazon Deep Learning AMI. See AWS Quickstart Guide
-
Docker Image. See Docker Quickstart Guide
Status
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
@bharathsanthanam94 hello! It looks like you're trying to train with non-square images by specifying two dimensions for the --img-size
argument. In YOLOv5, you should provide a single integer for --img-size
, which will be used to resize the longest side of your images while maintaining the aspect ratio. The --rect
flag you're using is correct for training with non-square images as it allows for rectangular training.
To train with your 1280x720 images, you can set --img-size
to 1280 if you want to keep the longer dimension as is. The command would look something like this:
python3 segment/train.py --img-size 1280 --rect --epochs 20 --data custom_dataset.yaml --weights yolov5s-seg.pt --cfg models/segment/yolov5s-seg.yaml --batch 16 --workers 16 --name yolov5_instance_segmentation_run1
This will resize your images so that the longest side is 1280 pixels while maintaining the aspect ratio. The --rect
flag ensures that the rectangular shape is preserved during training.
For more detailed information on training with custom data and image sizes, please refer to our documentation. 😊 Happy training!
@glenn-jocher Thank you for very quick response. I trained with the above command. Now I want to export to onnx and eventually to tensorrt to perform inference on the Nvidia jetson AGX Orin.
My question is, does inference using onnx and tensorrt support rectangle images? In this case, my command to export pytorch to onnx is,
python3 export.py --weights best.pt --imgz 1280 --include onnx --data custom_dataset.yaml --device 0 --opset 12 --batch-size 1
is the above command correct? I don't want the inference pipeline to perform any resize operation to make it square. I wonder if adding the size 1280
in the above command would reshape my input image of size 1280x720
to 1280x1280
during inference.
Just FYI, I trained with
python3 segment/train.py --img-size 1280 --rect --epochs 20 --data custom_dataset.yaml --weights yolov5s-seg.pt --cfg models/segment/yolov5s-seg.yaml --batch 16 --workers 16 --name yolov5_instance_segmentation_run1
and using the best model saved as best.pt
to convert to onnx.
@bharathsanthanam94, glad to hear you've successfully trained your model! For exporting to ONNX, the command you've provided is almost correct, but there's a small typo in the --img-size
flag (it should be --img-size
instead of --imgz
). Also, to maintain the aspect ratio during export, you should use the same --img-size
and --rect
flags as you did during training.
Here's the corrected command:
python3 export.py --weights best.pt --img-size 1280 --rect --include onnx --data custom_dataset.yaml --device 0 --opset 12 --batch-size 1
This command will export your model to ONNX format, and with the --rect
flag, it will maintain the aspect ratio of your images during inference, just like during training.
Regarding ONNX and TensorRT inference, they should support rectangular images as long as the model was exported with the correct flags, as shown above. The ONNX model will expect inputs with the same aspect ratio as it was trained on. However, do note that TensorRT might require additional steps to handle dynamic shapes if your input sizes vary during inference.
Remember to check the documentation for any specific details regarding ONNX and TensorRT compatibility and requirements. Happy inferencing on your Jetson AGX Orin! 🚀
python3 export.py --weights best.pt --img-size 1280 --rect --include onnx --data custom_dataset.yaml --device 0 --opset 12 --batch-size 1
Does export.py take "--rect" arguement?
@YigitKaanBingol apologies for the confusion earlier. The export.py
script does not take a --rect
argument. The --rect
flag is specific to training and is not used during the export process. The --img-size
flag in the export.py
script will set the maximum dimension for the ONNX model, and the aspect ratio will be maintained automatically.
Here's the corrected command for exporting your model to ONNX:
python3 export.py --weights best.pt --img-size 1280 --include onnx --data custom_dataset.yaml --device 0 --opset 12 --batch-size 1
This command will export your model with the longest dimension set to 1280 pixels, and the aspect ratio will be preserved. The ONNX model will expect the input size to be consistent with the size used during training.
Thank you for your understanding, and good luck with your deployment! 🌟
@YigitKaanBingol that's correct! --rect argument is not working for export.py. @glenn-jocher Is there a way to export to onnx format that supports non-square image inference?
@bharathsanthanam94 apologies for the confusion earlier. When exporting a YOLOv5 model to ONNX format, the model will be exported with a fixed input size, which is determined by the --img-size
parameter. However, the ONNX model itself does not inherently support dynamic input shapes out-of-the-box.
To perform inference with non-square images, you typically need to preprocess your images to match the input size expected by the model. This often involves resizing and padding the images to maintain the aspect ratio without distortion.
If you require dynamic input shapes for ONNX, you would need to modify the exported model to accept dynamic shapes, which can be more complex and is not directly supported by the export.py
script. This often involves additional steps such as editing the ONNX graph to replace fixed dimensions with symbolic dimensions or using advanced features of the ONNX runtime or the framework you are deploying with (like TensorRT).
For now, you can export your model with the desired input size that matches the aspect ratio of your training data:
python3 export.py --weights best.pt --img-size 1280 --include onnx --data custom_dataset.yaml --device 0 --opset 12 --batch-size 1
This will export your model with an input size that has the longer dimension of 1280 pixels. During inference, you would need to resize your images to this input size while maintaining the aspect ratio.
For deploying models that require dynamic input shapes, you would typically need to look into the specific documentation of the deployment framework you are using (e.g., ONNX Runtime, TensorRT) for guidance on handling dynamic shapes.
I hope this clarifies the situation. If you have further questions, feel free to ask! 🛠️
👋 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:
- Docs: https://docs.ultralytics.com
- HUB: https://hub.ultralytics.com
- Community: https://community.ultralytics.com
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 ⭐