forked from saakur/EventSegmentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluateBreakfastActions.py
170 lines (145 loc) · 4.47 KB
/
evaluateBreakfastActions.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
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
import numpy as np
from sys import argv, exit
from sklearn.metrics import jaccard_similarity_score, adjusted_rand_score, precision_score, f1_score, adjusted_rand_score, fowlkes_mallows_score
from scipy import signal
import matplotlib.pyplot as plt
from os import listdir
from os.path import isfile, join, isdir
from math import factorial
from collections import Counter
from math import log
from statsmodels.nonparametric.smoothers_lowess import lowess
import warnings
def warn(*args, **kwargs):
pass
warnings.warn = warn
def movingaverage(interval, window_size):
window = np.ones(int(window_size))/float(window_size)
return np.convolve(interval, window, 'same')
def savitzky_golay(y, window_size, order, deriv=0, rate=1):
try:
window_size = np.abs(np.int(window_size))
order = np.abs(np.int(order))
except ValueError, msg:
raise ValueError("window_size and order have to be of type int")
if window_size % 2 != 1 or window_size < 1:
raise TypeError("window_size size must be a positive odd number")
if window_size < order + 2:
raise TypeError("window_size is too small for the polynomials order")
order_range = range(order+1)
half_window = (window_size -1) // 2
b = np.mat([[k**i for i in order_range] for k in range(-half_window, half_window+1)])
m = np.linalg.pinv(b).A[deriv] * rate**deriv * factorial(deriv)
firstvals = y[0] - np.abs( y[1:half_window+1][::-1] - y[0] )
lastvals = y[-1] + np.abs(y[-half_window-1:-1][::-1] - y[-1])
y = np.concatenate((firstvals, y, lastvals))
return np.convolve( m[::-1], y, mode='valid')
predFilePath = argv[1]
gtFilePath = argv[2]
predFiles = sorted([join(predFilePath, f) for f in listdir(predFilePath) if isfile(join(predFilePath, f)) and f.endswith('.txt')])
gtFiles = {}
for gtFilePath1 in listdir(gtFilePath):
if not isdir(join(gtFilePath, gtFilePath1)):
continue
# print gtFilePath1
for f in listdir(join(gtFilePath, gtFilePath1)):
# print f
if f.endswith('.coarse'):
gtFiles[f.split('.')[0]] = join(gtFilePath, gtFilePath1, f)
avgFrames = []
avgBoundaries = []
avgClasses = []
VoI = []
ARI = []
avgIoD = []
fps=30
winSize = np.ceil(fps/2) // 2 * 2 + 1
print winSize
# print gtFiles
numVideos=0
for predFile in predFiles:
# if 'cereal' not in predFile:
# continue
numVideos += 1
vidKey = predFile.split('/')[-1].split('_')[:3]
vidKey.pop(1)
vidKey= '_'.join(vidKey)
# print "\n\n", vidKey, predFile
vidKey = vidKey.replace('salat', 'salad')
vidKey = vidKey.replace('cereals', 'cereal')
gtFile = gtFiles[vidKey]
gtFrames = []
predFrames = []
predFrames1 = []
predErrors = []
avgFr = []
classNo = 0
BGClass = []
actBoundary = []
# print gtFile
with open(gtFile, 'rb') as file:
for line in file:
# print line
data, className = line.replace(' \n', '').replace(' ', '').split(' ')
if className == 'SIL':
BGClass.append(classNo)
fromFrame,toFrame = [int(x) for x in data.split('-')]
avgFr.append(toFrame - fromFrame)
actBoundary.append(toFrame)
# if toFrame - fromFrame == 0:
# print gtFile, line
for i in range(fromFrame, toFrame+1):
gtFrames.append(classNo)
classNo += 1
avgFrames.append(np.mean(avgFr))
with open(predFile, 'rb') as file:
lineNo = 0
for line in file:
data = line.replace('\n', '').split('\t')
frameNo,predError = data
frameNo, predError = int(frameNo), float(predError)
predErrors.append(predError)
# print predErrors
predErrors_Ori = predErrors
predErrors = movingaverage(predErrors, 80)
predErrors = np.gradient(np.array(predErrors)).tolist()
predBoundaries = signal.argrelextrema(np.array(predErrors), np.greater, order=int(0.57899*200))[0].tolist()
predBoundaries.append(len(gtFrames)-1)
prevFrame = 0
predClass = 0
for nextFrame in predBoundaries:
for i in range(prevFrame, nextFrame):
predFrames.append(predClass)
prevFrame = nextFrame
predClass += 1
if len(predFrames) < len(gtFrames):
x = gtFrames[:len(predFrames)]
y = predFrames
else:
x = gtFrames
y = predFrames[:len(gtFrames)]
TP = 0
FP = 0
TN = 0
FN = 0
ignoreClass = 0
d = {x:0 for x in range(classNo)}
for i in range(len(y)):
if y[i] >= classNo:
ignoreClass += 1
if y[i] == x[i]:
d[y[i]] += 1
TP += 1
else:
FP += 1
p = []
for c,val in d.iteritems():
detNo = sum([1 for i in y if i == c])
if detNo == 0:
continue
p.append(1.0*val/detNo)
p = np.mean(p)
avgClasses.append(classNo)
print predBoundaries, actBoundary, p
avgIoD.append(p)
print "MoF", np.mean(avgIoD)