YOLO11 classifier not padding during resize?

I’m investigating some inference discrepancies between a YOLO11m-cls YOLO model and its exported ONNX equivalent. In the course of debugging, I added a few lines to the BasePredictor inference code around line 260 (ultralytics/ultralytics/engine/predictor.py at main · ultralytics/ultralytics · GitHub):

              import matplotlib.pyplot as plt
              import time
              now = time.strftime('%H-%M-%S')
              imnp = np.uint8(255*im[0].cpu().numpy().transpose((1, 2, 0)))
              plt.imsave(f'some/directory/check{now}.png', imnp)

And this confirmed that when i pass a non-square image for inference with code like:

    yolo_model = YOLO(yolo_checkpoint)
    yolo_res = yolo_model(test_image_file, imgsz=input_shape, verbose=False)

The image gets cropped, not padded.

From reading the ultralytics documentation, I was under the impression that the intended behavior is for images to be square-padded when an imgsz argument is passed. Have I misunderstood something? Is there a built-in way to enforce square padding during classifier inference?

Thanks!

Oh yeah, and after discovering this, I went back to inspect some output from custom classifier training more closely, and the sample ‘train_batch0.jpg’-type files showed the same was happening during training. Probably to the detriment of this particular training job. So a way to enforce square padding during training would be greatly appreciated as well.

Classification models use center crop.

You can try this to use padding: How to disable auto augument option in training yolov11 classification model? · Issue #19389 · ultralytics/ultralytics · GitHub

This does not work during inference. I’m looking at modifying the classify_transforms function (ultralytics/ultralytics/data/augment.py at 3fceec57be02140c5d6c5779b0310d4496c3aa90 · ultralytics/ultralytics · GitHub) in a similar manner.

Beyond the immediate fix, it seems like this is something that should be very clear in the documentation – right now if you search how imgsz works, all doc references just say images are padded and resized. If classifier transforms work differently, it should be noted here: Classify - Ultralytics YOLO Docs

You could manually preprocess the image and pass the tensor instead. It would skip preprocessing and use your preprocessed tensor.

There’s a PR for non-square inference with classification models.