linux下YoLov3学习(二):可视化loss,iou

目录

 

1.添加log路径

2.格式化log

3.绘制loss

4.绘制iou


1.添加log路径

新建visualization文件夹,用于后续可视化

train训练命令如下:

./darknet detector train cfg/voc.data cfg/yolov3-voc.cfg darknet53.conv.74 -gpus 0 | tee visualization/train_yolov3.log

在之前的基础继续训练

把darknet53.conv.74换成自己的权重文件

./darknet detector train cfg/voc.data cfg/yolov3-voc.cfg backup/yolov3-voc_900.weights -gpus 0 | tee visualization/train_yolov3.log

2.格式化log

需要先使用extract_log.py脚本,格式化log的extract_log.py脚本如下(和生成的log文件同一目录):

#!/usr/bin/python
#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') as f:
        with open(new_log_file, 'w') as train_log:
            for line in f:
                #去除多GPU的同步log;去除除零错误的log
                if ('Syncing' in line) or ('nan' in line):
                    continue
                if key_word in line:
                    train_log.write(line)
    f.close()
    train_log.close()
extract_log('train_yolov3.log', './plane/log_loss2.txt', 'images')
extract_log('train_yolov3.log', './plane/log_iou2.txt', 'IOU')

3.绘制loss

同上新建visualization_loss.py

#!/usr/bin/python
#coding=utf-8
 
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
 
 
#根据自己的log_loss.txt中的行数修改lines, 修改训练时的迭代起始次数(start_ite)和结束次数(end_ite)。
lines = 1126
start_ite = 1 #log_loss.txt里面的最小迭代次数
end_ite = 1125 #log_loss.txt里面的最大迭代次数
step = 10 #跳行数,决定画图的稠密程度
igore = 300 #当开始的loss较大时,你需要忽略前igore次迭代,注意这里是迭代次数,不忽略,差值较大画不出来
 
 
y_ticks = [0.1,0.5, 1.0, 1.5,2.0, 2.5 ,3, 4, 5, 6]#纵坐标的值,可以自己设置。
data_path =  './plane/log_loss2.txt' #log_loss的路径。
result_path = './plane/avg_loss' #保存结果的路径。
 
####-----------------只需要改上面的,下面的可以不改动
names = ['loss', 'avg', 'rate', 'seconds', 'images']
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!=9)], 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
print(result['avg'].values)
 
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
 
 
###-----------设置横坐标的值。
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)
#print(x)
print('total = %d\n' %x_num)
print('start = %d, end = %d\n' %(x[0], x[-1]))
###----------
 
 
ax.plot(x, result['avg'].values, label='avg_loss')
#ax.plot(result['loss'].values, label='loss')
plt.yticks(y_ticks)#如果不想自己设置纵坐标,可以注释掉。
plt.grid()
ax.legend(loc = 'best')
ax.set_title('The loss curves')
ax.set_xlabel('batches')
fig.savefig(result_path)

4.绘制iou

同上新建visualization_loss.pyvisualization_iou.py

#!/usr/bin/python
#coding=utf-8
 
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
 
#根据log_iou修改行数
lines = 35701
step = 100
start_ite = 0
end_ite = 1125
igore = 500
data_path =  './plane/log_iou2.txt' #log_loss的路径。
result_path = './plane/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)

参考博客

https://blog.csdn.net/qq_33614902/article/details/83418441

https://blog.csdn.net/qq_34806812/article/details/81459982

https://blog.csdn.net/cgt19910923/article/details/80783614

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值