-
Notifications
You must be signed in to change notification settings - Fork 0
/
training.py
53 lines (45 loc) · 1.73 KB
/
training.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
import numpy as np
import motionInfuenceGenerator as mig
import createMegaBlocks as cmb
from pyspark import SparkContext
import sys
import time
def reject_outliers(data, m=2):
return data[abs(data - np.mean(data)) < m * np.std(data)]
def write_mega_block_to_file(megaBlockMotInfVal):
print ("megaBlockMotInfVal file write starts")
thefile = open('megaBlockMotInfVal.txt', 'w')
for x in megaBlockMotInfVal:
thefile.write("%s\n" % x)
thefile.close()
print ("megaBlockMotInfVal file write ends")
def train_from_video(vid, sc):
'''
calls all methods to train from the given video
May return codewords or store them.
'''
print "Training From ", vid
MotionInfOfFrames, rows, cols = mig.getMotionInfuenceMap(vid, sc)
#print "Motion Inf Map", len(MotionInfOfFrames)
#numpy.save("MotionInfluenceMaps", np.array(MotionInfOfFrames), allow_pickle=True, fix_imports=True)
megaBlockMotInfVal = cmb.createMegaBlocks(MotionInfOfFrames, rows, cols)
#np.save("/home/yash/work/project/unusual/code/videos/scene1/megaBlockMotInfVal_set1_p1_train_40-40_k5.npy",megaBlockMotInfVal)
# write_mega_block_to_file(megaBlockMotInfVal)
codewords = cmb.kmeans_map_reduce(megaBlockMotInfVal, sc)
#cmb.kmeans(megaBlockMotInfVal)
#sc.stop()
#sys.exit()
np.save("/codewords.npy",codewords)
return
if __name__ == '__main__':
'''
defines training set and calls trainFromVideo for every vid
'''
trainingSet = [r"/2_train1.avi"]
sc = SparkContext("local", "Simple App")
start_time = time.time()
for video in trainingSet:
train_from_video(video, sc)
print("--- %s seconds ---" % (time.time() - start_time))
sc.stop()
print "Done"