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
|
| 7 |
+
import pandas as pd
|
| 8 |
+
import numpy as np
|
| 9 |
+
|
| 10 |
+
from GFMBench.datasets.base_dataset import GFMBenchDataset
|
| 11 |
+
|
| 12 |
+
S2_MEAN = [0.05197577, 0.04783991, 0.04056812, 0.03163572, 0.02972606, 0.03457443, 0.03875053, 0.03436435, 0.0392113, 0.02358126, 0.01588816]
|
| 13 |
+
|
| 14 |
+
S2_STD = [0.04725893, 0.04743808, 0.04699043, 0.04967381, 0.04946782, 0.06458357, 0.07594915, 0.07120246, 0.08251058, 0.05111466, 0.03524419]
|
| 15 |
+
|
| 16 |
+
class MARIDADataset(datasets.GeneratorBasedBuilder):
|
| 17 |
+
VERSION = datasets.Version("1.0.0")
|
| 18 |
+
|
| 19 |
+
DATA_URL = "https://huggingface.co/datasets/GFM-Bench/MARIDA/resolve/main/MARIDA.zip"
|
| 20 |
+
|
| 21 |
+
metadata = {
|
| 22 |
+
"s2c": {
|
| 23 |
+
"bands": ["B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", "B8A", "B11", "B12"],
|
| 24 |
+
"channel_wv": [442.7, 492.4, 559.8, 664.6, 704.1, 740.5, 782.8, 832.8, 864.7, 1613.7, 2202.4],
|
| 25 |
+
"mean": S2_MEAN,
|
| 26 |
+
"std": S2_STD,
|
| 27 |
+
},
|
| 28 |
+
"s1": {
|
| 29 |
+
"bands": None,
|
| 30 |
+
"channel_wv": None,
|
| 31 |
+
"mean": None,
|
| 32 |
+
"std": None
|
| 33 |
+
}
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
SIZE = HEIGHT = WIDTH = 96
|
| 37 |
+
|
| 38 |
+
spatial_resolution = 10
|
| 39 |
+
|
| 40 |
+
NUM_CLASSES = 11
|
| 41 |
+
|
| 42 |
+
def __init__(self, *args, **kwargs):
|
| 43 |
+
super().__init__(*args, **kwargs)
|
| 44 |
+
mean = np.array(S2_MEAN).astype(np.float32)
|
| 45 |
+
self.impute_nan = np.tile(mean, (self.SIZE, self.SIZE, 1))
|
| 46 |
+
|
| 47 |
+
def _info(self):
|
| 48 |
+
metadata = self.metadata
|
| 49 |
+
metadata['size'] = self.SIZE
|
| 50 |
+
metadata['num_classes'] = self.NUM_CLASSES
|
| 51 |
+
metadata['spatial_resolution'] = self.spatial_resolution
|
| 52 |
+
return datasets.DatasetInfo(
|
| 53 |
+
description=json.dumps(metadata),
|
| 54 |
+
features=datasets.Features({
|
| 55 |
+
"optical": datasets.Array3D(shape=(11, self.HEIGHT, self.WIDTH), dtype="float32"),
|
| 56 |
+
"label": datasets.Array2D(shape=(self.HEIGHT, self.WIDTH), dtype="int32"),
|
| 57 |
+
"optical_channel_wv": datasets.Sequence(datasets.Value("float32")),
|
| 58 |
+
"spatial_resolution": datasets.Value("int32"),
|
| 59 |
+
}),
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
def _split_generators(self, dl_manager):
|
| 63 |
+
if isinstance(self.DATA_URL, list):
|
| 64 |
+
downloaded_files = dl_manager.download(self.DATA_URL)
|
| 65 |
+
combined_file = os.path.join(dl_manager.download_config.cache_dir, "combined.tar.gz")
|
| 66 |
+
with open(combined_file, 'wb') as outfile:
|
| 67 |
+
for part_file in downloaded_files:
|
| 68 |
+
with open(part_file, 'rb') as infile:
|
| 69 |
+
shutil.copyfileobj(infile, outfile)
|
| 70 |
+
data_dir = dl_manager.extract(combined_file)
|
| 71 |
+
os.remove(combined_file)
|
| 72 |
+
else:
|
| 73 |
+
data_dir = dl_manager.download_and_extract(self.DATA_URL)
|
| 74 |
+
|
| 75 |
+
return [
|
| 76 |
+
datasets.SplitGenerator(
|
| 77 |
+
name="train",
|
| 78 |
+
gen_kwargs={
|
| 79 |
+
"split": 'train',
|
| 80 |
+
"data_dir": data_dir,
|
| 81 |
+
},
|
| 82 |
+
),
|
| 83 |
+
datasets.SplitGenerator(
|
| 84 |
+
name="val",
|
| 85 |
+
gen_kwargs={
|
| 86 |
+
"split": 'val',
|
| 87 |
+
"data_dir": data_dir,
|
| 88 |
+
},
|
| 89 |
+
),
|
| 90 |
+
datasets.SplitGenerator(
|
| 91 |
+
name="test",
|
| 92 |
+
gen_kwargs={
|
| 93 |
+
"split": 'test',
|
| 94 |
+
"data_dir": data_dir,
|
| 95 |
+
},
|
| 96 |
+
)
|
| 97 |
+
]
|
| 98 |
+
|
| 99 |
+
def _generate_examples(self, split, data_dir):
|
| 100 |
+
optical_channel_wv = self.metadata["s2c"]["channel_wv"]
|
| 101 |
+
spatial_resolution = self.spatial_resolution
|
| 102 |
+
|
| 103 |
+
data_dir = os.path.join(data_dir, "MARIDA")
|
| 104 |
+
metadata = pd.read_csv(os.path.join(data_dir, "metadata.csv"))
|
| 105 |
+
metadata = metadata[metadata["split"] == split].reset_index(drop=True)
|
| 106 |
+
|
| 107 |
+
for index, row in metadata.iterrows():
|
| 108 |
+
optical_path = os.path.join(data_dir, row.optical_path)
|
| 109 |
+
optical = self._read_image(optical_path).astype(np.float32) # CxHxW
|
| 110 |
+
optical = np.transpose(optical, (1, 2, 0))
|
| 111 |
+
nan_mask = np.isnan(optical)
|
| 112 |
+
optical[nan_mask] = self.impute_nan[nan_mask]
|
| 113 |
+
optical = np.transpose(optical, (2, 0, 1))
|
| 114 |
+
|
| 115 |
+
label_path = os.path.join(data_dir, row.label_path)
|
| 116 |
+
label = self._read_image(label_path).astype(np.int32)
|
| 117 |
+
label[label==15] = 7
|
| 118 |
+
label[label==14] = 7
|
| 119 |
+
label[label==13] = 7
|
| 120 |
+
label[label==12] = 7
|
| 121 |
+
label -= 1
|
| 122 |
+
label[label==-1] = 255
|
| 123 |
+
|
| 124 |
+
sample = {
|
| 125 |
+
"optical": optical,
|
| 126 |
+
"optical_channel_wv": optical_channel_wv,
|
| 127 |
+
"label": label,
|
| 128 |
+
"spatial_resolution": spatial_resolution,
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
yield f"{index}", sample
|
| 132 |
+
|
| 133 |
+
def _read_image(self, image_path):
|
| 134 |
+
"""Read tiff image from image_path
|
| 135 |
+
Args:
|
| 136 |
+
image_path:
|
| 137 |
+
Image path to read from
|
| 138 |
+
|
| 139 |
+
Return:
|
| 140 |
+
image:
|
| 141 |
+
C, H, W numpy array image
|
| 142 |
+
"""
|
| 143 |
+
image = tifffile.imread(image_path)
|
| 144 |
+
if len(image.shape) == 3:
|
| 145 |
+
image = np.transpose(image, (2, 0, 1))
|
| 146 |
+
|
| 147 |
+
return image
|