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

How to change training input image size? #12966

Open
1 task done
EXGHLI opened this issue Apr 26, 2024 · 8 comments
Open
1 task done

How to change training input image size? #12966

EXGHLI opened this issue Apr 26, 2024 · 8 comments
Labels
question Further information is requested

Comments

@EXGHLI
Copy link

EXGHLI commented Apr 26, 2024

Search before asking

Question

How to change the training input image size from the default 640640 to 1248384?

I have tried changing the imgsz parameter default value of 640 in train.py to default=(1248, 384), as shown below
parser.add_argument("--imgsz", "--img", "--img-size", type=int, default=(1248, 384), help="train, val image size (pixels)")

But the following problems occurred
Traceback (most recent call last):
File "D:\PycharmProjects\yolov5_attention\train.py", line 849, in
main(opt)
File "D:\PycharmProjects\yolov5_attention\train.py", line 624, in main
train(opt.hyp, opt, device, callbacks)
File "D:\PycharmProjects\yolov5_attention\train.py", line 254, in train
train_loader, dataset = create_dataloader(
File "D:\PycharmProjects\yolov5_attention\utils\dataloaders.py", line 181, in create_dataloader
dataset = LoadImagesAndLabels(
File "D:\PycharmProjects\yolov5_attention\utils\dataloaders.py", line 561, in init
self.mosaic_border = [-img_size // 2, -img_size // 2]
TypeError: bad operand type for unary -: 'list'

I also tried another method
python train.py --imgsz 1248 384 --rect

But the following problems occurred
usage: train.py [-h] [--weights WEIGHTS] [--cfg CFG] [--data DATA] [--hyp HYP] [--epochs EPOCHS] [--batch-size BATCH_SIZE] [--imgsz IMGSZ] [--rect] [--resume [RESUME]] [--nosave]
[--noval] [--noautoanchor] [--noplots] [--evolve [EVOLVE]] [--evolve_population EVOLVE_POPULATION] [--resume_evolve RESUME_EVOLVE] [--bucket BUCKET]
[--cache [CACHE]] [--image-weights] [--device DEVICE] [--multi-scale] [--single-cls] [--optimizer {SGD,Adam,AdamW}] [--sync-bn] [--workers WORKERS]
[--project PROJECT] [--name NAME] [--exist-ok] [--quad] [--cos-lr] [--label-smoothing LABEL_SMOOTHING] [--patience PATIENCE] [--freeze FREEZE [FREEZE ...]]
[--save-period SAVE_PERIOD] [--seed SEED] [--local_rank LOCAL_RANK] [--entity ENTITY] [--upload_dataset [UPLOAD_DATASET]] [--bbox_interval BBOX_INTERVAL]
[--artifact_alias ARTIFACT_ALIAS] [--ndjson-console] [--ndjson-file]
train.py: error: unrecognized arguments: 384

Additional

No response

@EXGHLI EXGHLI added the question Further information is requested label Apr 26, 2024
Copy link
Contributor

👋 Hello @EXGHLI, 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
Copy link
Member

Hello! It looks like you’re trying to adjust the input image size for training in YOLOv5 🚀. The way to do this is through the command line rather than modifying train.py directly. For a non-square image size like 1248x384, you were on the right track with using the --imgsz argument, but the syntax needs a little adjustment.

To specify a custom image size, you can use the --imgsz option followed by your desired dimensions, but you should pass them as a single argument quoted to ensure they are correctly interpreted as a tuple by the parser. For your case:

python train.py --imgsz '1248 384' --rect

This tells YOLOv5 to use an input resolution of 1248x384 pixels for both training and validation. The --rect flag is used for rectangular training, which matches your non-square dimension.

Remember, adjusting the input size can affect the model's performance and computational requirements, so it's good to experiment to find the best size for your particular application.

Happy training! 🌟

@EXGHLI
Copy link
Author

EXGHLI commented Apr 27, 2024

你好!看起來您正在嘗試調整輸入圖像大小以在 YOLOv5 🚀 中進行訓練。實現的方法是透過命令列而不是train.py直接修改。對於像 1248x384 這樣的非方形影像大小,使用參數的做法是正確的--imgsz,但語法需要一些調整。

若要指定自訂影像大小,您可以使用--imgsz後跟所需尺寸的選項,但您應該將它們作為引用的單一參數傳遞,以確保解析器將它們正確解釋為元組。對於你的情況:

python train.py --imgsz '1248 384' --rect

這告訴 YOLOv5 使用 1248x384 像素的輸入解析度進行訓練和驗證。此--rect標誌用於矩形訓練,與您的非正方形尺寸相符。

請記住,調整輸入大小可能會影響模型的效能和計算要求,因此最好進行試驗以找到適合您的特定應用程式的最佳大小。

快樂訓練! 🌟

Thank you for your reply.
How do I modify the parameters?
parser.add_argument("--imgsz", "--img", "--img-size", type=int, default=640, help="train, val image size (pixels)")
Taking the original code as an example, how should I modify it?

@glenn-jocher
Copy link
Member

你好!如果您想通過代码而非命令行参数调整图像尺寸,您需要留意的是--imgsz参数在代码中被定义接受整型,而非元组。为了适应非方形尺寸(例如1248x384),您不能直接通过修改train.py文件中的parser.add_argument的默认值来实现这一点。

然而,您可以通过命令行传递单一尺寸的方法适应该需求,将图像调整到一个尺寸,然后使用--rect参数以矩形方式进行训练,尽量减少空白填充区域。命令行参数如下:

python train.py --imgsz 640 --rect

如果您坚持要通过代码更改(不推荐这样做,因为命令行更灵活),您需要在数据加载时自己处理图像尺寸的调整,这意味着您可能需要深入dataloaders.py和相关文件,手动实现对图像的调整逻辑。

祝您训练愉快!如果有其他问题,欢迎继续提问。

@EXGHLI
Copy link
Author

EXGHLI commented Apr 27, 2024

你好--imgsztrain.py​中的parser.add_argument預設值來實現這一點。

然而,您可以透過命令列單一輸入尺寸的方法訓練適應需求,將影像調整到一個尺寸,然後使用--rect參數以擬合方式進行,盡量減少空白填充區域。

python train.py --imgsz 640 --rect

如果您堅持要透過程式碼更改(不建議這樣做,因為命令列更靈活),您需要在資料加載時自己處理圖像尺寸的調整,這意味著您可能需要深入dataloaders.py和相關文件,手動實現對圖像的調整邏輯。

祝您訓練愉快!

Sorry, I'm still confused.
Do you mean that if I enter the following command, the input image size will be specified as 1248*1248, and the --rect parameter is used to reduce the blank area between 384 and 1248?

python train.py --imgsz 1248 --rect

@glenn-jocher
Copy link
Member

你好!对不起让你感到困惑了。我来澄清一下。

当你使用--imgsz 1248 --rect命令行参数时,你指定的是训练和验证图像的最大边长尺寸为1248像素。--rect参数确实是用来以矩形训练,帮助减少图像周围的空白填充区域。这样,YOLOv5会动态调整图像以保持原有比例,最大边为1248像素,而另一边则按比例缩放,这样可以有效减少模型处理不必要的空白区域,提高训练效率和检测性能。

例如,如果原始图像是1248x384,使用--imgsz 1248 --rect会保持这个大小比例,直接用于训练,而不是将图像扩展到1248x1248。

希望这次解释能帮助您更好地理解!🌟

@EXGHLI
Copy link
Author

EXGHLI commented Apr 28, 2024

你好!对不起让你感到困惑了。我来澄清一下。

当你使用--imgsz 1248 --rect命令行参数时,你指定的是训练和验证图像的最大边长尺寸为1248像素。--rect参数确实是用来以矩形训练,帮助减少图像周围的空白填充区域。这样,YOLOv5会动态调整图像以保持原有比例,最大边为1248像素,而另一边则按比例缩放,这样可以有效减少模型处理不必要的空白区域,提高训练效率和检测性能。

例如,如果原始图像是1248x384,使用--imgsz 1248 --rect会保持这个大小比例,直接用于训练,而不是将图像扩展到1248x1248。

希望这次解释能帮助您更好地理解!🌟

Thank you very much for your teaching!
Your assistance is very important to me.

@glenn-jocher
Copy link
Member

You're very welcome! I'm glad to have been able to help clarify things for you. If you have any more questions or need further assistance down the line, please don't hesitate to reach out. Happy training with YOLOv5! 🌟

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants