|
|
import os |
|
|
import json |
|
|
import shutil |
|
|
import tifffile |
|
|
import datasets |
|
|
|
|
|
import pandas as pd |
|
|
import numpy as np |
|
|
|
|
|
from PIL import Image |
|
|
|
|
|
S2_MEAN = [414.02531376, 318.62204958, 245.42788408] |
|
|
|
|
|
S2_STD = [106.80525378, 90.46894665, 96.68293436] |
|
|
|
|
|
class FBPDataset(datasets.GeneratorBasedBuilder): |
|
|
VERSION = datasets.Version("1.0.0") |
|
|
|
|
|
DATA_URL = "https://huggingface.co/datasets/yuxuanw8/FBP/resolve/main/Five-Billion-Pixels.zip" |
|
|
|
|
|
metadata = { |
|
|
"s2c": { |
|
|
"bands": ["B2", "B3", "B4"], |
|
|
"channel_wv": [492.4, 559.8, 664.6], |
|
|
"mean": S2_MEAN, |
|
|
"std": S2_STD, |
|
|
}, |
|
|
"s1": { |
|
|
"bands": None, |
|
|
"channel_wv": None, |
|
|
"mean": None, |
|
|
"std": None |
|
|
} |
|
|
} |
|
|
|
|
|
SIZE = HEIGHT = WIDTH = 96 |
|
|
|
|
|
spatial_resolution = 4 |
|
|
|
|
|
NUM_CLASSES = 25 |
|
|
|
|
|
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=(3, self.HEIGHT, self.WIDTH), dtype="float32"), |
|
|
"label": datasets.Array2D(shape=(self.HEIGHT, self.WIDTH), dtype="int32"), |
|
|
"optical_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"] |
|
|
spatial_resolution = self.spatial_resolution |
|
|
|
|
|
data_dir = os.path.join(data_dir, "Five-Billion-Pixels") |
|
|
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.img_path) |
|
|
optical = self._read_image(optical_path).astype(np.float32) |
|
|
|
|
|
label_path = os.path.join(data_dir, row.label_path) |
|
|
label = self._read_label(label_path).astype(np.int32) |
|
|
|
|
|
sample = { |
|
|
"optical": optical, |
|
|
"optical_channel_wv": optical_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 = image[:, :, :3] |
|
|
image = np.transpose(image, (2, 0, 1)) |
|
|
|
|
|
return image |
|
|
|
|
|
def _read_label(self, label_path): |
|
|
"""Read label image from label_path |
|
|
Args: |
|
|
label_path: |
|
|
Label path to read from |
|
|
Return: |
|
|
label: |
|
|
H, W numpy array label |
|
|
""" |
|
|
label = Image.open(label_path) |
|
|
label = np.array(label, dtype=np.int32) |
|
|
|
|
|
return label |