-
Notifications
You must be signed in to change notification settings - Fork 4
/
data_provider.py
188 lines (154 loc) · 5.84 KB
/
data_provider.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
"""Loads data from the dataset."""
import os.path
from typing import Iterable, Tuple, Union
import tensorflow as tf
_SHUFFLE_BUFFER_SIZE = 10_000
def image_dataset_from_files(data_dir,
image_shape,
batch_size = 0,
shuffle = True,
repeat = -1):
"""Loads images from individual JPG or PNG files.
Args:
data_dir: Parent directory where input images are located. All JPEG and PNG
files under this directory (either directly or indirectly) will be
included.
image_shape: Shape of the images in (H, W, C) format.
batch_size: 0 means images are not batched. Positive values define the batch
size. The batched images have shape (B, H, W, C).
shuffle: Whether to randomize the order of the images.
repeat: 0 means the dataset is not repeated. -1 means it's repeated
indefinitely. A positive value means it's repeated for the specified
number of times (epochs).
Returns:
A Dataset object containing (H, W, C) or (B, H, W, C) image tensors.
"""
extensions = ['jpg', 'jpeg', 'png', 'JPG', 'JPEG', 'PNG']
# Images directly under the given directory.
globs = [os.path.join(data_dir, f'*.{e}') for e in extensions]
# Images under subdirectories.
globs += [os.path.join(data_dir, '**', f'*.{e}') for e in extensions]
files = tf.data.Dataset.list_files(globs, shuffle, seed=0)
def _parser(file_name):
blob = tf.io.read_file(file_name)
image = tf.io.decode_image(blob, dtype=tf.float32)
image.set_shape(image_shape)
return image
images = files.map(
_parser, num_parallel_calls=tf.data.AUTOTUNE, deterministic=not shuffle)
if repeat < 0:
images = images.repeat()
elif repeat > 0:
images = images.repeat(repeat)
if batch_size > 0:
images = images.batch(batch_size, drop_remainder=True)
images = images.prefetch(tf.data.AUTOTUNE)
return images
def image_dataset_from_tfrecords(globs,
tag,
image_shape,
batch_size = 0,
shuffle = True,
repeat = -1):
"""Loads images from sharded TFRecord files.
Args:
globs: One or more glob pattern matching the TFRecord files.
tag: Name of the TFExample "feature" to decode.
image_shape: Shape of the images in (H, W, C) format.
batch_size: 0 means images are not batched. Positive values define the batch
size. The batched images have shape (B, H, W, C).
shuffle: Whether to randomize the order of the images.
repeat: 0 means the dataset is not repeated. -1 means it's repeated
indefinitely. A positive value means it's repeated for the specified
number of times (epochs).
Returns:
A Dataset object containing (H, W, C) or (B, H, W, C) image tensors.
"""
files = tf.data.Dataset.list_files(globs, shuffle, seed=0)
examples = files.interleave(
tf.data.TFRecordDataset,
num_parallel_calls=tf.data.AUTOTUNE,
deterministic=not shuffle)
if shuffle:
examples = examples.shuffle(
buffer_size=_SHUFFLE_BUFFER_SIZE, seed=0, reshuffle_each_iteration=True)
def _parser(example):
features = tf.io.parse_single_example(
example, features={tag: tf.io.FixedLenFeature([], tf.string)})
image_u8 = tf.reshape(
tf.io.decode_raw(features[tag], tf.uint8), image_shape)
image_f32 = tf.image.convert_image_dtype(image_u8, tf.float32)
return image_f32
images = examples.map(
_parser, num_parallel_calls=tf.data.AUTOTUNE, deterministic=not shuffle)
if repeat < 0:
images = images.repeat()
elif repeat > 0:
images = images.repeat(repeat)
if batch_size > 0:
images = images.batch(batch_size, drop_remainder=True)
images = images.prefetch(tf.data.AUTOTUNE)
return images
def get_scene_dataset(path,
source,
batch_size,
input_shape = (640, 640, 3),
repeat = 0):
"""Returns scene images according to configuration."""
if source == 'tfrecord':
return image_dataset_from_tfrecords(
globs=os.path.join(path, '*.tfrecord'),
tag='image',
image_shape=input_shape,
batch_size=batch_size,
repeat=repeat)
elif source == 'jpg':
return image_dataset_from_files(
data_dir=path,
image_shape=input_shape,
batch_size=batch_size,
repeat=repeat)
else:
raise ValueError('Unrecognized data source', source)
def get_flare_dataset(path,
source,
batch_size,
input_shape = (752, 1008, 3),
repeat = -1):
"""Returns flare images according to configuration."""
if source == 'tfrecord':
return image_dataset_from_tfrecords(
globs=path,
tag='flare',
image_shape=input_shape,
batch_size=batch_size,
repeat=repeat)
elif source == 'jpg':
return image_dataset_from_files(
data_dir=path,
image_shape=input_shape,
batch_size=batch_size,
repeat=repeat)
else:
raise ValueError('Unrecognized data source', source)
def get_flare_dataset2(path,
source,
batch_size,
input_shape = (752, 1008, 3),
repeat = -1):
"""Returns flare images according to configuration."""
if source == 'tfrecord':
return image_dataset_from_tfrecords(
globs=path,
tag='flare',
image_shape=input_shape,
batch_size=batch_size,
repeat=repeat)
elif source == 'jpg':
return image_dataset_from_files(
data_dir=path,
image_shape=input_shape,
batch_size=batch_size,
repeat=repeat)
else:
raise ValueError('Unrecognized data source', source)