-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfeature_generator.py
92 lines (63 loc) · 2.8 KB
/
feature_generator.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
import numpy as np
from random import shuffle
import glob
class CustomDataGenerator(object):
def __init__(self,
data_path_action,
data_path_context,
batch_size=32,
temporal_length=50,
N_C=21):
self.batch_size = batch_size
self.data_path_action = data_path_action
self.data_path_context = data_path_context
self.temporal_length = temporal_length
self.classes = N_C
self.features_action = glob.glob(self.data_path_action + '/feature_*.npy')
self.features_action.sort()
print(self.data_path_context)
self.features_context = glob.glob(self.data_path_context + '/feature_*.npy')
self.features_context.sort()
self.labels = glob.glob(self.data_path_context + '/label_*.npy')
self.labels.sort()
self.pairs = zip(self.features_context, self.features_action, self.labels)
shuffle(self.pairs)
self.data_size = len(self.pairs)
self.current = 0
def generate(self):
while True:
if self.current < self.data_size - self.batch_size:
X_c = np.zeros((self.batch_size,self.temporal_length, 4096))
X_a = np.zeros((self.batch_size, self.temporal_length, 4096))
y = np.zeros((self.batch_size, self.temporal_length, self.classes))
cnt = 0
for pair in range(self.current,self.current+self.batch_size):
X_c[cnt] = np.load(self.pairs[pair][0])
X_a[cnt] = np.load(self.pairs[pair][1])
y[cnt] = np.load(self.pairs[pair][2])
cnt += 1
yield X_c, X_a, y, y
self.current += self.batch_size
else:
self.current = 0
shuffle(self.pairs)
X_c = np.zeros((self.batch_size, self.temporal_length, 4096))
X_a = np.zeros((self.batch_size, self.temporal_length, 4096))
y = np.zeros((self.batch_size, self.temporal_length, self.classes))
cnt = 0
for pair in range(self.current, self.current + self.batch_size):
X_c[cnt] = np.load(self.pairs[pair][0])
X_a[cnt] = np.load(self.pairs[pair][1])
y[cnt] = np.load(self.pairs[pair][2])
cnt += 1
yield X_c, X_a, y, y
self.current += self.batch_size
'''
how to use:
train_generator = CustomDataGenerator(*params1).generator()
validation_generator = CustomDataGenerator(*params2).generator()
model.fit_generator(generator = training_generator,
validation_data = validation_generator,
nb_epoch = 50,
verbose = 1)
'''