torch_em.data.datasets.histopathology.camelyon
This dataset contains annotations for tumor region segmentation in whole-slide histopathology images of breast cancer sentinel lymph node sections.
The data is from the CAMELYON16 and CAMELYON17 challenges, described in https://doi.org/10.1093/gigascience/giy065 ("1399 H&E-stained sentinel lymph node sections of breast cancer patients: the CAMELYON dataset"). It is hosted on the public AWS Open Data Registry (https://registry.opendata.aws/camelyon/) under a CC0 license. Please cite the publication if you use this dataset.
Two segmentation-compatible sources are exposed:
- CAMELYON16: all 399 whole-slide images (159 normal, 111 tumor, 129 test), each paired with a tumor segmentation mask.
- CAMELYON17: the subset of 100 training whole-slide images that ship with a segmentation mask. The remaining CAMELYON17 slides only carry a patient-level pN-stage classification label and are out of scope for this loader.
Mask label values: 0 (background / non-tumor tissue), 1 (tumor), and for CAMELYON16 tumor slides additionally 2 (non-tumor tissue excluded from a tumor annotation).
NOTE: The whole-slide images and masks are multi-resolution pyramidal TIFFs of several gigabytes each. On the first use each requested slide is converted into a chunked HDF5 file at the requested pyramid level, which requires some time and disk space.
1"""This dataset contains annotations for tumor region segmentation in 2whole-slide histopathology images of breast cancer sentinel lymph node sections. 3 4The data is from the CAMELYON16 and CAMELYON17 challenges, described in 5https://doi.org/10.1093/gigascience/giy065 ("1399 H&E-stained sentinel lymph 6node sections of breast cancer patients: the CAMELYON dataset"). It is hosted 7on the public AWS Open Data Registry (https://registry.opendata.aws/camelyon/) 8under a CC0 license. Please cite the publication if you use this dataset. 9 10Two segmentation-compatible sources are exposed: 11- CAMELYON16: all 399 whole-slide images (159 normal, 111 tumor, 129 test), 12 each paired with a tumor segmentation mask. 13- CAMELYON17: the subset of 100 training whole-slide images that ship with a 14 segmentation mask. The remaining CAMELYON17 slides only carry a patient-level 15 pN-stage classification label and are out of scope for this loader. 16 17Mask label values: 0 (background / non-tumor tissue), 1 (tumor), and for 18CAMELYON16 tumor slides additionally 2 (non-tumor tissue excluded from a 19tumor annotation). 20 21NOTE: The whole-slide images and masks are multi-resolution pyramidal TIFFs of 22several gigabytes each. On the first use each requested slide is converted into 23a chunked HDF5 file at the requested pyramid level, which requires some time 24and disk space. 25""" 26 27import os 28from pathlib import Path 29from typing import List, Literal, Optional, Tuple, Union 30 31from tqdm import tqdm 32 33import torch 34from torch.utils.data import Dataset, DataLoader 35 36import torch_em 37 38from .. import util 39 40 41BASE_URL = "https://camelyon-dataset.s3.us-west-2.amazonaws.com" 42 43CHECKSUM_URLS = { 44 "CAMELYON16": f"{BASE_URL}/CAMELYON16/checksums.md5", 45 "CAMELYON17": f"{BASE_URL}/CAMELYON17/checksums.md5", 46} 47 48 49def _get_checksums(path, version, download): 50 checksum_path = os.path.join(path, f"{version.lower()}_checksums.md5") 51 util.download_source(path=checksum_path, url=CHECKSUM_URLS[version], download=download, checksum=None) 52 53 checksums = {} 54 with open(checksum_path) as f: 55 for line in f: 56 checksum, name = line.split(maxsplit=1) 57 checksums[name.strip().lstrip("*")] = checksum 58 return checksums 59 60 61def _verify_md5(path, expected): 62 import hashlib 63 64 actual = hashlib.md5(Path(path).read_bytes()).hexdigest() 65 if actual != expected: 66 raise RuntimeError(f"The checksum of '{path}' does not match the expected checksum: {expected} != {actual}") 67 68 69def _download_slide(path, version, stem, checksums, download): 70 raw_dir = os.path.join(path, "raw", version) 71 os.makedirs(raw_dir, exist_ok=True) 72 73 image_path = os.path.join(raw_dir, f"{stem}.tif") 74 mask_path = os.path.join(raw_dir, f"{stem}_mask.tif") 75 76 for out_path, key in [(image_path, f"images/{stem}.tif"), (mask_path, f"masks/{stem}_mask.tif")]: 77 if os.path.exists(out_path): 78 continue 79 util.download_source(path=out_path, url=f"{BASE_URL}/{version}/{key}", download=download, checksum=None) 80 _verify_md5(out_path, checksums[key]) 81 82 return image_path, mask_path 83 84 85def _open_level(series, level_index): 86 import zarr 87 88 # The pyramidal TIFFs are natively tiled, so a zarr view reads only the requested tiles lazily. 89 array = zarr.open(series.aszarr(), mode="r") 90 return array if hasattr(array, "shape") else array[str(level_index)] 91 92 93def _convert_slide(image_path, mask_path, output_path, resolution_level, tile=4096): 94 import h5py 95 import tifffile 96 97 image_series = tifffile.TiffFile(image_path).series[0] 98 mask_series = tifffile.TiffFile(mask_path).series[0] 99 100 # Some scanners (e.g. Philips) round the declared level shape slightly differently between the raw 101 # slide and the independently rasterized mask, so tolerate a small mismatch and crop to the overlap. 102 image_height, image_width = image_series.levels[resolution_level].shape[:2] 103 mask_height, mask_width = mask_series.levels[resolution_level].shape[:2] 104 height, width = min(image_height, mask_height), min(image_width, mask_width) 105 tolerance = 0.02 106 if abs(image_height - mask_height) > tolerance * image_height or \ 107 abs(image_width - mask_width) > tolerance * image_width: 108 raise RuntimeError( 109 f"The mask '{mask_path}' does not match the raw shape ({image_height}, {image_width}) " 110 f"at level {resolution_level}: got ({mask_height}, {mask_width})." 111 ) 112 113 image = _open_level(image_series, resolution_level) 114 mask = _open_level(mask_series, resolution_level) 115 116 tmp_path = output_path + ".tmp" 117 with h5py.File(tmp_path, "w") as f: 118 raw = f.create_dataset( 119 "images/raw", shape=(3, height, width), dtype="uint8", compression="gzip", chunks=(1, 512, 512) 120 ) 121 labels = f.create_dataset( 122 "labels/mask", shape=(height, width), dtype="uint8", compression="gzip", chunks=(512, 512) 123 ) 124 for y in tqdm(range(0, height, tile), desc=f"Converting {Path(image_path).stem}"): 125 for x in range(0, width, tile): 126 th, tw = min(tile, height - y), min(tile, width - x) 127 raw[:, y:y + th, x:x + tw] = image[y:y + th, x:x + tw].transpose(2, 0, 1) 128 labels[y:y + th, x:x + tw] = mask[y:y + th, x:x + tw] 129 130 os.replace(tmp_path, output_path) 131 132 133def _restrict_to_sample_ids(stems, sample_ids): 134 if sample_ids is None: 135 return stems 136 missing = sorted(set(sample_ids) - set(stems)) 137 if missing: 138 raise ValueError(f"The following sample ids are not part of this dataset: {missing}") 139 return [stem for stem in stems if stem in sample_ids] 140 141 142def _resolve_camelyon16_stems(checksums, split, sample_ids): 143 stems = sorted(Path(name).stem for name in checksums if name.startswith("images/")) 144 if split is not None: 145 assert split in ("train", "test"), "Please choose from the available `train` / `test` splits" 146 prefixes = ("normal_", "tumor_") if split == "train" else ("test_",) 147 stems = [stem for stem in stems if stem.startswith(prefixes)] 148 return _restrict_to_sample_ids(stems, sample_ids) 149 150 151def _resolve_camelyon17_stems(checksums, sample_ids): 152 stems = sorted(Path(name).stem[:-len("_mask")] for name in checksums if name.startswith("masks/")) 153 return _restrict_to_sample_ids(stems, sample_ids) 154 155 156def get_camelyon16_data( 157 path: Union[os.PathLike, str], 158 split: Optional[Literal["train", "test"]] = None, 159 sample_ids: Optional[List[str]] = None, 160 resolution_level: int = 0, 161 download: bool = False, 162) -> str: 163 """Download and preprocess the CAMELYON16 tumor segmentation data. 164 165 Args: 166 path: Filepath to a folder where the data will be saved. 167 split: The split to use. Either 'train' (normal and tumor slides) or 'test'. By default all slides are used. 168 sample_ids: The slide names to restrict the data to, e.g. ['tumor_091', 'normal_108']. 169 By default all slides (matching `split`) are used. 170 resolution_level: The pyramid level to convert. 0 is the native resolution; use a higher level to 171 reduce the size of the preprocessed data. 172 download: Whether to download the data if it is not present. 173 174 Returns: 175 Filepath to the folder where the preprocessed data is stored. 176 """ 177 checksums = _get_checksums(path, "CAMELYON16", download) 178 stems = _resolve_camelyon16_stems(checksums, split, sample_ids) 179 180 preprocessed_dir = os.path.join(path, "preprocessed", "CAMELYON16") 181 os.makedirs(preprocessed_dir, exist_ok=True) 182 183 for stem in stems: 184 output_path = os.path.join(preprocessed_dir, f"{stem}_level{resolution_level}.h5") 185 if os.path.exists(output_path): 186 continue 187 image_path, mask_path = _download_slide(path, "CAMELYON16", stem, checksums, download) 188 _convert_slide(image_path, mask_path, output_path, resolution_level) 189 190 return preprocessed_dir 191 192 193def get_camelyon16_paths( 194 path: Union[os.PathLike, str], 195 split: Optional[Literal["train", "test"]] = None, 196 sample_ids: Optional[List[str]] = None, 197 resolution_level: int = 0, 198 download: bool = False, 199) -> List[str]: 200 """Get paths to the CAMELYON16 tumor segmentation data. 201 202 Args: 203 path: Filepath to a folder where the data will be saved. 204 split: The split to use. Either 'train' (normal and tumor slides) or 'test'. By default all slides are used. 205 sample_ids: The slide names to restrict the data to, e.g. ['tumor_091', 'normal_108']. 206 By default all slides (matching `split`) are used. 207 resolution_level: The pyramid level to convert. 0 is the native resolution; use a higher level to 208 reduce the size of the preprocessed data. 209 download: Whether to download the data if it is not present. 210 211 Returns: 212 List of filepaths to the preprocessed HDF5 files. 213 """ 214 preprocessed_dir = get_camelyon16_data(path, split, sample_ids, resolution_level, download) 215 stems = _resolve_camelyon16_stems(_get_checksums(path, "CAMELYON16", download=False), split, sample_ids) 216 return [os.path.join(preprocessed_dir, f"{stem}_level{resolution_level}.h5") for stem in stems] 217 218 219def get_camelyon17_data( 220 path: Union[os.PathLike, str], 221 sample_ids: Optional[List[str]] = None, 222 resolution_level: int = 0, 223 download: bool = False, 224) -> str: 225 """Download and preprocess the CAMELYON17 tumor segmentation subset. 226 227 This is the subset of CAMELYON17 training slides that ship with a segmentation mask. 228 The remaining slides only carry a patient-level classification label and are not included. 229 230 Args: 231 path: Filepath to a folder where the data will be saved. 232 sample_ids: The slide names to restrict the data to, e.g. ['patient_000_node_4']. 233 By default all slides with a segmentation mask are used. 234 resolution_level: The pyramid level to convert. 0 is the native resolution; use a higher level to 235 reduce the size of the preprocessed data. 236 download: Whether to download the data if it is not present. 237 238 Returns: 239 Filepath to the folder where the preprocessed data is stored. 240 """ 241 checksums = _get_checksums(path, "CAMELYON17", download) 242 stems = _resolve_camelyon17_stems(checksums, sample_ids) 243 244 preprocessed_dir = os.path.join(path, "preprocessed", "CAMELYON17") 245 os.makedirs(preprocessed_dir, exist_ok=True) 246 247 for stem in stems: 248 output_path = os.path.join(preprocessed_dir, f"{stem}_level{resolution_level}.h5") 249 if os.path.exists(output_path): 250 continue 251 image_path, mask_path = _download_slide(path, "CAMELYON17", stem, checksums, download) 252 _convert_slide(image_path, mask_path, output_path, resolution_level) 253 254 return preprocessed_dir 255 256 257def get_camelyon17_paths( 258 path: Union[os.PathLike, str], 259 sample_ids: Optional[List[str]] = None, 260 resolution_level: int = 0, 261 download: bool = False, 262) -> List[str]: 263 """Get paths to the CAMELYON17 tumor segmentation subset. 264 265 Args: 266 path: Filepath to a folder where the data will be saved. 267 sample_ids: The slide names to restrict the data to, e.g. ['patient_000_node_4']. 268 By default all slides with a segmentation mask are used. 269 resolution_level: The pyramid level to convert. 0 is the native resolution; use a higher level to 270 reduce the size of the preprocessed data. 271 download: Whether to download the data if it is not present. 272 273 Returns: 274 List of filepaths to the preprocessed HDF5 files. 275 """ 276 preprocessed_dir = get_camelyon17_data(path, sample_ids, resolution_level, download) 277 stems = _resolve_camelyon17_stems(_get_checksums(path, "CAMELYON17", download=False), sample_ids) 278 return [os.path.join(preprocessed_dir, f"{stem}_level{resolution_level}.h5") for stem in stems] 279 280 281def _get_dataset( 282 volume_paths, patch_shape, label_dtype, resize_inputs, kwargs, 283) -> Dataset: 284 if resize_inputs: 285 resize_kwargs = {"patch_shape": patch_shape, "is_rgb": True} 286 kwargs, patch_shape = util.update_kwargs_for_resize_trafo( 287 kwargs=kwargs, patch_shape=patch_shape, resize_inputs=resize_inputs, resize_kwargs=resize_kwargs 288 ) 289 290 return torch_em.default_segmentation_dataset( 291 raw_paths=volume_paths, 292 raw_key="images/raw", 293 label_paths=volume_paths, 294 label_key="labels/mask", 295 patch_shape=patch_shape, 296 label_dtype=label_dtype, 297 is_seg_dataset=True, 298 with_channels=True, 299 ndim=2, 300 **kwargs 301 ) 302 303 304def get_camelyon16_dataset( 305 path: Union[os.PathLike, str], 306 patch_shape: Tuple[int, int], 307 split: Optional[Literal["train", "test"]] = None, 308 sample_ids: Optional[List[str]] = None, 309 resolution_level: int = 0, 310 download: bool = False, 311 label_dtype: torch.dtype = torch.int64, 312 resize_inputs: bool = False, 313 **kwargs 314) -> Dataset: 315 """Get the CAMELYON16 dataset for tumor segmentation in whole-slide histopathology images. 316 317 Args: 318 path: Filepath to a folder where the data will be saved. 319 patch_shape: The patch shape to use for training. 320 split: The split to use. Either 'train' (normal and tumor slides) or 'test'. By default all slides are used. 321 sample_ids: The slide names to restrict the data to, e.g. ['tumor_091', 'normal_108']. 322 By default all slides (matching `split`) are used. 323 resolution_level: The pyramid level to convert. 0 is the native resolution; use a higher level to 324 reduce the size of the preprocessed data. 325 download: Whether to download the data if it is not present. 326 label_dtype: The datatype of the labels. 327 resize_inputs: Whether to resize the input images. 328 kwargs: Additional keyword arguments for `torch_em.default_segmentation_dataset`. 329 330 Returns: 331 The segmentation dataset. 332 """ 333 volume_paths = get_camelyon16_paths(path, split, sample_ids, resolution_level, download) 334 return _get_dataset(volume_paths, patch_shape, label_dtype, resize_inputs, kwargs) 335 336 337def get_camelyon16_loader( 338 path: Union[os.PathLike, str], 339 patch_shape: Tuple[int, int], 340 batch_size: int, 341 split: Optional[Literal["train", "test"]] = None, 342 sample_ids: Optional[List[str]] = None, 343 resolution_level: int = 0, 344 download: bool = False, 345 label_dtype: torch.dtype = torch.int64, 346 resize_inputs: bool = False, 347 **kwargs 348) -> DataLoader: 349 """Get the CAMELYON16 dataloader for tumor segmentation in whole-slide histopathology images. 350 351 Args: 352 path: Filepath to a folder where the data will be saved. 353 patch_shape: The patch shape to use for training. 354 batch_size: The batch size for training. 355 split: The split to use. Either 'train' (normal and tumor slides) or 'test'. By default all slides are used. 356 sample_ids: The slide names to restrict the data to, e.g. ['tumor_091', 'normal_108']. 357 By default all slides (matching `split`) are used. 358 resolution_level: The pyramid level to convert. 0 is the native resolution; use a higher level to 359 reduce the size of the preprocessed data. 360 download: Whether to download the data if it is not present. 361 label_dtype: The datatype of the labels. 362 resize_inputs: Whether to resize the input images. 363 kwargs: Additional keyword arguments for `torch_em.default_segmentation_dataset` or for the PyTorch DataLoader. 364 365 Returns: 366 The DataLoader. 367 """ 368 ds_kwargs, loader_kwargs = util.split_kwargs(torch_em.default_segmentation_dataset, **kwargs) 369 dataset = get_camelyon16_dataset( 370 path=path, patch_shape=patch_shape, split=split, sample_ids=sample_ids, resolution_level=resolution_level, 371 download=download, label_dtype=label_dtype, resize_inputs=resize_inputs, **ds_kwargs 372 ) 373 return torch_em.get_data_loader(dataset, batch_size, **loader_kwargs) 374 375 376def get_camelyon17_dataset( 377 path: Union[os.PathLike, str], 378 patch_shape: Tuple[int, int], 379 sample_ids: Optional[List[str]] = None, 380 resolution_level: int = 0, 381 download: bool = False, 382 label_dtype: torch.dtype = torch.int64, 383 resize_inputs: bool = False, 384 **kwargs 385) -> Dataset: 386 """Get the CAMELYON17 tumor segmentation subset for whole-slide histopathology images. 387 388 Args: 389 path: Filepath to a folder where the data will be saved. 390 patch_shape: The patch shape to use for training. 391 sample_ids: The slide names to restrict the data to, e.g. ['patient_000_node_4']. 392 By default all slides with a segmentation mask are used. 393 resolution_level: The pyramid level to convert. 0 is the native resolution; use a higher level to 394 reduce the size of the preprocessed data. 395 download: Whether to download the data if it is not present. 396 label_dtype: The datatype of the labels. 397 resize_inputs: Whether to resize the input images. 398 kwargs: Additional keyword arguments for `torch_em.default_segmentation_dataset`. 399 400 Returns: 401 The segmentation dataset. 402 """ 403 volume_paths = get_camelyon17_paths(path, sample_ids, resolution_level, download) 404 return _get_dataset(volume_paths, patch_shape, label_dtype, resize_inputs, kwargs) 405 406 407def get_camelyon17_loader( 408 path: Union[os.PathLike, str], 409 patch_shape: Tuple[int, int], 410 batch_size: int, 411 sample_ids: Optional[List[str]] = None, 412 resolution_level: int = 0, 413 download: bool = False, 414 label_dtype: torch.dtype = torch.int64, 415 resize_inputs: bool = False, 416 **kwargs 417) -> DataLoader: 418 """Get the CAMELYON17 tumor segmentation subset dataloader for whole-slide histopathology images. 419 420 Args: 421 path: Filepath to a folder where the data will be saved. 422 patch_shape: The patch shape to use for training. 423 batch_size: The batch size for training. 424 sample_ids: The slide names to restrict the data to, e.g. ['patient_000_node_4']. 425 By default all slides with a segmentation mask are used. 426 resolution_level: The pyramid level to convert. 0 is the native resolution; use a higher level to 427 reduce the size of the preprocessed data. 428 download: Whether to download the data if it is not present. 429 label_dtype: The datatype of the labels. 430 resize_inputs: Whether to resize the input images. 431 kwargs: Additional keyword arguments for `torch_em.default_segmentation_dataset` or for the PyTorch DataLoader. 432 433 Returns: 434 The DataLoader. 435 """ 436 ds_kwargs, loader_kwargs = util.split_kwargs(torch_em.default_segmentation_dataset, **kwargs) 437 dataset = get_camelyon17_dataset( 438 path=path, patch_shape=patch_shape, sample_ids=sample_ids, resolution_level=resolution_level, 439 download=download, label_dtype=label_dtype, resize_inputs=resize_inputs, **ds_kwargs 440 ) 441 return torch_em.get_data_loader(dataset, batch_size, **loader_kwargs)
157def get_camelyon16_data( 158 path: Union[os.PathLike, str], 159 split: Optional[Literal["train", "test"]] = None, 160 sample_ids: Optional[List[str]] = None, 161 resolution_level: int = 0, 162 download: bool = False, 163) -> str: 164 """Download and preprocess the CAMELYON16 tumor segmentation data. 165 166 Args: 167 path: Filepath to a folder where the data will be saved. 168 split: The split to use. Either 'train' (normal and tumor slides) or 'test'. By default all slides are used. 169 sample_ids: The slide names to restrict the data to, e.g. ['tumor_091', 'normal_108']. 170 By default all slides (matching `split`) are used. 171 resolution_level: The pyramid level to convert. 0 is the native resolution; use a higher level to 172 reduce the size of the preprocessed data. 173 download: Whether to download the data if it is not present. 174 175 Returns: 176 Filepath to the folder where the preprocessed data is stored. 177 """ 178 checksums = _get_checksums(path, "CAMELYON16", download) 179 stems = _resolve_camelyon16_stems(checksums, split, sample_ids) 180 181 preprocessed_dir = os.path.join(path, "preprocessed", "CAMELYON16") 182 os.makedirs(preprocessed_dir, exist_ok=True) 183 184 for stem in stems: 185 output_path = os.path.join(preprocessed_dir, f"{stem}_level{resolution_level}.h5") 186 if os.path.exists(output_path): 187 continue 188 image_path, mask_path = _download_slide(path, "CAMELYON16", stem, checksums, download) 189 _convert_slide(image_path, mask_path, output_path, resolution_level) 190 191 return preprocessed_dir
Download and preprocess the CAMELYON16 tumor segmentation data.
Arguments:
- path: Filepath to a folder where the data will be saved.
- split: The split to use. Either 'train' (normal and tumor slides) or 'test'. By default all slides are used.
- sample_ids: The slide names to restrict the data to, e.g. ['tumor_091', 'normal_108'].
By default all slides (matching
split) are used. - resolution_level: The pyramid level to convert. 0 is the native resolution; use a higher level to reduce the size of the preprocessed data.
- download: Whether to download the data if it is not present.
Returns:
Filepath to the folder where the preprocessed data is stored.
194def get_camelyon16_paths( 195 path: Union[os.PathLike, str], 196 split: Optional[Literal["train", "test"]] = None, 197 sample_ids: Optional[List[str]] = None, 198 resolution_level: int = 0, 199 download: bool = False, 200) -> List[str]: 201 """Get paths to the CAMELYON16 tumor segmentation data. 202 203 Args: 204 path: Filepath to a folder where the data will be saved. 205 split: The split to use. Either 'train' (normal and tumor slides) or 'test'. By default all slides are used. 206 sample_ids: The slide names to restrict the data to, e.g. ['tumor_091', 'normal_108']. 207 By default all slides (matching `split`) are used. 208 resolution_level: The pyramid level to convert. 0 is the native resolution; use a higher level to 209 reduce the size of the preprocessed data. 210 download: Whether to download the data if it is not present. 211 212 Returns: 213 List of filepaths to the preprocessed HDF5 files. 214 """ 215 preprocessed_dir = get_camelyon16_data(path, split, sample_ids, resolution_level, download) 216 stems = _resolve_camelyon16_stems(_get_checksums(path, "CAMELYON16", download=False), split, sample_ids) 217 return [os.path.join(preprocessed_dir, f"{stem}_level{resolution_level}.h5") for stem in stems]
Get paths to the CAMELYON16 tumor segmentation data.
Arguments:
- path: Filepath to a folder where the data will be saved.
- split: The split to use. Either 'train' (normal and tumor slides) or 'test'. By default all slides are used.
- sample_ids: The slide names to restrict the data to, e.g. ['tumor_091', 'normal_108'].
By default all slides (matching
split) are used. - resolution_level: The pyramid level to convert. 0 is the native resolution; use a higher level to reduce the size of the preprocessed data.
- download: Whether to download the data if it is not present.
Returns:
List of filepaths to the preprocessed HDF5 files.
220def get_camelyon17_data( 221 path: Union[os.PathLike, str], 222 sample_ids: Optional[List[str]] = None, 223 resolution_level: int = 0, 224 download: bool = False, 225) -> str: 226 """Download and preprocess the CAMELYON17 tumor segmentation subset. 227 228 This is the subset of CAMELYON17 training slides that ship with a segmentation mask. 229 The remaining slides only carry a patient-level classification label and are not included. 230 231 Args: 232 path: Filepath to a folder where the data will be saved. 233 sample_ids: The slide names to restrict the data to, e.g. ['patient_000_node_4']. 234 By default all slides with a segmentation mask are used. 235 resolution_level: The pyramid level to convert. 0 is the native resolution; use a higher level to 236 reduce the size of the preprocessed data. 237 download: Whether to download the data if it is not present. 238 239 Returns: 240 Filepath to the folder where the preprocessed data is stored. 241 """ 242 checksums = _get_checksums(path, "CAMELYON17", download) 243 stems = _resolve_camelyon17_stems(checksums, sample_ids) 244 245 preprocessed_dir = os.path.join(path, "preprocessed", "CAMELYON17") 246 os.makedirs(preprocessed_dir, exist_ok=True) 247 248 for stem in stems: 249 output_path = os.path.join(preprocessed_dir, f"{stem}_level{resolution_level}.h5") 250 if os.path.exists(output_path): 251 continue 252 image_path, mask_path = _download_slide(path, "CAMELYON17", stem, checksums, download) 253 _convert_slide(image_path, mask_path, output_path, resolution_level) 254 255 return preprocessed_dir
Download and preprocess the CAMELYON17 tumor segmentation subset.
This is the subset of CAMELYON17 training slides that ship with a segmentation mask. The remaining slides only carry a patient-level classification label and are not included.
Arguments:
- path: Filepath to a folder where the data will be saved.
- sample_ids: The slide names to restrict the data to, e.g. ['patient_000_node_4']. By default all slides with a segmentation mask are used.
- resolution_level: The pyramid level to convert. 0 is the native resolution; use a higher level to reduce the size of the preprocessed data.
- download: Whether to download the data if it is not present.
Returns:
Filepath to the folder where the preprocessed data is stored.
258def get_camelyon17_paths( 259 path: Union[os.PathLike, str], 260 sample_ids: Optional[List[str]] = None, 261 resolution_level: int = 0, 262 download: bool = False, 263) -> List[str]: 264 """Get paths to the CAMELYON17 tumor segmentation subset. 265 266 Args: 267 path: Filepath to a folder where the data will be saved. 268 sample_ids: The slide names to restrict the data to, e.g. ['patient_000_node_4']. 269 By default all slides with a segmentation mask are used. 270 resolution_level: The pyramid level to convert. 0 is the native resolution; use a higher level to 271 reduce the size of the preprocessed data. 272 download: Whether to download the data if it is not present. 273 274 Returns: 275 List of filepaths to the preprocessed HDF5 files. 276 """ 277 preprocessed_dir = get_camelyon17_data(path, sample_ids, resolution_level, download) 278 stems = _resolve_camelyon17_stems(_get_checksums(path, "CAMELYON17", download=False), sample_ids) 279 return [os.path.join(preprocessed_dir, f"{stem}_level{resolution_level}.h5") for stem in stems]
Get paths to the CAMELYON17 tumor segmentation subset.
Arguments:
- path: Filepath to a folder where the data will be saved.
- sample_ids: The slide names to restrict the data to, e.g. ['patient_000_node_4']. By default all slides with a segmentation mask are used.
- resolution_level: The pyramid level to convert. 0 is the native resolution; use a higher level to reduce the size of the preprocessed data.
- download: Whether to download the data if it is not present.
Returns:
List of filepaths to the preprocessed HDF5 files.
305def get_camelyon16_dataset( 306 path: Union[os.PathLike, str], 307 patch_shape: Tuple[int, int], 308 split: Optional[Literal["train", "test"]] = None, 309 sample_ids: Optional[List[str]] = None, 310 resolution_level: int = 0, 311 download: bool = False, 312 label_dtype: torch.dtype = torch.int64, 313 resize_inputs: bool = False, 314 **kwargs 315) -> Dataset: 316 """Get the CAMELYON16 dataset for tumor segmentation in whole-slide histopathology images. 317 318 Args: 319 path: Filepath to a folder where the data will be saved. 320 patch_shape: The patch shape to use for training. 321 split: The split to use. Either 'train' (normal and tumor slides) or 'test'. By default all slides are used. 322 sample_ids: The slide names to restrict the data to, e.g. ['tumor_091', 'normal_108']. 323 By default all slides (matching `split`) are used. 324 resolution_level: The pyramid level to convert. 0 is the native resolution; use a higher level to 325 reduce the size of the preprocessed data. 326 download: Whether to download the data if it is not present. 327 label_dtype: The datatype of the labels. 328 resize_inputs: Whether to resize the input images. 329 kwargs: Additional keyword arguments for `torch_em.default_segmentation_dataset`. 330 331 Returns: 332 The segmentation dataset. 333 """ 334 volume_paths = get_camelyon16_paths(path, split, sample_ids, resolution_level, download) 335 return _get_dataset(volume_paths, patch_shape, label_dtype, resize_inputs, kwargs)
Get the CAMELYON16 dataset for tumor segmentation in whole-slide histopathology images.
Arguments:
- path: Filepath to a folder where the data will be saved.
- patch_shape: The patch shape to use for training.
- split: The split to use. Either 'train' (normal and tumor slides) or 'test'. By default all slides are used.
- sample_ids: The slide names to restrict the data to, e.g. ['tumor_091', 'normal_108'].
By default all slides (matching
split) are used. - resolution_level: The pyramid level to convert. 0 is the native resolution; use a higher level to reduce the size of the preprocessed data.
- download: Whether to download the data if it is not present.
- label_dtype: The datatype of the labels.
- resize_inputs: Whether to resize the input images.
- kwargs: Additional keyword arguments for
torch_em.default_segmentation_dataset.
Returns:
The segmentation dataset.
338def get_camelyon16_loader( 339 path: Union[os.PathLike, str], 340 patch_shape: Tuple[int, int], 341 batch_size: int, 342 split: Optional[Literal["train", "test"]] = None, 343 sample_ids: Optional[List[str]] = None, 344 resolution_level: int = 0, 345 download: bool = False, 346 label_dtype: torch.dtype = torch.int64, 347 resize_inputs: bool = False, 348 **kwargs 349) -> DataLoader: 350 """Get the CAMELYON16 dataloader for tumor segmentation in whole-slide histopathology images. 351 352 Args: 353 path: Filepath to a folder where the data will be saved. 354 patch_shape: The patch shape to use for training. 355 batch_size: The batch size for training. 356 split: The split to use. Either 'train' (normal and tumor slides) or 'test'. By default all slides are used. 357 sample_ids: The slide names to restrict the data to, e.g. ['tumor_091', 'normal_108']. 358 By default all slides (matching `split`) are used. 359 resolution_level: The pyramid level to convert. 0 is the native resolution; use a higher level to 360 reduce the size of the preprocessed data. 361 download: Whether to download the data if it is not present. 362 label_dtype: The datatype of the labels. 363 resize_inputs: Whether to resize the input images. 364 kwargs: Additional keyword arguments for `torch_em.default_segmentation_dataset` or for the PyTorch DataLoader. 365 366 Returns: 367 The DataLoader. 368 """ 369 ds_kwargs, loader_kwargs = util.split_kwargs(torch_em.default_segmentation_dataset, **kwargs) 370 dataset = get_camelyon16_dataset( 371 path=path, patch_shape=patch_shape, split=split, sample_ids=sample_ids, resolution_level=resolution_level, 372 download=download, label_dtype=label_dtype, resize_inputs=resize_inputs, **ds_kwargs 373 ) 374 return torch_em.get_data_loader(dataset, batch_size, **loader_kwargs)
Get the CAMELYON16 dataloader for tumor segmentation in whole-slide histopathology images.
Arguments:
- path: Filepath to a folder where the data will be saved.
- patch_shape: The patch shape to use for training.
- batch_size: The batch size for training.
- split: The split to use. Either 'train' (normal and tumor slides) or 'test'. By default all slides are used.
- sample_ids: The slide names to restrict the data to, e.g. ['tumor_091', 'normal_108'].
By default all slides (matching
split) are used. - resolution_level: The pyramid level to convert. 0 is the native resolution; use a higher level to reduce the size of the preprocessed data.
- download: Whether to download the data if it is not present.
- label_dtype: The datatype of the labels.
- resize_inputs: Whether to resize the input images.
- kwargs: Additional keyword arguments for
torch_em.default_segmentation_datasetor for the PyTorch DataLoader.
Returns:
The DataLoader.
377def get_camelyon17_dataset( 378 path: Union[os.PathLike, str], 379 patch_shape: Tuple[int, int], 380 sample_ids: Optional[List[str]] = None, 381 resolution_level: int = 0, 382 download: bool = False, 383 label_dtype: torch.dtype = torch.int64, 384 resize_inputs: bool = False, 385 **kwargs 386) -> Dataset: 387 """Get the CAMELYON17 tumor segmentation subset for whole-slide histopathology images. 388 389 Args: 390 path: Filepath to a folder where the data will be saved. 391 patch_shape: The patch shape to use for training. 392 sample_ids: The slide names to restrict the data to, e.g. ['patient_000_node_4']. 393 By default all slides with a segmentation mask are used. 394 resolution_level: The pyramid level to convert. 0 is the native resolution; use a higher level to 395 reduce the size of the preprocessed data. 396 download: Whether to download the data if it is not present. 397 label_dtype: The datatype of the labels. 398 resize_inputs: Whether to resize the input images. 399 kwargs: Additional keyword arguments for `torch_em.default_segmentation_dataset`. 400 401 Returns: 402 The segmentation dataset. 403 """ 404 volume_paths = get_camelyon17_paths(path, sample_ids, resolution_level, download) 405 return _get_dataset(volume_paths, patch_shape, label_dtype, resize_inputs, kwargs)
Get the CAMELYON17 tumor segmentation subset for whole-slide histopathology images.
Arguments:
- path: Filepath to a folder where the data will be saved.
- patch_shape: The patch shape to use for training.
- sample_ids: The slide names to restrict the data to, e.g. ['patient_000_node_4']. By default all slides with a segmentation mask are used.
- resolution_level: The pyramid level to convert. 0 is the native resolution; use a higher level to reduce the size of the preprocessed data.
- download: Whether to download the data if it is not present.
- label_dtype: The datatype of the labels.
- resize_inputs: Whether to resize the input images.
- kwargs: Additional keyword arguments for
torch_em.default_segmentation_dataset.
Returns:
The segmentation dataset.
408def get_camelyon17_loader( 409 path: Union[os.PathLike, str], 410 patch_shape: Tuple[int, int], 411 batch_size: int, 412 sample_ids: Optional[List[str]] = None, 413 resolution_level: int = 0, 414 download: bool = False, 415 label_dtype: torch.dtype = torch.int64, 416 resize_inputs: bool = False, 417 **kwargs 418) -> DataLoader: 419 """Get the CAMELYON17 tumor segmentation subset dataloader for whole-slide histopathology images. 420 421 Args: 422 path: Filepath to a folder where the data will be saved. 423 patch_shape: The patch shape to use for training. 424 batch_size: The batch size for training. 425 sample_ids: The slide names to restrict the data to, e.g. ['patient_000_node_4']. 426 By default all slides with a segmentation mask are used. 427 resolution_level: The pyramid level to convert. 0 is the native resolution; use a higher level to 428 reduce the size of the preprocessed data. 429 download: Whether to download the data if it is not present. 430 label_dtype: The datatype of the labels. 431 resize_inputs: Whether to resize the input images. 432 kwargs: Additional keyword arguments for `torch_em.default_segmentation_dataset` or for the PyTorch DataLoader. 433 434 Returns: 435 The DataLoader. 436 """ 437 ds_kwargs, loader_kwargs = util.split_kwargs(torch_em.default_segmentation_dataset, **kwargs) 438 dataset = get_camelyon17_dataset( 439 path=path, patch_shape=patch_shape, sample_ids=sample_ids, resolution_level=resolution_level, 440 download=download, label_dtype=label_dtype, resize_inputs=resize_inputs, **ds_kwargs 441 ) 442 return torch_em.get_data_loader(dataset, batch_size, **loader_kwargs)
Get the CAMELYON17 tumor segmentation subset dataloader for whole-slide histopathology images.
Arguments:
- path: Filepath to a folder where the data will be saved.
- patch_shape: The patch shape to use for training.
- batch_size: The batch size for training.
- sample_ids: The slide names to restrict the data to, e.g. ['patient_000_node_4']. By default all slides with a segmentation mask are used.
- resolution_level: The pyramid level to convert. 0 is the native resolution; use a higher level to reduce the size of the preprocessed data.
- download: Whether to download the data if it is not present.
- label_dtype: The datatype of the labels.
- resize_inputs: Whether to resize the input images.
- kwargs: Additional keyword arguments for
torch_em.default_segmentation_datasetor for the PyTorch DataLoader.
Returns:
The DataLoader.