#!/usr/bin/python
# -*- coding: UTF-8 -*-
import numpy as np
import matplotlib.pyplot as plt
#from urlgrabber.grabber import _file_object_test
from cProfile import label
############################################################
"""
在终端中输入
python C:\MYCaffe\caffe-master\tools\extra\parse_log.py D:\CaffeInfo\D_TrainVal\train_output.log D:\CaffeInfo\D_TrainVal
得到
train_output.log.test
train_output.log.train
"""
############################################################
def size_text_file (file_object):
str_file_object = str(file_object)
s1 = "open file '/"
s2 = "', mode"
a=0
b=0
i=0
for ch in str_file_object:
if str_file_object[i : i+len(s1) - 1] == s1[0:-1] :
a = i+len(s1) - 1
elif str_file_object[i : i+len(s2) - 1] == s2[0:-1] :
b=i
i = i + 1
#close the object
file_object.close()
print("path2text_file/name2text_file: " + str_file_object[a:b] + "\n")
#open the object
file_object = open(str_file_object[a:b])
count_rows = 0
count_cols = 0
line = file_object.readline()
for ch in line:
if ch == ',' or ch == '\r' :
count_cols = count_cols+1
while line:
line = file_object.readline()
count_rows = count_rows + 1
#close the object
file_object.close()
return [ count_rows, count_cols ]
############################################################
def data_text_file(file_object):
str_file_object = str(file_object)
s1 = "open file '/"
s2 = "', mode"
a=0
b=0
i=0
for ch in str_file_object:
if str_file_object[i : i+len(s1) - 1] == s1[0:-1] :
a = i+len(s1) - 1
elif str_file_object[i : i+len(s2) - 1] == s2[0:-1] :
b=i
i = i + 1
[ count_rows, count_cols ] = size_text_file (file_object)
file_object.close()
print("path2text_file/name2text_file: " + str_file_object[a:b] + "\n")
file_object = open(str_file_object[a:b])
line = file_object.readline()
k=0
p=-1
i=0
j=0
data = np.zeros( ( count_rows - 1, count_cols ) )
while line:
line = file_object.readline()
for ch in line:
if ch == ',' or ch == '\r' :
data[i][j] =float ( line[p+1:k] )
p=k
j=j+1
k=k+1
i=i+1
k=0
p=-1
j=0
file_object.close()
return data
############################################################
############################################################
file_train_log = open("D:\CaffeInfo\\D_TrainVal\\train_output.log.train")
file_train_log_data = data_text_file( file_train_log )
print(file_train_log_data)
############################################################
file_test_log = open( "D:\\CaffeInfo\\D_TrainVal\\train_output.log.test" )
file_test_log_data = data_text_file( file_test_log )
############################################################
print '\nplot the train loss and test accuracy\n'
_,ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(file_train_log_data[0:, 0], file_train_log_data[0:, 3], 'k', label = 'train loss')
ax1.plot(file_test_log_data[0:, 0], file_test_log_data[0:, 4], 'g', label = 'test loss')
ax2.plot(file_test_log_data[0:, 0], file_test_log_data[0:, 3], 'r', label = 'test accuracy')
ax1.set_xlabel('iteration')
ax1.set_ylabel('loss')
ax2.set_ylabel('accuracy')
handles1, labels1 = ax1.get_legend_handles_labels()
handles2, labels2 = ax2.get_legend_handles_labels()
ax1.legend(handles1[::-1], labels1[::-1], bbox_to_anchor=(0.74,0.11)) #left right, up down
ax2.legend(handles2[::-1], labels2[::-1], bbox_to_anchor=(0.6,0.8))
plt.show()