-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathprepare_dataset.py
266 lines (200 loc) · 8.51 KB
/
prepare_dataset.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
"""Prepare image data."""
import argparse
import functools
import os
from pathlib import Path
import numpy as np
import tensorflow as tf
from src.dataset import image_paths, serialize_data
from src.image_np import dct2, load_image, normalize, scale_image
from src.math import log_scale, welford
TRAIN_SIZE = 100_000
VAL_SIZE = 20_000
TEST_SIZE = 30_000
# TRAIN_SIZE = 20_000
# VAL_SIZE = 2_000
# TEST_SIZE = 5_000
def _collect_image_paths(dirictory):
images = list(sorted(image_paths(dirictory)))
assert len(images) >= TRAIN_SIZE + VAL_SIZE + \
TEST_SIZE, f"{len(images)} - {dirictory}"
train_dataset = images[:TRAIN_SIZE]
val_dataset = images[TRAIN_SIZE: TRAIN_SIZE + VAL_SIZE]
test_dataset = images[TRAIN_SIZE +
VAL_SIZE: TRAIN_SIZE + VAL_SIZE + TEST_SIZE]
assert len(
train_dataset) == TRAIN_SIZE, f"{len(train_dataset)} - {dirictory}"
assert len(val_dataset) == VAL_SIZE, f"{len(val_dataset)} - {dirictory}"
assert len(test_dataset) == TEST_SIZE, f"{len(test_dataset)} - {dirictory}"
return (train_dataset, val_dataset, test_dataset)
def collect_all_paths(dirs):
directories = sorted(map(str, filter(
lambda x: x.is_dir(), Path(dirs).iterdir())))
train_dataset = []
val_dataset = []
test_dataset = []
for i, directory in enumerate(directories):
train, val, test = _collect_image_paths(directory)
train = zip(train, [i] * len(train))
val = zip(val, [i] * len(val))
test = zip(test, [i] * len(test))
train_dataset.extend(train)
val_dataset.extend(val)
test_dataset.extend(test)
del train, val, test
train_dataset = np.asarray(train_dataset)
val_dataset = np.asarray(val_dataset)
test_dataset = np.asarray(test_dataset)
np.random.shuffle(train_dataset)
np.random.shuffle(val_dataset)
np.random.shuffle(test_dataset)
return train_dataset, val_dataset, test_dataset
def convert_images(inputs, load_function, transformation_function=None, absolute_function=None, normalize_function=None):
image, label = inputs
image = load_function(image)
if transformation_function is not None:
image = transformation_function(image)
if absolute_function is not None:
image = absolute_function(image)
if normalize_function is not None:
image = normalize_function(image)
return (image, label)
def _dct2_wrapper(image, log=False):
image = np.asarray(image)
image = dct2(image)
if log:
image = log_scale(image)
return image
def create_directory_np(output_path, images, convert_function):
os.makedirs(output_path, exist_ok=True)
converted_images = map(convert_function, images)
labels = []
for i, (img, label) in enumerate(converted_images):
print(f"\rConverted {i:06d} images!", end="")
with open(f"{output_path}/{i:06}.npy", "wb+") as f:
np.save(f, img)
labels.append(label)
with open(f"{output_path}/labels.npy", "wb+") as f:
np.save(f, labels)
def normal_mode(directory, encode_function, outpath):
(train_dataset, val_dataset, test_dataset) = collect_all_paths(directory)
create_directory_np(f"{outpath}_train",
train_dataset, encode_function)
print(f"\nConverted train images!")
create_directory_np(f"{outpath}_val",
val_dataset, encode_function)
print(f"\nConverted val images!")
create_directory_np(f"{outpath}_test",
test_dataset, encode_function)
print(f"\nConverted test images!")
def create_directory_tf(output_path, images, convert_function):
os.makedirs(output_path, exist_ok=True)
converted_images = map(convert_function, images)
converted_images = map(serialize_data, converted_images)
def gen():
i = 0
for serialized in converted_images:
i += 1
print(f"\rConverted {i:06d} images!", end="")
yield serialized
dataset = tf.data.Dataset.from_generator(
gen, output_types=tf.string, output_shapes=())
filename = f"{output_path}/data.tfrecords"
writer = tf.data.experimental.TFRecordWriter(filename)
writer.write(dataset)
def tfmode(directory, encode_function, outpath):
train_dataset, val_dataset, test_dataset = collect_all_paths(directory)
create_directory_tf(f"{outpath}_train_tf",
train_dataset, encode_function)
print(f"\nConverted train images!")
create_directory_tf(f"{outpath}_val_tf",
val_dataset, encode_function)
print(f"\nConverted val images!")
create_directory_tf(f"{outpath}_test_tf",
test_dataset, encode_function)
print(f"\nConverted test images!")
def main(args):
output = f"{args.DIRECTORY.rstrip('/')}"
# we always load images into numpy arrays
# we additionally set a flag if we later convert to tensorflow records
load_function = functools.partial(
load_image, tf=args.mode == "tfrecords")
transformation_function = None
normalize_function = None
absolute_function = None
if args.color:
load_function = functools.partial(load_function, grayscale=False)
output += "_color"
# dct or raw image data?
if args.raw:
output += "_raw"
# normalization sclaes to [-1, 1]
if args.normalize:
normalize_function = scale_image
output += "_normalized"
else:
output += "_dct"
transformation_function = _dct2_wrapper
if args.log:
# log sclae only for dct coefficients
assert args.raw is False
transformation_function = functools.partial(
_dct2_wrapper, log=True)
output += "_log_scaled"
if args.abs:
# normalize to zero mean and unit variance
train, _, _ = collect_all_paths(args.DIRECTORY)
train = train[:TRAIN_SIZE * len(args.DIRECTORY) * 0.1]
images = map(lambda x: x[0], train)
images = map(load_function, images)
images = map(transformation_function, images)
first = next(images)
current_max = np.absolute(first)
for data in images:
max_values = np.absolute(data)
mask = current_max > max_values
current_max *= mask
current_max += max_values * ~mask
def scale_by_absolute(image):
return image / current_max
absolute_function = scale_by_absolute
if args.normalize:
# normalize to zero mean and unit variance
train, _, _ = collect_all_paths(args.DIRECTORY)
images = map(lambda x: x[0], train)
images = map(load_function, images)
images = map(transformation_function, images)
if absolute_function is not None:
images = map(absolute_function, images)
mean, var = welford(images)
std = np.sqrt(var)
output += "_normalized"
normalize_function = functools.partial(
normalize, mean=mean, std=std)
encode_function = functools.partial(convert_images, load_function=load_function,
transformation_function=transformation_function, normalize_function=normalize_function, absolute_function=absolute_function)
if args.mode == "normal":
normal_mode(args.DIRECTORY, encode_function, output)
elif args.mode == "tfrecords":
tfmode(args.DIRECTORY, encode_function, output)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("DIRECTORY", help="Directory to convert.",
type=str)
parser.add_argument("--raw", "-r", help="Save image data as raw image.",
action="store_true")
parser.add_argument("--log", "-l", help="Log scale Images.",
action="store_true")
parser.add_argument("--abs", "-a", help="Scale each feature by its max absolute value.",
action="store_true")
parser.add_argument("--color", "-c", help="Compute as color instead.",
action="store_true")
parser.add_argument("--normalize", "-n", help="Normalize data.",
action="store_true")
modes = parser.add_subparsers(
help="Select the mode {normal|tfrecords}", dest="mode")
_ = modes.add_parser("normal")
_ = modes.add_parser("tfrecords")
return parser.parse_args()
if __name__ == "__main__":
main(parse_args())