项目场景:
yolov5模型用的是github上YOLOv5官方的:GitHub - ultralytics/yolov5: YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite
问题描述
运行YOLOv5-master时detect.py指令出现以下情况:
Downloading https://github.com/ultralytics/yolov5/releases/download/v6.1/yolov5n6.pt to yolov5n6.pt...
^CTraceback (most recent call last)
File "/home/.../yolov5-master/utils/downloads.py", line 33, in safe_download
torch.hub.download_url_to_file(url, str(file), progress=LOGGER.level <= logging.INFO)
File "/home/.../anaconda3/lib/python3.6/site-packages/torch/hub.py", line 419, in download_url_to_file
u = urlopen(req)
..............
File "/home/.../anaconda3/lib/python3.6/socket.py", line 713, in create_connection
sock.connect(sa)
KeyboardInterrupt
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "val.py", line 396, in <module>
main(opt)
File "val.py", line 387, in main
r, _, t = run(**vars(opt), plots=False)
File "/home/.../anaconda3/lib/python3.6/site-packages/torch/autograd/grad_mode.py", line 28, in decorate_context
return func(*args, **kwargs)
...............
file.unlink(missing_ok=True) # remove partial downloads
TypeError: unlink() got an unexpected keyword argument 'missing_ok'
原因分析:
无法顺利下载权重文件
yolov5n6.pt
1.报错提示:“KeyboardInterrupt
During handling of the above exception, another exception occurred:”出现这个报错往往是由于连接问题,及网络连接不稳定或者下载文件的路径需要翻墙却翻不出去,但经过检查却发现网络稳定且能科学上网,但还是无法解决这个问题。
解决方案:
修改权重文件下载指令
YOLOv5权重文件下载:https://github.com/ultralytics/yolov5/releases/tag/v6.1
我尝试直接将权重文件下载至YOLO根目录下,但Downloading https://github.com/ultralytics/yolov5/releases/download/v6.1/yolov5n6.pt to yolov5n6.pt...的下载指令是不会先对文件夹里是否有权重文件作出判断再下载的,所以还需要对判断指令作出修改,通过查找发现:
def parse_opt():
parser = argparse.ArgumentParser()
parser.add_argument('--weights', nargs='+', type=str, default='yolov5s.pt', help='model path(s)')
# parser.add_argument('--source', type=str, default=ROOT / 'data/images', help='file/dir/URL/glob, 0 for webcam')
parser.add_argument('--source', type=str, default= '/home/savergb', help='file/dir/URL/glob, 0 for webcam')
parser.add_argument('--data', type=str, default=ROOT / 'data/coco128.yaml', help='(optional) dataset.yaml path')
权重文件对应的是'-weights',进一步查找weights,可以看到:
def run(
weights=ROOT / '', # model.pt path(s) ,
#source=ROOT / 'data/images', # file/dir/URL/glob, 0 for webcam
source= '/home/savergb', # file/dir/URL/glob, 0 for webcam
data=ROOT / 'data/coco128.yaml', # dataset.yaml path
权重文件会自动下载的原因就是weights=ROOT / ''中没有指定读取的文件,所以后续才需要下载,所以将下载好的权重文件的绝对位置粘贴进来,即
weights= '/home/...../yolov5-master/yolov5s'
再运行就可以顺利检测图片了!选择哪个文件看个人对检测效果的要求哦