YOLO 训练可视化:loss、iou、F1、map、precision、recall

保存训练日志

在命令最后加上:>>train.log 或者 | tee train.log
训练过程就会生成一个log文件。

提取log文件

在log文件目录下,新建 extract_log.py 脚本:

# coding=utf-8
# 该文件用来提取训练log,去除不可解析的log后使log文件格式化,生成新的log文件供可视化工具绘图
 
import inspect
import os
import random
import sys
def extract_log(log_file,new_log_file,key_word):
    with open(log_file, 'r',encoding='utf-16LE') as f:
      with open(new_log_file, 'w') as train_log:
  #f = open(log_file)
    #train_log = open(new_log_file, 'w')
        for line in f:
    # 去除多gpu的同步log
          if 'Syncing' in line:
            continue
    # 去除除零错误的log
          if 'nan' in line:
            continue
          if key_word in line:
            train_log.write(line)
    f.close()
    train_log.close()
 
extract_log('train_yolov3.log','train_log_loss.txt','images')
extract_log('train_yolov3.log','train_log_iou.txt','IOU')
extract_log('train_yolov3.log','train_log_map.txt','mAP@0.50') 
extract_log('train_yolov3.log','train_log_F1.txt','F1-score')

运行之后将会生成:train_log_loss.txt、train_log_iou.txt、train_log_map.txt、train_log_F1.txt 四个文件。

loss曲线

新建visualization_loss.py脚本:根据自己要求修改

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#%matplotlib inline
 
lines =20000    #改为自己生成的train_log_loss.txt中的行数
start_ite = 1 #log_loss.txt里面的最小迭代次数
end_ite = 20000 #log_loss.txt里面的最大迭代次数
step = 50 #跳行数,决定画图的稠密程度
igore = 1500 #当开始的loss较大时,你需要忽略前igore次迭代,注意这里是迭代次数
result = pd.read_csv('train_log_loss.txt',skiprows=[x for x in range(lines) if (x<lines*1.0/((end_ite - start_ite)*1.0)*igore or x%step!=9)]\
    , error_bad_lines=False, names=['loss', 'avg', 'rate', 'seconds', 'images'])
result.head()
 
result['loss']=result['loss'].str.split(' ').str.get(1)
result['avg']=result['avg'].str.split(' ').str.get(1)
result['rate']=result['rate'].str.split(' ').str.get(1)
result['seconds']=result['seconds'].str.split(' ').str.get(1)
result['images']=result['images'].str.split(' ').str.get(1)


result['loss']=pd.to_numeric(result['loss'],errors='ignore')
result['avg']=pd.to_numeric(result['avg'],errors='ignore')
result['rate']=pd.to_numeric(result['rate'],errors='ignore')
result['seconds']=pd.to_numeric(result['seconds'],errors='ignore')
result['images']=pd.to_numeric(result['images'],errors='ignore')

x_num = len(result['avg'].values)
tmp = (end_ite-start_ite - igore)/(x_num*1.0)
x = []
for i in range(x_num):
	x.append(i*tmp + start_ite + igore)

fig = plt.figure(1, dpi=160)
ax = fig.add_subplot(1, 1, 1)
ax.plot(x,result['avg'].values,'r', label='avg_loss')
ax.legend(loc='best')
ax.set_ylim([0.1, 1.25])
#ax.set_xlim([0, 200])
ax.set_title('The loss curves')
ax.set_xlabel('iteration')
ax.spines['top'].set_visible(False)     #去掉上边框
ax.spines['right'].set_visible(False)   #去掉右边框
fig.savefig('avg_loss')

运行后将会生成loss曲线
在这里插入图片描述

iou曲线

新建visualization_iou.py脚本:

#!/usr/bin/python
#coding=utf-8
 
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
 
#根据log_iou修改行数
lines = 588539
step = 5000
start_ite = 0
end_ite = 20000
igore = 100
data_path =  'train_log_iou.txt' #log_loss的路径。
result_path = 'Region Avg IOU' #保存结果的路径。
 
names = ['Region Avg IOU', 'Class', 'Obj', 'No Obj', '.5_Recall', '.7_Recall', 'count']
#result = pd.read_csv('log_iou.txt', skiprows=[x for x in range(lines) if (x%10==0 or x%10==9)]\
result = pd.read_csv(data_path, skiprows=[x for x in range(lines) if (x<lines*1.0/((end_ite - start_ite)*1.0)*igore or x%step!=0)]\
, error_bad_lines=False, names=names)
result.head()
 
for name in names:
    result[name] = result[name].str.split(': ').str.get(1)
result.head()
result.tail()
for name in names:
    result[name] = pd.to_numeric(result[name])
result.dtypes
 
 
####--------------
x_num = len(result['Region Avg IOU'].values)
tmp = (end_ite-start_ite - igore)/(x_num*1.0)
x = []
for i in range(x_num):
	x.append(i*tmp + start_ite + igore)
#print(x)
print('total = %d\n' %x_num)
print('start = %d, end = %d\n' %(x[0], x[-1]))
####-------------
 
 
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(x, result['Region Avg IOU'].values, label='Region Avg IOU')
#ax.plot(result['Avg Recall'].values, label='Avg Recall')
plt.grid()
ax.legend(loc='best')
ax.set_title('The Region Avg IOU curves')
ax.set_xlabel('batches')
fig.savefig(result_path)

运行后即可生成IOU曲线

下面的F1、map、precision、recall四条曲线都是在train_log_F1.txt文件内提取相关数据得到的。代码基本相同,只需修改相关字符就行。

F1曲线

#!/usr/bin/python
#coding=utf-8
 
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

 
#根据log_iou修改行数
lines = 191
step = 1
start_ite = 0
end_ite = 191
igore = 0
data_path =  'train_log_F1.txt' #f1的路径。
result_path = 'F1' #保存结果的路径。
 
names = ['conf_thresh', 'precision', 'recall', 'F1-score']
#result = pd.read_csv('log_iou.txt', skiprows=[x for x in range(lines) if (x%10==0 or x%10==9)]\
result = pd.read_csv(data_path, skiprows=[x for x in range(lines) if (x<lines*1.0/((end_ite - start_ite)*1.0)*igore or x%step!=0)]\
, error_bad_lines=False, names=names)
result.head()
for name in names:
    result[name] = result[name].str.split(' = ').str.get(1)
result.head()
result.tail()
for name in names:
    result[name] = pd.to_numeric(result[name])
result.dtypes
 
 
####--------------
x_num = len(result['F1-score'].values)
tmp = (end_ite-start_ite - igore)/(x_num*1.0)
x = []
for i in range(x_num):
	x.append(i*tmp + start_ite + igore)
#print(x)
print('total = %d\n' %x_num)
print('start = %d, end = %d\n' %(x[0], x[-1]))
####-------------
 
 
fig = plt.figure(1, dpi=160)
ax = fig.add_subplot(1,1,1)
ax.plot(x, result['F1-score'].values, 'r',label='F1-score')
#ax.plot(result['Avg Recall'].values, label='Avg Recall')
#plt.grid()
ax.legend(loc='best')
ax.set_ylim([0.4, 0.85])
ax.set_xlim([-5, 200])
ax.set_title('The F1-score curves')
ax.set_xlabel('batches')
ax.spines['top'].set_visible(False)     #去掉上边框
ax.spines['right'].set_visible(False)   #去掉右边框
fig.savefig(result_path)

map曲线

#!/usr/bin/python
#coding=utf-8
 
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
 
#根据log_iou修改行数
lines = 191
step = 1
start_ite = 0
end_ite = 191
igore = 0
data_path =  'train_log_map.txt' #f1的路径。
result_path = 'map' #保存结果的路径。
 
names = ['(mAP@0.50)', 'or']
#result = pd.read_csv('log_iou.txt', skiprows=[x for x in range(lines) if (x%10==0 or x%10==9)]\
result = pd.read_csv(data_path, skiprows=[x for x in range(lines) if (x<lines*1.0/((end_ite - start_ite)*1.0)*igore or x%step!=0)]\
, error_bad_lines=False, names=names)
result.head()
 
for name in names:
    result[name] = result[name].str.split(' = ').str.get(1)
result.head()
result.tail()
for name in names:
    result[name] = pd.to_numeric(result[name])
result.dtypes
 
 
####--------------
x_num = len(result['(mAP@0.50)'].values)
tmp = (end_ite-start_ite - igore)/(x_num*1.0)
x = []
for i in range(x_num):
	x.append(i*tmp + start_ite + igore)
#print(x)
print('total = %d\n' %x_num)
print('start = %d, end = %d\n' %(x[0], x[-1]))
####-------------
 
 
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(x, result['(mAP@0.50)'].values, label='mAP')
#ax.plot(result['Avg Recall'].values, label='Avg Recall')
plt.grid()
ax.legend(loc='best')
ax.set_title('The mAP curves')
ax.set_xlabel('epoch')
fig.savefig(result_path)

precision曲线

#!/usr/bin/python
#coding=utf-8
 
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
 
#根据log_iou修改行数
lines = 191
step = 1
start_ite = 0
end_ite = 191
igore = 0
data_path =  'train_log_F1.txt' #f1的路径。
result_path = 'precision' #保存结果的路径。
 
names = ['conf_thresh', 'precision', 'recall', 'F1-score']
#result = pd.read_csv('log_iou.txt', skiprows=[x for x in range(lines) if (x%10==0 or x%10==9)]\
result = pd.read_csv(data_path, skiprows=[x for x in range(lines) if (x<lines*1.0/((end_ite - start_ite)*1.0)*igore or x%step!=0)]\
, error_bad_lines=False, names=names)
result.head()
 
for name in names:
    result[name] = result[name].str.split(' = ').str.get(1)
result.head()
result.tail()
for name in names:
    result[name] = pd.to_numeric(result[name])
result.dtypes
 
 
####--------------
x_num = len(result['precision'].values)
tmp = (end_ite-start_ite - igore)/(x_num*1.0)
x = []
for i in range(x_num):
	x.append(i*tmp + start_ite + igore)
#print(x)
print('total = %d\n' %x_num)
print('start = %d, end = %d\n' %(x[0], x[-1]))
####-------------
 
 
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(x, result['precision'].values, label='precision')
#ax.plot(result['Avg Recall'].values, label='Avg Recall')
plt.grid()
ax.legend(loc='best')
ax.set_title('The precision curves')
ax.set_xlabel('batches')
fig.savefig(result_path)

recall曲线

#!/usr/bin/python
#coding=utf-8
 
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
 
#根据log_iou修改行数
lines = 191
step = 1
start_ite = 0
end_ite = 191
igore = 0
data_path =  'train_log_F1.txt' #f1的路径。
result_path = 'recall' #保存结果的路径。
 
names = ['conf_thresh', 'precision', 'recall', 'F1-score']
#result = pd.read_csv('log_iou.txt', skiprows=[x for x in range(lines) if (x%10==0 or x%10==9)]\
result = pd.read_csv(data_path, skiprows=[x for x in range(lines) if (x<lines*1.0/((end_ite - start_ite)*1.0)*igore or x%step!=0)]\
, error_bad_lines=False, names=names)
result.head()
 
for name in names:
    result[name] = result[name].str.split(' = ').str.get(1)
result.head()
result.tail()
for name in names:
    result[name] = pd.to_numeric(result[name])
result.dtypes
 
 
####--------------
x_num = len(result['recall'].values)
tmp = (end_ite-start_ite - igore)/(x_num*1.0)
x = []
for i in range(x_num):
	x.append(i*tmp + start_ite + igore)
#print(x)
print('total = %d\n' %x_num)
print('start = %d, end = %d\n' %(x[0], x[-1]))
####-------------
 
 
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(x, result['recall'].values, label='recall')
#ax.plot(result['Avg Recall'].values, label='Avg Recall')
plt.grid()
ax.legend(loc='best')
ax.set_title('The recall curves')
ax.set_xlabel('batches')
fig.savefig(result_path)

运行以上代码即可得到相应曲线。

参考链接:
YOLO-V3可视化训练过程中的参数,绘制loss、IOU、avg Recall等的曲线图
win10 + YOLOv3 在darknet下可视化训练过程的参数

  • 10
    点赞
  • 191
    收藏
    觉得还不错? 一键收藏
  • 15
    评论
要使用YOLO训练自己的模型,你需要按照以下步骤进行操作: 1. 数据准备:首先,你需要准备好训练所需的数据集。数据集应包含带有标签的图像,每个标签指定了图像中的对象位置和类别。确保你的数据集符合YOLO的格式要求,每个图像对应一个同名的.txt文件,其中包含了对象的位置和类别信息。 2. 标注工具:使用标注工具(如LabelImg、RectLabel等)对图像进行标注,将对象位置和类别信息添加到每个图像中,并生成对应的标签文件。 3. 配置文件:创建YOLO的配置文件,其中包含模型的相关参数,如网络结构、训练参数和路径等。确保配置文件与你的数据集和类别信息相匹配。 4. 下载预训练权重:从YOLO官方网站或其他可靠来源下载预训练的权重文件,这将作为模型的初始参数。 5. 训练模型:使用YOLO训练代码(如Darknet)加载配置文件和预训练权重,并开始训练模型。在训练过程中,模型将根据你提供的数据集进行学习和优化。 6. 模型评估和调优:训练完成后,使用测试集评估模型的性能,并根据需要进行调整和优化。你可以通过计算指标(如精确度、召回率和平均精确度均值(mAP))来评估模型。 7. 模型应用:训练完成的YOLO模型可以用于物体检测任务。你可以使用训练好的模型对新的图像或视频进行物体检测,并获取对象的位置和类别信息。 请注意,YOLO训练流程较为复杂,需要一定的计算资源和时间。如果你刚开始接触物体检测和深度学习,建议先阅读相关文档和教程,并尝试使用开源实现的YOLO模型进行学习和实验。
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值