-
Notifications
You must be signed in to change notification settings - Fork 30
/
data_loader.py
181 lines (158 loc) · 5.61 KB
/
data_loader.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
import torch
import os
import random
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
from torchvision import transforms
from PIL import Image
import numpy as np
import imageio
import tqdm
import glob
class BP4D(Dataset):
def __init__(self,
image_size,
metadata_path,
transform,
mode,
shuffling=False,
OF=False,
verbose=False):
self.transform = transform
self.mode = mode
self.shuffling = shuffling
self.image_size = image_size
self.OF = OF
self.verbose = verbose
self.meta = '/home/afromero/datos/Databases/BP4D/'
self.metaSSD = '/home/afromero/ssd2'
# self.metaSSD = '../BP4D'
if mode == 'sample':
mode = 'train'
file_txt = os.path.join(metadata_path, mode + '.txt')
self.lines = open(file_txt, 'r').readlines()
if verbose:
print('Start preprocessing dataset (OF: %s): %s | file: %s!' %
(str(OF), mode, file_txt))
random.seed(1234)
self.preprocess()
if verbose:
print('Finished preprocessing dataset (OF: %s): %s!' % (str(OF),
mode))
self.num_data = len(self.filenames)
def preprocess(self):
self.filenames = []
self.labels = []
lines = [i.replace(self.meta, '').strip() for i in self.lines]
if self.shuffling:
random.shuffle(lines) # random shuffling
if self.verbose:
iter_ = tqdm.tqdm(
enumerate(lines),
ncols=10,
total=len(lines),
desc='Preprocessing...')
else:
iter_ = enumerate(lines)
for i, line in iter_:
splits = line.split()
filename = splits[0]
if self.OF:
filename = filename.replace('Faces', 'Faces_Flow')
label = [int(splits[1])]
if self.mode == 'sample' and 'Jitter' in filename:
continue
self.filenames.append(filename)
self.labels.append(label)
def __getitem__(self, index):
img_file = os.path.join(self.metaSSD, self.filenames[index])
if not os.path.isfile(img_file):
print('%s not found' % (img_file))
imageio.imwrite(
img_file,
np.zeros((self.image_size, self.image_size,
3)).astype(np.uint8))
image = Image.open(img_file)
label = self.labels[index]
return self.transform(image), torch.FloatTensor(
label), self.filenames[index]
def __len__(self):
return self.num_data
class DEMO(Dataset):
def __init__(self, path, transform, OF=False):
self.transform = transform
if OF and os.path.isfile(path):
raise TypeError(
"Cannot perform DEMO with Optical Flow for one single image. \
Please supply a directory with more than one image")
if OF:
of_path = path + '_OF'
if os.path.isfile(path):
self.filenames = [path]
elif os.path.isdir(path):
self.filenames = glob.glob(path + '/*')
if OF and len(glob.glob(of_path + '/*')) <= 1:
raise TypeError(
"Cannot perform DEMO with Optical Flow for one single \
image. Please supply a directory with more than one \
image")
elif OF and not os.path.isdir(of_path):
os.makedirs(of_path)
os.system(
'./generate_data/OF/broxDir --source {} --ziel {}'.format(
path, of_path))
if OF:
of_filenames = glob.glob(of_path + '/*')
assert len(self.filenames) == len(
of_filenames), "RGB and OF images must be the same"
self.filenames = of_filenames
self.filenames = sorted(self.filenames)
self.num_data = len(self.filenames)
def __getitem__(self, index):
image = Image.open(self.filenames[index])
return self.transform(image), torch.FloatTensor(
[0]), self.filenames[index]
def __len__(self):
return self.num_data
def get_loader(metadata_path,
crop_size,
image_size,
batch_size,
mode='train',
imagenet=False,
OF=False,
num_workers=0,
verbose=False,
demo=''):
"""Build and return data loader."""
# ImageNet normalization
if imagenet:
normalize = transforms.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
else:
normalize = transforms.Normalize(
mean=[0.485, 0.456, 0.406], std=[1.0, 1.0, 1.0])
transform = transforms.Compose([
transforms.Resize((image_size, image_size),
interpolation=Image.ANTIALIAS),
transforms.ToTensor(),
normalize,
])
if demo:
batch_size = 1
dataset = DEMO(demo, transform, OF=OF)
else:
dataset = BP4D(
image_size,
metadata_path,
transform,
mode,
shuffling=mode == 'train' or mode == 'sample',
OF=OF,
verbose=verbose)
data_loader = DataLoader(
dataset=dataset,
batch_size=batch_size,
shuffle=False,
num_workers=num_workers)
return data_loader