-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatagenerator.py
218 lines (156 loc) · 7.41 KB
/
datagenerator.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
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
import glob
import os
import sys
import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
import keras
from frame import files2frames, images_normalize, frames_show
class FramesGenerator(keras.utils.Sequence):
def __init__(self, sPath:str, \
nBatchSize:int, nFrames:int, nHeight:int, nWidth:int, nChannels:int, \
liClassesFull:list = None, bShuffle:bool = True):
'Initialization'
self.nBatchSize = nBatchSize
self.nFrames = nFrames
self.nHeight = nHeight
self.nWidth = nWidth
self.nChannels = nChannels
self.tuXshape = (nFrames, nHeight, nWidth, nChannels)
self.bShuffle = bShuffle
# retrieve all videos = frame directories
self.dfVideos = pd.DataFrame(sorted(glob.glob(sPath + "/*/*")), columns=["sFrameDir"])
self.nSamples = len(self.dfVideos)
# if self.nSamples == 0: raise ValueError("Found no frame directories files in " + sPath)
print("Detected %d samples in %s ..." % (self.nSamples, sPath))
# extract (text) labels from path
seLabels = self.dfVideos.sFrameDir.apply(lambda s: s.split("/")[-2])
self.dfVideos.loc[:, "sLabel"] = seLabels
# extract unique classes from all detected labels
self.liClasses = sorted(list(self.dfVideos.sLabel.unique()))
self.nClasses = len(self.liClasses)
# encode labels
trLabelEncoder = LabelEncoder()
trLabelEncoder.fit(self.liClasses)
self.dfVideos.loc[:, "nLabel"] = trLabelEncoder.transform(self.dfVideos.sLabel)
self.on_epoch_end()
return
def __len__(self):
'Denotes the number of batches per epoch'
return int(np.ceil(self.nSamples / self.nBatchSize))
def on_epoch_end(self):
'Updates indexes after each epoch'
self.indexes = np.arange(self.nSamples)
if self.bShuffle == True:
np.random.shuffle(self.indexes)
def __getitem__(self, nStep):
'Generate one batch of data'
# Generate indexes of the batch
indexes = self.indexes[nStep*self.nBatchSize:(nStep+1)*self.nBatchSize]
# get batch of videos
dfVideosBatch = self.dfVideos.loc[indexes, :]
nBatchSize = len(dfVideosBatch)
# initialize arrays
arX = np.empty((nBatchSize, ) + self.tuXshape, dtype = float)
arY = np.empty((nBatchSize), dtype = int)
# Generate data
for i in range(nBatchSize):
# generate data for single video(frames)
arX[i,], arY[i] = self.__data_generation(dfVideosBatch.iloc[i,:])
#print("Sample #%d" % (indexes[i]))
# onehot the labels
return arX, keras.utils.to_categorical(arY, num_classes=self.nClasses)
def __data_generation(self, seVideo:pd.Series) -> (np.array(float), int):
"Returns frames for 1 video, including normalizing & preprocessing"
# Get the frames from disc
ar_nFrames = files2frames(seVideo.sFrameDir)
# only use the first nChannels (typically 3, but maybe 2 for optical flow)
ar_nFrames = ar_nFrames[..., 0:self.nChannels]
ar_fFrames = images_normalize(ar_nFrames, self.nFrames, self.nHeight, self.nWidth, bRescale = True)
return ar_fFrames, seVideo.nLabel
def data_generation(self, seVideo:pd.Series) -> (np.array(float), int):
return self.__data_generation(seVideo)
class FeaturesGenerator(keras.utils.Sequence):
"""Reads and yields (preprocessed) I3D features for Keras.model.fit_generator
Generator can be used for multi-threading.
Substantial initialization and checks upfront, including one-hot-encoding of labels.
"""
def __init__(self, sPath:str, nBatchSize:int, tuXshape, \
liClassesFull:list = None, bShuffle:bool = True):
"""
Assume directory structure:
... / sPath / class / feature.npy
"""
'Initialization'
self.nBatchSize = nBatchSize
self.tuXshape = tuXshape
self.bShuffle = bShuffle
# retrieve all feature files
self.dfSamples = pd.DataFrame(sorted(glob.glob(sPath + "/*/*.npy")), columns=["sPath"])
self.nSamples = len(self.dfSamples)
# if self.nSamples == 0: raise ValueError("Found no feature files in " + sPath)
# print("Detected %d samples in %s ..." % (self.nSamples, sPath))
# test shape of first sample
arX = np.load(self.dfSamples.sPath[0])
if arX.shape != tuXshape: raise ValueError("Wrong feature shape: " + str(arX.shape) + str(tuXshape))
# extract (text) labels from path
seLabels = self.dfSamples.sPath.apply(lambda s: s.split("/")[-2])
self.dfSamples.loc[:, "sLabel"] = seLabels
# extract unique classes from all detected labels
self.liClasses = sorted(list(self.dfSamples.sLabel.unique()))
# if classes are provided upfront
if liClassesFull != None:
liClassesFull = sorted(np.unique(liClassesFull))
# check detected vs provided classes
if set(self.liClasses).issubset(set(liClassesFull)) == False:
raise ValueError("Detected classes are NOT subset of provided classes")
# use superset of provided classes
self.liClasses = liClassesFull
self.nClasses = len(self.liClasses)
# encode labels
trLabelEncoder = LabelEncoder()
trLabelEncoder.fit(self.liClasses)
self.dfSamples.loc[:, "nLabel"] = trLabelEncoder.transform(self.dfSamples.sLabel)
self.on_epoch_end()
return
def __len__(self):
'Denotes the number of batches per epoch'
return int(np.ceil(self.nSamples / self.nBatchSize))
def on_epoch_end(self):
'Updates indexes after each epoch'
self.indexes = np.arange(self.nSamples)
if self.bShuffle == True:
np.random.shuffle(self.indexes)
def __getitem__(self, nStep):
'Generate one batch of data'
# Generate indexes of the batch
indexes = self.indexes[nStep*self.nBatchSize:(nStep+1)*self.nBatchSize]
# Find selected samples
dfSamplesBatch = self.dfSamples.loc[indexes, :]
nBatchSize = len(dfSamplesBatch)
# initialize arrays
arX = np.empty((nBatchSize, ) + self.tuXshape, dtype = float)
arY = np.empty((nBatchSize), dtype = int)
# Generate data
for i in range(nBatchSize):
# generate single sample data
arX[i,], arY[i] = self.__data_generation(dfSamplesBatch.iloc[i,:])
# onehot the labels
return arX, keras.utils.to_categorical(arY, num_classes=self.nClasses)
def __data_generation(self, seSample:pd.Series) -> (np.array(float), int):
'Generates data for 1 sample'
arX = np.load(seSample.sPath)
return arX, seSample.nLabel
class VideoClasses():
"""
Loads the video classes (incl descriptions) from a csv file
"""
def __init__(self, sClassFile:str):
# load label description: index, sClass, sLong, sCat, sDetail
self.dfClass = pd.read_csv(sClassFile)
# sort the classes
self.dfClass = self.dfClass.sort_values("sClass").reset_index(drop=True)
self.liClasses = list(self.dfClass.sClass)
self.nClasses = len(self.dfClass)
print("Loaded %d classes from %s" % (self.nClasses, sClassFile))
return