-
Notifications
You must be signed in to change notification settings - Fork 92
/
loss_from_log.py
123 lines (95 loc) · 3.54 KB
/
loss_from_log.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python
# Martin Kersner, 2016/01/13
from __future__ import print_function
import sys
import re
import numpy as np
import matplotlib.pyplot as plt
from utils import strstr
def main():
log_files = process_arguments(sys.argv)
train_iteration = []
train_loss = []
test_iteration = []
test_loss = []
test_accuracy = []
pixel_accuracy = []
mean_accuracy = []
mean_IU = []
frequency_weighted_IU = []
base_test_iter = 0
base_train_iter = 0
for log_file in log_files:
with open(log_file, 'rb') as f:
if len(train_iteration) != 0:
base_train_iter = train_iteration[-1]
base_test_iter = test_iteration[-1]
for line in f:
# TRAIN NET
if strstr(line, 'Iteration') and strstr(line, 'lr'):
matched = match_iteration(line)
train_iteration.append(int(matched.group(1))+base_train_iter)
elif strstr(line, 'Train net output'):
matched = match_loss(line)
train_loss.append(float(matched.group(1)))
elif strstr(line, 'pixel_accuracy'):
matched = re.search(r'pixel_accuracy: (.*)', line)
pixel_accuracy.append(float(matched.group(1)))
elif strstr(line, 'mean_accuracy'):
matched = re.search(r'mean_accuracy: (.*)', line)
mean_accuracy.append(float(matched.group(1)))
elif strstr(line, 'mean_IU'):
matched = re.search(r'mean_IU: (.*)', line)
mean_IU.append(float(matched.group(1)))
elif strstr(line, 'frequency_weighted'):
matched = re.search(r'frequency_weighted: (.*)', line)
frequency_weighted_IU.append(float(matched.group(1)))
# TEST NET
elif strstr(line, 'Testing net'):
matched = match_iteration(line)
test_iteration.append(int(matched.group(1))+base_test_iter)
elif strstr(line, 'Test net output'):
matched = match_loss(line)
if matched:
test_loss.append(float(matched.group(1)))
else:
matched = match_accuracy(line)
test_accuracy.append(float(matched.group(1)))
print("TRAIN", train_iteration, train_loss)
print("TEST", test_iteration, test_loss)
print("ACCURACY", test_iteration, test_accuracy)
# loss
plt.plot(train_iteration, train_loss, 'k', label='Train loss')
plt.plot(test_iteration, test_loss, 'r', label='Test loss')
plt.legend()
plt.ylabel('Loss')
plt.xlabel('Number of iterations')
plt.savefig('loss.png')
# evaluation
plt.clf()
plt.plot(range(len(pixel_accuracy)), pixel_accuracy, 'k', label='pixel accuracy')
plt.plot(range(len(mean_accuracy)), mean_accuracy, 'r', label='mean accuracy')
plt.plot(range(len(mean_IU)), mean_IU, 'g', label='mean IU')
plt.plot(range(len(frequency_weighted_IU)), frequency_weighted_IU, 'b', label='frequency weighted IU')
plt.legend(loc=0)
plt.savefig('evaluation.png')
def match_iteration(line):
return re.search(r'Iteration (.*),', line)
def match_loss(line):
return re.search(r'loss-ft = (.*) \(', line)
def match_accuracy(line):
return re.search(r'seg-accuracy = (.*)', line)
def process_arguments(argv):
if len(argv) < 2:
help()
log_files = argv[1:]
return log_files
def help():
print('Usage: python loss_from_log.py [LOG_FILE]+\n'
'LOG_FILE is text file containing log produced by caffe.'
'At least one LOG_FILE has to be specified.'
'Files has to be given in correct order (the oldest logs as the first ones).'
, file=sys.stderr)
exit()
if __name__ == '__main__':
main()