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