Datasets:
Tasks:
Image Segmentation
ArXiv:
File size: 5,701 Bytes
a901fa0 |
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 |
import os
import json
import shutil
import tifffile
import datasets
import pandas as pd
import numpy as np
S2_MEAN = [1370.19151926, 1184.3824625, 1120.77120066, 1136.26026392, 1263.73947144, 1645.40315151, 1846.87040806, 1762.59530783, 1972.62420416, 582.72633433, 14.77112979, 1732.16362238, 1247.91870117]
S2_STD = [633.15169573, 650.2842772, 712.12507725, 965.23119807, 948.9819932, 1108.06650639, 1258.36394548, 1233.1492281, 1364.38688993, 472.37967789, 14.3114637, 1310.36996126, 1087.6020813]
S1_MEAN = [-12.54847273, -20.19237134]
S1_STD = [5.25697717, 5.91150917]
class DFC2020Dataset(datasets.GeneratorBasedBuilder):
VERSION = datasets.Version("1.0.0")
DATA_URL = "https://huggingface.co/datasets/GFM-Bench/DFC2020/resolve/main/data/DFC2020.zip"
metadata = {
"s2c": {
"bands": ["B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", "B8A", "B9", "B10", "B11", "B12"],
"channel_wv": [442.7, 492.4, 559.8, 664.6, 704.1, 740.5, 782.8, 832.8, 864.7, 945.1, 1373.5, 1613.7, 2202.4],
"mean": S2_MEAN,
"std": S2_STD,
},
"s1": {
"bands": ["VV", "VH"],
"channel_wv": [5500, 5700],
"mean": S1_MEAN,
"std": S1_STD
}
}
SIZE = HEIGHT = WIDTH = 96
spatial_resolution = 10
DFC2020_CLASSES = [
255, # class 0 unused in both schemes
0, 0, 0, 0, 0,
1, 1,
255, # --> will be masked if no_savanna == True
255, # --> will be masked if no_savanna == True
2,
3,
4, # 12 --> 6
5, # 13 --> 7
4, # 14 --> 6
255,
6,
7
]
NUM_CLASSES = 8
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def _info(self):
metadata = self.metadata
metadata['size'] = self.SIZE
metadata['num_classes'] = self.NUM_CLASSES
metadata['spatial_resolution'] = self.spatial_resolution
return datasets.DatasetInfo(
description=json.dumps(metadata),
features=datasets.Features({
"optical": datasets.Array3D(shape=(13, self.HEIGHT, self.WIDTH), dtype="float32"),
"radar": datasets.Array3D(shape=(2, self.HEIGHT, self.WIDTH), dtype="float32"),
"label": datasets.Array2D(shape=(self.HEIGHT, self.WIDTH), dtype="int32"),
"optical_channel_wv": datasets.Sequence(datasets.Value("float32")),
"radar_channel_wv": datasets.Sequence(datasets.Value("float32")),
"spatial_resolution": datasets.Value("int32"),
}),
)
def _split_generators(self, dl_manager):
if isinstance(self.DATA_URL, list):
downloaded_files = dl_manager.download(self.DATA_URL)
combined_file = os.path.join(dl_manager.download_config.cache_dir, "combined.tar.gz")
with open(combined_file, 'wb') as outfile:
for part_file in downloaded_files:
with open(part_file, 'rb') as infile:
shutil.copyfileobj(infile, outfile)
data_dir = dl_manager.extract(combined_file)
os.remove(combined_file)
else:
data_dir = dl_manager.download_and_extract(self.DATA_URL)
return [
datasets.SplitGenerator(
name="train",
gen_kwargs={
"split": 'train',
"data_dir": data_dir,
},
),
datasets.SplitGenerator(
name="val",
gen_kwargs={
"split": 'val',
"data_dir": data_dir,
},
),
datasets.SplitGenerator(
name="test",
gen_kwargs={
"split": 'test',
"data_dir": data_dir,
},
)
]
def _generate_examples(self, split, data_dir):
optical_channel_wv = self.metadata["s2c"]["channel_wv"]
radar_channel_wv = self.metadata["s1"]["channel_wv"]
spatial_resolution = self.spatial_resolution
data_dir = os.path.join(data_dir, "DFC2020")
metadata = pd.read_csv(os.path.join(data_dir, "metadata.csv"))
metadata = metadata[metadata["split"] == split].reset_index(drop=True)
for index, row in metadata.iterrows():
optical_path = os.path.join(data_dir, row.optical_path)
optical = self._read_image(optical_path).astype(np.float32) # CxHxW
radar_path = os.path.join(data_dir, row.radar_path)
radar = self._read_image(radar_path).astype(np.float32)
label_path = os.path.join(data_dir, row.label_path)
label = self._read_image(label_path)[0, :, :]
label = np.take(self.DFC2020_CLASSES, label.astype(np.int64))
label = label.astype(np.int32)
sample = {
"optical": optical,
"radar": radar,
"optical_channel_wv": optical_channel_wv,
"radar_channel_wv": radar_channel_wv,
"label": label,
"spatial_resolution": spatial_resolution,
}
yield f"{index}", sample
def _read_image(self, image_path):
"""Read tiff image from image_path
Args:
image_path:
Image path to read from
Return:
image:
C, H, W numpy array image
"""
image = tifffile.imread(image_path)
image = np.transpose(image, (2, 0, 1))
return image |