第一种:训练自己的数据集支持中文,解决办法
yolov5/utils/general.py - > check_dataset(data, autodownload=True)方法下的
def check_dataset(data, autodownload=True):
# Download and/or unzip dataset if not found locally
# Usage: https://github.com/ultralytics/yolov5/releases/download/v1.0/coco128_with_yaml.zip
# Download (optional)
extract_dir = ''
if isinstance(data, (str, Path)) and str(data).endswith('.zip'): # i.e. gs://bucket/dir/coco128.zip
download(data, dir=DATASETS_DIR, unzip=True, delete=False, curl=False, threads=1)
data = next((DATASETS_DIR / Path(data).stem).rglob('*.yaml'))
extract_dir, autodownload = data.parent, False
# Read yaml (optional)
if isinstance(data, (str, Path)):
# with open(data, errors='ignore') as f: #原代码
with open(data, errors='ignore',encoding='UTF-8') as f: #新代码添加encoding='UTF-8'用于支持中文编码
data = yaml.safe_load(f) # dictionary
# Resolve paths
path = Path(extract_dir or data.get('path') or '') # optional 'path' default to '.'
if not path.is_absolute():
path = (ROOT / path).resolve()
for k in 'train', 'val', 'test':
if data.get(k): # prepend path
data[k] = str(path / data[k]) if isinstance(data[k], str) else [str(path / x) for x in data[k]]
# Parse yaml
assert 'nc' in data, "Dataset 'nc' key missing."
if 'names' not in data:
data['names'] = [f'class{i}' for i in range(data['nc'])] # assign class names if missing
train, val, test, s = (data.get(x) for x in ('train', 'val', 'test', 'download'))
第二种:训练完自己的数据集后,使用权重文件 weights/best.pt 验证时,标签显示中文乱码的解决办法
1)单独创建以个py文件,并写入代码,运行
import torch
#%%
ckpt1 = torch.load('runs\\weights\\best.pt')
2)在python控制台将显示ckpt1变量信息
3)写代码修改乱码为正常显示中文并保存
import torch
# %%
ckpt1 = torch.load('runs\\weights\\best.pt')
ckpt1['model'].names = ['电动车'] # 修改乱码为正常中文显示
torch.save(ckpt1, 'runs\\weights\\best.pt') # 重新保存文件
4)再次打开权重文件就显示正常中文了
5)测试 python detect.py --source data/images --weights runs/weights/best.pt