File size: 4,810 Bytes
057a926 8fbe29d 057a926 cb7237c 057a926 cb7237c | 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 | # fluo_sc.py
import os
import csv
import glob
import datasets
_CITATION = r"""
@dataset{rocha_fluo_sc_2024,
title = {FLUO-SC: a fluorescence image dataset of skin lesions collected from smartphones},
author = {Rocha, Matheus Becali and Krohling, Renato and Pratavieira, Sebasti{\~a}o and others},
year = {2024},
publisher = {Mendeley Data},
version = {1},
doi = {10.17632/s8n68jj678.1},
url = {https://doi.org/10.17632/s8n68jj678.1}
}
"""
_DESCRIPTION = """
FLUO-SC: Fluorescence skin lesion image dataset (clinical/white-light and fluorescence images).
This Hugging Face repository mirrors the original dataset published on Mendeley Data (CC BY 4.0).
"""
_HOMEPAGE = "https://data.mendeley.com/datasets/s8n68jj678/1"
_LICENSE = "CC BY 4.0"
LABELS = ["BCC", "SCC", "MEL", "ACK", "SEK", "NEV"]
MODALITIES = ["CLI", "FLUO"]
def _repo_root():
# On HF Hub, working dir is repo root.
return os.getcwd()
def _find_data_dir():
data_dir = os.path.join(_repo_root(), "data")
if not os.path.isdir(data_dir):
raise FileNotFoundError("Could not find 'data/' directory at repo root.")
return data_dir
def iter_images(root):
for ext in exts:
yield from glob.glob(os.path.join(root, "**", ext), recursive=True)
class FluoSc(datasets.GeneratorBasedBuilder):
VERSION = datasets.Version("1.0.3")
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"image": datasets.Image(),
"label": datasets.ClassLabel(names=LABELS),
"modality": datasets.ClassLabel(names=MODALITIES),
"path": datasets.Value("string"),
}
),
homepage=_HOMEPAGE,
citation=_CITATION,
license=_LICENSE,
)
def _split_generators(self, dl_manager):
data_dir = _find_data_dir()
# If zipped shards exist, extract them (reduces Hub request count massively).
cli_zip = os.path.join(data_dir, "CLI.zip")
fluo_zip = os.path.join(data_dir, "FLUO.zip")
extracted_base = None
extracted_any = False
# dl_manager.extract returns a folder path where the archive is extracted
# For ZIP containing folder "CLI/...", extracted path typically ends with ".../CLI"
# We'll use its parent as base to have ".../<CLI|FLUO>/..."
if os.path.isfile(cli_zip):
cli_extracted = dl_manager.extract(cli_zip)
extracted_base = os.path.dirname(cli_extracted)
extracted_any = True
if os.path.isfile(fluo_zip):
fluo_extracted = dl_manager.extract(fluo_zip)
# If only FLUO.zip exists, base should be parent of FLUO extracted dir
if extracted_base is None:
extracted_base = os.path.dirname(fluo_extracted)
extracted_any = True
# Prefer extracted content if present; otherwise use plain folders under data/
scan_base = extracted_base if extracted_any else data_dir
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={"scan_base": scan_base, "repo_root": _repo_root(), "data_dir": data_dir},
)
]
def _generate_examples(self, scan_base, repo_root, data_dir):
import os
import glob
idx = 0
exts = ("*.jpg", "*.jpeg", "*.png", "*.JPG", "*.JPEG", "*.PNG")
def iter_images(root):
for ext in exts:
yield from glob.glob(os.path.join(root, "**", ext), recursive=True)
for img_path in sorted(iter_images(scan_base)):
parts = os.path.normpath(img_path).split(os.sep)
# 1) find the modality at any depth
mod_i = None
modality = None
for i, p in enumerate(parts):
if p in MODALITIES: # ["CLI", "FLUO"]
modality = p
mod_i = i
break
if modality is None:
continue
# 2) find the FIRST valid class after the modality
label = None
for j in range(mod_i + 1, len(parts)):
if parts[j] in LABELS: # ["BCC","SCC","MEL","ACK","SEK","NEV"]
label = parts[j]
break
if label is None:
continue
yield idx, {
"image": img_path,
"label": label,
"modality": modality,
"path": f"{modality}/{label}/{os.path.basename(img_path)}",
}
idx += 1
if idx == 0:
raise FileNotFoundError(
"No valid images found. Expected paths containing CLI/ or FLUO/ and a label folder (BCC/SCC/MEL/ACK/SEK/NEV)."
) |