# Copyright 2022 for msynth dataset # # 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. ''' Custom dataset-builder for ssynth dataset ''' import os import datasets import glob import re logger = datasets.logging.get_logger(__name__) _CITATION = """\ @article{kim2024ssynth, title={Knowledge-based in silico models and dataset for the comparative evaluation of mammography AI for a range of breast characteristics, lesion conspicuities and doses}, author={Kim, Andrea and Saharkhiz, Niloufar and Sizikova, Elena and Lago, Miguel, and Sahiner, Berkman and Delfino, Jana G., and Badano, Aldo}, journal={International Conference on Medical Image Computing and Computer Assisted Intervention (MICCAI)}, volume={}, pages={}, year={2024} } """ _DESCRIPTION = """\ S-SYNTH is an open-source, flexible skin simulation framework to rapidly generate synthetic skin models and images using digital rendering of an anatomically inspired multi-layer, multi-component skin and growing lesion model. It allows for generation of highly-detailed 3D skin models and digitally rendered synthetic images of diverse human skin tones, with full control of underlying parameters and the image formation process. Curated by: Andrea Kim, Niloufar Saharkhiz, Elena Sizikova, Miguel Lago, Berkman Sahiner, Jana Delfino, Aldo Badano License: Creative Commons 1.0 Universal License (CC0) """ _HOMEPAGE = "https://github.com/DIDSR/ssynth-release?tab=readme-ov-file" _REPO = "https://huggingface.co/datasets/didsr/ssynth_data/resolve/main" # Initialize an empty list to store the file paths _CROPPED = True _URLS = { "synthetic_data": f"{_REPO}/data/synthetic_dataset/output_10k.zip", "read_me": f"{_REPO}/README.md" } DATA_DIR = {"all_data": "output_10k"} class ssynth_dataConfig(datasets.BuilderConfig): """ssynth dataset""" def __init__(self, name, **kwargs): super(ssynth_dataConfig, self).__init__( version=datasets.Version("1.0.0"), name=name, description="ssynth_data", **kwargs, ) class ssynth_data(datasets.GeneratorBasedBuilder): """ssynth dataset.""" DEFAULT_WRITER_BATCH_SIZE = 256 BUILDER_CONFIGS = [ ssynth_dataConfig("output_10k"), ] def _info(self): if self.config.name == "output_10k": # Define dataset features and keys features = datasets.Features( { "Cropped": datasets.Features({ "image": datasets.Value("string"), "mask": datasets.Value("string") }), "Uncropped": datasets.Features({ "image": datasets.Value("string"), "mask": datasets.Value("string") }) } ) return datasets.DatasetInfo( description=_DESCRIPTION, features=features, supervised_keys=None, homepage=_HOMEPAGE, citation=_CITATION, ) def _split_generators( self, dl_manager: datasets.utils.download_manager.DownloadManager): if self.config.name == "output_10k": data_dir = dl_manager.download_and_extract(_URLS['synthetic_data']) return [ datasets.SplitGenerator( name="output_10k", gen_kwargs={ "files": data_dir, "name": "all_data", }, ), ] def get_all_file_paths(self, root_directory): file_paths = [] # List to store file paths # Walk through the directory and its subdirectories using os.walk for folder, _, files in os.walk(root_directory): for file in files: if file == "cropped_image.png": # Get the full path of the file file_path = os.path.join(folder, file) file_paths.append(file_path) return file_paths def get_other_images(self, cropped_image_path, file_name): other_image_paths = [] # Get the directory containing the cropped_image.png directory = os.path.dirname(cropped_image_path) # Walk through the directory to find other image files for file in os.listdir(directory): if file == file_name: # Get the full path of the other image file file_path = os.path.join(directory, file) #other_image_paths.append(file_path) return file_path return None def _generate_examples(self, files, name): if self.config.name == "output_10k": key = 0 data_paths = self.get_all_file_paths(os.path.join(files, DATA_DIR[name])) cropped_images = [] uncropped_images = [] for path in data_paths: res_dic = {} cropped_image = path cropped_mask = self.get_other_images(path,"cropped_mask.png") image = self.get_other_images(path,"image.png") mask = self.get_other_images(path,"mask.png") cropped_data = { "image": cropped_image, "mask": cropped_mask } uncropped_data = { "image": image, "mask": mask } res_dic["Cropped"] = cropped_data res_dic["Uncropped"] = uncropped_data yield key, res_dic key += 1