WongKinYiu / yolov9

Implementation of paper - YOLOv9: Learning What You Want to Learn Using Programmable Gradient Information
GNU General Public License v3.0
8.98k stars 1.42k forks source link

About reparameterization.ipynb #198

Open wgqhandsome opened 8 months ago

wgqhandsome commented 8 months ago

I used reparameterization.ipynb to convert the trained yolov9-c model, why does the accuracy drop significantly? Can this kind of conversion maintain the accuracy? Has anyone verified the accuracy after the conversion?

WongKinYiu commented 8 months ago

You could evaluate yolov9-c.pt and yolov9-c-converted.pt, they have almost same accuracy.

WongKinYiu commented 8 months ago

Do you modify nc in gelan-c.yaml and reparameterization.ipynb to match your dataset?

wgqhandsome commented 8 months ago

Thank you for your response. I am honored to receive your reply, and I need to delve further into this issue. Thanks again.

wgqhandsome commented 8 months ago

Do you modify nc in gelan-c.yaml and reparameterization.ipynb to match your dataset?

My task only requires identifying a single category. I trained the model corresponding to yolov9-c.yaml using the train_dual method, but did not modify the nc (number of classes) in yolov9-c.yaml. When using reparameterization.ipynb for model conversion, I set cfg='gelan-c' and initialized the Model class with ch=3 (number of channels), nc=1 (number of classes), and anchors=3 (number of anchors). Theoretically, should such settings successfully complete the model conversion?

wgqhandsome commented 8 months ago

Do you modify nc in gelan-c.yaml and reparameterization.ipynb to match your dataset?

My task only requires identifying a single category. I trained the model corresponding to yolov9-c.yaml using the train_dual method, but did not modify the nc (number of classes) in yolov9-c.yaml. When using reparameterization.ipynb for model conversion, I set cfg='gelan-c' and initialized the Model class with ch=3 (number of channels), nc=1 (number of classes), and anchors=3 (number of anchors). Theoretically, should such settings successfully complete the model conversion?

Fusing layers... gelan-c summary: 387 layers, 25227859 parameters, 4035 gradients, 101.8 GFLOPs Traceback (most recent call last): File "code-object-detection/detect.py", line 232, in main(opt) File "code-object-detection/detect.py", line 227, in main run(*vars(opt)) File "/root/miniconda3/lib/python3.8/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context return func(args, *kwargs) File "code-object-detection/detect.py", line 131, in run s += f"{n} {names[int(c)]}{'s' (n > 1)}, " # add to string KeyError: 4795

onnx20 commented 7 months ago

I used reparameterization.ipynb to convert the trained yolov9-c model, why does the accuracy drop significantly? Can this kind of conversion maintain the accuracy? Has anyone verified the accuracy after the conversion?

I encountered the same problem, I converted yolov9-e.pt to yolov9-e-converted.pt, This model is trained on my own dataset, and I have modified nc. mean_p mean_r map50 map yolov9-e.pt: 0.3393 0.2703 0.3045 0.2291 yolov9-e-converted.pt: 0.3019 0.2105 0.2484 0.1838

WongKinYiu commented 7 months ago

I guess you do not finish the training of all epochs, and convert the temp best weights.

in this case, you have to modify

ckpt = torch.load('./yolov9-e.pt', map_location='cpu')
model.names = ckpt['model'].names
model.nc = ckpt['model'].nc

to

ckpt = torch.load('./yolov9-e.pt', map_location='cpu')
ckpt['model'] = ckpt['ema']
model.names = ckpt['model'].names
model.nc = ckpt['model'].nc
onnx20 commented 7 months ago

I guess you do not finish the training of all epochs, and convert the temp best weights.

in this case, you have to modify

ckpt = torch.load('./yolov9-e.pt', map_location='cpu')
model.names = ckpt['model'].names
model.nc = ckpt['model'].nc

to

ckpt = torch.load('./yolov9-e.pt', map_location='cpu')
ckpt['model'] = ckpt['ema']
model.names = ckpt['model'].names
model.nc = ckpt['model'].nc

Great!your method is useful,yolov9-e.pt and yolov9-e-converted.pt have the same accuracy. But what is the reason for this situation? what is the difference between ckpt['model'] and ckpt['ema'] ? Finally, thank you very much for your work!

mo-lx commented 7 months ago

Why do errors occur when training with converted weight files?

D:\anaconda\envs\yolov9\python.exe D:/Allofdata/scientific_research/perception/Repositories/track/YOLOv9-DeepSORT-Object-Tracking-main/detect.py detect: weights=weights\yolov9-c-converted.pt, source=data\images, data=data\mydata_ship_coco.yaml, imgsz=[640, 640], conf_thres=0.25, iou_thres=0.45, max_det=1000, device=, view_img=False, save_txt=False, save_conf=False, save_crop=False, nosave=False, classes=None, agnostic_nms=False, augment=False, visualize=False, update=False, project=runs\detect, name=exp, exist_ok=False, line_thickness=3, hide_labels=False, hide_conf=False, half=False, dnn=False, vid_stride=1 YOLO 2024-4-9 Python-3.7.12 torch-1.13.1+cpu CPU

Fusing layers... gelan-c-class2 summary: 387 layers, 25228630 parameters, 4806 gradients Traceback (most recent call last): File "D:/Allofdata/scientific_research/perception/Repositories/track/YOLOv9-DeepSORT-Object-Tracking-main/detect.py", line 233, in main(opt) File "D:/Allofdata/scientific_research/perception/Repositories/track/YOLOv9-DeepSORT-Object-Tracking-main/detect.py", line 228, in main run(*vars(opt)) File "D:\anaconda\envs\yolov9\lib\site-packages\torch\autograd\grad_mode.py", line 27, in decorate_context return func(args, *kwargs) File "D:/Allofdata/scientific_research/perception/Repositories/track/YOLOv9-DeepSORT-Object-Tracking-main/detect.py", line 130, in run s += f"{n} {names[int(c)]}{'s' (n > 1)}, " # add to string KeyError: 2075

2314-long commented 7 months ago

I guess you do not finish the training of all epochs, and convert the temp best weights.

in this case, you have to modify

ckpt = torch.load('./yolov9-e.pt', map_location='cpu')
model.names = ckpt['model'].names
model.nc = ckpt['model'].nc

to

ckpt = torch.load('./yolov9-e.pt', map_location='cpu')
ckpt['model'] = ckpt['ema']
model.names = ckpt['model'].names
model.nc = ckpt['model'].nc

I use this method,add "ckpt['model'] = ckpt['ema']",but this problem persists .what should i do

tuansunday05 commented 6 months ago

Why do errors occur when training with converted weight files?

D:\anaconda\envs\yolov9\python.exe D:/Allofdata/scientific_research/perception/Repositories/track/YOLOv9-DeepSORT-Object-Tracking-main/detect.py detect: weights=weights\yolov9-c-converted.pt, source=data\images, data=data\mydata_ship_coco.yaml, imgsz=[640, 640], conf_thres=0.25, iou_thres=0.45, max_det=1000, device=, view_img=False, save_txt=False, save_conf=False, save_crop=False, nosave=False, classes=None, agnostic_nms=False, augment=False, visualize=False, update=False, project=runs\detect, name=exp, exist_ok=False, line_thickness=3, hide_labels=False, hide_conf=False, half=False, dnn=False, vid_stride=1 YOLO 2024-4-9 Python-3.7.12 torch-1.13.1+cpu CPU

Fusing layers... gelan-c-class2 summary: 387 layers, 25228630 parameters, 4806 gradients Traceback (most recent call last): File "D:/Allofdata/scientific_research/perception/Repositories/track/YOLOv9-DeepSORT-Object-Tracking-main/detect.py", line 233, in main(opt) File "D:/Allofdata/scientific_research/perception/Repositories/track/YOLOv9-DeepSORT-Object-Tracking-main/detect.py", line 228, in main run(*vars(opt)) File "D:\anaconda\envs\yolov9\lib\site-packages\torch\autograd\grad_mode.py", line 27, in decorate_context return func(args, *kwargs) File "D:/Allofdata/scientific_research/perception/Repositories/track/YOLOv9-DeepSORT-Object-Tracking-main/detect.py", line 130, in run s += f"{n} {names[int(c)]}{'s' (n > 1)}, " # add to string KeyError: 2075

I have met the same issue and the solution was insert 1 more line after with dt[2]:: pred = pred[0][1] if isinstance(pred[0], list) else pred[0]