-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathanalyse_ofile.py
More file actions
242 lines (174 loc) · 7.37 KB
/
analyse_ofile.py
File metadata and controls
242 lines (174 loc) · 7.37 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import csv
import itertools
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
# Global variables
ofile = open('/z/home/madantrg/tf-activity-recognition-framework/scripts/PBS/i3d/test/MIT_Mini/TFRecords_1gpu_I3D_Train_MIT_Mini_Test_MIT_Mini_Avg_Pooling_250.o82119').readlines()
#ofile = open('/z/home/madantrg/tf-activity-recognition-framework/scripts/PBS/c3d/test/MIT_Mini/TFRecords_1gpu_C3D_Train_MIT_Mini_Test_MIT_Mini.o82125').readlines()
#ofile = open('/z/home/madantrg/tf-activity-recognition-framework/scripts/PBS/tsn/test/MIT_Mini/TFRecords_1gpu_TSN_Train_MIT_Mini_Test_MIT_Mini.o82124').readlines()
lfile = open('/z/dat/Moments_in_Time_Mini/moments_categories.txt').readlines()
def plot_confusion_matrix(true_labels, pred_labels,
classes, normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
""" Plots confusion matrix
Args:
true_labels: List of true labels
pred_labels: List of predicted labels
classes: List of class names
normalize: Boolean indicating normalization of confusion matrix values
title: Title of confusion matrix plot
cmap: Matplotlib color map
Returns:
Nothing
"""
cm = confusion_matrix(true_labels, pred_labels)
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
# END IF
print(cm)
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()
def plot_classwise_recog_acc(true_labels, pred_labels, output_dims, class_labels, delta=50):
""" Plots classwise recognition accuracy across the entire dataset
Args:
true_labels: List of true labels
pred_labels: List of predicted labels
class_labels: List of class names
output_dims: Number of output dimensions
delta: Integer indicating number of classes to display in a figure
Returns:
Nothing
"""
pred_dict = {}
total_dict = {}
# Dictionary initializer
for item in range(output_dims):
pred_dict[str(item)] = 0
total_dict[str(item)] = 0
# END FOR
# Update dictionary element counts
for item in range(len(true_labels)):
total_dict[str(true_labels[item])] += 1
if true_labels[item] == pred_labels[item]:
pred_dict[str(true_labels[item])] += 1
# END IF
# END FOR
# Verification variables
total = 0
corr_total = 0
plotx = []
ploty = []
for item in range(len(total_dict.keys())):
plotx.append(item)
ploty.append(float(pred_dict[str(item)])/total_dict[str(item)])
# Verification variables
total +=total_dict[str(item)]
corr_total +=pred_dict[str(item)]
print total, corr_total
for item in range(0, output_dims, delta):
plt.gcf()
plt.bar(plotx[item:item+delta], ploty[item:item+delta])
plt.xticks(plotx[item:item+delta], class_labels[item:item+delta], rotation='vertical')
title_str = 'Recognition accuracy for class between '+str(item)+':'+str(item + len(plotx[item:item+delta]))
plt.title(title_str)
plt.show()
# END FOR
def print_alt_class(true_labels, pred_labels, class_labels, output_dims, k=3):
""" Print topk alternative class predictions for every dataset
Args:
true_labels: List of true labels
pred_labels: List of predicted labels
class_labels: List of class names
output_dims: Number of output dimensions
k: Number of alternative class predictions to display
Returns:
Nothing
"""
cm = confusion_matrix(true_labels, pred_labels)
for item in range(output_dims):
val_not_class = cm[item, :]
index_not_class = np.argsort(val_not_class)
print "############################################"
print "Current class: ", class_labels[item]
print "Top class prediction: ", class_labels[index_not_class[-1]]
print "Top ",3,
print "Alternative class predicted: ", [class_labels[X] for X in index_not_class[-1-k:-1]]
print "############################################"
print "\n"
def alt_class(true_labels, pred_labels, class_labels, output_dims, k=3):
""" Generate CSV file containing topk alternative class predictions for the entire dataset
Args:
true_labels: List of true labels
pred_labels: List of predicted labels
class_labels: List of class names
output_dims: Number of output dimensions
k: Number of alternative class predictions to display
Returns:
Nothing
"""
cm = confusion_matrix(true_labels, pred_labels)
with open('family_labels.csv','wb') as family_file:
wr = csv.writer(family_file)
for item in range(output_dims):
val_not_class = cm[item, :]
index_not_class = np.argsort(val_not_class)
op_alt_classes = []
op_alt_classes = index_not_class[-k:]
op_alt_classes = [word for word in op_alt_classes if word!=item]
if item in index_not_class[-k:]:
op_alt_classes.insert(0, index_not_class[-k-1])
# END IF
op_alt_class_names = []
for X in op_alt_classes:
op_alt_class_names.append(class_labels[X])
# END FOR
op_alt_classes = op_alt_class_names + op_alt_classes
# END FOR
wr.writerow(op_alt_classes)
# END WITH
if __name__=='__main__':
output_dims = 200
class_labels = []
for label in lfile:
class_labels.append(label.split(',')[0])
# END FOR
################## Parsing output of .O File from required point onwards ###########
data_index = 0
for index in range(len(ofile)):
if 'accuracy' in ofile[index]:
data_index = index + 1
# END IF
# END FOR
data = ofile[data_index]
data = eval(data)
#####################################################################################
# Collect data into required format
true_labels = []
pred_labels = []
for data_item in data:
true_labels.append(int(data_item[1]))
pred_labels.append(int(data_item[0]))
# END FOR
assert(len(true_labels) == len(pred_labels))
# Analysis plots
# 1. Classwise recognition accuracy
#plot_classwise_recog_acc(true_labels, pred_labels, output_dims, class_labels, 50)
# 2. Confusion matrix
#plot_confusion_matrix(true_labels, pred_labels, class_labels, normalize=True, title='Normalized confusion matrix')
# 3. Print top-k alternative class predictions apart from true class
#print_alt_class(true_labels, pred_labels, class_labels, output_dims, 5)
# 4. Generate simple family classes
family_file = alt_class(true_labels, pred_labels, class_labels, output_dims, 5)