yolov7
yolov7 copied to clipboard
“KeyError: 30” After running detect.py with custom-made weights
I've trained YOLOv7 on an instance segmentation dataset I made on roboflow. Using the weights from the runs file after training I tried to run inference on some images from my validation dataset:
python detect.py --source /home/lukas/PycharmProjects/yolo/yolov7/valid/images/ --weights best.pt --conf 0.25 --name pet-yolo
After running this I got a similar error to the Issue posted here: https://github.com/WongKinYiu/yolov7/issues/578
I did the recommended changes and the error I got before dissapeared. However now I have another error that I can't find a solution to:
File "/home/lukas/PycharmProjects/yolo/yolov7/seg/detect.py", line 154, in run s += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string KeyError: 30
@WongKinYiu Could you please take a look?
Can you explain more?
This is the entire error message:
detect: weights=['best.pt'], source=/home/lukas/PycharmProjects/yolo/yolov7/valid/images/, data=data/coco128.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=pet-yolo, exist_ok=False, line_thickness=3, hide_labels=False, hide_conf=False, half=False, dnn=False
YOLOv5 🚀 2023-2-5 Python-3.9.16 torch-1.13.1+cu117 CUDA:0 (NVIDIA GeForce RTX 3060, 12036MiB)
Fusing layers...
yolov7-seg summary: 325 layers, 37864052 parameters, 0 gradients
Traceback (most recent call last):
File "/home/lukas/PycharmProjects/yolo/yolov7/seg/detect.py", line 254, in <module>
main(opt)
File "/home/lukas/PycharmProjects/yolo/yolov7/seg/detect.py", line 249, in main
run(**vars(opt))
File "/home/lukas/miniconda3/envs/segmentation/lib/python3.9/site-packages/torch/autograd/grad_mode.py", line 27, in decorate_context
return func(*args, **kwargs)
File "/home/lukas/PycharmProjects/yolo/yolov7/seg/detect.py", line 154, in run
s += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string
KeyError: 30
I get this error after trying to train best.pt which are weights of a YOLOv7 instance segmentation model that I trained earlier. Now I want to run inference with this model. The path /home/lukas/PycharmProjects/yolo/yolov7/valid/images/ is a folder that has around 30 or so images.
If you want some more clarification as to the changes that I made to the detect.py file I can give you that:
Before I got the error mentioned above I had another Error identical to the one posted in this issue: https://github.com/WongKinYiu/yolov7/issues/1501. Previous Error message:
detect: weights=['best.pt'], source=/home/lukas/PycharmProjects/yolo/yolov7/valid/images/image5_jpg.rf.cd431eb26be9daec606ef8b28251ab48.jpg, data=data/coco128.yaml, imgsz=[640, 640], conf_thres=0.5, 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=pet-yolo, exist_ok=False, line_thickness=3, hide_labels=False, hide_conf=False, half=False, dnn=False
YOLOv5 🚀 2023-2-19 Python-3.9.16 torch-1.13.1+cu117 CUDA:0 (NVIDIA GeForce RTX 3060, 12045MiB)
Fusing layers...
yolov7-seg summary: 325 layers, 37864052 parameters, 0 gradients
Traceback (most recent call last):
File "/home/lukas/PycharmProjects/yolo/yolov7/seg/detect.py", line 260, in <module>
main(opt)
File "/home/lukas/PycharmProjects/yolo/yolov7/seg/detect.py", line 255, in main
run(**vars(opt))
File "/home/lukas/miniconda3/envs/segmentation/lib/python3.9/site-packages/torch/autograd/grad_mode.py", line 27, in decorate_context
return func(*args, **kwargs)
File "/home/lukas/PycharmProjects/yolo/yolov7/seg/detect.py", line 130, in run
pred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det)
File "/home/lukas/PycharmProjects/yolo/yolov7/seg/./utils/general.py", line 822, in non_max_suppression
bs = prediction.shape[0] # batch size
AttributeError: 'tuple' object has no attribute 'shape'
Then I did the following changes within detect.py:
pred = model(im, augement=augment, visualize=visualize)
to
pred = model(im, augement=augment, visualize=visualize)[0]
Now I have the error that I've posted above.
If you have anymore questions please ask!
I am running into the exact same issue.
I was able to do the inference on a custom trained model using this script.
use segment/predict.py
Thanks @Yoki185 , it works.
use segment/predict.py
Thank you, it does work!