Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/bin/env python
2# coding=utf-8
4"""REFUGE for Optic Disc and Cup Segmentation
6The dataset consists of 1200 color fundus photographs, created for a MICCAI
7challenge. The goal of the challenge is to evaluate and compare automated
8algorithms for glaucoma detection and optic disc/cup segmentation on a common
9dataset of retinal fundus images.
11* Reference (including train/dev/test split): [REFUGE-2018]_
12* Protocols ``optic-disc`` and ``cup``:
14 * Training samples:
16 * 400
17 * includes optic-disc and cup labels
18 * includes label: glaucomatous and non-glaucomatous
19 * original resolution: 2056 x 2124
21 * Validation samples:
23 * 400
24 * includes optic-disc and cup labels
25 * original resolution: 1634 x 1634
27 * Test samples:
29 * 400
30 * includes optic-disc and cup labels
31 * includes label: glaucomatous and non-glaucomatous
32 * original resolution:
33"""
35import os
37import pkg_resources
39import bob.extension
41from ..dataset import JSONDataset
42from ..loader import load_pil_1, load_pil_rgb, make_delayed
44_protocols = {
45 "optic-disc": pkg_resources.resource_filename(__name__, "default.json"),
46 "optic-cup": pkg_resources.resource_filename(__name__, "default.json"),
47}
49_root_path = bob.extension.rc.get(
50 "bob.ip.binseg.refuge.datadir", os.path.realpath(os.curdir)
51)
52_pkg_path = pkg_resources.resource_filename(__name__, "masks")
55def _disc_loader(sample):
56 retval = dict(
57 data=load_pil_rgb(os.path.join(_root_path, sample["data"])),
58 label=load_pil_rgb(os.path.join(_root_path, sample["label"])),
59 mask=load_pil_1(os.path.join(_pkg_path, sample["mask"])),
60 )
61 if "glaucoma" in sample:
62 retval["glaucoma"] = sample["glaucoma"]
63 retval["label"] = retval["label"].convert("L")
64 retval["label"] = retval["label"].point(lambda p: p <= 150, mode="1")
65 return retval
68def _cup_loader(sample):
69 retval = dict(
70 data=load_pil_rgb(os.path.join(_root_path, sample["data"])),
71 label=load_pil_rgb(os.path.join(_root_path, sample["label"])),
72 mask=load_pil_1(os.path.join(_pkg_path, sample["mask"])),
73 )
74 if "glaucoma" in sample:
75 retval["glaucoma"] = sample["glaucoma"]
76 retval["label"] = retval["label"].convert("L")
77 retval["label"] = retval["label"].point(lambda p: p <= 100, mode="1")
78 return retval
81def _loader(context, sample):
83 if context["subset"] == "train":
84 # adds binary metadata for glaucoma/non-glaucoma patients
85 sample["glaucoma"] = os.path.basename(sample["label"]).startswith("g")
86 elif context["subset"] == "test":
87 sample["glaucoma"] = sample["label"].split(os.sep)[-2] == "G"
88 elif context["subset"] == "validation":
89 pass
90 else:
91 raise RuntimeError(f"Unknown subset {context['subset']}")
93 # optic disc is drawn with gray == 128 and includes the cup, drawn with
94 # black == 0. The rest is white == 255.
95 if context["protocol"] == "optic-disc":
96 return make_delayed(sample, _disc_loader)
97 elif context["protocol"] == "optic-cup":
98 return make_delayed(sample, _cup_loader)
99 else:
100 raise RuntimeError(f"Unknown protocol {context['protocol']}")
103dataset = JSONDataset(
104 protocols=_protocols,
105 fieldnames=("data", "label", "mask"),
106 loader=_loader,
107)
108"""REFUGE dataset object"""