YOLOv3之loss和iou可视化(横坐标和纵坐标与迭代次数完美对齐)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33614902/article/details/83418441

建议大家先去看看其他博客的代码,就能体会到它的魅力了。

Table of Contents

一、extract_log.py

二、visualization_loss.py

三、visualization_iou.py


一、extract_log.py

#!/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('./2048/train_log2.txt', './2048/log_loss2.txt', 'images')
extract_log('./2048/train_log2.txt', 'log_iou2.txt', 'IOU')

二、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 = 4500
step = 10 #跳行数,决定画图的稠密程度
igore = 0 #当前面的loss较大时,你需要忽略前igore行
gpus = 2 #训练时所用的gpu个数
start_ite = 6000+igore*gpus
end_ite = 15000 #cfg文件中的max_batches
y_ticks = [0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4]#纵坐标的值,可以自己设置。
data_path =  '2048/log_loss2.txt' #log_loss的路径。
result_path = './2048/avg_loss' #保存结果的路径。

####-----------------只需要改上面的,下面的可以不改动
names = ['loss', 'avg', 'rate', 'seconds', 'images']
result = pd.read_csv(data_path, skiprows=[x for x in range(lines) if (x<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)/(x_num*1.0)
x = []
for i in range(x_num):
	x.append(i*tmp + start_ite)
#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)
#fig.savefig('loss')

三、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 = 319583
step = 100
start_ite = 6000
end_ite = 15000
data_path =  './2048/log_iou2.txt' #log_loss的路径。
result_path = './2048/Region Avg IOU' #保存结果的路径。

names = ['Region Avg IOU', 'Class', 'Obj', 'No Obj', 'Avg 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%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)/(x_num*1.0)
x = []
for i in range(x_num):
	x.append(i*tmp + start_ite)
#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)

猜你喜欢

转载自blog.csdn.net/qq_33614902/article/details/83418441