-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinputs.py
136 lines (122 loc) · 5.17 KB
/
inputs.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
from glob import glob
import tensorflow as tf
BAND_STATS = {
"mean": {
"B02": 429.9430203,
"B03": 614.21682446,
"B04": 590.23569706,
"B05": 950.68368468,
"B06": 1792.46290469,
"B07": 2075.46795189,
"B08": 2218.94553375,
"B8A": 2266.46036911,
"B11": 1594.42694882,
"B12": 1009.32729131,
"VV": -12.619993741972035,
"VH": -19.29044597721542,
},
"std": {
"B02": 572.41639287,
"B03": 582.87945694,
"B04": 675.88746967,
"B05": 729.89827633,
"B06": 1096.01480586,
"B07": 1273.45393088,
"B08": 1365.45589904,
"B8A": 1356.13789355,
"B11": 1079.19066363,
"B12": 818.86747235,
"VV": 5.115911777546365,
"VH": 5.464428464912864,
},
}
def _parse_function(example_proto, label_type):
"""Parse an example from a tfrecord."""
nb_class = 43 if label_type == "original" else 19
parsed_features = tf.io.parse_single_example(
example_proto,
{
"B02": tf.io.FixedLenFeature([120 * 120], tf.int64),
"B03": tf.io.FixedLenFeature([120 * 120], tf.int64),
"B04": tf.io.FixedLenFeature([120 * 120], tf.int64),
"B05": tf.io.FixedLenFeature([60 * 60], tf.int64),
"B06": tf.io.FixedLenFeature([60 * 60], tf.int64),
"B07": tf.io.FixedLenFeature([60 * 60], tf.int64),
"B08": tf.io.FixedLenFeature([120 * 120], tf.int64),
"B8A": tf.io.FixedLenFeature([60 * 60], tf.int64),
"B11": tf.io.FixedLenFeature([60 * 60], tf.int64),
"B12": tf.io.FixedLenFeature([60 * 60], tf.int64),
"VV": tf.io.FixedLenFeature([120 * 120], tf.float32),
"VH": tf.io.FixedLenFeature([120 * 120], tf.float32),
"patch_name_s1": tf.io.VarLenFeature(dtype=tf.string),
"patch_name_s2": tf.io.VarLenFeature(dtype=tf.string),
label_type + "_labels": tf.io.VarLenFeature(dtype=tf.string),
label_type
+ "_labels_multi_hot": tf.io.FixedLenFeature([nb_class], tf.int64),
},
)
return {
"B02": tf.cast(tf.reshape(parsed_features["B02"], [120, 120]), tf.float32),
"B03": tf.cast(tf.reshape(parsed_features["B03"], [120, 120]), tf.float32),
"B04": tf.cast(tf.reshape(parsed_features["B04"], [120, 120]), tf.float32),
"B05": tf.cast(tf.reshape(parsed_features["B05"], [60, 60]), tf.float32),
"B06": tf.cast(tf.reshape(parsed_features["B06"], [60, 60]), tf.float32),
"B07": tf.cast(tf.reshape(parsed_features["B07"], [60, 60]), tf.float32),
"B08": tf.cast(tf.reshape(parsed_features["B08"], [120, 120]), tf.float32),
"B8A": tf.cast(tf.reshape(parsed_features["B8A"], [60, 60]), tf.float32),
"B11": tf.cast(tf.reshape(parsed_features["B11"], [60, 60]), tf.float32),
"B12": tf.cast(tf.reshape(parsed_features["B12"], [60, 60]), tf.float32),
"VV": tf.reshape(parsed_features["VV"], [120, 120]),
"VH": tf.reshape(parsed_features["VV"], [120, 120]),
"patch_name_s1": parsed_features["patch_name_s1"],
"patch_name_s2": parsed_features["patch_name_s2"],
label_type + "_labels": parsed_features[label_type + "_labels"],
label_type
+ "_labels_multi_hot": parsed_features[label_type + "_labels_multi_hot"],
}
def _preprocess_function(example, label_type):
"""Preprocess an example."""
newexample = {}
for band in BAND_STATS["mean"].keys():
band_tensor = example[band]
newexample[band] = tf.divide(
tf.math.subtract(
band_tensor,
tf.constant(BAND_STATS["mean"][band], shape=band_tensor.shape),
),
tf.constant(BAND_STATS["std"][band], shape=band_tensor.shape),
)
newexample[label_type + "_labels"] = example[label_type + "_labels"]
newexample[label_type + "_labels_multi_hot"] = example[
label_type + "_labels_multi_hot"
]
newexample["patch_name_s1"] = example["patch_name_s1"]
newexample["patch_name_s2"] = example["patch_name_s2"]
return newexample
def create_batched_dataset(
TFRecord_paths,
batch_size,
shuffle_buffer_size,
label_type,
num_workers,
worker_index,
num_parallel_calls=tf.data.experimental.AUTOTUNE,
):
"""Create an input batched dataset."""
parse_fn = lambda x: _parse_function(x, label_type=label_type)
preprocess_fn = lambda x: _preprocess_function(x, label_type=label_type)
records = []
for i in TFRecord_paths:
records.extend(glob(i))
dataset = tf.data.Dataset.list_files(TFRecord_paths)
dataset = dataset.shard(num_workers, worker_index)
dataset = dataset.interleave(
tf.data.TFRecordDataset, num_parallel_calls=num_parallel_calls
)
if shuffle_buffer_size > 0:
dataset = dataset.shuffle(buffer_size=shuffle_buffer_size)
dataset = dataset.map(parse_fn, num_parallel_calls=num_parallel_calls)
dataset = dataset.map(preprocess_fn, num_parallel_calls=num_parallel_calls)
dataset = dataset.batch(batch_size, drop_remainder=False)
batched_dataset = dataset.prefetch(num_parallel_calls)
return batched_dataset