|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
""" |
|
lesion dataset - ISIC 2018 Task 2 |
|
""" |
|
|
|
|
|
import numpy as np |
|
import os |
|
from PIL import Image |
|
|
|
import datasets |
|
|
|
from datasets import Sequence, Value |
|
from urllib.parse import urlparse |
|
|
|
|
|
|
|
|
|
_CITATION = """\ |
|
@InProceedings{huggingface:dataset, |
|
title = {A great new dataset}, |
|
author={huggingface, Inc. |
|
}, |
|
year={2020} |
|
} |
|
""" |
|
|
|
|
|
|
|
_DESCRIPTION = """\ |
|
This dataset has been modified for project use case. |
|
""" |
|
|
|
|
|
_HOMEPAGE = "" |
|
|
|
|
|
_LICENSE = "" |
|
|
|
|
|
|
|
|
|
|
|
_URLS = { |
|
"full": "https://storage.googleapis.com/lesion-dataset/dataset-images.zip", |
|
"globules": "https://storage.googleapis.com/lesion-dataset/dataset-globules.zip", |
|
"milia_like_cyst": "https://storage.googleapis.com/lesion-dataset/dataset-milia_like_cyst.zip", |
|
"negative_network": "https://storage.googleapis.com/lesion-dataset/dataset-negative_network.zip", |
|
"pigment_network": "https://storage.googleapis.com/lesion-dataset/dataset-pigment_network.zip", |
|
"streaks": "https://storage.googleapis.com/lesion-dataset/dataset-streaks.zip", |
|
"task1": "https://storage.googleapis.com/lesion-dataset/dataset-task1.zip", |
|
} |
|
|
|
|
|
|
|
class LesionDataset(datasets.GeneratorBasedBuilder): |
|
"""TODO: Short description of my dataset.""" |
|
|
|
VERSION = datasets.Version("1.1.0") |
|
|
|
BUILDER_CONFIGS = [ |
|
datasets.BuilderConfig(name='full', version=VERSION, description="This will return the full dataset with all classes"), |
|
datasets.BuilderConfig(name="globules", version=VERSION, description="This will return the dataset with only globules class"), |
|
datasets.BuilderConfig(name="milia_like_cyst", version=VERSION, description="This will return the dataset with only milia_like_cyst class"), |
|
datasets.BuilderConfig(name="negative_network", version=VERSION, description="This will return the dataset with only negative_network class"), |
|
datasets.BuilderConfig(name="pigment_network", version=VERSION, description="This will return the dataset with only pigment_network class"), |
|
datasets.BuilderConfig(name="streaks", version=VERSION, description="This will return the dataset with only streaks class"), |
|
datasets.BuilderConfig(name="task1", version=VERSION, description="This will return the dataset for task1"), |
|
] |
|
DEFAULT_CONFIG_NAME = "task1" |
|
|
|
def _info(self): |
|
|
|
if self.config.name == "full": |
|
features=datasets.Features( |
|
{ |
|
"image": datasets.Image(), |
|
"label0": datasets.Image(), |
|
"label1": datasets.Image(), |
|
"label2": datasets.Image(), |
|
"label3": datasets.Image(), |
|
"label4": datasets.Image(), |
|
} |
|
) |
|
|
|
elif self.config.name in ['globules', 'milia_like_cyst', 'negative_network', 'pigment_network', 'streaks', 'task1']: |
|
features = datasets.Features( |
|
{ |
|
"image": datasets.Image(), |
|
"label": datasets.Image(), |
|
} |
|
) |
|
|
|
return datasets.DatasetInfo( |
|
|
|
description=_DESCRIPTION, |
|
|
|
features=features, |
|
|
|
|
|
|
|
|
|
homepage=_HOMEPAGE, |
|
|
|
license=_LICENSE, |
|
|
|
citation=_CITATION, |
|
) |
|
|
|
|
|
def _split_generators(self, dl_manager): |
|
|
|
|
|
|
|
|
|
|
|
|
|
url = _URLS[self.config.name] |
|
data_dir = dl_manager.download_and_extract(url) |
|
|
|
parsed_url = urlparse(url) |
|
|
|
base_name_with_extension = os.path.basename(parsed_url.path) |
|
|
|
base_name = os.path.splitext(base_name_with_extension)[0] |
|
|
|
|
|
self.label2id = { |
|
'globules': 0, |
|
'milia_like_cyst': 1, |
|
'negative_network': 2, |
|
'pigment_network': 3, |
|
'streaks': 4 |
|
} |
|
|
|
|
|
if self.config.name in ['full', 'globules', 'milia_like_cyst', 'negative_network', 'pigment_network', 'streaks']: |
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
|
|
gen_kwargs={ |
|
"filepath": os.path.join(data_dir, f"{base_name}/ISIC2018_Task1-2_Training_Input"), |
|
"labelpath": os.path.join(data_dir, f"{base_name}/ISIC2018_Task2_Training_GroundTruth_v3"), |
|
"split": "train", |
|
}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.VALIDATION, |
|
|
|
gen_kwargs={ |
|
"filepath": os.path.join(data_dir, f"{base_name}/ISIC2018_Task1-2_Validation_Input"), |
|
"labelpath": os.path.join(data_dir, f"{base_name}/ISIC2018_Task2_Validation_GroundTruth"), |
|
"split": "validation", |
|
}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TEST, |
|
|
|
gen_kwargs={ |
|
"filepath": os.path.join(data_dir, f"{base_name}/ISIC2018_Task1-2_Test_Input"), |
|
"labelpath": os.path.join(data_dir, f"{base_name}/ISIC2018_Task2_Test_GroundTruth"), |
|
"split": "test" |
|
}, |
|
), |
|
] |
|
|
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
|
|
gen_kwargs={ |
|
"filepath": os.path.join(data_dir, f"{base_name}/ISIC2018_Task1-2_Training_Input"), |
|
"labelpath": os.path.join(data_dir, f"{base_name}/ISIC2018_Task1_Training_GroundTruth"), |
|
"split": "train", |
|
}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.VALIDATION, |
|
|
|
gen_kwargs={ |
|
"filepath": os.path.join(data_dir, f"{base_name}/ISIC2018_Task1-2_Validation_Input"), |
|
"labelpath": os.path.join(data_dir, f"{base_name}/ISIC2018_Task1_Validation_GroundTruth"), |
|
"split": "validation", |
|
}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TEST, |
|
|
|
gen_kwargs={ |
|
"filepath": os.path.join(data_dir, f"{base_name}/ISIC2018_Task1-2_Test_Input"), |
|
"labelpath": os.path.join(data_dir, f"{base_name}/ISIC2018_Task1_Test_GroundTruth"), |
|
"split": "test" |
|
}, |
|
), |
|
] |
|
|
|
|
|
|
|
|
|
def _generate_examples(self, filepath, labelpath, split): |
|
|
|
|
|
|
|
|
|
if self.config.name == "full": |
|
|
|
for filename in os.listdir(filepath): |
|
|
|
if filename.endswith('.jpg') or filename.endswith('.jpeg'): |
|
|
|
base_name = os.path.splitext(filename)[0] |
|
|
|
yield_result = {"image": os.path.join(filepath, filename)} |
|
for k, v in self.label2id.items(): |
|
label_filename = f'{base_name}_attribute_{k}.png' |
|
label_file_path = os.path.join(labelpath, label_filename) |
|
|
|
|
|
if not os.path.exists(label_file_path): |
|
|
|
img = Image.open(yield_result['image']) |
|
width, height = img.size |
|
|
|
|
|
black_img = Image.fromarray(np.zeros((height, width), dtype=np.uint8)) |
|
|
|
|
|
black_img.save(label_file_path) |
|
|
|
yield_result[f"label{v}"] = label_file_path |
|
|
|
yield base_name, yield_result |
|
|
|
elif self.config.name in ['globules', 'milia_like_cyst', 'negative_network', 'pigment_network', 'streaks']: |
|
for filename in os.listdir(filepath): |
|
if filename.endswith('.jpg') or filename.endswith('.jpeg'): |
|
base_name = os.path.splitext(filename)[0] |
|
|
|
yield_result = {"image": os.path.join(filepath, filename)} |
|
label_filename = f'{base_name}_attribute_{self.config.name}.png' |
|
label_file_path = os.path.join(labelpath, label_filename) |
|
|
|
|
|
if not os.path.exists(label_file_path): |
|
|
|
img = Image.open(yield_result['image']) |
|
width, height = img.size |
|
|
|
|
|
black_img = Image.fromarray(np.zeros((height, width), dtype=np.uint8)) |
|
|
|
|
|
black_img.save(label_file_path) |
|
|
|
yield_result["label"] = label_file_path |
|
|
|
yield base_name, yield_result |
|
|
|
elif self.config.name == "task1": |
|
for filename in os.listdir(filepath): |
|
if filename.endswith('.jpg') or filename.endswith('.jpeg'): |
|
base_name = os.path.splitext(filename)[0] |
|
|
|
yield_result = {"image": os.path.join(filepath, filename)} |
|
label_filename = f'{base_name}_segmentation.png' |
|
label_file_path = os.path.join(labelpath, label_filename) |
|
|
|
|
|
if not os.path.exists(label_file_path): |
|
|
|
img = Image.open(yield_result['image']) |
|
width, height = img.size |
|
|
|
|
|
black_img = Image.fromarray(np.zeros((height, width), dtype=np.uint8)) |
|
|
|
|
|
black_img.save(label_file_path) |
|
|
|
yield_result["label"] = label_file_path |
|
|
|
yield base_name, yield_result |
|
|
|
|
|
|
|
|