-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_loader.py
More file actions
286 lines (238 loc) · 10.8 KB
/
data_loader.py
File metadata and controls
286 lines (238 loc) · 10.8 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import re
from typing import Dict, List, Optional, Text, Tuple
import tensorflow as tf
import numpy as np
from tqdm import tqdm
from typing import Callable, Tuple
from tensorflow.keras.layers import *
from tensorflow.keras.models import Model
from tensorflow.keras import backend as K
from tensorflow.python.keras.utils.losses_utils import reduce_weighted_loss
INPUT_FEATURES = ['elevation', 'th', 'vs', 'tmmn', 'tmmx', 'sph',
'pr', 'pdsi', 'NDVI', 'population', 'erc', 'PrevFireMask']
OUTPUT_FEATURES = ['FireMask', ]
# Data statistics
# For each variable, the statistics are ordered in the form:
# (min_clip, max_clip, mean, standard deviation)
DATA_STATS = {
# Elevation in m.
'elevation': (0.0, 3141.0, 657.3003, 649.0147),
# Pressure
'pdsi': (-6.12974870967865, 7.876040384292651, -0.0052714925, 2.6823447),
'NDVI': (-9821.0, 9996.0, 5157.625, 2466.6677), # min, max
# Precipitation in mm.
'pr': (0.0, 44.53038024902344, 1.7398051, 4.482833),
# Specific humidity.
'sph': (0., 1., 0.0071658953, 0.0042835088),
# Wind direction in degrees clockwise from north.
'th': (0., 360.0, 190.32976, 72.59854),
# Min/max temperature in Kelvin.
'tmmn': (253.15, 298.94891357421875, 281.08768, 8.982386),
'tmmx': (253.15, 315.09228515625, 295.17383, 9.815496),
# Wind speed in m/s.
'vs': (0.0, 10.024310074806237, 3.8500874, 1.4109988),
# NFDRS fire danger index energy release component expressed in BTU's per square foot.
'erc': (0.0, 106.24891662597656, 37.326267, 20.846027),
# Population density
'population': (0., 2534.06298828125, 25.531384, 154.72331),
# 1 indicates fire, 0 no fire, -1 unlabeled data
'PrevFireMask': (-1., 1., 0., 1.),
'FireMask': (-1., 1., 0., 1.)
}
def random_crop_input_and_output_images(
input_img: tf.Tensor,
output_img: tf.Tensor,
sample_size: int,
num_in_channels: int,
num_out_channels: int,
) -> Tuple[tf.Tensor, tf.Tensor]:
combined = tf.concat([input_img, output_img], axis=2)
combined = tf.image.random_crop(
combined,
[sample_size, sample_size, num_in_channels + num_out_channels])
input_img = combined[:, :, 0:num_in_channels]
output_img = combined[:, :, -num_out_channels:]
return input_img, output_img
def center_crop_input_and_output_images(
input_img: tf.Tensor,
output_img: tf.Tensor,
sample_size: int,
) -> Tuple[tf.Tensor, tf.Tensor]:
central_fraction = sample_size / input_img.shape[0]
input_img = tf.image.central_crop(input_img, central_fraction)
output_img = tf.image.central_crop(output_img, central_fraction)
return input_img, output_img
"""Dataset reader for Earth Engine data."""
def _get_base_key(key: Text) -> Text:
"""Extracts the base key from the provided key.
Earth Engine exports TFRecords containing each data variable with its
corresponding variable name. In the case of time sequences, the name of the
data variable is of the form 'variable_1', 'variable_2', ..., 'variable_n',
where 'variable' is the name of the variable, and n the number of elements
in the time sequence. Extracting the base key ensures that each step of the
time sequence goes through the same normalization steps.
The base key obeys the following naming pattern: '([a-zA-Z]+)'
For instance, for an input key 'variable_1', this function returns 'variable'.
For an input key 'variable', this function simply returns 'variable'.
Args:
key: Input key.
Returns:
The corresponding base key.
Raises:
ValueError when `key` does not match the expected pattern.
"""
match = re.match(r'([a-zA-Z]+)', key)
if match:
return match.group(1)
raise ValueError(
'The provided key does not match the expected pattern: {}'.format(key))
def _clip_and_rescale(inputs: tf.Tensor, key: Text) -> tf.Tensor:
"""Clips and rescales inputs with the stats corresponding to `key`.
Args:
inputs: Inputs to clip and rescale.
key: Key describing the inputs.
Returns:
Clipped and rescaled input.
Raises:
ValueError if there are no data statistics available for `key`.
"""
base_key = _get_base_key(key)
if base_key not in DATA_STATS:
raise ValueError(
'No data statistics available for the requested key: {}.'.format(key))
min_val, max_val, _, _ = DATA_STATS[base_key]
inputs = tf.clip_by_value(inputs, min_val, max_val)
return tf.math.divide_no_nan((inputs - min_val), (max_val - min_val))
def _clip_and_normalize(inputs: tf.Tensor, key: Text) -> tf.Tensor:
"""Clips and normalizes inputs with the stats corresponding to `key`.
Args:
inputs: Inputs to clip and normalize.
key: Key describing the inputs.
Returns:
Clipped and normalized input.
Raises:
ValueError if there are no data statistics available for `key`.
"""
base_key = _get_base_key(key)
if base_key not in DATA_STATS:
raise ValueError(
'No data statistics available for the requested key: {}.'.format(key))
min_val, max_val, mean, std = DATA_STATS[base_key]
inputs = tf.clip_by_value(inputs, min_val, max_val)
inputs = inputs - mean
return tf.math.divide_no_nan(inputs, std)
def _zscore_normalize(inputs: tf.Tensor, key: str) -> tf.Tensor:
"""Clip 없이 Z-score 정규화만 수행 (논문 방식).
Zi = (xi - mean_i) / std_i
"""
base_key = _get_base_key(key)
if base_key not in DATA_STATS:
raise ValueError(
f'No data statistics available for the requested key: {key}.'
)
_, _, mean, std = DATA_STATS[base_key]
inputs = tf.cast(inputs, tf.float32)
inputs = inputs - mean
return tf.math.divide_no_nan(inputs, std)
def _get_features_dict(
sample_size: int,
features: List[Text],
) -> Dict[Text, tf.io.FixedLenFeature]:
"""Creates a features dictionary for TensorFlow IO.
Args:
sample_size: Size of the input tiles (square).
features: List of feature names.
Returns:
A features dictionary for TensorFlow IO.
"""
sample_shape = [sample_size, sample_size]
features = set(features)
columns = [
tf.io.FixedLenFeature(shape=sample_shape, dtype=tf.float32)
for _ in features
]
return dict(zip(features, columns))
def _parse_fn(
example_proto: tf.train.Example, data_size: int, sample_size: int,
num_in_channels: int, clip_and_normalize: bool,
clip_and_rescale: bool, random_crop: bool, center_crop: bool,
) -> Tuple[tf.Tensor, tf.Tensor]:
"""Reads a serialized example.
Args:
example_proto: A TensorFlow example protobuf.
data_size: Size of tiles (square) as read from input files.
sample_size: Size the tiles (square) when input into the model.
num_in_channels: Number of input channels.
clip_and_normalize: True if the data should be clipped and normalized.
clip_and_rescale: True if the data should be clipped and rescaled.
random_crop: True if the data should be randomly cropped.
center_crop: True if the data should be cropped in the center.
Returns:
(input_img, output_img) tuple of inputs and outputs to the ML model.
"""
if (random_crop and center_crop):
raise ValueError('Cannot have both random_crop and center_crop be True')
input_features, output_features = INPUT_FEATURES, OUTPUT_FEATURES
feature_names = input_features + output_features
features_dict = _get_features_dict(data_size, feature_names)
features = tf.io.parse_single_example(example_proto, features_dict)
if clip_and_normalize:
inputs_list = [
_zscore_normalize(features.get(key), key) for key in input_features
]
elif clip_and_rescale:
inputs_list = [
_clip_and_rescale(features.get(key), key) for key in input_features
]
else:
inputs_list = [features.get(key) for key in input_features]
inputs_stacked = tf.stack(inputs_list, axis=0)
input_img = tf.transpose(inputs_stacked, [1, 2, 0])
outputs_list = [features.get(key) for key in output_features]
assert outputs_list, 'outputs_list should not be empty'
outputs_stacked = tf.stack(outputs_list, axis=0)
outputs_stacked_shape = outputs_stacked.get_shape().as_list()
assert len(outputs_stacked.shape) == 3, ('outputs_stacked should be rank 3'
'but dimensions of outputs_stacked'
f' are {outputs_stacked_shape}')
output_img = tf.transpose(outputs_stacked, [1, 2, 0])
if random_crop:
input_img, output_img = random_crop_input_and_output_images(
input_img, output_img, sample_size, num_in_channels, 1)
if center_crop:
input_img, output_img = center_crop_input_and_output_images(
input_img, output_img, sample_size)
return input_img, output_img
def get_dataset(file_pattern: Text, data_size: int, sample_size: int,
batch_size: int, num_in_channels: int, compression_type: Text,
clip_and_normalize: bool, clip_and_rescale: bool,
random_crop: bool, center_crop: bool) -> tf.data.Dataset:
if (clip_and_normalize and clip_and_rescale):
raise ValueError('Cannot have both normalize and rescale.')
dataset = tf.data.Dataset.list_files(file_pattern)
dataset = dataset.interleave(
lambda x: tf.data.TFRecordDataset(x, compression_type=compression_type),
num_parallel_calls=tf.data.experimental.AUTOTUNE)
dataset = dataset.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
dataset = dataset.map(
lambda x: _parse_fn( # pylint: disable=g-long-lambda
x, data_size, sample_size, num_in_channels, clip_and_normalize,
clip_and_rescale, random_crop, center_crop),
num_parallel_calls=tf.data.experimental.AUTOTUNE)
dataset = dataset.batch(batch_size)
dataset = dataset.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
return dataset
BATCH_SIZE = 32
SAMPLE_SIZE = 64
train_dataset = get_dataset('./next_day_wildfire_spread_train*',
data_size=64, sample_size=SAMPLE_SIZE, batch_size=BATCH_SIZE,
num_in_channels=12, compression_type=None, clip_and_normalize=True,
clip_and_rescale=False, random_crop=True, center_crop=False)
validation_dataset = get_dataset('./next_day_wildfire_spread_eval*',
data_size=64, sample_size=SAMPLE_SIZE, batch_size=BATCH_SIZE,
num_in_channels=12, compression_type=None, clip_and_normalize=True,
clip_and_rescale=False, random_crop=True, center_crop=False)
test_dataset = get_dataset('./next_day_wildfire_spread_test*',
data_size=64, sample_size=SAMPLE_SIZE, batch_size=BATCH_SIZE,
num_in_channels=12, compression_type=None, clip_and_normalize=True,
clip_and_rescale=False, random_crop=True, center_crop=False)