-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerator.py
80 lines (67 loc) · 2.71 KB
/
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
import numpy as np
import os
import pandas as pd
from keras.utils import Sequence
from PIL import Image
from skimage.transform import resize
from skimage import exposure
from sklearn.preprocessing import StandardScaler, MinMaxScaler
class FeatruesSequence(Sequence):
"""
Thread-safe image generator with imgaug support
For more information of imgaug see: https://github.com/aleju/imgaug
"""
def __init__(self, dataset_csv_file, batch_size=16, verbose=0, shuffle_on_epoch_end=True,
random_state=1, test=False, n_timesteps=180):
"""
:param dataset_csv_file: str, path of dataset csv file
:param batch_size: int
:param verbose: int
"""
self.dataset_df = pd.read_csv(dataset_csv_file)
self.batch_size = batch_size
self.verbose = verbose
self.shuffle = shuffle_on_epoch_end
self.random_state = random_state
self.test = test
self.n_timesteps = n_timesteps
self.prepare_dataset()
self.steps = int(np.ceil(len(self.x_path) / float(self.batch_size)))
def __bool__(self):
return True
def __len__(self):
return self.steps
def __getitem__(self, idx):
batch_x_path = self.x_path[idx * self.batch_size:(idx + 1) * self.batch_size]
batch_x = np.asarray([self.load_au_csv(x_path) for x_path in batch_x_path])
batch_y = self.y[idx * self.batch_size:(idx + 1) * self.batch_size]
return batch_x, batch_y
def load_au_csv(self, openface_file):
features_array = pd.read_csv(openface_file).values.astype('float32')
if features_array.shape[0] == 0:
print(f'-----------------Error:{openface_file} size is 0!!!')
else:
stdsc = StandardScaler()
# stdsc = MinMaxScaler()
features_array = stdsc.fit_transform(features_array)
return features_array
def get_y_true(self):
"""
Use this function to get y_true for predict_generator
In order to get correct y, you have to set shuffle_on_epoch_end=False.
"""
if self.shuffle:
raise ValueError("""
You're trying run get_y_true() when generator option 'shuffle_on_epoch_end' is True.
""")
return self.y[:self.steps*self.batch_size]
def prepare_dataset(self):
if self.test:
df = self.dataset_df
else:
df = self.dataset_df.sample(frac=1., random_state=self.random_state)
self.x_path, self.y = df["file_path"].values, df[["frame_start", "frame_end"]].values.astype('float32') / self.n_timesteps
def on_epoch_end(self):
if self.shuffle:
self.random_state += 1
self.prepare_dataset()