-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
86 lines (70 loc) · 2.97 KB
/
main.py
File metadata and controls
86 lines (70 loc) · 2.97 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
from glob import glob
import numpy as np
from utils import load_pkl, dump_pkl
from config.global_parameters import frameWidth, frameHeight, genreLabels
from config.resources import video_resource
from video import extract_feature_video, gather_videos
from model_utils import spatial_model
from keras.utils.np_utils import to_categorical
"""testing hualos"""
from keras import callbacks
remote = callbacks.RemoteMonitor(root='http://localhost:9000')
#collect videos for each genre
#extract spatial features for each
#create your model
#train/fit it
#save the model
#test it
def gather_genre(genre, limit_videos=100):
print "Gathering features for",genre,
genreFeatures = gather_videos(genre, limit_videos)
print "OK."
print genreFeatures.shape
dump_pkl(genreFeatures, genre+str(limit_videos))
def gather():
gather_genre('action', limit_videos=3)
gather_genre('horror',3)
gather_genre('romance',3)
def train_classifier(genres=['romance', 'horror', 'action'], num_of_videos=100):
"""Gather data for selected genres"""
trainingData = []
trainingLabels = []
num_of_random_frames = 75
num_of_classes = len(genres)
print "Number of classes:",num_of_classes
for genreIndex, genre in enumerate(genres):
print "Looking for pickle file: data/{0}{1}.p".format(genre, str(num_of_videos)),
try:
genreFeatures = load_pkl(genre+str(num_of_videos))
genreFeatures = np.array([np.array(f) for f in genreFeatures]) # numpy hack
except Exception as e:
print e
return
print "OK."
for videoFeatures in genreFeatures:
if len(videoFeatures) > num_of_random_frames:
randomIndices = np.random.randint(0, len(videoFeatures), num_of_random_frames)
selectedFeatures = np.array(videoFeatures[randomIndices])
for feature in selectedFeatures:
trainingData.append(feature)
trainingLabels.append([genreIndex])
trainingData = np.array(trainingData)
trainingLabels = np.array(trainingLabels)
print trainingData.shape
print trainingLabels.shape
# trainingLabels = to_categorical(trainingLabels, num_of_classes)
print trainingLabels
# trainingLabels = trainingLabels.reshape((-1,num_of_classes))
"""Initialize the mode"""
model = spatial_model(num_of_classes)
model.compile(optimizer='sgd', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
"""Start training"""
batch_size = 32
nb_epoch = 50
model.fit(trainingData, trainingLabels, batch_size=batch_size, nb_epoch=nb_epoch)#, callbacks=[remote])
modelOutPath ='data/models/spatial_'+str(num_of_classes)+"g_bs"+str(batch_size)+"_ep"+str(nb_epoch)+"_nf_"+str(num_of_random_frames)+".h5"
model.save(modelOutPath)
print "Model saved at",modelOutPath
if __name__=="__main__":
from sys import argv
train_classifier(genres=['action', 'horror', 'comedy'],num_of_videos=100)