hukaixuan19970627 / yolov5_obb

yolov5 + csl_label.(Oriented Object Detection)(Rotation Detection)(Rotated BBox)基于yolov5的旋转目标检测
GNU General Public License v3.0
1.81k stars 425 forks source link

bug when trying to save txt bboxes with detect.py --save-txt #375

Open vshesh opened 2 years ago

vshesh commented 2 years ago

In Colab

%cd /content/yolov5_obb
!python detect.py --weights '/content/best.pt' --source '../datasets/sample/valid/images' --conf-thres 0.5 --img 640 --device 0 --agnostic-nms --save-txt

Result

/content/yolov5_obb
detect: weights=['/content/sample_data/best.pt'], source=../datasets/sample/valid/images, imgsz=[640, 640], conf_thres=0.5, iou_thres=0.4, max_det=1000, device=0, view_img=False, save_txt=True, save_conf=False, save_crop=False, nosave=False, classes=None, agnostic_nms=True, augment=False, visualize=False, update=False, project=runs/detect, name=exp, exist_ok=False, line_thickness=2, hide_labels=False, hide_conf=False, half=False, dnn=False
YOLOv5 🚀 c7409ff torch 1.10.1+cu113 CUDA:0 (Tesla P100-PCIE-16GB, 16281MiB)

Fusing layers... 
Model Summary: 290 layers, 21584355 parameters, 0 gradients
Traceback (most recent call last):
  File "detect.py", line 251, in <module>
    main(opt)
  File "detect.py", line 246, in main
    run(**vars(opt))
  File "/usr/local/lib/python3.7/dist-packages/torch/autograd/grad_mode.py", line 28, in decorate_context
    return func(*args, **kwargs)
  File "detect.py", line 159, in run
    poly = poly.tolist()
AttributeError: 'list' object has no attribute 'tolist'

Removing --save-txt makes the error go away and detection is fine.

vshesh commented 2 years ago

I see sorry I'm supposed to use val.py.

vshesh commented 2 years ago

Actually now I am confused because there is detect.py at the bottom of the docs with --hide-txt --hide-conf. This is useful for me because I have a --source /some/folder instead of a dataset with validation that needs to happen.

archg2021 commented 2 years ago

@vshesh I get the same error. Were you able to save the detected bounding boxes in text files?

vshesh commented 2 years ago

I was able to run detection on the validation set using val.py. So I have those in txt files. Beyond that no, I wasn't able to get detect.py working with --save-txt and --save-conf

archg2021 commented 2 years ago

I am working with just one class, so the below workaround worked for me for saving labels with detect.py https://github.com/hukaixuan19970627/yolov5_obb/blob/6f9ea7d78851d2638cc0d53112cf0b825778e1b3/detect.py#L159-L162

                   # poly = poly.tolist()
                   # line = (cls, *poly, conf) if save_conf else (cls, *poly)  # label format
                    poly_value = [x.item() for x in poly]
                    line = (cls, *poly, conf) if save_conf else (*poly,)  # label format

                    with open(txt_path + '.txt', 'a') as f:
                        f.write(('%g ' * len(line)).rstrip() % line +  ' screws 0'+ '\n')

Where screws is the object class and difficulty is always 0

FireCuda commented 1 year ago

maybe you can try "poly = torch.tensor(poly).view(1, 8).view(-1).tolist()" instead "poly = poly.tolist()"