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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2022 for msynth dataset
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
'''
|
| 15 |
+
Custom dataset-builder for ssynth dataset
|
| 16 |
+
'''
|
| 17 |
+
|
| 18 |
+
import os
|
| 19 |
+
import datasets
|
| 20 |
+
import glob
|
| 21 |
+
import re
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
logger = datasets.logging.get_logger(__name__)
|
| 25 |
+
|
| 26 |
+
_CITATION = """\
|
| 27 |
+
@article{kim2024ssynth,
|
| 28 |
+
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},
|
| 29 |
+
author={Kim, Andrea and Saharkhiz, Niloufar and Sizikova, Elena and Lago, Miguel, and Sahiner, Berkman and Delfino, Jana G., and Badano, Aldo},
|
| 30 |
+
journal={International Conference on Medical Image Computing and Computer Assisted Intervention (MICCAI)},
|
| 31 |
+
volume={},
|
| 32 |
+
pages={},
|
| 33 |
+
year={2024}
|
| 34 |
+
}
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
_DESCRIPTION = """\
|
| 39 |
+
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.
|
| 40 |
+
Curated by: Andrea Kim, Niloufar Saharkhiz, Elena Sizikova, Miguel Lago, Berkman Sahiner, Jana Delfino, Aldo Badano
|
| 41 |
+
License: Creative Commons 1.0 Universal License (CC0)
|
| 42 |
+
"""
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
_HOMEPAGE = "https://github.com/DIDSR/ssynth-release?tab=readme-ov-file"
|
| 46 |
+
|
| 47 |
+
_REPO = "https://huggingface.co/datasets/didsr/ssynth_data/resolve/main"
|
| 48 |
+
|
| 49 |
+
# Initialize an empty list to store the file paths
|
| 50 |
+
_CROPPED = True
|
| 51 |
+
|
| 52 |
+
_URLS = {
|
| 53 |
+
"synthetic_data": f"{_REPO}/data/synthetic_dataset/output_10k.zip",
|
| 54 |
+
"read_me": f"{_REPO}/README.md"
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
DATA_DIR = {"all_data": "output_10k"}
|
| 58 |
+
|
| 59 |
+
class ssynth_dataConfig(datasets.BuilderConfig):
|
| 60 |
+
"""ssynth dataset"""
|
| 61 |
+
def __init__(self, name, **kwargs):
|
| 62 |
+
super(ssynth_dataConfig, self).__init__(
|
| 63 |
+
version=datasets.Version("1.0.0"),
|
| 64 |
+
name=name,
|
| 65 |
+
description="ssynth_data",
|
| 66 |
+
**kwargs,
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
class ssynth_data(datasets.GeneratorBasedBuilder):
|
| 70 |
+
"""ssynth dataset."""
|
| 71 |
+
|
| 72 |
+
DEFAULT_WRITER_BATCH_SIZE = 256
|
| 73 |
+
BUILDER_CONFIGS = [
|
| 74 |
+
ssynth_dataConfig("output_10k"),
|
| 75 |
+
]
|
| 76 |
+
|
| 77 |
+
def _info(self):
|
| 78 |
+
if self.config.name == "output_10k":
|
| 79 |
+
# Define dataset features and keys
|
| 80 |
+
features = datasets.Features(
|
| 81 |
+
{
|
| 82 |
+
"Cropped": datasets.Features({
|
| 83 |
+
"image": datasets.Value("string"),
|
| 84 |
+
"mask": datasets.Value("string")
|
| 85 |
+
}),
|
| 86 |
+
"Uncropped": datasets.Features({
|
| 87 |
+
"image": datasets.Value("string"),
|
| 88 |
+
"mask": datasets.Value("string")
|
| 89 |
+
})
|
| 90 |
+
}
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
return datasets.DatasetInfo(
|
| 94 |
+
description=_DESCRIPTION,
|
| 95 |
+
features=features,
|
| 96 |
+
supervised_keys=None,
|
| 97 |
+
homepage=_HOMEPAGE,
|
| 98 |
+
citation=_CITATION,
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
def _split_generators(
|
| 102 |
+
self, dl_manager: datasets.utils.download_manager.DownloadManager):
|
| 103 |
+
|
| 104 |
+
if self.config.name == "output_10k":
|
| 105 |
+
data_dir = dl_manager.download_and_extract(_URLS['synthetic_data'])
|
| 106 |
+
return [
|
| 107 |
+
datasets.SplitGenerator(
|
| 108 |
+
name="output_10k",
|
| 109 |
+
gen_kwargs={
|
| 110 |
+
"files": data_dir,
|
| 111 |
+
"name": "all_data",
|
| 112 |
+
},
|
| 113 |
+
),
|
| 114 |
+
]
|
| 115 |
+
|
| 116 |
+
def get_all_file_paths(self, root_directory):
|
| 117 |
+
file_paths = [] # List to store file paths
|
| 118 |
+
|
| 119 |
+
# Walk through the directory and its subdirectories using os.walk
|
| 120 |
+
for folder, _, files in os.walk(root_directory):
|
| 121 |
+
for file in files:
|
| 122 |
+
if file == "cropped_image.png":
|
| 123 |
+
# Get the full path of the file
|
| 124 |
+
file_path = os.path.join(folder, file)
|
| 125 |
+
file_paths.append(file_path)
|
| 126 |
+
return file_paths
|
| 127 |
+
|
| 128 |
+
def get_other_images(self, cropped_image_path, file_name):
|
| 129 |
+
other_image_paths = []
|
| 130 |
+
|
| 131 |
+
# Get the directory containing the cropped_image.png
|
| 132 |
+
directory = os.path.dirname(cropped_image_path)
|
| 133 |
+
|
| 134 |
+
# Walk through the directory to find other image files
|
| 135 |
+
for file in os.listdir(directory):
|
| 136 |
+
if file == file_name:
|
| 137 |
+
# Get the full path of the other image file
|
| 138 |
+
file_path = os.path.join(directory, file)
|
| 139 |
+
#other_image_paths.append(file_path)
|
| 140 |
+
return file_path
|
| 141 |
+
return None
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
def _generate_examples(self, files, name):
|
| 145 |
+
if self.config.name == "output_10k":
|
| 146 |
+
key = 0
|
| 147 |
+
data_paths = self.get_all_file_paths(os.path.join(files, DATA_DIR[name]))
|
| 148 |
+
|
| 149 |
+
cropped_images = []
|
| 150 |
+
uncropped_images = []
|
| 151 |
+
for path in data_paths:
|
| 152 |
+
res_dic = {}
|
| 153 |
+
cropped_image = path
|
| 154 |
+
cropped_mask = self.get_other_images(path,"cropped_mask.png")
|
| 155 |
+
image = self.get_other_images(path,"image.png")
|
| 156 |
+
mask = self.get_other_images(path,"mask.png")
|
| 157 |
+
cropped_data = {
|
| 158 |
+
"image": cropped_image,
|
| 159 |
+
"mask": cropped_mask
|
| 160 |
+
}
|
| 161 |
+
uncropped_data = {
|
| 162 |
+
"image": image,
|
| 163 |
+
"mask": mask
|
| 164 |
+
}
|
| 165 |
+
res_dic["Cropped"] = cropped_data
|
| 166 |
+
res_dic["Uncropped"] = uncropped_data
|
| 167 |
+
|
| 168 |
+
yield key, res_dic
|
| 169 |
+
key += 1
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
|