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 Quantization #1252

Closed
gkoi00 opened this issue May 31, 2023 · 15 comments
Closed

YOLOv5 Quantization #1252

gkoi00 opened this issue May 31, 2023 · 15 comments

Comments

@gkoi00
Copy link

gkoi00 commented May 31, 2023

I am trying to load and quantize the YOLOv5 model, using the following code:

`device = torch.device('cpu')

model = torch.hub.load('ultralytics/yolov5', 'yolov5n', pretrained=True)

rand_in = torch.randn(1, 3, 640, 640)
quantizer = torch_quantizer(quant_mode, model, rand_in)
quantized_model = quantizer.quant_model
quantized_model = quantized_model.to(device)

quantized_model.eval()
results = quantized_model(rand_in)

if quant_mode == 'calib':
quantizer.export_quant_config()
if quant_mode == 'test':
quantizer.export_xmodel(deploy_check=False)`

The problem occurs when calling the model inference function, specifically i get the following error:

image

@wgshun
Copy link

wgshun commented Jun 1, 2023

The model should be loaded using the source code of the "ultratics/yolov5" project, and cannot be loaded using the "torch. hub. load" method; You also need to modify the 'forward' function in the yolo. py script:

def forward(self, x):
        for i in range(self.nl):
            x[i] = self.m[i](x[i])  # conv
        return x

@gkoi00
Copy link
Author

gkoi00 commented Jun 1, 2023

Thank you very much!

@gkoi00 gkoi00 closed this as completed Jun 1, 2023
@zijian98
Copy link

Hi, may I know how do you load the model using source code instead of using torch.hub?

Thank you.

@gkoi00
Copy link
Author

gkoi00 commented Jun 21, 2023

Hello @zijian98!
They way i did it was to first clone the yolov5 repo and install requirements.txt like so:

git clone https://github.com/ultralytics/yolov5 
cd yolov5
pip install -r requirements.txt

Then import the DetectMultiBackend() function from yolov5/models/common.py
and use it to load either a model you trained yourself, or let it load a pretrained model.

from yolov5.models.common import DetectMultiBackend

model = DetectMultiBackend(weights='path/to/model')
#or
model = DetectMultiBackend(weights='yolov5s.pt')

The rest of the code I left he same.

@Zach227
Copy link

Zach227 commented Jun 26, 2023

I followed the same method described in this thread to quantize yolov5 and it successfully converted to xmodel. However it gave one warning at the end:

WARNING: Logging before InitGoogleLogging() is written to STDERR
W20230626 11:18:07.572243  3639 tool_function.cpp:171] [UNILOG][WARNING] The operator named DetectMultiBackend__DetectMultiBackend_DetectionModel_model__Focus_model__Focus_0__Conv_conv__SiLU_act__input_5, type: aten::silu_, is not defined in XIR. 
XIR creates the definition of this operator automatically. You should specify the shape and the data_type of the output tensor of this operation by set_attr("shape", std::vector<int>) and set_attr("data_type", std::string)

nn.SiLU() is the default activation function for the Conv() class in the yolov5 source code. It is used many times in my model and it seems bad that this function is not defined in XIR. Is this an important warning to address? If so how would I fix it?

@gkoi00
Copy link
Author

gkoi00 commented Jun 26, 2023

Yes, the SiLU function is not supported (as seen here https://docs.xilinx.com/r/en-US/ug1414-vitis-ai/Currently-Supported-Operators). You need to replace it with LeakyReLU. The specific files that need to be modified are the common.py and experimental.py files, located in yolov5/models/, which are modified as follows.

#old
self.act = nn.SiLU
#new
self.act = nn.LeakyReLU(0.1, inplace=True)

After these changes you have to train this modified network.
I used Google Colab and it took approximately 4 hrs using this command:
!python train.py --img 640 --batch 16 --epochs 3 --data coco.yaml --weights yolov5n.pt
i got from here https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data

You have done it right if after you compile the model for your specific board, you get something like this with only 1 DPU subgraph:
image

@Zach227
Copy link

Zach227 commented Jun 28, 2023

Thank you @gkoi00 @wgshun! That worked great. Would you also know the best way to interpret the output of the quantized model? After feeding an image through, I used the yolo non_max_suppression() function on the output. This should give a list for each detected object which contains the bounding box coordinates, a confidence score, and a classification. However, the results of this function didn't make sense which I assume is due to the fact that we changed the forward function in yolo.py.

Here is my quantization script which includes a custom dataset class and the non max suppression function: quantize.py

Any help in this area would be much appreciated.

@zijian98
Copy link

zijian98 commented Jul 3, 2023 via email

@yadus97
Copy link

yadus97 commented Sep 21, 2023

Hi may I know how you found out the application docker image for running yolov5?
or do you have any application code for running yolov5 on board?

@IkrameBeggar
Copy link

Yes, the SiLU function is not supported (as seen here https://docs.xilinx.com/r/en-US/ug1414-vitis-ai/Currently-Supported-Operators). You need to replace it with LeakyReLU. The specific files that need to be modified are the common.py and experimental.py files, located in yolov5/models/, which are modified as follows.

#old
self.act = nn.SiLU
#new
self.act = nn.LeakyReLU(0.1, inplace=True)

After these changes you have to train this modified network. I used Google Colab and it took approximately 4 hrs using this command: !python train.py --img 640 --batch 16 --epochs 3 --data coco.yaml --weights yolov5n.pt i got from here https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data

You have done it right if after you compile the model for your specific board, you get something like this with only 1 DPU subgraph: image

Hello I am having a huge drop of map@0.5. It dropped from 93% to 1% after the quantization. Do you have any recommendations

@DrLEO11153024
Copy link

I am currently trying yolov5 quantification and an error occurred. How can I correct it?

RuntimeError: Given groups=1, weight of size [32, 3, 6, 6], expected input[1, 1, 640, 640] to have 3 channels, but got 1 channels instead

@Muffinxz
Copy link

@Zach227 I tried to use the code you provided and I am getting this error "ImportError: cannot import name 'TryExcept' from 'utils' (unknown location)"

Can you tell me how I can resolve this issue please. Or Anyone, I am kinda stuck and I got a pretty short deadline coming ahead. Any help would be really appreciated

@60rw311
Copy link

60rw311 commented Jul 12, 2024

@Muffinxz I'm having the same error, did you solve it?

@SoroushSemer
Copy link

@Muffinxz @60rw311 I had the same issue the way, I resolved it by moving the quantizer python script under the yolov5 directory.

This is because the common.py is trying to look for the utils directory but since it is inheriting the root directory of your quantizer script it cannot find it. (or at least i think)

@MuhammadBabarAli
Copy link

Hi, I took the quantization script from this tutorial: https://www.hackster.io/LogicTronix/yolov5-quantization-compilation-with-vitis-ai-3-0-for-kria-7b005d and made the necessary changes to the files. Can anyone please tell me how I can evaluate the quantized model, I used the yolov5 val.py but got errors when using the quantized .pt model and got mAP 0 when using the quantized .onnx model. Any help would be appreciated.

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

No branches or pull requests