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

YOLOv5 Output Size Issue #13008

Closed
1 task done
lllittleX opened this issue May 14, 2024 · 6 comments
Closed
1 task done

YOLOv5 Output Size Issue #13008

lllittleX opened this issue May 14, 2024 · 6 comments
Labels
question Further information is requested

Comments

@lllittleX
Copy link

lllittleX commented May 14, 2024

Search before asking

Question

I want to convert the results to ONNX first, and then to RKNN. However, when testing the ONNX model, I found that my output sizes are as follows:
torch.Size([1, 15120, 133])
torch.Size([1, 3, 48, 80, 133])
torch.Size([1, 3, 24, 40, 133])
torch.Size([1, 3, 12, 20, 133])
But from what I understand, the usual size is 80,80 instead of 48,80. I would like to ask if this is normal?

Additional

No response

@lllittleX lllittleX added the question Further information is requested label May 14, 2024
Copy link
Contributor

github-actions bot commented May 14, 2024

👋 Hello @lllittleX, 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

@lllittleX hello! Thanks for reaching out with your question about the output size from the YOLOv5 model when converted to ONNX.

The output sizes you are seeing (e.g., torch.Size([1, 3, 48, 80, 133])) depend on the input image size you are using and the architecture of the model. Normally, YOLOv5 uses a grid that scales down from the input size by factors of 32, 16, and 8. If your input size isn't a multiple of these, the resulting grid sizes could appear unconventional, as is likely the case with a dimension of 80 instead of the usual 40 or 160, etc.

Ensure that your input image size is a multiple of the largest factor (generally 32) to maintain consistent grid dimensions across the different layers. You can adjust this in your initial configuration before training or inference.

Let's verify that input sizes and model configurations align with the expected outputs. Good luck with your conversions! 🚀

@VishnuRao27
Copy link

VishnuRao27 commented Mar 7, 2025

Hi there!
I searched quite a lot for this but I couldnt really find it anywhere, let me give a little bit of context:
I am trying to perform knowledge distillation on yolov5n using yolov5x which are both already trained on my custom dataset containing 2 classes.
I saw this issue #13124 where a tutorial for knowledge distillation was given and I see that my pretrained custom_model.pt when loaded through pytorch (autoshape False) in eval mode gives 2 outputs:

  1. A tensor of shape (batch_size, 25200, num_classes+5) ( [4, 25200, 7] in my case which is supposed to be [x, y, w, h, objectness_score, class_logits...] ? )
  2. A list (of length 3) of tensors of shape (batch_size, 3, 80, 80, num_classes+5), (batch_size, 3, 40, 40, num_classes+5) and (batch_size, 3, 20, 20, num_classes+5). I dont know what the 7 values here correspond to.

The same model in train mode gives only the 2nd output.

At first I thought just stacking them would give me the same tensor as output1 but then I compared the values to see they are completely different even after applying softmax to them.
I experimented by actually applying softmax to the concatenated version of output 2, saving it and comparing it with the values of output1 saved without any modifications.

Now for my problem:
Since knowledge distillation relies on the output probabilities of the teacher model which is in eval mode, I figured that the raw outputs from the teacher can be used directly since it gives the probabilities of each class. But since the student is in the train mode, which gives only output 2, how do I convert this list of tensors of shape (batch_size, 3, x, x, num_classes+5) to a raw logs score like how I see in output1 ?
I do not obviously want to switch to eval mode because I want to use these values to compute the kl_div loss and backpropogate.
I can also just stack the output2 of student and teacher to give something with the shape of output1 and pass it to kl_div but then if the values are not actually the probability distributions of each class then it defeats the purpose of knowledge distillation.

@pderrenger
Copy link
Member

@VishnuRao27 for knowledge distillation in YOLOv5, ensure both teacher and student models are in train mode to access raw feature outputs (output 2). The three tensors in the list correspond to detection layers at different scales. To align outputs:

  1. Reshape each detection layer tensor to [batch, anchors, (xywh + obj + classes)]
  2. Concatenate along the anchors dimension to match the 25200 anchors in eval output
  3. Apply sigmoid to class logits (not softmax) as YOLOv5 uses independent class probabilities

Example reshaping:

student_output = torch.cat([x.view(x.shape[0], -1, x.shape[-1]) for x in train_output], 1)
teacher_output = torch.cat([x.view(x.shape[0], -1, x.shape[-1]) for x in teacher_train_output], 1)

For implementation details, see the YOLOv5 Model class in models/yolo.py which handles output reshaping internally during training.

@VishnuRao27
Copy link

VishnuRao27 commented Mar 7, 2025

Hi @pderrenger ! Thank you for your reply
This surely helps, I was doing it right but applying the wrong function in the end (softmax instead of sigmoid).
I just have one question, I have the teacher model currently in eval mode and I am doing this:

teacher.eval()
with torch.no_grad():
     _, teacher_fm = teacher(images)

This is also the output of the 3 detection layers not concatenated yet right?

Edit: So if I follow the steps, use that code snippet that you shared above and get the outputs and apply sigmoid to them, I will basically get the output1 that is returned along with output 2 in the inference mode right?

@pderrenger
Copy link
Member

Yes, when using teacher.eval(), the teacher_fm output will be the raw detection layers (unconcatenated). Concatenating them via torch.cat([x.view(...) for x in teacher_fm], 1) and applying sigmoid to class/objectness outputs will align with the output1 format. The Model class in models/yolo.py handles this reshaping internally during inference.

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

4 participants