# Copyright 2022 Cristóbal Alcázar # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Rock Glacier dataset with images of the chilean andes.""" import os import re import datasets #datasets.logging.set_verbosity_debug() #datasets.logging.set_verbosity_info() #logger = datasets.logging.get_logger(__name__) _HOMEPAGE = "https://github.com/alcazar90/rock-glacier-detection" _CITATION = """\ @ONLINE {rock-glacier-dataset, author="CMM-Glaciares", title="Rock Glacier Dataset", month="October", year="2022", url="https://github.com/alcazar90/rock-glacier-detection" } """ _DESCRIPTION = """\ TODO: Add a description... """ _URLS = { "train": "https://huggingface.co/datasets/alkzar90/rock-glacier-dataset/resolve/main/data/data_v01/train.zip", "validation": "https://huggingface.co/datasets/alkzar90/rock-glacier-dataset/resolve/main/data/data_v01/val.zip", "test": "https://huggingface.co/datasets/alkzar90/rock-glacier-dataset/resolve/main/data/data_v01/test.zip", } _CORDILLERA_DEFAULT_MASK = "https://huggingface.co/datasets/alkzar90/rock-glacier-dataset/resolve/main/data/data_v01/zeros.png" _NAMES = ["cordillera", "glaciar"] class RockGlacierConfig(datasets.BuilderConfig): def __init__(self, name, **kwargs): super(RockGlacierConfig, self).__init__( version=datasets.Version("1.0.0"), name=name, description="Rock Glacier Dataset", **kwargs, ) class RockGlacierDataset(datasets.GeneratorBasedBuilder): """Rock Glacier images dataset.""" BUILDER_CONFIGS = [ RockGlacierConfig("image-classification"), RockGlacierConfig("image-segmentation"), ] def _info(self): if self.config.name == "image-classification": features = datasets.Features({ "image": datasets.Image(), "labels": datasets.features.ClassLabel(names=_NAMES), "path": datasets.Value("string"), }) keys = ("image", "labels") if self.config.name == "image-segmentation": features = datasets.Features({ "image": datasets.Image(), "masks": datasets.Image(), "path": datasets.Value("string"), }) keys = ("image", "masks") return datasets.DatasetInfo( description=_DESCRIPTION, features=features, supervised_keys=keys, homepage=_HOMEPAGE, citation=_CITATION, ) def _split_generators(self, dl_manager): data_files = dl_manager.download_and_extract(_URLS) splits = [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={ "files": dl_manager.iter_files([data_files["train"]]), "split": "training", }, ), datasets.SplitGenerator( name=datasets.Split.VALIDATION, gen_kwargs={ "files": dl_manager.iter_files([data_files["validation"]]), "split": "validation", }, ), datasets.SplitGenerator( name=datasets.Split.TEST, gen_kwargs={ "files": dl_manager.iter_files([data_files["test"]]), "split": "test", }, ), ] if self.config.name == "image-classification": return splits if self.config.name == "image-segmentation": return splits def _generate_examples(self, files, split): if self.config.name == "image-classification": for i, path in enumerate(files): file_name = os.path.basename(path) dir_name = os.path.basename(os.path.dirname(path)).lower() if dir_name != "masks" and file_name.endswith(".png"): yield i, { "image": path, "labels": dir_name, "path": "/".join(path.split("/")[-3:]), } if self.config.name == "image-segmentation": for i, path in enumerate(files): file_name = os.path.basename(path) dir_name = os.path.basename(os.path.dirname(path)).lower() if dir_name == "glaciar" and file_name.endswith(".png"): yield i, { "image": path, "masks": path.replace(dir_name, "masks"), "path": "/".join(path.split("/")[-3:]), } elif dir_name == "cordillera" and file_name.endswith(".png"): yield i, { "image": path, "masks": _CORDILLERA_DEFAULT_MASK, "path": "/".join(path.split("/")[-3:]), }