Yolov5修改支持中文标签训练

step1:在yolov5course\yolov5-master\utils路径下找到general.py文件,修改yaml_loda函数改为下图

def yaml_load(file='data.yaml'):
    # Single-line safe yaml loading
    #with open(file, errors='ignore') as f:
    with open(file, errors='ignore',encoding='UTF-8') as f:
        return yaml.safe_load(f)
step2:在yolov5course\yolov5-master\utils路径下找到plots.py文件,在setting注释栏下面添加代码,如下图
# Settings
RANK = int(os.getenv('RANK', -1))
matplotlib.rc('font', **{'size': 11})
matplotlib.use('Agg')  # for writing to files only
plt.rcParams['font.sans-serif'] = ["SimHei"]
plt.rcParams['axes.unicode_minus'] = False

step3:在yolov5course\yolov5-master\utils路径下找metrics.py文件,找到plot函数如图修改

    def plot(self, normalize=True, save_dir='', names=()):
        import seaborn as sn
        rc = {'font.sans-serif': ['SimHei']}
        array = self.matrix / ((self.matrix.sum(0).reshape(1, -1) + 1E-9) if normalize else 1)  # normalize columns
        array[array < 0.005] = np.nan  # don't annotate (would appear as 0.00)

        fig, ax = plt.subplots(1, 1, figsize=(12, 9), tight_layout=True)
        nc, nn = self.nc, len(names)  # number of classes, names
        sn.set(font_scale=1.0 if nc < 50 else 0.8,rc=rc)  # for label size
        labels = (0 < nn < 99) and (nn == nc)  # apply names to ticklabels
        ticklabels = (names + ['background']) if labels else "auto"

PS:若matplotlib库中没有SimHei.ttf,请自行手动安装安装链接:https://us-logger1.oss-cn-beijing.aliyuncs.com/SimHei.ttf

训练前请确保yaml文件中标签类为中文

猜你喜欢

转载自blog.csdn.net/qq_50972633/article/details/129937208