首先,贴上运行test.py时出现的错误提醒。
"C:\Users\Smlile Sun\Anaconda3\envs\yolov5\python.exe" D:/objectimage/yolov3andv5/yolov3-master-dense/test.py
Namespace(augment=False, batch_size=16, conf_thres=0.001, data='data/dataset.yaml', device='', exist_ok=False, img_size=640, iou_thres=0.6, name='exp', project='runs/test', save_conf=False, save_hybrid=False, save_json=False, save_txt=False, single_cls=False, task='val', verbose=False, weights='D:/objectimage/yolov3andv5/yolov3-master-dense/yolov3-master-dense/runs/train/exp11/weights/last.pt')
YOLOv3 2021-4-19 torch 1.10.1+cpu CPU
Fusing layers...
C:\Users\Smlile Sun\Anaconda3\envs\yolov5\lib\site-packages\torch\functional.py:445: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at ..\aten\src\ATen\native\TensorShape.cpp:2157.)
return _VF.meshgrid(tensors, **kwargs) # type: ignore[attr-defined]
Model Summary: 431 layers, 59591743 parameters, 0 gradients, 249.2 GFLOPS
Traceback (most recent call last):
File "D:/objectimage/yolov3andv5/yolov3-master-dense/test.py", line 327, in
save_conf=opt.save_conf,
File "D:/objectimage/yolov3andv5/yolov3-master-dense/test.py", line 74, in test
data = yaml.load(f, Loader=yaml.SafeLoader)
File "C:\Users\Smlile Sun\Anaconda3\envs\yolov5\lib\site-packages\yaml\__init__.py", line 112, in load
loader = Loader(stream)
File "C:\Users\Smlile Sun\Anaconda3\envs\yolov5\lib\site-packages\yaml\loader.py", line 34, in __init__
Reader.__init__(self, stream)
File "C:\Users\Smlile Sun\Anaconda3\envs\yolov5\lib\site-packages\yaml\reader.py", line 85, in __init__
self.determine_encoding()
File "C:\Users\Smlile Sun\Anaconda3\envs\yolov5\lib\site-packages\yaml\reader.py", line 124, in determine_encoding
self.update_raw()
File "C:\Users\Smlile Sun\Anaconda3\envs\yolov5\lib\site-packages\yaml\reader.py", line 178, in update_raw
data = self.stream.read(size)
UnicodeDecodeError: 'gbk' codec can't decode byte 0x89 in position 285: illegal multibyte sequence
因为是图省事搬过来别人的yolov3代码,所以在很多地方需要根据自己的环境改,要不就会出现各种错误。这就是我运行test.py时遇到的一个十分头疼的问题,我一开始在一个新电脑(win11,i7,RTX3060)上面跑的,出现这个问题以后,我就怀疑是我的电脑的问题,所以我又移到了一个旧电脑上运行,发现还是有这个问题。网上也查到很多,虽然大部分也讲出来了原因,就是编码问题,但是基本都无法帮助我解决问题,因为大部分解决办法都是相对于某个自己编写的文件来说的,我对yolov3的代码不熟悉,不清楚具体问题出在哪行代码上。主要是因为代码里读取的文件和pycharm的编码方式不同,一个是“gbk”,一个是“UTF-8”。关于这两个编码方式的具体知识可以参考这篇文章,讲的很详细。
真正点醒我的,是这一篇文章——python读取yaml文件报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0xa5 in position 136: illegal multibyte sequence - 圆觉 - 博客园 (cnblogs.com)https://www.cnblogs.com/xiamaojjie/p/11670923.html 里面提到是读取yaml文件时出现了编码不匹配的问题,所以我赶紧用ctrl+F搜索test.py里面读取yaml文件的函数,找到了这一部分
# Configure
model.eval()
if isinstance(data, str):
is_coco = data.endswith('coco.yaml')
with open(data) as f:
data = yaml.load(f, Loader=yaml.SafeLoader)
check_dataset(data) # check
nc = 1 if single_cls else int(data['nc']) # number of classes
iouv = torch.linspace(0.5, 0.95, 10).to(device) # iou vector for [email protected]:0.95
niou = iouv.numel()
在这里读取了一个coco.yaml 的文件,,对比上面那个网页的读取代码部分,发现with open(data) as f ——这一行代码少了一部分,少了读取文件时对文件编码方式的设置,所以我在data后面加了编码方式‘rb’,发现问题就解决了,就这么一个问题,我搜了两三天,一直想不到是哪里出错的(新手,大佬勿喷),总算解决了,在最后也附上改过来之后的代码。
# Configure
model.eval()
if isinstance(data, str):
is_coco = data.endswith('coco.yaml')
with open(data, 'rb') as f:
data = yaml.load(f, Loader=yaml.SafeLoader)
check_dataset(data) # check
nc = 1 if single_cls else int(data['nc']) # number of classes
iouv = torch.linspace(0.5, 0.95, 10).to(device) # iou vector for [email protected]:0.95
niou = iouv.numel()