Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Kvasir-SEG dataset #267

Merged
merged 5 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions scripts/datasets/check_kvasir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from torch_em.util.debug import check_loader
from torch_em.data.datasets.medical import get_kvasir_loader


ROOT = "/media/anwai/ANWAI/data/kvasir"


def check_kvasir():
loader = get_kvasir_loader(
path=ROOT,
patch_shape=(512, 512),
batch_size=2,
download=True,
resize_inputs=False,
)

check_loader(loader, 8)


if __name__ == "__main__":
check_kvasir()
1 change: 1 addition & 0 deletions torch_em/data/datasets/medical/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
from .btcv import get_btcv_dataset, get_btcv_loader
from .camus import get_camus_dataset, get_camus_loader
from .drive import get_drive_dataset, get_drive_loader
from .kvasir import get_kvasir_dataset, get_kvasir_loader
from .papila import get_papila_dataset, get_papila_loader
from .siim_acr import get_siim_acr_dataset, get_siim_acr_loader
110 changes: 110 additions & 0 deletions torch_em/data/datasets/medical/kvasir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import os
from glob import glob
from tqdm import tqdm
from pathlib import Path
from typing import Union, Tuple

import numpy as np
import imageio.v3 as imageio

import torch_em
from torch_em.transform.generic import ResizeInputs

from .. import util
from ... import ImageCollectionDataset


URL = "https://datasets.simula.no/downloads/kvasir-seg.zip"
CHECKSUM = "03b30e21d584e04facf49397a2576738fd626815771afbbf788f74a7153478f7"


def get_kvasir_data(path, download):
os.makedirs(path, exist_ok=True)

data_dir = os.path.join(path, "Kvasir-SEG")
if os.path.exists(data_dir):
return data_dir

zip_path = os.path.join(path, "kvasir-seg.zip")
util.download_source(path=zip_path, url=URL, download=download, checksum=CHECKSUM)
util.unzip(zip_path=zip_path, dst=path)

return data_dir


def _get_kvasir_paths(path, download):
data_dir = get_kvasir_data(path=path, download=download)

image_paths = sorted(glob(os.path.join(data_dir, "images", "*.jpg")))
gt_paths = sorted(glob(os.path.join(data_dir, "masks", "*.jpg")))

neu_gt_dir = os.path.join(data_dir, "masks", "preprocessed")
os.makedirs(neu_gt_dir, exist_ok=True)

neu_gt_paths = []
for gt_path in tqdm(gt_paths):
neu_gt_path = os.path.join(neu_gt_dir, f"{Path(gt_path).stem}.tif")
neu_gt_paths.append(neu_gt_path)
if os.path.exists(neu_gt_path):
continue

gt = imageio.imread(gt_path)
gt = np.mean(gt, axis=-1)
gt = (gt >= 240).astype("uint8")
imageio.imwrite(neu_gt_path, gt)

return image_paths, neu_gt_paths


def get_kvasir_dataset(
path: Union[os.PathLike, str],
patch_shape: Tuple[int, int],
resize_inputs: bool = False,
download: bool = False,
**kwargs
):
"""Dataset for polyp segmentation in colonoscopy images.

The dataset is located at https://datasets.simula.no/kvasir-seg/

This dataset is from Jha et al. - https://doi.org/10.1007/978-3-030-37734-2_37
Please cite it if you use this dataset for a publication.
"""
image_paths, gt_paths = _get_kvasir_paths(path=path, download=download)

if resize_inputs:
raw_trafo = ResizeInputs(target_shape=patch_shape, is_rgb=True)
label_trafo = ResizeInputs(target_shape=patch_shape, is_label=True)
anwai98 marked this conversation as resolved.
Show resolved Hide resolved
patch_shape = None
else:
patch_shape = patch_shape
raw_trafo, label_trafo = None, None

dataset = ImageCollectionDataset(
anwai98 marked this conversation as resolved.
Show resolved Hide resolved
raw_image_paths=image_paths,
label_image_paths=gt_paths,
patch_shape=patch_shape,
raw_transform=raw_trafo,
label_transform=label_trafo,
**kwargs
)

return dataset


def get_kvasir_loader(
path: Union[os.PathLike, str],
patch_shape: Tuple[int, int],
batch_size: int,
resize_inputs: bool = False,
download: bool = False,
**kwargs
):
"""Dataloader for polyp segmentation in colonoscopy images. See `get_kvasir_dataset` for details.
"""
ds_kwargs, loader_kwargs = util.split_kwargs(torch_em.default_segmentation_dataset, **kwargs)
dataset = get_kvasir_dataset(
path=path, patch_shape=patch_shape, resize_inputs=resize_inputs, download=download, **ds_kwargs
)
loader = torch_em.get_data_loader(dataset=dataset, batch_size=batch_size, **loader_kwargs)
return loader