Skip to content
Snippets Groups Projects
Commit 1e726a8c authored by Daniel CARRON's avatar Daniel CARRON :b: Committed by André Anjos
Browse files

[segmetnation.data] Add refuge database

parent 33e14296
No related branches found
No related tags found
1 merge request!46Create common library
Showing
with 6177 additions and 0 deletions
# SPDX-FileCopyrightText: Copyright © 2024 Idiap Research Institute <contact@idiap.ch>
#
# SPDX-License-Identifier: GPL-3.0-or-later
"""REFUGE dataset for Optic Cup Segmentation (default protocol).
* Configuration resolution: 1632 x 1632 (after resizing and padding)
* Reference (including split): [REFUGE-2018]_
* See :py:mod:`deepdraw.data.refuge` for dataset details
"""
from mednet.libs.segmentation.config.data.refuge.datamodule import DataModule
datamodule = DataModule("default.json", target_type="cup")
# SPDX-FileCopyrightText: Copyright © 2024 Idiap Research Institute <contact@idiap.ch>
#
# SPDX-License-Identifier: GPL-3.0-or-later
"""DREFUGE for Optic Disc and Cup Segmentation."""
import os
import pathlib
import PIL.Image
import pkg_resources
from mednet.libs.common.data.datamodule import CachingDataModule
from mednet.libs.common.data.split import make_split
from mednet.libs.common.data.typing import Sample
from mednet.libs.segmentation.data.typing import (
SegmentationRawDataLoader as _SegmentationRawDataLoader,
)
from torchvision import tv_tensors
from torchvision.transforms.functional import to_tensor
from ....utils.rc import load_rc
CONFIGURATION_KEY_DATADIR = "datadir." + (__name__.rsplit(".", 2)[-2])
"""Key to search for in the configuration file for the root directory of this
database."""
class SegmentationRawDataLoader(_SegmentationRawDataLoader):
"""A specialized raw-data-loader for the drishtigs1 dataset.
Parameters
----------
target_type
Indicate whether to use the "cup" or "disc" target.
"""
datadir: pathlib.Path
"""This variable contains the base directory where the database raw data is
stored."""
def __init__(self, target_type: str):
self.datadir = pathlib.Path(
load_rc().get(CONFIGURATION_KEY_DATADIR, os.path.realpath(os.curdir))
)
self._pkg_path = pathlib.Path(
pkg_resources.resource_filename(__name__, "masks")
)
self.target_type = target_type
def sample(self, sample: tuple[str, str, str]) -> Sample:
"""Load a single image sample from the disk.
Parameters
----------
sample
A tuple containing path suffixes to the sample image, target, and mask
to be loaded, within the dataset root folder.
Returns
-------
The sample representation.
"""
image = to_tensor(PIL.Image.open(self.datadir / sample[0]).convert(mode="RGB"))
if self.target_type == "disc":
target = to_tensor(
PIL.Image.open(self.datadir / sample[1])
.convert(mode="RGB", dither=None)
.convert("L")
.point(lambda p: p <= 150, mode="1")
)
elif self.target_type == "cup":
target = to_tensor(
PIL.Image.open(self.datadir / sample[1])
.convert(mode="RGB", dither=None)
.convert("L")
.point(lambda p: p <= 100, mode="1")
)
else:
raise ValueError(
f"Target type {self.target_type} is not an option. Available options are 'cup' and 'disc'."
)
mask = to_tensor(
PIL.Image.open(self._pkg_path / sample[2]).convert(mode="1", dither=None)
)
tensor = tv_tensors.Image(image)
target = tv_tensors.Image(target)
mask = tv_tensors.Mask(mask)
return tensor, dict(target=target, mask=mask, name=sample[0]) # type: ignore[arg-type]
class DataModule(CachingDataModule):
"""REFUGE for Optic Disc and Cup Segmentation.
The dataset consists of 1200 color fundus photographs, created for a MICCAI
challenge. The goal of the challenge is to evaluate and compare automated
algorithms for glaucoma detection and optic disc/cup segmentation on a common
dataset of retinal fundus images.
* Reference (including train/dev/test split): [REFUGE-2018]_
* Protocols ``optic-disc`` and ``cup``:
* Training samples:
* 400
* includes optic-disc and cup labels
* includes label: glaucomatous and non-glaucomatous
* original resolution: 2056 x 2124
* Validation samples:
* 400
* includes optic-disc and cup labels
* original resolution: 1634 x 1634
* Test samples:
* 400
* includes optic-disc and cup labels
* includes label: glaucomatous and non-glaucomatous
* original resolution:
Parameters
----------
split_filename
Name of the .json file containing the split to load.
target_type
Indicate whether to use the "cup" or "disc" target.
"""
def __init__(self, split_filename: str, target_type: str):
assert __package__ is not None
super().__init__(
database_split=make_split(__package__, split_filename),
raw_data_loader=SegmentationRawDataLoader(target_type),
database_name=__package__.rsplit(".", 1)[1],
split_name=pathlib.Path(split_filename).stem,
)
This diff is collapsed.
# SPDX-FileCopyrightText: Copyright © 2024 Idiap Research Institute <contact@idiap.ch>
#
# SPDX-License-Identifier: GPL-3.0-or-later
"""REFUGE dataset for Optic Disc Segmentation (default protocol).
* Configuration resolution: 1632 x 1632 (after resizing and padding)
* Reference (including split): [REFUGE-2018]_
* See :py:mod:`deepdraw.data.refuge` for dataset details
"""
from mednet.libs.segmentation.config.data.refuge.datamodule import DataModule
datamodule = DataModule("default.json", target_type="disc")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment