YOLOv3之loss和iou可視化(橫座標和縱座標與迭代次數完美對齊)

建議大家先去看看其他博客的代碼,就能體會到它的魅力了。

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
start_ite = 6000 #log_loss.txt裏面的最小迭代次數
end_ite = 15000 #log_loss.txt裏面的最大迭代次數
step = 10 #跳行數,決定畫圖的稠密程度
igore = 0 #當開始的loss較大時,你需要忽略前igore次迭代,注意這裏是迭代次數


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<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)
#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 = 1736397
step = 5000
start_ite = 0
end_ite = 50200
igore = 1000
data_path =  './my_coco3/log_iou.txt' #log_loss的路徑。
result_path = './my_coco3/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<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)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章