loss curve

Before drawing curves using a script, the script need to use a extract_log.py, log format, visualization tools for drawing a new log file is generated, the formatted log extract_log.py following script (and the generated log file in the same directory)

coding=utf-8

The log file is used to extract the training, after removing the log so that the log can not be parsed file format, generates a new log file for visualization tools Drawing

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:
#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',
'the IOU') -------------- -
after running, parses the log file loss rows and rows iou get two txt file

Use train_loss_visualization.py script can plot loss curve
train_loss_visualization.py script as follows (also the same directory as the New py file):

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#%matplotlib inline

lines =5124 #改为自己生成的train_log_loss.txt中的行数
result = pd.read_csv(‘train_log_loss.txt’, skiprows=[x for x in range(lines) if ((x%10!=9) |(x<1000))] ,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.head()
result.tail()

print(result.head())

print(result.tail())

print(result.dtypes)

print(result[‘loss’])
print(result[‘avg’])
print(result[‘rate’])
print(result[‘seconds’])
print(result[‘images’])

result[‘loss’]=pd.to_numeric(result[‘loss’])
result[‘avg’]=pd.to_numeric(result[‘avg’])
result[‘rate’]=pd.to_numeric(result[‘rate’])
result[‘seconds’]=pd.to_numeric(result[‘seconds’])
result[‘images’]=pd.to_numeric(result[‘images’])
result.dtypes

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(result[‘avg’].values,label=‘avg_loss’)

ax.plot(result[‘loss’].values,label=‘loss’)

ax.legend (loc = 'best') # column of FIG adaptive position
ax.set_title ( 'Loss of The Curves')
ax.set_xlabel ( 'Batches')
fig.savefig ( 'avg_loss')

Modify number train_log_loss.txt train_loss_visualization.py lines in rows, and change the number of lines to be skipped as required:

skiprows=[x for x in range(lines) if ((x%10!=9) |(x<1000))]

Run train_loss_visualization.py will generate avg_loss.png in the path of the script lies.
----------------
by analyzing loss curves, learning rate variations modify cfg strategy.

In addition to visual loss, you may also be visualized Avg IOU, Avg Recall parameters
visualized 'Region Avg IOU', 'Class ', 'Obj', 'No Obj', 'Avg Recall', 'count' parameters can script train_iou_visualization.py , and use the same train_loss_visualization.py, train_iou_visualization.py script is as follows (#lines the number of rows of modified
train_log_iou.txt): ----------------

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#%matplotlib inline

lines = 122956 #根据train_log_iou.txt的行数修改
result = pd.read_csv(‘train_log_iou.txt’, skiprows=[x for x in range(lines) if (x%100 or x%109) ] ,error_bad_lines=False, names=[‘Region Avg IOU’, ‘Class’, ‘Obj’, ‘No Obj’, ‘Avg Recall’,‘count’])
result.head()

result[‘Region Avg IOU’]=result[‘Region Avg IOU’].str.split(’: ‘).str.get(1)
result[‘Class’]=result[‘Class’].str.split(’: ‘).str.get(1)
result[‘Obj’]=result[‘Obj’].str.split(’: ‘).str.get(1)
result[‘No Obj’]=result[‘No Obj’].str.split(’: ‘).str.get(1)
result[‘Avg Recall’]=result[‘Avg Recall’].str.split(’: ‘).str.get(1)
result[‘count’]=result[‘count’].str.split(’: ').str.get(1)
result.head()
result.tail()

print(result.head())

print(result.tail())

print(result.dtypes)

print(result[‘Region Avg IOU’])

result[‘Region Avg IOU’]=pd.to_numeric(result[‘Region Avg IOU’])
result[‘Class’]=pd.to_numeric(result[‘Class’])
result[‘Obj’]=pd.to_numeric(result[‘Obj’])
result[‘No Obj’]=pd.to_numeric(result[‘No Obj’])
result[‘Avg Recall’]=pd.to_numeric(result[‘Avg Recall’])
result[‘count’]=pd.to_numeric(result[‘count’])
result.dtypes

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(result[‘Region Avg IOU’].values,label=‘Region Avg IOU’)

ax.plot(result[‘Class’].values,label=‘Class’)

ax.plot(result[‘Obj’].values,label=‘Obj’)

ax.plot(result[‘No Obj’].values,label=‘No Obj’)

ax.plot(result[‘Avg Recall’].values,label=‘Avg Recall’)

ax.plot(result[‘count’].values,label=‘count’)

ax.legend(loc=‘best’)

ax.set_title(‘The Region Avg IOU curves’)

ax.set_title(‘The Region Avg IOU curves’)
ax.set_xlabel(‘batches’)

fig.savefig ( 'Avg IOU')

fig.savefig(‘Region Avg IOU’)
————————————————
#!/usr/bin/python
#coding=utf-8

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Modify the lines according to the number of rows own log_loss.txt, modify the initial number of iterations (start_ite) at the end of training and the number of times (end_ite).
4500 = Lines
start_ite = minimum number of iterations 6000 # log_loss.txt inside
end_ite = maximum number of iterations 15000 # log_loss.txt inside
step = 10 # skips number, determine the extent of dense drawing
igore = 0 # loss when starting a larger when you need to ignore the first igore iterations, note that this is the number of iterations

y_ticks = [0.4, 0.5, 0.6 , 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4] # ordinate value or may set up their own.
Path data_path = '2048 / log_loss2.txt' #log_loss of.
result_path = './2048/avg_loss' # save the result.

----------------- #### only need to change the above, the following may not change
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. 9 or X =% STEP )], error_bad_lines =
False, names = names)
result.head ()
for names in name:
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)

### ----------- set value of the abscissa.
len = x_num (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 ( 'D Total =% \ n-' x_num%)
Print ( '% D = Start, 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) # if you do not set up their own vertical axis, you can comment.
plt.grid ()
ax.legend (LOC = 'Best')
ax.set_title ( 'Loss of The Curves')
ax.set_xlabel ( 'Batches')
fig.savefig (result_path)

OI ---------------
Imprt aplotlib.pyplot AS PLT
# The LO
igore = 10
Data_Path = './

Published 41 original articles · won praise 7 · views 3699

Guess you like

Origin blog.csdn.net/weixin_43091087/article/details/102738776