-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
125 lines (96 loc) · 3.52 KB
/
utils.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
124
125
from __future__ import division
import csv
import numpy as np
import pdb
import os
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
class Logger(object):
def __init__(self, path, header, overlay=True):
if overlay:
self.log_file = open(path, 'w')
self.logger = csv.writer(self.log_file, delimiter='\t')
self.logger.writerow(header)
else:
self.log_file = open(path, 'a')
self.logger = csv.writer(self.log_file, delimiter='\t')
self.header = header
def __del(self):
self.log_file.close()
def log(self, values):
write_values = []
for col in self.header:
assert col in values
write_values.append(values[col])
self.logger.writerow(write_values)
self.log_file.flush()
class Logger_MARS(object):
def __init__(self, path, header, resume_path, begin_epoch):
if resume_path == '':
self.log_file = open(path, 'w+')
self.logger = csv.writer(self.log_file, delimiter='\t')
self.logger.writerow(header)
else:
self.log_file = open(path, 'r+')
self.logger = csv.writer(self.log_file, delimiter='\t')
reader = csv.reader(self.log_file, delimiter='\t')
lines = []
print("begin = ", begin_epoch)
for line in reader:
lines.append(line)
if len(lines) == begin_epoch +1 :
break
self.log_file.close()
self.log_file = open(path, 'w')
self.logger = csv.writer(self.log_file, delimiter='\t')
self.logger.writerows(lines[:begin_epoch+1])
self.log_file.flush()
self.header = header
def __del(self):
self.log_file.close()
def log(self, values):
write_values = []
for col in self.header:
assert col in values
write_values.append(values[col])
self.logger.writerow(write_values)
self.log_file.flush()
def load_value_file(file_path):
with open(file_path, 'r') as input_file:
value = float(input_file.read().rstrip('\n\r'))
return value
def calculate_accuracy(outputs, targets):
batch_size = targets.size(0)
_, pred = outputs.topk(1, 1, True)
pred = pred.t()
correct = pred.eq(targets.view(1, -1))
n_correct_elems = correct.float().sum().item()
return n_correct_elems / batch_size
def calculate_accuracy5(outputs, targets):
batch_size = targets.size(0)
pred = outputs.topk(5, 1, True)[1].data[0].tolist()
print("true = ", targets.view(1, -1).data[0].tolist()[0], "pred = ", pred)
correct = targets.view(1, -1).data[0].tolist()[0] in pred
print(correct)
n_correct_elems = int(correct)
return n_correct_elems / batch_size
def calculate_accuracy_video(output_buffer, i):
true_value = output_buffer[: i+1,-1]
pred_value = np.argmax(output_buffer[:i+1, :-1], axis = 1)
# print(output_buffer[0:3,:])
# print(true_value)
# print(pred_value)
# print("accuracy = ", 1*(np.equal(true_value, pred_value)).sum()/len(true_value))
return 1*(np.equal(true_value, pred_value)).sum()/len(true_value)