CholecSeg8k / CholecSeg8k.py
minwoosun's picture
Create CholecSeg8k.py for custom data loading
daa5db2 verified
raw
history blame
3.54 kB
import os
import datasets
_CITATION = ""
_DESCRIPTION = "CholecSeg8K dataset for semantic segmentation in laparoscopic cholecystectomy surgery."
_HOMEPAGE_URL = "https://www.kaggle.com/datasets/newslab/cholecseg8k"
_DATA_URL = "data/CholecSeg8k.zip"
_LICENSE= "cc-by-nc-sa-4.0"
class CholecSeg8KConfig(datasets.BuilderConfig):
"""CholecSeg8K dataset for semantic segmentation in laparoscopic cholecystectomy surgery."""
def __init__(self, name, description, homepage, data_url):
"""BuilderConfig for CholecSeg8k.
Args:
data_url: `string`, url to download the zip file from.
**kwargs: keyword arguments forwarded to super.
"""
super(CholecSeg8KConfig, self).__init__(
name=self.name,
version=datasets.Version("1.0.0"),
description=self.description,
)
self.name = name
self.description = description
self.homepage = homepage
self.data_url = data_url
def _build_config(name):
return CholecSeg8KConfig(
name=name,
description=_DESCRIPTION,
homepage=_HOMEPAGE_URL,
data_url=_DATA_URL,
)
class CholecSeg8K(datasets.GeneratorBasedBuilder):
BUILDER_CONFIGS = [_build_config("all")]
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"image": datasets.Image(),
"color_mask": datasets.Image(),
"watershed_mask": datasets.Image(),
"annotation_mask": datasets.Image(),
}
),
supervised_keys=None,
homepage=_HOMEPAGE_URL,
citation=_CITATION,
license=_LICENSE,
)
def _split_generators(self, dl_manager):
datapath = dl_manager.download_and_extract(_DATA_URL)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={"datapath": datapath},
),
]
def _generate_examples(self, datapath):
"""Yields examples."""
key=0
datapath = os.path.join(datapath, "CholecSeg8k")
for video_folder in os.listdir(datapath):
video_folder_path = os.path.join(datapath, video_folder)
for clip_folder in os.listdir(video_folder_path):
clip_folder_path = os.path.join(video_folder_path, clip_folder)
for file in os.listdir(clip_folder_path):
if file.endswith("_endo.png"): # Check for endoscopic images
image_path = os.path.join(clip_folder_path, file)
# Construct paths for each mask type
base_filename = file.replace("_endo.png", "")
color_mask_path = os.path.join(clip_folder_path, f"{base_filename}_endo_color_mask.png")
watershed_mask_path = os.path.join(clip_folder_path, f"{base_filename}_endo_watershed_mask.png")
annotation_mask_path = os.path.join(clip_folder_path, f"{base_filename}_endo_mask.png")
yield key, {
"image": image_path,
"color_mask": color_mask_path,
"watershed_mask": watershed_mask_path,
"annotation_mask": annotation_mask_path,
}
key+=1