Code examples / Computer Vision / 3D Multimodal Brain Tumor Segmentation

3D Multimodal Brain Tumor Segmentation

Author: Mohammed Innat
Date created: 2026/02/02
Last modified: 2026/02/02
Description: Implementing 3D semantic segmentation pipeline for medical imaging.

ⓘ This example uses Keras 2

View in Colab GitHub source

Brain Tumor Segmentation

Brain tumor segmentation is a core task in medical image analysis, where the goal is to automatically identify and label different tumor sub-regions from 3D MRI scans. Accurate segmentation helps clinicians with diagnosis, treatment planning, and disease monitoring. In this tutorial, we focus on multimodal MRI-based brain tumor segmentation using the widely adopted BraTS (Brain Tumor Segmentation) dataset.


The BraTS Dataset

The BraTS dataset provides multimodal 3D brain MRI scans, released as NIfTI files (.nii.gz). For each patient, four MRI modalities are available:

  • T1 – native T1-weighted MRI
  • T1Gd – post-contrast T1-weighted MRI
  • T2 – T2-weighted MRI
  • T2-FLAIR – Fluid Attenuated Inversion Recovery MRI

These scans are collected using different scanners and clinical protocols from 19 institutions, making the dataset diverse and realistic. More details about the dataset can be found in the official BraTS documentation.


Segmentation Labels

Each scan is manually annotated by one to four expert raters, following a standardized annotation protocol and reviewed by experienced neuroradiologists. The segmentation masks contain the following tumor sub-regions:

  • NCR / NET (label 1) – Necrotic and non-enhancing tumor core
  • ED (label 2) – Peritumoral edema
  • ET (label 4) – GD-enhancing tumor
  • 0 – Background (non-tumor tissue)

The data are released after preprocessing:

  • All modalities are co-registered
  • Resampled to 1 mm³ isotropic resolution
  • Skull-stripped for consistency

Dataset Format and TFRecord Conversion

The original BraTS scans are provided in .nii format and can be accessed from Kaggle here. To enable efficient training pipelines, we convert the NIfTI files into TFRecord format:

  • The conversion process is documented here
  • The preprocessed TFRecord dataset is available here
  • Each TFRecord file contains up to 20 cases

Since BraTS does not provide publicly available ground-truth labels for validation or test sets, we will hold out a subset of TFRecord files from training for validation purposes.

What This Tutorial Covers

In this tutorial, we provide a step-by-step, end-to-end workflow for brain tumor segmentation using medicai, a Keras-based medical imaging library with multi-backend support. We will walk through:

  1. Loading the Dataset - Read TFRecord files that contain image, label, and affine matrix information. - Build efficient data pipelines using the tf.data API for training and evaluation.
  2. Medical Image Preprocessing - Apply image transformations provided by medicai to prepare the data for model input.
  3. Model Building - Construct a 3D segmentation model with SwinUNETR You can also experiment with other available 3D architectures, including UNETR, SegFormer, and UNETR++..
  4. Loss and Metrics Definition - Using Dice-based loss functions and segmentation metrics tailored for medical imaging
  5. Model Evaluation - Performing inference on large 3D volumes using sliding window inference - Computing per-class evaluation metrics
  6. Visualization of Results - Visualizing predicted segmentation masks for qualitative analysis

By the end of this tutorial, you will have a complete brain tumor segmentation pipeline, from data loading and preprocessing to model training, evaluation, and visualization using modern 3D deep learning techniques and the medicai framework.


Installation

We will install the following packages: kagglehub for downloading the dataset from Kaggle, and medicai for accessing specialized methods for medical imaging, including 3D transformations, model architectures, loss functions, metrics, and other essential components.

!pip install kagglehub -qU
!pip install git+https://github.com/innat/medic-ai.git -qU
import os
import warnings
import shutil

warnings.filterwarnings("ignore")

import kagglehub

if "KAGGLE_USERNAME" not in os.environ or "KAGGLE_KEY" not in os.environ:
    kagglehub.login()
VBox(children=(HTML(value='<center> <img\nsrc=https://www.kaggle.com/static/images/site-logo.png\nalt=\'Kaggle…

Download the dataset from kaggle.

dataset_id = "ipythonx/brats2020"
destination_path = "brats2020_subset"
os.makedirs(destination_path, exist_ok=True)

# Download the 3 shards: 0 and 1st for training set, 36th for validation set.
for i in [0, 1, 36]:
    filename = f"training_shard_{i}.tfrec"
    print(f"Downloading {filename}...")
    path = kagglehub.dataset_download(dataset_id, path=filename)
    shutil.move(path, destination_path)
Downloading training_shard_0.tfrec...

Downloading to /home/jupyter/.cache/kagglehub/datasets/ipythonx/brats2020/versions/1/training_shard_0.tfrec...

0%| | 0.00/1.66G [00:00<?, ?B/s]

0%| | 1.00M/1.66G [00:00<12:12, 2.44MB/s]

0%|▏ | 3.00M/1.66G [00:00<04:26, 6.68MB/s]

0%|▍ | 8.00M/1.66G [00:00<01:40, 17.7MB/s]

1%|▌ | 13.0M/1.66G [00:00<01:07, 26.2MB/s]

1%|▉ | 19.0M/1.66G [00:00<00:52, 33.9MB/s]

1%|█▏ | 24.0M/1.66G [00:00<00:45, 38.5MB/s]

2%|█▍ | 30.0M/1.66G [00:01<00:41, 42.7MB/s]

2%|█▋ | 35.0M/1.66G [00:01<00:38, 45.3MB/s]

2%|█▉ | 41.0M/1.66G [00:01<00:36, 47.4MB/s]

3%|██▏ | 46.0M/1.66G [00:01<00:35, 48.7MB/s]

3%|██▍ | 52.0M/1.66G [00:01<00:34, 49.5MB/s]

3%|██▋ | 58.0M/1.66G [00:01<00:33, 51.1MB/s]

4%|██▉ | 63.0M/1.66G [00:01<00:33, 50.6MB/s]

4%|███▏ | 69.0M/1.66G [00:01<00:33, 51.9MB/s]

4%|███▍ | 74.0M/1.66G [00:02<00:33, 50.7MB/s]

5%|███▊ | 80.0M/1.66G [00:02<00:32, 52.4MB/s]

5%|████ | 86.0M/1.66G [00:02<00:33, 51.1MB/s]

5%|████▎ | 92.0M/1.66G [00:02<00:33, 50.7MB/s]

6%|████▌ | 98.0M/1.66G [00:02<00:31, 52.7MB/s]

6%|████▉ | 104M/1.66G [00:02<00:32, 52.1MB/s]

6%|█████▏ | 109M/1.66G [00:02<00:32, 52.2MB/s]

7%|█████▍ | 114M/1.66G [00:02<00:32, 50.8MB/s]

7%|█████▋ | 120M/1.66G [00:02<00:31, 52.5MB/s]

7%|█████▉ | 126M/1.66G [00:03<00:32, 51.1MB/s]

8%|██████▏ | 131M/1.66G [00:03<00:33, 48.7MB/s]

8%|██████▍ | 136M/1.66G [00:03<00:36, 45.5MB/s]

8%|██████▊ | 143M/1.66G [00:03<00:34, 47.2MB/s]

9%|███████▏ | 150M/1.66G [00:03<00:33, 48.3MB/s]

9%|███████▌ | 158M/1.66G [00:03<00:32, 50.3MB/s]

10%|███████▊ | 165M/1.66G [00:03<00:31, 51.1MB/s]

10%|████████▏ | 172M/1.66G [00:04<00:31, 51.5MB/s]

11%|████████▌ | 180M/1.66G [00:04<00:30, 52.9MB/s]

11%|████████▉ | 187M/1.66G [00:04<00:30, 53.0MB/s]

11%|█████████▏ | 194M/1.66G [00:04<00:29, 52.9MB/s]

12%|█████████▌ | 201M/1.66G [00:04<00:29, 52.6MB/s]

12%|█████████▉ | 209M/1.66G [00:04<00:29, 53.6MB/s]

13%|██████████▏ | 215M/1.66G [00:04<00:32, 47.3MB/s]

13%|██████████▌ | 221M/1.66G [00:05<00:32, 48.5MB/s]

13%|██████████▊ | 227M/1.66G [00:05<00:30, 50.4MB/s]

14%|███████████ | 232M/1.66G [00:05<00:30, 50.3MB/s]

14%|███████████▎ | 238M/1.66G [00:05<00:29, 51.9MB/s]

14%|███████████▌ | 244M/1.66G [00:05<00:29, 51.8MB/s]

15%|███████████▉ | 250M/1.66G [00:05<00:29, 51.8MB/s]

15%|████████████▏ | 256M/1.66G [00:05<00:28, 52.9MB/s]

15%|████████████▍ | 262M/1.66G [00:05<00:28, 52.6MB/s]

16%|████████████▋ | 268M/1.66G [00:05<00:28, 52.5MB/s]

16%|█████████████ | 274M/1.66G [00:06<00:28, 53.3MB/s]

16%|█████████████▎ | 280M/1.66G [00:06<00:28, 53.0MB/s]

17%|█████████████▌ | 286M/1.66G [00:06<00:28, 52.8MB/s]

17%|█████████████▉ | 292M/1.66G [00:06<00:27, 53.3MB/s]

17%|██████████████▏ | 298M/1.66G [00:06<00:27, 53.1MB/s]

18%|██████████████▍ | 304M/1.66G [00:06<00:27, 53.0MB/s]

18%|██████████████▋ | 310M/1.66G [00:06<00:27, 53.3MB/s]

19%|███████████████ | 316M/1.66G [00:06<00:27, 53.2MB/s]

19%|███████████████▎ | 322M/1.66G [00:07<00:27, 53.0MB/s]

19%|███████████████▌ | 328M/1.66G [00:07<00:27, 53.3MB/s]

20%|███████████████▉ | 334M/1.66G [00:07<00:27, 53.0MB/s]

20%|████████████████▏ | 340M/1.66G [00:07<00:26, 53.1MB/s]

20%|████████████████▍ | 346M/1.66G [00:07<00:26, 53.2MB/s]

21%|████████████████▋ | 352M/1.66G [00:07<00:26, 53.2MB/s]

21%|█████████████████ | 358M/1.66G [00:07<00:26, 53.0MB/s]

21%|█████████████████▎ | 364M/1.66G [00:07<00:26, 53.6MB/s]

22%|█████████████████▌ | 370M/1.66G [00:07<00:26, 53.3MB/s]

22%|█████████████████▉ | 376M/1.66G [00:08<00:25, 53.6MB/s]

22%|██████████████████▏ | 382M/1.66G [00:08<00:25, 53.4MB/s]

23%|██████████████████▍ | 388M/1.66G [00:08<00:25, 53.1MB/s]

23%|██████████████████▋ | 394M/1.66G [00:08<00:25, 53.6MB/s]

23%|███████████████████ | 400M/1.66G [00:08<00:25, 53.4MB/s]

24%|███████████████████▎ | 406M/1.66G [00:08<00:25, 53.5MB/s]

24%|███████████████████▌ | 412M/1.66G [00:08<00:25, 53.4MB/s]

25%|███████████████████▉ | 418M/1.66G [00:08<00:25, 53.2MB/s]

25%|████████████████████▏ | 424M/1.66G [00:09<00:25, 53.5MB/s]

25%|████████████████████▍ | 430M/1.66G [00:09<00:24, 53.4MB/s]

26%|████████████████████▋ | 436M/1.66G [00:09<00:24, 53.7MB/s]

26%|█████████████████████ | 442M/1.66G [00:09<00:24, 53.2MB/s]

26%|█████████████████████▎ | 448M/1.66G [00:09<00:24, 53.7MB/s]

27%|█████████████████████▌ | 454M/1.66G [00:09<00:24, 53.4MB/s]

27%|█████████████████████▉ | 460M/1.66G [00:09<00:24, 53.4MB/s]

27%|██████████████████████▏ | 466M/1.66G [00:09<00:24, 53.4MB/s]

28%|██████████████████████▍ | 472M/1.66G [00:09<00:24, 53.2MB/s]

28%|██████████████████████▋ | 478M/1.66G [00:10<00:23, 53.9MB/s]

28%|███████████████████████ | 484M/1.66G [00:10<00:23, 53.6MB/s]

29%|███████████████████████▎ | 490M/1.66G [00:10<00:23, 53.5MB/s]

29%|███████████████████████▌ | 496M/1.66G [00:10<00:23, 53.6MB/s]

29%|███████████████████████▉ | 502M/1.66G [00:10<00:23, 53.2MB/s]

30%|████████████████████████▏ | 508M/1.66G [00:10<00:23, 53.4MB/s]

30%|████████████████████████▍ | 514M/1.66G [00:10<00:23, 53.2MB/s]

31%|████████████████████████▋ | 520M/1.66G [00:10<00:23, 53.6MB/s]

31%|█████████████████████████ | 526M/1.66G [00:11<00:23, 53.2MB/s]

31%|█████████████████████████▎ | 532M/1.66G [00:11<00:23, 53.0MB/s]

32%|█████████████████████████▌ | 538M/1.66G [00:11<00:22, 53.6MB/s]

32%|█████████████████████████▉ | 544M/1.66G [00:11<00:22, 53.6MB/s]

32%|██████████████████████████▏ | 550M/1.66G [00:11<00:22, 53.5MB/s]

33%|██████████████████████████▍ | 556M/1.66G [00:11<00:26, 45.4MB/s]

33%|██████████████████████████▋ | 562M/1.66G [00:11<00:24, 48.4MB/s]

33%|██████████████████████████▉ | 567M/1.66G [00:11<00:24, 48.8MB/s]

34%|███████████████████████████▎ | 573M/1.66G [00:12<00:23, 51.0MB/s]

34%|███████████████████████████▍ | 578M/1.66G [00:12<00:23, 50.8MB/s]

34%|███████████████████████████▊ | 584M/1.66G [00:12<00:22, 52.3MB/s]

35%|████████████████████████████ | 590M/1.66G [00:12<00:22, 52.2MB/s]

35%|████████████████████████████▎ | 596M/1.66G [00:12<00:22, 52.2MB/s]

35%|████████████████████████████▋ | 602M/1.66G [00:12<00:21, 53.3MB/s]

36%|████████████████████████████▉ | 608M/1.66G [00:12<00:25, 44.7MB/s]

36%|█████████████████████████████▏ | 614M/1.66G [00:12<00:23, 48.6MB/s]

36%|█████████████████████████████▍ | 619M/1.66G [00:13<00:24, 47.1MB/s]

37%|█████████████████████████████▋ | 625M/1.66G [00:13<00:22, 50.3MB/s]

37%|█████████████████████████████▉ | 630M/1.66G [00:13<00:22, 49.3MB/s]

37%|██████████████████████████████▎ | 636M/1.66G [00:13<00:21, 52.4MB/s]

38%|██████████████████████████████▌ | 642M/1.66G [00:13<00:21, 51.7MB/s]

38%|██████████████████████████████▊ | 648M/1.66G [00:13<00:21, 52.4MB/s]

38%|███████████████████████████████ | 654M/1.66G [00:13<00:20, 53.0MB/s]

39%|███████████████████████████████▍ | 660M/1.66G [00:13<00:20, 52.1MB/s]

39%|███████████████████████████████▋ | 666M/1.66G [00:13<00:20, 53.0MB/s]

39%|███████████████████████████████▉ | 672M/1.66G [00:14<00:20, 53.0MB/s]

40%|████████████████████████████████▏ | 678M/1.66G [00:14<00:20, 52.7MB/s]

40%|████████████████████████████████▌ | 684M/1.66G [00:14<00:19, 54.2MB/s]

41%|████████████████████████████████▊ | 690M/1.66G [00:14<00:20, 53.0MB/s]

41%|█████████████████████████████████ | 696M/1.66G [00:14<00:20, 52.6MB/s]

41%|█████████████████████████████████▍ | 702M/1.66G [00:14<00:19, 54.1MB/s]

42%|█████████████████████████████████▋ | 708M/1.66G [00:14<00:19, 53.5MB/s]

42%|█████████████████████████████████▉ | 714M/1.66G [00:14<00:21, 49.3MB/s]

42%|██████████████████████████████████▏ | 719M/1.66G [00:15<00:23, 43.9MB/s]

43%|██████████████████████████████████▍ | 724M/1.66G [00:15<00:23, 43.2MB/s]

43%|██████████████████████████████████▋ | 730M/1.66G [00:15<00:21, 46.6MB/s]

43%|██████████████████████████████████▉ | 735M/1.66G [00:15<00:21, 47.4MB/s]

44%|███████████████████████████████████▏ | 741M/1.66G [00:15<00:20, 49.8MB/s]

44%|███████████████████████████████████▍ | 746M/1.66G [00:15<00:20, 49.9MB/s]

44%|███████████████████████████████████▊ | 752M/1.66G [00:15<00:19, 51.8MB/s]

44%|████████████████████████████████████ | 757M/1.66G [00:15<00:19, 51.4MB/s]

45%|████████████████████████████████████▎ | 763M/1.66G [00:15<00:18, 52.7MB/s]

45%|████████████████████████████████████▌ | 769M/1.66G [00:16<00:20, 48.1MB/s]

45%|████████████████████████████████████▊ | 774M/1.66G [00:16<00:21, 45.9MB/s]

46%|█████████████████████████████████████ | 780M/1.66G [00:16<00:20, 47.5MB/s]

46%|█████████████████████████████████████▍ | 786M/1.66G [00:16<00:19, 50.0MB/s]

46%|█████████████████████████████████████▌ | 791M/1.66G [00:16<00:19, 49.9MB/s]

47%|█████████████████████████████████████▉ | 797M/1.66G [00:16<00:18, 51.8MB/s]

47%|██████████████████████████████████████▏ | 802M/1.66G [00:16<00:18, 51.2MB/s]

47%|██████████████████████████████████████▍ | 808M/1.66G [00:16<00:17, 52.7MB/s]

48%|██████████████████████████████████████▋ | 814M/1.66G [00:17<00:17, 52.4MB/s]

48%|███████████████████████████████████████ | 820M/1.66G [00:17<00:17, 52.3MB/s]

49%|███████████████████████████████████████▎ | 826M/1.66G [00:17<00:17, 53.2MB/s]

49%|███████████████████████████████████████▌ | 832M/1.66G [00:17<00:17, 53.0MB/s]

49%|███████████████████████████████████████▊ | 838M/1.66G [00:17<00:17, 52.9MB/s]

50%|████████████████████████████████████████▏ | 844M/1.66G [00:17<00:16, 53.8MB/s]

50%|████████████████████████████████████████▍ | 850M/1.66G [00:17<00:16, 53.2MB/s]

50%|████████████████████████████████████████▋ | 856M/1.66G [00:17<00:16, 54.0MB/s]

51%|█████████████████████████████████████████ | 862M/1.66G [00:17<00:16, 53.3MB/s]

51%|█████████████████████████████████████████▎ | 868M/1.66G [00:18<00:16, 53.0MB/s]

51%|█████████████████████████████████████████▌ | 874M/1.66G [00:18<00:16, 53.9MB/s]

52%|█████████████████████████████████████████▊ | 880M/1.66G [00:18<00:16, 53.4MB/s]

52%|██████████████████████████████████████████▏ | 886M/1.66G [00:18<00:16, 52.9MB/s]

52%|██████████████████████████████████████████▍ | 892M/1.66G [00:18<00:15, 54.1MB/s]

53%|██████████████████████████████████████████▋ | 898M/1.66G [00:18<00:15, 53.2MB/s]

53%|██████████████████████████████████████████▉ | 904M/1.66G [00:18<00:15, 52.8MB/s]

53%|███████████████████████████████████████████▎ | 910M/1.66G [00:18<00:15, 53.9MB/s]

54%|███████████████████████████████████████████▌ | 916M/1.66G [00:19<00:15, 53.4MB/s]

54%|███████████████████████████████████████████▊ | 922M/1.66G [00:19<00:15, 52.5MB/s]

54%|████████████████████████████████████████████▏ | 928M/1.66G [00:19<00:15, 54.0MB/s]

55%|████████████████████████████████████████████▍ | 934M/1.66G [00:19<00:15, 53.4MB/s]

55%|████████████████████████████████████████████▋ | 940M/1.66G [00:19<00:14, 54.5MB/s]

56%|████████████████████████████████████████████▉ | 946M/1.66G [00:19<00:14, 53.6MB/s]

56%|█████████████████████████████████████████████▎ | 952M/1.66G [00:19<00:14, 52.9MB/s]

56%|█████████████████████████████████████████████▌ | 958M/1.66G [00:19<00:14, 54.1MB/s]

57%|█████████████████████████████████████████████▊ | 964M/1.66G [00:19<00:14, 53.3MB/s]

57%|██████████████████████████████████████████████▏ | 970M/1.66G [00:20<00:14, 52.7MB/s]

57%|██████████████████████████████████████████████▍ | 976M/1.66G [00:20<00:14, 54.3MB/s]

58%|██████████████████████████████████████████████▋ | 982M/1.66G [00:20<00:14, 51.5MB/s]

58%|██████████████████████████████████████████████▉ | 988M/1.66G [00:20<00:14, 51.1MB/s]

58%|███████████████████████████████████████████████▎ | 994M/1.66G [00:20<00:13, 53.5MB/s]

59%|██████████████████████████████████████████████▉ | 0.98G/1.66G [00:20<00:14, 52.6MB/s]

59%|███████████████████████████████████████████████▎ | 0.98G/1.66G [00:20<00:13, 53.2MB/s]

59%|███████████████████████████████████████████████▌ | 0.99G/1.66G [00:20<00:13, 53.9MB/s]

60%|███████████████████████████████████████████████▊ | 0.99G/1.66G [00:21<00:13, 52.6MB/s]

60%|████████████████████████████████████████████████ | 1.00G/1.66G [00:21<00:13, 53.4MB/s]

60%|████████████████████████████████████████████████▍ | 1.01G/1.66G [00:21<00:13, 53.7MB/s]

61%|████████████████████████████████████████████████▋ | 1.01G/1.66G [00:21<00:13, 52.6MB/s]

61%|████████████████████████████████████████████████▉ | 1.02G/1.66G [00:21<00:12, 54.8MB/s]

62%|█████████████████████████████████████████████████▏ | 1.02G/1.66G [00:21<00:12, 53.3MB/s]

62%|█████████████████████████████████████████████████▌ | 1.03G/1.66G [00:21<00:13, 52.2MB/s]

62%|█████████████████████████████████████████████████▊ | 1.04G/1.66G [00:21<00:13, 48.6MB/s]

63%|██████████████████████████████████████████████████ | 1.04G/1.66G [00:22<00:14, 45.4MB/s]

63%|██████████████████████████████████████████████████▎ | 1.05G/1.66G [00:22<00:13, 49.2MB/s]

63%|██████████████████████████████████████████████████▌ | 1.05G/1.66G [00:22<00:13, 48.7MB/s]

64%|██████████████████████████████████████████████████▊ | 1.06G/1.66G [00:22<00:12, 51.8MB/s]

64%|███████████████████████████████████████████████████ | 1.06G/1.66G [00:22<00:12, 51.2MB/s]

64%|███████████████████████████████████████████████████▍ | 1.07G/1.66G [00:22<00:12, 51.0MB/s]

65%|███████████████████████████████████████████████████▋ | 1.07G/1.66G [00:22<00:11, 53.5MB/s]

65%|███████████████████████████████████████████████████▉ | 1.08G/1.66G [00:22<00:11, 52.5MB/s]

65%|████████████████████████████████████████████████████▏ | 1.09G/1.66G [00:22<00:11, 54.7MB/s]

66%|████████████████████████████████████████████████████▌ | 1.09G/1.66G [00:23<00:11, 53.1MB/s]

66%|████████████████████████████████████████████████████▊ | 1.10G/1.66G [00:23<00:11, 52.1MB/s]

66%|█████████████████████████████████████████████████████ | 1.10G/1.66G [00:23<00:11, 54.5MB/s]

67%|█████████████████████████████████████████████████████▎ | 1.11G/1.66G [00:23<00:11, 53.0MB/s]

67%|█████████████████████████████████████████████████████▋ | 1.12G/1.66G [00:23<00:11, 52.2MB/s]

67%|█████████████████████████████████████████████████████▉ | 1.12G/1.66G [00:23<00:10, 54.4MB/s]

68%|██████████████████████████████████████████████████████▏ | 1.13G/1.66G [00:23<00:11, 52.2MB/s]

68%|██████████████████████████████████████████████████████▍ | 1.13G/1.66G [00:23<00:10, 52.7MB/s]

68%|██████████████████████████████████████████████████████▊ | 1.14G/1.66G [00:24<00:10, 52.7MB/s]

69%|███████████████████████████████████████████████████████ | 1.14G/1.66G [00:24<00:10, 53.0MB/s]

69%|███████████████████████████████████████████████████████▎ | 1.15G/1.66G [00:24<00:10, 53.1MB/s]

70%|███████████████████████████████████████████████████████▌ | 1.16G/1.66G [00:24<00:10, 52.9MB/s]

70%|███████████████████████████████████████████████████████▉ | 1.16G/1.66G [00:24<00:10, 53.6MB/s]

70%|████████████████████████████████████████████████████████▏ | 1.17G/1.66G [00:24<00:09, 53.4MB/s]

71%|████████████████████████████████████████████████████████▍ | 1.17G/1.66G [00:24<00:09, 53.1MB/s]

71%|████████████████████████████████████████████████████████▊ | 1.18G/1.66G [00:24<00:09, 53.5MB/s]

71%|█████████████████████████████████████████████████████████ | 1.19G/1.66G [00:24<00:09, 53.2MB/s]

72%|█████████████████████████████████████████████████████████▎ | 1.19G/1.66G [00:25<00:09, 53.4MB/s]

72%|█████████████████████████████████████████████████████████▌ | 1.20G/1.66G [00:25<00:10, 49.5MB/s]

72%|█████████████████████████████████████████████████████████▊ | 1.20G/1.66G [00:25<00:12, 41.2MB/s]

73%|██████████████████████████████████████████████████████████ | 1.21G/1.66G [00:25<00:10, 45.0MB/s]

73%|██████████████████████████████████████████████████████████▎ | 1.21G/1.66G [00:25<00:10, 46.2MB/s]

73%|██████████████████████████████████████████████████████████▋ | 1.22G/1.66G [00:25<00:09, 48.9MB/s]

74%|██████████████████████████████████████████████████████████▊ | 1.22G/1.66G [00:25<00:10, 46.1MB/s]

74%|███████████████████████████████████████████████████████████ | 1.23G/1.66G [00:26<00:10, 44.1MB/s]

74%|███████████████████████████████████████████████████████████▎ | 1.23G/1.66G [00:26<00:09, 46.1MB/s]

75%|███████████████████████████████████████████████████████████▌ | 1.24G/1.66G [00:26<00:09, 47.8MB/s]

75%|███████████████████████████████████████████████████████████▉ | 1.25G/1.66G [00:26<00:08, 50.3MB/s]

75%|████████████████████████████████████████████████████████████▏ | 1.25G/1.66G [00:26<00:08, 50.2MB/s]

76%|████████████████████████████████████████████████████████████▍ | 1.26G/1.66G [00:26<00:08, 51.8MB/s]

76%|████████████████████████████████████████████████████████████▋ | 1.26G/1.66G [00:26<00:08, 51.2MB/s]

76%|████████████████████████████████████████████████████████████▉ | 1.27G/1.66G [00:26<00:08, 51.9MB/s]

77%|█████████████████████████████████████████████████████████████▏ | 1.27G/1.66G [00:26<00:07, 52.6MB/s]

77%|█████████████████████████████████████████████████████████████▍ | 1.28G/1.66G [00:27<00:07, 52.3MB/s]

77%|█████████████████████████████████████████████████████████████▊ | 1.28G/1.66G [00:27<00:07, 53.5MB/s]

78%|██████████████████████████████████████████████████████████████ | 1.29G/1.66G [00:27<00:07, 52.7MB/s]

78%|██████████████████████████████████████████████████████████████▎ | 1.30G/1.66G [00:27<00:07, 52.6MB/s]

78%|██████████████████████████████████████████████████████████████▌ | 1.30G/1.66G [00:27<00:07, 53.0MB/s]

79%|██████████████████████████████████████████████████████████████▉ | 1.31G/1.66G [00:27<00:07, 52.9MB/s]

79%|███████████████████████████████████████████████████████████████▏ | 1.31G/1.66G [00:27<00:07, 52.7MB/s]

79%|███████████████████████████████████████████████████████████████▍ | 1.32G/1.66G [00:27<00:06, 53.2MB/s]

80%|███████████████████████████████████████████████████████████████▊ | 1.33G/1.66G [00:27<00:06, 53.1MB/s]

80%|████████████████████████████████████████████████████████████████ | 1.33G/1.66G [00:28<00:06, 53.1MB/s]

80%|████████████████████████████████████████████████████████████████▎ | 1.34G/1.66G [00:28<00:06, 53.3MB/s]

81%|████████████████████████████████████████████████████████████████▌ | 1.34G/1.66G [00:28<00:06, 53.0MB/s]

81%|████████████████████████████████████████████████████████████████▉ | 1.35G/1.66G [00:28<00:06, 53.2MB/s]

81%|█████████████████████████████████████████████████████████████████▏ | 1.35G/1.66G [00:28<00:06, 53.8MB/s]

82%|█████████████████████████████████████████████████████████████████▍ | 1.36G/1.66G [00:28<00:06, 53.2MB/s]

82%|█████████████████████████████████████████████████████████████████▋ | 1.37G/1.66G [00:28<00:05, 53.5MB/s]

83%|██████████████████████████████████████████████████████████████████ | 1.37G/1.66G [00:28<00:05, 53.5MB/s]

83%|██████████████████████████████████████████████████████████████████▎ | 1.38G/1.66G [00:29<00:05, 52.9MB/s]

83%|██████████████████████████████████████████████████████████████████▌ | 1.38G/1.66G [00:29<00:05, 53.0MB/s]

84%|██████████████████████████████████████████████████████████████████▊ | 1.39G/1.66G [00:29<00:05, 53.5MB/s]

84%|███████████████████████████████████████████████████████████████████▏ | 1.40G/1.66G [00:29<00:05, 53.3MB/s]

84%|███████████████████████████████████████████████████████████████████▍ | 1.40G/1.66G [00:29<00:05, 53.8MB/s]

85%|███████████████████████████████████████████████████████████████████▋ | 1.41G/1.66G [00:29<00:05, 53.5MB/s]

85%|███████████████████████████████████████████████████████████████████▉ | 1.41G/1.66G [00:29<00:05, 53.0MB/s]

85%|████████████████████████████████████████████████████████████████████▎ | 1.42G/1.66G [00:29<00:04, 53.7MB/s]

86%|████████████████████████████████████████████████████████████████████▌ | 1.42G/1.66G [00:29<00:04, 53.3MB/s]

86%|████████████████████████████████████████████████████████████████████▊ | 1.43G/1.66G [00:30<00:04, 52.9MB/s]

86%|█████████████████████████████████████████████████████████████████████ | 1.44G/1.66G [00:30<00:04, 53.7MB/s]

87%|█████████████████████████████████████████████████████████████████████▍ | 1.44G/1.66G [00:30<00:04, 53.1MB/s]

87%|█████████████████████████████████████████████████████████████████████▋ | 1.45G/1.66G [00:30<00:04, 53.5MB/s]

87%|█████████████████████████████████████████████████████████████████████▉ | 1.45G/1.66G [00:30<00:04, 53.6MB/s]

88%|██████████████████████████████████████████████████████████████████████▏ | 1.46G/1.66G [00:30<00:04, 53.0MB/s]

88%|██████████████████████████████████████████████████████████████████████▌ | 1.47G/1.66G [00:30<00:03, 53.6MB/s]

88%|██████████████████████████████████████████████████████████████████████▊ | 1.47G/1.66G [00:30<00:03, 53.9MB/s]

89%|███████████████████████████████████████████████████████████████████████ | 1.48G/1.66G [00:31<00:03, 53.1MB/s]

89%|███████████████████████████████████████████████████████████████████████▎ | 1.48G/1.66G [00:31<00:03, 53.5MB/s]

90%|███████████████████████████████████████████████████████████████████████▋ | 1.49G/1.66G [00:31<00:03, 53.7MB/s]

90%|███████████████████████████████████████████████████████████████████████▉ | 1.50G/1.66G [00:31<00:03, 52.9MB/s]

90%|████████████████████████████████████████████████████████████████████████▏ | 1.50G/1.66G [00:31<00:03, 51.2MB/s]

91%|████████████████████████████████████████████████████████████████████████▍ | 1.51G/1.66G [00:31<00:03, 47.9MB/s]

91%|████████████████████████████████████████████████████████████████████████▋ | 1.51G/1.66G [00:31<00:03, 48.3MB/s]

91%|█████████████████████████████████████████████████████████████████████████ | 1.52G/1.66G [00:31<00:03, 51.3MB/s]

92%|█████████████████████████████████████████████████████████████████████████▏ | 1.52G/1.66G [00:32<00:03, 50.2MB/s]

92%|█████████████████████████████████████████████████████████████████████████▌ | 1.53G/1.66G [00:32<00:02, 53.0MB/s]

92%|█████████████████████████████████████████████████████████████████████████▊ | 1.53G/1.66G [00:32<00:02, 52.2MB/s]

93%|██████████████████████████████████████████████████████████████████████████ | 1.54G/1.66G [00:32<00:02, 54.4MB/s]

93%|██████████████████████████████████████████████████████████████████████████▎ | 1.55G/1.66G [00:32<00:02, 53.2MB/s]

93%|██████████████████████████████████████████████████████████████████████████▋ | 1.55G/1.66G [00:32<00:02, 52.2MB/s]

94%|██████████████████████████████████████████████████████████████████████████▉ | 1.56G/1.66G [00:32<00:02, 54.4MB/s]

94%|███████████████████████████████████████████████████████████████████████████▏ | 1.56G/1.66G [00:32<00:02, 53.1MB/s]

94%|███████████████████████████████████████████████████████████████████████████▍ | 1.57G/1.66G [00:32<00:01, 52.2MB/s]

95%|███████████████████████████████████████████████████████████████████████████▊ | 1.58G/1.66G [00:33<00:01, 54.3MB/s]

95%|████████████████████████████████████████████████████████████████████████████ | 1.58G/1.66G [00:33<00:01, 52.9MB/s]

95%|████████████████████████████████████████████████████████████████████████████▎ | 1.59G/1.66G [00:33<00:01, 51.2MB/s]

96%|████████████████████████████████████████████████████████████████████████████▌ | 1.59G/1.66G [00:33<00:01, 54.0MB/s]

96%|████████████████████████████████████████████████████████████████████████████▉ | 1.60G/1.66G [00:33<00:01, 52.7MB/s]

96%|█████████████████████████████████████████████████████████████████████████████▏ | 1.60G/1.66G [00:33<00:01, 52.1MB/s]

97%|█████████████████████████████████████████████████████████████████████████████▍ | 1.61G/1.66G [00:33<00:01, 53.2MB/s]

97%|█████████████████████████████████████████████████████████████████████████████▊ | 1.62G/1.66G [00:33<00:00, 51.4MB/s]

98%|██████████████████████████████████████████████████████████████████████████████ | 1.62G/1.66G [00:34<00:00, 53.0MB/s]

98%|██████████████████████████████████████████████████████████████████████████████▎ | 1.63G/1.66G [00:34<00:00, 53.0MB/s]

98%|██████████████████████████████████████████████████████████████████████████████▌ | 1.63G/1.66G [00:34<00:00, 52.1MB/s]

99%|██████████████████████████████████████████████████████████████████████████████▉ | 1.64G/1.66G [00:34<00:00, 53.5MB/s]

99%|███████████████████████████████████████████████████████████████████████████████▏| 1.65G/1.66G [00:34<00:00, 53.4MB/s]

99%|███████████████████████████████████████████████████████████████████████████████▍| 1.65G/1.66G [00:34<00:00, 53.2MB/s]

100%|███████████████████████████████████████████████████████████████████████████████▋| 1.66G/1.66G [00:34<00:00, 43.2MB/s]

100%|███████████████████████████████████████████████████████████████████████████████▉| 1.66G/1.66G [00:34<00:00, 44.2MB/s]

100%|████████████████████████████████████████████████████████████████████████████████| 1.66G/1.66G [00:34<00:00, 51.1MB/s]

Downloading training_shard_1.tfrec...

Downloading to /home/jupyter/.cache/kagglehub/datasets/ipythonx/brats2020/versions/1/training_shard_1.tfrec...

0%| | 0.00/1.66G [00:00<?, ?B/s]

0%| | 1.00M/1.66G [00:00<12:46, 2.33MB/s]

0%|▏ | 3.00M/1.66G [00:00<04:37, 6.43MB/s]

0%|▍ | 8.00M/1.66G [00:00<01:43, 17.2MB/s]

1%|▌ | 13.0M/1.66G [00:00<01:09, 25.7MB/s]

1%|▉ | 19.0M/1.66G [00:00<00:51, 34.3MB/s]

1%|█▏ | 24.0M/1.66G [00:01<00:46, 38.2MB/s]

2%|█▍ | 30.0M/1.66G [00:01<00:40, 43.0MB/s]

2%|█▋ | 35.0M/1.66G [00:01<00:38, 44.8MB/s]

2%|█▉ | 41.0M/1.66G [00:01<00:36, 47.1MB/s]

3%|██▏ | 47.0M/1.66G [00:01<00:35, 48.9MB/s]

3%|██▍ | 52.0M/1.66G [00:01<00:34, 49.5MB/s]

3%|██▋ | 58.0M/1.66G [00:01<00:34, 50.7MB/s]

4%|██▉ | 63.0M/1.66G [00:01<00:33, 50.6MB/s]

4%|███▏ | 69.0M/1.66G [00:01<00:33, 51.5MB/s]

4%|███▍ | 74.0M/1.66G [00:02<00:33, 51.4MB/s]

5%|███▋ | 79.0M/1.66G [00:02<00:33, 50.6MB/s]

5%|███▉ | 85.0M/1.66G [00:02<00:33, 51.3MB/s]

5%|████▏ | 90.0M/1.66G [00:02<00:33, 51.2MB/s]

6%|████▌ | 96.0M/1.66G [00:02<00:32, 51.1MB/s]

6%|████▊ | 102M/1.66G [00:02<00:31, 53.1MB/s]

6%|█████▏ | 108M/1.66G [00:02<00:32, 51.9MB/s]

7%|█████▎ | 113M/1.66G [00:02<00:37, 44.3MB/s]

7%|█████▋ | 120M/1.66G [00:03<00:35, 46.3MB/s]

7%|██████ | 127M/1.66G [00:03<00:34, 47.8MB/s]

8%|██████▎ | 133M/1.66G [00:03<00:32, 51.4MB/s]

8%|██████▌ | 139M/1.66G [00:03<00:32, 50.5MB/s]

9%|██████▉ | 145M/1.66G [00:03<00:33, 49.1MB/s]

9%|███████▏ | 151M/1.66G [00:03<00:31, 52.4MB/s]

9%|███████▍ | 157M/1.66G [00:03<00:31, 51.2MB/s]

10%|███████▊ | 163M/1.66G [00:03<00:30, 53.7MB/s]

10%|████████ | 169M/1.66G [00:04<00:31, 51.5MB/s]

10%|████████▎ | 175M/1.66G [00:04<00:31, 50.6MB/s]

11%|████████▌ | 181M/1.66G [00:04<00:29, 53.3MB/s]

11%|████████▉ | 187M/1.66G [00:04<00:30, 51.3MB/s]

11%|█████████▏ | 193M/1.66G [00:04<00:31, 50.3MB/s]

12%|█████████▍ | 199M/1.66G [00:04<00:29, 53.0MB/s]

12%|█████████▊ | 205M/1.66G [00:04<00:30, 51.2MB/s]

12%|██████████ | 211M/1.66G [00:04<00:30, 50.7MB/s]

13%|██████████▎ | 217M/1.66G [00:04<00:29, 52.9MB/s]

13%|██████████▌ | 223M/1.66G [00:05<00:30, 51.4MB/s]

13%|██████████▉ | 229M/1.66G [00:05<00:30, 50.4MB/s]

14%|███████████▏ | 235M/1.66G [00:05<00:29, 52.6MB/s]

14%|███████████▍ | 241M/1.66G [00:05<00:29, 51.9MB/s]

15%|███████████▋ | 247M/1.66G [00:05<00:28, 53.6MB/s]

15%|████████████ | 253M/1.66G [00:05<00:29, 50.9MB/s]

15%|████████████▎ | 259M/1.66G [00:05<00:29, 51.3MB/s]

16%|████████████▌ | 264M/1.66G [00:05<00:29, 51.4MB/s]

16%|████████████▊ | 269M/1.66G [00:06<00:33, 45.3MB/s]

16%|█████████████ | 274M/1.66G [00:06<00:35, 41.9MB/s]

16%|█████████████▎ | 280M/1.66G [00:06<00:32, 45.5MB/s]

17%|█████████████▌ | 285M/1.66G [00:06<00:32, 46.1MB/s]

17%|█████████████▊ | 291M/1.66G [00:06<00:30, 49.0MB/s]

17%|██████████████ | 296M/1.66G [00:06<00:30, 48.6MB/s]

18%|██████████████▎ | 302M/1.66G [00:06<00:28, 51.0MB/s]

18%|██████████████▌ | 307M/1.66G [00:06<00:29, 49.7MB/s]

18%|██████████████▉ | 313M/1.66G [00:07<00:28, 52.0MB/s]

19%|███████████████▏ | 318M/1.66G [00:07<00:28, 50.4MB/s]

19%|███████████████▍ | 324M/1.66G [00:07<00:27, 52.2MB/s]

19%|███████████████▋ | 329M/1.66G [00:07<00:28, 50.7MB/s]

20%|███████████████▉ | 335M/1.66G [00:07<00:27, 52.7MB/s]

20%|████████████████▏ | 341M/1.66G [00:07<00:27, 52.7MB/s]

20%|████████████████▌ | 347M/1.66G [00:07<00:28, 50.4MB/s]

21%|████████████████▋ | 352M/1.66G [00:07<00:28, 49.1MB/s]

21%|████████████████▉ | 357M/1.66G [00:07<00:28, 49.9MB/s]

21%|█████████████████▏ | 362M/1.66G [00:08<00:29, 48.0MB/s]

22%|█████████████████▍ | 367M/1.66G [00:08<00:29, 47.3MB/s]

22%|█████████████████▋ | 372M/1.66G [00:08<00:30, 45.5MB/s]

22%|█████████████████▉ | 378M/1.66G [00:08<00:29, 47.3MB/s]

22%|██████████████████▏ | 383M/1.66G [00:08<00:30, 45.6MB/s]

23%|██████████████████▌ | 389M/1.66G [00:08<00:29, 47.2MB/s]

23%|██████████████████▋ | 394M/1.66G [00:08<00:30, 45.7MB/s]

23%|███████████████████ | 400M/1.66G [00:08<00:28, 47.1MB/s]

24%|███████████████████▎ | 405M/1.66G [00:09<00:29, 45.4MB/s]

24%|███████████████████▌ | 411M/1.66G [00:09<00:28, 47.2MB/s]

24%|███████████████████▊ | 416M/1.66G [00:09<00:31, 42.6MB/s]

25%|████████████████████ | 422M/1.66G [00:09<00:31, 43.1MB/s]

25%|████████████████████▎ | 427M/1.66G [00:09<00:30, 43.8MB/s]

25%|████████████████████▌ | 433M/1.66G [00:09<00:29, 44.8MB/s]

26%|████████████████████▊ | 438M/1.66G [00:09<00:29, 45.1MB/s]

26%|█████████████████████ | 444M/1.66G [00:09<00:28, 45.7MB/s]

26%|█████████████████████▎ | 449M/1.66G [00:10<00:28, 45.8MB/s]

27%|█████████████████████▋ | 455M/1.66G [00:10<00:28, 46.3MB/s]

27%|█████████████████████▉ | 460M/1.66G [00:10<00:28, 46.0MB/s]

27%|██████████████████████▏ | 466M/1.66G [00:10<00:27, 46.3MB/s]

28%|██████████████████████▍ | 471M/1.66G [00:10<00:28, 45.8MB/s]

28%|██████████████████████▋ | 477M/1.66G [00:10<00:27, 46.6MB/s]

28%|██████████████████████▉ | 482M/1.66G [00:10<00:27, 45.9MB/s]

29%|███████████████████████▏ | 488M/1.66G [00:10<00:28, 44.7MB/s]

29%|███████████████████████▍ | 494M/1.66G [00:11<00:26, 48.5MB/s]

29%|███████████████████████▋ | 499M/1.66G [00:11<00:28, 44.6MB/s]

30%|████████████████████████ | 505M/1.66G [00:11<00:25, 48.4MB/s]

30%|████████████████████████▎ | 510M/1.66G [00:11<00:28, 44.6MB/s]

30%|████████████████████████▌ | 516M/1.66G [00:11<00:25, 48.5MB/s]

31%|████████████████████████▊ | 521M/1.66G [00:11<00:27, 44.7MB/s]

31%|█████████████████████████ | 527M/1.66G [00:11<00:25, 48.5MB/s]

31%|█████████████████████████▎ | 532M/1.66G [00:11<00:27, 44.7MB/s]

32%|█████████████████████████▌ | 538M/1.66G [00:12<00:25, 48.5MB/s]

32%|█████████████████████████▊ | 543M/1.66G [00:12<00:27, 44.7MB/s]

32%|██████████████████████████ | 549M/1.66G [00:12<00:24, 48.4MB/s]

33%|██████████████████████████▎ | 554M/1.66G [00:12<00:26, 44.7MB/s]

33%|██████████████████████████▋ | 560M/1.66G [00:12<00:24, 48.4MB/s]

33%|██████████████████████████▊ | 565M/1.66G [00:12<00:32, 36.6MB/s]

34%|███████████████████████████▏ | 571M/1.66G [00:12<00:31, 37.2MB/s]

34%|███████████████████████████▍ | 578M/1.66G [00:13<00:26, 44.6MB/s]

34%|███████████████████████████▋ | 583M/1.66G [00:13<00:28, 41.4MB/s]

35%|████████████████████████████ | 589M/1.66G [00:13<00:25, 45.9MB/s]

35%|████████████████████████████▎ | 594M/1.66G [00:13<00:27, 42.8MB/s]

35%|████████████████████████████▌ | 600M/1.66G [00:13<00:27, 42.1MB/s]

36%|████████████████████████████▊ | 606M/1.66G [00:13<00:24, 46.4MB/s]

36%|█████████████████████████████ | 611M/1.66G [00:13<00:26, 43.3MB/s]

36%|█████████████████████████████▎ | 617M/1.66G [00:13<00:24, 47.2MB/s]

37%|█████████████████████████████▌ | 622M/1.66G [00:14<00:25, 44.0MB/s]

37%|█████████████████████████████▊ | 628M/1.66G [00:14<00:23, 47.7MB/s]

37%|██████████████████████████████ | 633M/1.66G [00:14<00:25, 44.4MB/s]

38%|██████████████████████████████▍ | 639M/1.66G [00:14<00:23, 48.1MB/s]

38%|██████████████████████████████▋ | 644M/1.66G [00:14<00:24, 44.4MB/s]

38%|██████████████████████████████▉ | 650M/1.66G [00:14<00:22, 48.0MB/s]

38%|███████████████████████████████▏ | 655M/1.66G [00:14<00:24, 44.4MB/s]

39%|███████████████████████████████▍ | 661M/1.66G [00:14<00:22, 48.2MB/s]

39%|███████████████████████████████▋ | 666M/1.66G [00:15<00:24, 44.0MB/s]

39%|███████████████████████████████▉ | 672M/1.66G [00:15<00:22, 48.5MB/s]

40%|████████████████████████████████▏ | 677M/1.66G [00:15<00:24, 44.2MB/s]

40%|████████████████████████████████▍ | 683M/1.66G [00:15<00:22, 48.6MB/s]

40%|████████████████████████████████▋ | 688M/1.66G [00:15<00:23, 44.4MB/s]

41%|████████████████████████████████▉ | 693M/1.66G [00:15<00:23, 45.5MB/s]

41%|█████████████████████████████████▏ | 698M/1.66G [00:15<00:24, 43.1MB/s]

41%|█████████████████████████████████▍ | 703M/1.66G [00:15<00:23, 44.6MB/s]

42%|█████████████████████████████████▋ | 708M/1.66G [00:16<00:24, 42.9MB/s]

42%|█████████████████████████████████▉ | 714M/1.66G [00:16<00:22, 46.4MB/s]

42%|██████████████████████████████████▏ | 719M/1.66G [00:16<00:23, 43.9MB/s]

43%|██████████████████████████████████▍ | 725M/1.66G [00:16<00:21, 47.3MB/s]

43%|██████████████████████████████████▋ | 730M/1.66G [00:16<00:22, 44.6MB/s]

43%|███████████████████████████████████ | 736M/1.66G [00:16<00:21, 47.0MB/s]

44%|███████████████████████████████████▏ | 741M/1.66G [00:16<00:22, 44.7MB/s]

44%|███████████████████████████████████▌ | 747M/1.66G [00:16<00:21, 47.4MB/s]

44%|███████████████████████████████████▊ | 752M/1.66G [00:17<00:22, 45.1MB/s]

45%|████████████████████████████████████ | 758M/1.66G [00:17<00:20, 47.5MB/s]

45%|████████████████████████████████████▎ | 763M/1.66G [00:17<00:21, 45.2MB/s]

45%|████████████████████████████████████▌ | 769M/1.66G [00:17<00:20, 47.8MB/s]

45%|████████████████████████████████████▊ | 774M/1.66G [00:17<00:21, 45.2MB/s]

46%|█████████████████████████████████████ | 779M/1.66G [00:17<00:20, 46.6MB/s]

46%|█████████████████████████████████████▎ | 784M/1.66G [00:17<00:20, 48.0MB/s]

46%|█████████████████████████████████████▌ | 789M/1.66G [00:17<00:21, 45.6MB/s]

47%|█████████████████████████████████████▊ | 794M/1.66G [00:18<00:20, 47.0MB/s]

47%|██████████████████████████████████████ | 799M/1.66G [00:18<00:21, 44.3MB/s]

47%|██████████████████████████████████████▎ | 805M/1.66G [00:18<00:19, 47.5MB/s]

48%|██████████████████████████████████████▌ | 810M/1.66G [00:18<00:20, 44.7MB/s]

48%|██████████████████████████████████████▊ | 816M/1.66G [00:18<00:19, 47.8MB/s]

48%|███████████████████████████████████████ | 821M/1.66G [00:18<00:20, 44.8MB/s]

49%|███████████████████████████████████████▎ | 827M/1.66G [00:18<00:19, 48.0MB/s]

49%|███████████████████████████████████████▌ | 832M/1.66G [00:18<00:20, 44.9MB/s]

49%|███████████████████████████████████████▊ | 838M/1.66G [00:19<00:18, 48.1MB/s]

50%|████████████████████████████████████████ | 843M/1.66G [00:19<00:20, 44.9MB/s]

50%|████████████████████████████████████████▍ | 849M/1.66G [00:19<00:18, 48.2MB/s]

50%|████████████████████████████████████████▌ | 854M/1.66G [00:19<00:19, 44.7MB/s]

51%|████████████████████████████████████████▉ | 860M/1.66G [00:19<00:18, 48.3MB/s]

51%|█████████████████████████████████████████▏ | 865M/1.66G [00:19<00:19, 44.7MB/s]

51%|█████████████████████████████████████████▍ | 871M/1.66G [00:19<00:18, 48.2MB/s]

51%|█████████████████████████████████████████▋ | 876M/1.66G [00:19<00:19, 44.8MB/s]

52%|█████████████████████████████████████████▉ | 882M/1.66G [00:20<00:18, 47.4MB/s]

52%|██████████████████████████████████████████▏ | 887M/1.66G [00:20<00:19, 45.0MB/s]

52%|██████████████████████████████████████████▍ | 893M/1.66G [00:20<00:17, 47.7MB/s]

53%|██████████████████████████████████████████▋ | 898M/1.66G [00:20<00:18, 45.1MB/s]

53%|██████████████████████████████████████████▉ | 904M/1.66G [00:20<00:17, 47.7MB/s]

53%|███████████████████████████████████████████▏ | 909M/1.66G [00:20<00:18, 45.2MB/s]

54%|███████████████████████████████████████████▍ | 914M/1.66G [00:20<00:17, 47.1MB/s]

54%|███████████████████████████████████████████▋ | 919M/1.66G [00:20<00:17, 47.8MB/s]

54%|███████████████████████████████████████████▉ | 924M/1.66G [00:20<00:18, 45.1MB/s]

55%|████████████████████████████████████████████▏ | 930M/1.66G [00:21<00:16, 47.8MB/s]

55%|████████████████████████████████████████████▍ | 935M/1.66G [00:21<00:18, 43.5MB/s]

55%|████████████████████████████████████████████▋ | 940M/1.66G [00:21<00:20, 39.4MB/s]

55%|████████████████████████████████████████████▉ | 945M/1.66G [00:21<00:18, 42.4MB/s]

56%|█████████████████████████████████████████████▏ | 951M/1.66G [00:21<00:18, 42.6MB/s]

56%|█████████████████████████████████████████████▍ | 956M/1.66G [00:21<00:17, 44.7MB/s]

56%|█████████████████████████████████████████████▋ | 961M/1.66G [00:21<00:18, 43.0MB/s]

57%|█████████████████████████████████████████████▉ | 966M/1.66G [00:21<00:17, 45.2MB/s]

57%|██████████████████████████████████████████████▏ | 971M/1.66G [00:22<00:16, 47.0MB/s]

57%|██████████████████████████████████████████████▍ | 976M/1.66G [00:22<00:17, 44.6MB/s]

58%|██████████████████████████████████████████████▋ | 981M/1.66G [00:22<00:16, 46.0MB/s]

58%|██████████████████████████████████████████████▉ | 987M/1.66G [00:22<00:16, 45.4MB/s]

58%|███████████████████████████████████████████████▏ | 992M/1.66G [00:22<00:16, 46.6MB/s]

59%|███████████████████████████████████████████████▍ | 998M/1.66G [00:22<00:16, 45.8MB/s]

59%|███████████████████████████████████████████████ | 0.98G/1.66G [00:22<00:15, 46.8MB/s]

59%|███████████████████████████████████████████████▍ | 0.99G/1.66G [00:22<00:16, 44.3MB/s]

60%|███████████████████████████████████████████████▋ | 0.99G/1.66G [00:23<00:15, 46.3MB/s]

60%|███████████████████████████████████████████████▊ | 1.00G/1.66G [00:23<00:16, 42.2MB/s]

60%|████████████████████████████████████████████████▏ | 1.00G/1.66G [00:23<00:15, 47.2MB/s]

60%|████████████████████████████████████████████████▍ | 1.01G/1.66G [00:23<00:16, 43.3MB/s]

61%|████████████████████████████████████████████████▋ | 1.01G/1.66G [00:23<00:14, 48.0MB/s]

61%|████████████████████████████████████████████████▉ | 1.02G/1.66G [00:23<00:15, 43.9MB/s]

61%|█████████████████████████████████████████████████▏ | 1.02G/1.66G [00:23<00:14, 48.4MB/s]

62%|█████████████████████████████████████████████████▍ | 1.03G/1.66G [00:23<00:15, 43.7MB/s]

62%|█████████████████████████████████████████████████▊ | 1.03G/1.66G [00:24<00:15, 44.6MB/s]

63%|██████████████████████████████████████████████████ | 1.04G/1.66G [00:24<00:13, 48.5MB/s]

63%|██████████████████████████████████████████████████▎ | 1.04G/1.66G [00:24<00:14, 44.6MB/s]

63%|██████████████████████████████████████████████████▌ | 1.05G/1.66G [00:24<00:13, 48.4MB/s]

63%|██████████████████████████████████████████████████▊ | 1.06G/1.66G [00:24<00:14, 44.6MB/s]

64%|███████████████████████████████████████████████████ | 1.06G/1.66G [00:24<00:13, 48.4MB/s]

64%|███████████████████████████████████████████████████▎ | 1.07G/1.66G [00:24<00:14, 44.6MB/s]

64%|███████████████████████████████████████████████████▌ | 1.07G/1.66G [00:24<00:13, 48.5MB/s]

65%|███████████████████████████████████████████████████▊ | 1.08G/1.66G [00:25<00:14, 44.2MB/s]

65%|████████████████████████████████████████████████████ | 1.08G/1.66G [00:25<00:12, 48.4MB/s]

65%|████████████████████████████████████████████████████▎ | 1.09G/1.66G [00:25<00:13, 44.2MB/s]

66%|████████████████████████████████████████████████████▌ | 1.09G/1.66G [00:25<00:12, 48.5MB/s]

66%|████████████████████████████████████████████████████▊ | 1.10G/1.66G [00:25<00:13, 44.4MB/s]

66%|█████████████████████████████████████████████████████▏ | 1.10G/1.66G [00:25<00:13, 43.9MB/s]

67%|█████████████████████████████████████████████████████▎ | 1.11G/1.66G [00:25<00:12, 45.8MB/s]

67%|█████████████████████████████████████████████████████▋ | 1.12G/1.66G [00:26<00:13, 44.8MB/s]

67%|█████████████████████████████████████████████████████▉ | 1.12G/1.66G [00:26<00:12, 46.6MB/s]

68%|██████████████████████████████████████████████████████▏ | 1.13G/1.66G [00:26<00:12, 45.3MB/s]

68%|██████████████████████████████████████████████████████▍ | 1.13G/1.66G [00:26<00:12, 47.0MB/s]

68%|██████████████████████████████████████████████████████▋ | 1.14G/1.66G [00:26<00:12, 45.5MB/s]

69%|██████████████████████████████████████████████████████▉ | 1.14G/1.66G [00:26<00:11, 47.1MB/s]

69%|███████████████████████████████████████████████████████▏ | 1.15G/1.66G [00:26<00:12, 45.7MB/s]

69%|███████████████████████████████████████████████████████▍ | 1.15G/1.66G [00:26<00:11, 47.1MB/s]

70%|███████████████████████████████████████████████████████▋ | 1.16G/1.66G [00:27<00:11, 45.6MB/s]

70%|███████████████████████████████████████████████████████▉ | 1.16G/1.66G [00:27<00:11, 47.2MB/s]

70%|████████████████████████████████████████████████████████▏ | 1.17G/1.66G [00:27<00:11, 45.7MB/s]

71%|████████████████████████████████████████████████████████▍ | 1.17G/1.66G [00:27<00:11, 47.3MB/s]

71%|████████████████████████████████████████████████████████▊ | 1.18G/1.66G [00:27<00:11, 45.8MB/s]

71%|████████████████████████████████████████████████████████▉ | 1.18G/1.66G [00:27<00:10, 47.2MB/s]

72%|█████████████████████████████████████████████████████████▎ | 1.19G/1.66G [00:27<00:11, 45.9MB/s]

72%|█████████████████████████████████████████████████████████▌ | 1.20G/1.66G [00:27<00:10, 47.1MB/s]

72%|█████████████████████████████████████████████████████████▊ | 1.20G/1.66G [00:27<00:10, 45.9MB/s]

73%|██████████████████████████████████████████████████████████ | 1.21G/1.66G [00:28<00:10, 47.0MB/s]

73%|██████████████████████████████████████████████████████████▎ | 1.21G/1.66G [00:28<00:10, 45.8MB/s]

73%|██████████████████████████████████████████████████████████▌ | 1.22G/1.66G [00:28<00:10, 47.0MB/s]

74%|██████████████████████████████████████████████████████████▊ | 1.22G/1.66G [00:28<00:10, 45.9MB/s]

74%|███████████████████████████████████████████████████████████ | 1.23G/1.66G [00:28<00:09, 47.1MB/s]

74%|███████████████████████████████████████████████████████████▎ | 1.23G/1.66G [00:28<00:10, 45.1MB/s]

75%|███████████████████████████████████████████████████████████▌ | 1.24G/1.66G [00:28<00:09, 48.7MB/s]

75%|███████████████████████████████████████████████████████████▊ | 1.24G/1.66G [00:29<00:10, 44.9MB/s]

75%|████████████████████████████████████████████████████████████▏ | 1.25G/1.66G [00:29<00:09, 48.6MB/s]

75%|████████████████████████████████████████████████████████████▎ | 1.25G/1.66G [00:29<00:09, 44.8MB/s]

76%|████████████████████████████████████████████████████████████▋ | 1.26G/1.66G [00:29<00:08, 48.5MB/s]

76%|████████████████████████████████████████████████████████████▉ | 1.27G/1.66G [00:29<00:09, 44.8MB/s]

76%|█████████████████████████████████████████████████████████████▏ | 1.27G/1.66G [00:29<00:08, 48.5MB/s]

77%|█████████████████████████████████████████████████████████████▍ | 1.28G/1.66G [00:29<00:09, 44.7MB/s]

77%|█████████████████████████████████████████████████████████████▋ | 1.28G/1.66G [00:29<00:08, 48.3MB/s]

77%|█████████████████████████████████████████████████████████████▉ | 1.29G/1.66G [00:29<00:09, 44.4MB/s]

78%|██████████████████████████████████████████████████████████████▏ | 1.29G/1.66G [00:30<00:08, 48.5MB/s]

78%|██████████████████████████████████████████████████████████████▍ | 1.30G/1.66G [00:30<00:08, 44.2MB/s]

78%|██████████████████████████████████████████████████████████████▋ | 1.30G/1.66G [00:30<00:08, 44.1MB/s]

79%|██████████████████████████████████████████████████████████████▉ | 1.31G/1.66G [00:30<00:08, 45.5MB/s]

79%|███████████████████████████████████████████████████████████████▏ | 1.31G/1.66G [00:30<00:08, 44.8MB/s]

79%|███████████████████████████████████████████████████████████████▍ | 1.32G/1.66G [00:30<00:07, 46.5MB/s]

80%|███████████████████████████████████████████████████████████████▊ | 1.33G/1.66G [00:30<00:08, 45.2MB/s]

80%|███████████████████████████████████████████████████████████████▉ | 1.33G/1.66G [00:30<00:07, 46.9MB/s]

80%|████████████████████████████████████████████████████████████████▏ | 1.33G/1.66G [00:31<00:07, 48.1MB/s]

81%|████████████████████████████████████████████████████████████████▍ | 1.34G/1.66G [00:31<00:07, 45.4MB/s]

81%|████████████████████████████████████████████████████████████████▋ | 1.34G/1.66G [00:31<00:07, 46.7MB/s]

81%|████████████████████████████████████████████████████████████████▉ | 1.35G/1.66G [00:31<00:08, 40.8MB/s]

81%|█████████████████████████████████████████████████████████████████▏ | 1.35G/1.66G [00:31<00:08, 38.3MB/s]

82%|█████████████████████████████████████████████████████████████████▍ | 1.36G/1.66G [00:31<00:07, 43.9MB/s]

82%|█████████████████████████████████████████████████████████████████▋ | 1.37G/1.66G [00:31<00:07, 41.2MB/s]

82%|█████████████████████████████████████████████████████████████████▉ | 1.37G/1.66G [00:32<00:06, 46.1MB/s]

83%|██████████████████████████████████████████████████████████████████▏ | 1.38G/1.66G [00:32<00:07, 42.8MB/s]

83%|██████████████████████████████████████████████████████████████████▍ | 1.38G/1.66G [00:32<00:06, 47.5MB/s]

83%|██████████████████████████████████████████████████████████████████▋ | 1.39G/1.66G [00:32<00:06, 43.7MB/s]

84%|██████████████████████████████████████████████████████████████████▉ | 1.39G/1.66G [00:32<00:06, 48.1MB/s]

84%|███████████████████████████████████████████████████████████████████▏ | 1.40G/1.66G [00:32<00:06, 44.0MB/s]

84%|███████████████████████████████████████████████████████████████████▌ | 1.40G/1.66G [00:32<00:05, 48.4MB/s]

85%|███████████████████████████████████████████████████████████████████▋ | 1.41G/1.66G [00:32<00:06, 44.0MB/s]

85%|████████████████████████████████████████████████████████████████████ | 1.42G/1.66G [00:33<00:05, 44.4MB/s]

85%|████████████████████████████████████████████████████████████████████▎ | 1.42G/1.66G [00:33<00:05, 46.6MB/s]

86%|████████████████████████████████████████████████████████████████████▌ | 1.43G/1.66G [00:33<00:05, 44.9MB/s]

86%|████████████████████████████████████████████████████████████████████▊ | 1.43G/1.66G [00:33<00:05, 47.1MB/s]

86%|█████████████████████████████████████████████████████████████████████ | 1.44G/1.66G [00:33<00:05, 45.2MB/s]

87%|█████████████████████████████████████████████████████████████████████▍ | 1.44G/1.66G [00:33<00:05, 47.4MB/s]

87%|█████████████████████████████████████████████████████████████████████▌ | 1.45G/1.66G [00:33<00:05, 45.2MB/s]

87%|█████████████████████████████████████████████████████████████████████▉ | 1.45G/1.66G [00:33<00:04, 47.6MB/s]

88%|██████████████████████████████████████████████████████████████████████▏ | 1.46G/1.66G [00:34<00:04, 45.0MB/s]

88%|██████████████████████████████████████████████████████████████████████▍ | 1.46G/1.66G [00:34<00:04, 47.6MB/s]

88%|██████████████████████████████████████████████████████████████████████▋ | 1.47G/1.66G [00:34<00:04, 45.1MB/s]

89%|██████████████████████████████████████████████████████████████████████▉ | 1.47G/1.66G [00:34<00:04, 47.8MB/s]

89%|███████████████████████████████████████████████████████████████████████▏ | 1.48G/1.66G [00:34<00:04, 45.2MB/s]

89%|███████████████████████████████████████████████████████████████████████▍ | 1.49G/1.66G [00:34<00:03, 47.8MB/s]

90%|███████████████████████████████████████████████████████████████████████▋ | 1.49G/1.66G [00:34<00:04, 45.2MB/s]

90%|███████████████████████████████████████████████████████████████████████▉ | 1.50G/1.66G [00:34<00:03, 47.7MB/s]

90%|████████████████████████████████████████████████████████████████████████▏ | 1.50G/1.66G [00:35<00:03, 45.3MB/s]

91%|████████████████████████████████████████████████████████████████████████▍ | 1.51G/1.66G [00:35<00:03, 47.8MB/s]

91%|████████████████████████████████████████████████████████████████████████▋ | 1.51G/1.66G [00:35<00:03, 45.4MB/s]

91%|█████████████████████████████████████████████████████████████████████████ | 1.52G/1.66G [00:35<00:03, 47.7MB/s]

92%|█████████████████████████████████████████████████████████████████████████▏ | 1.52G/1.66G [00:35<00:03, 45.4MB/s]

92%|█████████████████████████████████████████████████████████████████████████▌ | 1.53G/1.66G [00:35<00:03, 47.7MB/s]

92%|█████████████████████████████████████████████████████████████████████████▊ | 1.53G/1.66G [00:35<00:03, 45.5MB/s]

93%|██████████████████████████████████████████████████████████████████████████ | 1.54G/1.66G [00:35<00:02, 47.7MB/s]

93%|██████████████████████████████████████████████████████████████████████████▎ | 1.54G/1.66G [00:36<00:02, 45.5MB/s]

93%|██████████████████████████████████████████████████████████████████████████▌ | 1.55G/1.66G [00:36<00:02, 47.7MB/s]

93%|██████████████████████████████████████████████████████████████████████████▊ | 1.55G/1.66G [00:36<00:02, 45.4MB/s]

94%|███████████████████████████████████████████████████████████████████████████ | 1.56G/1.66G [00:36<00:02, 47.7MB/s]

94%|███████████████████████████████████████████████████████████████████████████▎ | 1.57G/1.66G [00:36<00:02, 45.0MB/s]

94%|███████████████████████████████████████████████████████████████████████████▌ | 1.57G/1.66G [00:36<00:02, 47.7MB/s]

95%|███████████████████████████████████████████████████████████████████████████▊ | 1.58G/1.66G [00:36<00:02, 45.2MB/s]

95%|████████████████████████████████████████████████████████████████████████████ | 1.58G/1.66G [00:36<00:01, 43.9MB/s]

95%|████████████████████████████████████████████████████████████████████████████▍ | 1.59G/1.66G [00:37<00:01, 47.9MB/s]

96%|████████████████████████████████████████████████████████████████████████████▌ | 1.59G/1.66G [00:37<00:01, 44.3MB/s]

96%|████████████████████████████████████████████████████████████████████████████▉ | 1.60G/1.66G [00:37<00:01, 48.0MB/s]

96%|█████████████████████████████████████████████████████████████████████████████▏ | 1.60G/1.66G [00:37<00:01, 44.6MB/s]

97%|█████████████████████████████████████████████████████████████████████████████▍ | 1.61G/1.66G [00:37<00:01, 48.1MB/s]

97%|█████████████████████████████████████████████████████████████████████████████▋ | 1.61G/1.66G [00:37<00:01, 44.7MB/s]

97%|█████████████████████████████████████████████████████████████████████████████▉ | 1.62G/1.66G [00:37<00:00, 48.3MB/s]

98%|██████████████████████████████████████████████████████████████████████████████▏ | 1.62G/1.66G [00:37<00:00, 44.8MB/s]

98%|██████████████████████████████████████████████████████████████████████████████▍ | 1.63G/1.66G [00:38<00:00, 46.5MB/s]

98%|██████████████████████████████████████████████████████████████████████████████▋ | 1.63G/1.66G [00:38<00:00, 47.3MB/s]

99%|██████████████████████████████████████████████████████████████████████████████▉ | 1.64G/1.66G [00:38<00:00, 45.3MB/s]

99%|███████████████████████████████████████████████████████████████████████████████ | 1.64G/1.66G [00:38<00:00, 46.7MB/s]

99%|███████████████████████████████████████████████████████████████████████████████▎| 1.65G/1.66G [00:38<00:00, 41.5MB/s]

100%|███████████████████████████████████████████████████████████████████████████████▋| 1.66G/1.66G [00:38<00:00, 46.1MB/s]

100%|███████████████████████████████████████████████████████████████████████████████▊| 1.66G/1.66G [00:38<00:00, 43.2MB/s]

100%|████████████████████████████████████████████████████████████████████████████████| 1.66G/1.66G [00:38<00:00, 46.0MB/s]

Downloading training_shard_36.tfrec...

Downloading to /home/jupyter/.cache/kagglehub/datasets/ipythonx/brats2020/versions/1/training_shard_36.tfrec...

0%| | 0.00/1.33G [00:00<?, ?B/s]

0%| | 1.00M/1.33G [00:00<10:19, 2.31MB/s]

0%| | 2.00M/1.33G [00:00<05:39, 4.21MB/s]

0%|▏ | 4.00M/1.33G [00:00<03:04, 7.73MB/s]

1%|▍ | 8.00M/1.33G [00:00<01:36, 14.8MB/s]

1%|▊ | 14.0M/1.33G [00:00<00:54, 26.2MB/s]

1%|█ | 19.0M/1.33G [00:01<00:44, 31.4MB/s]

2%|█▍ | 25.0M/1.33G [00:01<00:35, 39.3MB/s]

2%|█▊ | 30.0M/1.33G [00:01<00:34, 40.8MB/s]

3%|██ | 35.0M/1.33G [00:01<00:33, 41.2MB/s]

3%|██▍ | 42.0M/1.33G [00:01<00:31, 44.3MB/s]

4%|██▉ | 49.0M/1.33G [00:01<00:29, 46.4MB/s]

4%|███▎ | 56.0M/1.33G [00:01<00:28, 47.8MB/s]

5%|███▋ | 63.0M/1.33G [00:01<00:25, 53.4MB/s]

5%|████ | 69.0M/1.33G [00:02<00:26, 51.6MB/s]

6%|████▍ | 75.0M/1.33G [00:02<00:27, 49.5MB/s]

6%|████▊ | 82.0M/1.33G [00:02<00:26, 49.9MB/s]

7%|█████▏ | 89.0M/1.33G [00:02<00:26, 50.3MB/s]

7%|█████▋ | 96.0M/1.33G [00:02<00:26, 50.4MB/s]

8%|██████ | 103M/1.33G [00:02<00:24, 54.9MB/s]

8%|██████▍ | 109M/1.33G [00:02<00:25, 52.5MB/s]

8%|██████▊ | 115M/1.33G [00:03<00:25, 50.6MB/s]

9%|███████▎ | 122M/1.33G [00:03<00:25, 50.6MB/s]

9%|███████▌ | 128M/1.33G [00:03<00:25, 51.1MB/s]

10%|███████▉ | 133M/1.33G [00:03<00:28, 45.3MB/s]

10%|████████▎ | 140M/1.33G [00:03<00:27, 46.9MB/s]

11%|████████▋ | 147M/1.33G [00:03<00:26, 48.0MB/s]

11%|█████████▏ | 154M/1.33G [00:03<00:25, 49.0MB/s]

12%|█████████▋ | 162M/1.33G [00:04<00:24, 50.7MB/s]

12%|██████████ | 169M/1.33G [00:04<00:24, 50.6MB/s]

13%|██████████▍ | 176M/1.33G [00:04<00:24, 50.0MB/s]

13%|██████████▊ | 182M/1.33G [00:04<00:23, 52.6MB/s]

14%|███████████▏ | 188M/1.33G [00:04<00:25, 48.6MB/s]

14%|███████████▌ | 194M/1.33G [00:04<00:26, 46.2MB/s]

15%|███████████▉ | 200M/1.33G [00:04<00:24, 49.8MB/s]

15%|████████████▏ | 205M/1.33G [00:04<00:26, 45.3MB/s]

15%|████████████▌ | 211M/1.33G [00:05<00:24, 49.3MB/s]

16%|████████████▊ | 216M/1.33G [00:05<00:26, 44.9MB/s]

16%|█████████████▏ | 222M/1.33G [00:05<00:24, 48.8MB/s]

17%|█████████████▍ | 227M/1.33G [00:05<00:26, 44.7MB/s]

17%|█████████████▊ | 233M/1.33G [00:05<00:24, 48.6MB/s]

17%|██████████████▏ | 238M/1.33G [00:05<00:26, 44.7MB/s]

18%|██████████████▌ | 244M/1.33G [00:05<00:24, 48.5MB/s]

18%|██████████████▊ | 249M/1.33G [00:05<00:26, 44.7MB/s]

19%|███████████████ | 254M/1.33G [00:06<00:24, 46.6MB/s]

19%|███████████████▍ | 260M/1.33G [00:06<00:25, 45.2MB/s]

19%|███████████████▊ | 265M/1.33G [00:06<00:24, 46.8MB/s]

20%|████████████████ | 271M/1.33G [00:06<00:25, 45.1MB/s]

20%|████████████████▍ | 277M/1.33G [00:06<00:23, 48.9MB/s]

21%|████████████████▊ | 282M/1.33G [00:06<00:26, 43.1MB/s]

21%|█████████████████ | 287M/1.33G [00:06<00:29, 38.8MB/s]

21%|█████████████████▎ | 292M/1.33G [00:07<00:30, 37.0MB/s]

22%|█████████████████▊ | 299M/1.33G [00:07<00:28, 39.6MB/s]

22%|██████████████████▏ | 306M/1.33G [00:07<00:23, 46.4MB/s]

23%|██████████████████▍ | 311M/1.33G [00:07<00:25, 42.9MB/s]

23%|██████████████████▊ | 317M/1.33G [00:07<00:23, 47.3MB/s]

24%|███████████████████▏ | 322M/1.33G [00:07<00:25, 43.4MB/s]

24%|███████████████████▌ | 328M/1.33G [00:07<00:22, 47.8MB/s]

24%|███████████████████▊ | 333M/1.33G [00:07<00:24, 44.0MB/s]

25%|████████████████████▏ | 339M/1.33G [00:08<00:22, 48.2MB/s]

25%|████████████████████▍ | 344M/1.33G [00:08<00:24, 44.2MB/s]

26%|████████████████████▊ | 350M/1.33G [00:08<00:24, 43.1MB/s]

26%|█████████████████████▏ | 356M/1.33G [00:08<00:22, 47.5MB/s]

26%|█████████████████████▍ | 361M/1.33G [00:08<00:24, 42.0MB/s]

27%|█████████████████████▊ | 367M/1.33G [00:08<00:25, 41.0MB/s]

27%|██████████████████████▏ | 373M/1.33G [00:08<00:22, 45.4MB/s]

28%|██████████████████████▍ | 378M/1.33G [00:09<00:24, 42.6MB/s]

28%|██████████████████████▊ | 384M/1.33G [00:09<00:21, 46.7MB/s]

29%|███████████████████████▏ | 389M/1.33G [00:09<00:23, 43.6MB/s]

29%|███████████████████████▍ | 395M/1.33G [00:09<00:21, 47.0MB/s]

29%|███████████████████████▊ | 400M/1.33G [00:09<00:22, 44.3MB/s]

30%|████████████████████████▏ | 406M/1.33G [00:09<00:21, 47.6MB/s]

30%|████████████████████████▍ | 411M/1.33G [00:09<00:22, 44.6MB/s]

31%|████████████████████████▊ | 417M/1.33G [00:09<00:20, 48.0MB/s]

31%|█████████████████████████ | 422M/1.33G [00:10<00:22, 44.8MB/s]

31%|█████████████████████████▍ | 428M/1.33G [00:10<00:20, 48.1MB/s]

32%|█████████████████████████▋ | 433M/1.33G [00:10<00:21, 44.9MB/s]

32%|██████████████████████████ | 439M/1.33G [00:10<00:20, 48.2MB/s]

33%|██████████████████████████▍ | 444M/1.33G [00:10<00:21, 44.8MB/s]

33%|██████████████████████████▊ | 450M/1.33G [00:10<00:19, 48.3MB/s]

33%|███████████████████████████ | 455M/1.33G [00:10<00:21, 44.7MB/s]

34%|███████████████████████████▍ | 461M/1.33G [00:10<00:19, 48.2MB/s]

34%|███████████████████████████▋ | 466M/1.33G [00:11<00:21, 44.4MB/s]

35%|████████████████████████████ | 472M/1.33G [00:11<00:19, 48.5MB/s]

35%|████████████████████████████▎ | 477M/1.33G [00:11<00:20, 44.5MB/s]

35%|████████████████████████████▋ | 483M/1.33G [00:11<00:19, 48.4MB/s]

36%|█████████████████████████████ | 488M/1.33G [00:11<00:20, 44.5MB/s]

36%|█████████████████████████████▎ | 494M/1.33G [00:11<00:18, 48.5MB/s]

37%|█████████████████████████████▋ | 499M/1.33G [00:11<00:20, 44.5MB/s]

37%|██████████████████████████████ | 505M/1.33G [00:11<00:18, 48.6MB/s]

37%|██████████████████████████████▎ | 510M/1.33G [00:12<00:20, 44.5MB/s]

38%|██████████████████████████████▋ | 516M/1.33G [00:12<00:18, 48.7MB/s]

38%|██████████████████████████████▉ | 521M/1.33G [00:12<00:19, 44.5MB/s]

39%|███████████████████████████████▎ | 527M/1.33G [00:12<00:18, 48.5MB/s]

39%|███████████████████████████████▋ | 532M/1.33G [00:12<00:19, 44.4MB/s]

39%|███████████████████████████████▉ | 537M/1.33G [00:12<00:21, 39.7MB/s]

40%|████████████████████████████████▏ | 542M/1.33G [00:12<00:23, 37.0MB/s]

40%|████████████████████████████████▋ | 549M/1.33G [00:13<00:21, 39.6MB/s]

41%|█████████████████████████████████ | 556M/1.33G [00:13<00:18, 46.3MB/s]

41%|█████████████████████████████████▎ | 561M/1.33G [00:13<00:19, 42.9MB/s]

42%|█████████████████████████████████▋ | 567M/1.33G [00:13<00:17, 47.6MB/s]

42%|██████████████████████████████████ | 572M/1.33G [00:13<00:19, 43.3MB/s]

43%|██████████████████████████████████▍ | 579M/1.33G [00:13<00:18, 43.9MB/s]

43%|██████████████████████████████████▊ | 586M/1.33G [00:13<00:18, 44.4MB/s]

44%|███████████████████████████████████▎ | 593M/1.33G [00:14<00:18, 44.7MB/s]

44%|███████████████████████████████████▋ | 600M/1.33G [00:14<00:15, 50.2MB/s]

44%|███████████████████████████████████▉ | 605M/1.33G [00:14<00:17, 45.4MB/s]

45%|████████████████████████████████████▍ | 612M/1.33G [00:14<00:17, 45.4MB/s]

45%|████████████████████████████████████▊ | 619M/1.33G [00:14<00:17, 45.4MB/s]

46%|█████████████████████████████████████▏ | 625M/1.33G [00:14<00:15, 49.1MB/s]

46%|█████████████████████████████████████▍ | 630M/1.33G [00:14<00:17, 43.6MB/s]

47%|█████████████████████████████████████▊ | 635M/1.33G [00:15<00:18, 40.5MB/s]

47%|██████████████████████████████████████▏ | 642M/1.33G [00:15<00:15, 47.5MB/s]

47%|██████████████████████████████████████▍ | 647M/1.33G [00:15<00:17, 43.4MB/s]

48%|██████████████████████████████████████▉ | 654M/1.33G [00:15<00:16, 43.9MB/s]

49%|███████████████████████████████████████▎ | 661M/1.33G [00:15<00:16, 44.1MB/s]

49%|███████████████████████████████████████▋ | 667M/1.33G [00:15<00:15, 47.9MB/s]

49%|███████████████████████████████████████▉ | 672M/1.33G [00:15<00:16, 44.1MB/s]

50%|████████████████████████████████████████▎ | 678M/1.33G [00:15<00:14, 48.4MB/s]

50%|████████████████████████████████████████▌ | 683M/1.33G [00:16<00:16, 44.4MB/s]

51%|████████████████████████████████████████▉ | 689M/1.33G [00:16<00:14, 48.5MB/s]

51%|█████████████████████████████████████████▎ | 694M/1.33G [00:16<00:16, 42.9MB/s]

51%|█████████████████████████████████████████▌ | 699M/1.33G [00:16<00:17, 39.4MB/s]

52%|█████████████████████████████████████████▉ | 705M/1.33G [00:16<00:15, 44.6MB/s]

52%|██████████████████████████████████████████▏ | 710M/1.33G [00:16<00:16, 41.8MB/s]

53%|██████████████████████████████████████████▌ | 716M/1.33G [00:16<00:14, 46.6MB/s]

53%|██████████████████████████████████████████▊ | 721M/1.33G [00:17<00:15, 43.0MB/s]

53%|███████████████████████████████████████████▏ | 727M/1.33G [00:17<00:15, 42.2MB/s]

54%|███████████████████████████████████████████▌ | 733M/1.33G [00:17<00:14, 47.0MB/s]

54%|███████████████████████████████████████████▉ | 738M/1.33G [00:17<00:15, 43.3MB/s]

55%|████████████████████████████████████████████▏ | 744M/1.33G [00:17<00:13, 47.8MB/s]

55%|████████████████████████████████████████████▌ | 749M/1.33G [00:17<00:14, 43.8MB/s]

55%|████████████████████████████████████████████▉ | 755M/1.33G [00:17<00:13, 48.0MB/s]

56%|█████████████████████████████████████████████▏ | 760M/1.33G [00:17<00:14, 44.2MB/s]

56%|█████████████████████████████████████████████▌ | 766M/1.33G [00:18<00:12, 48.3MB/s]

57%|█████████████████████████████████████████████▊ | 771M/1.33G [00:18<00:13, 44.4MB/s]

57%|██████████████████████████████████████████████▏ | 777M/1.33G [00:18<00:12, 48.1MB/s]

57%|██████████████████████████████████████████████▍ | 782M/1.33G [00:18<00:13, 44.4MB/s]

58%|██████████████████████████████████████████████▊ | 788M/1.33G [00:18<00:12, 48.3MB/s]

58%|███████████████████████████████████████████████▏ | 793M/1.33G [00:18<00:13, 44.5MB/s]

59%|███████████████████████████████████████████████▌ | 799M/1.33G [00:18<00:12, 48.4MB/s]

59%|███████████████████████████████████████████████▊ | 804M/1.33G [00:18<00:13, 44.6MB/s]

59%|████████████████████████████████████████████████▏ | 810M/1.33G [00:19<00:11, 48.5MB/s]

60%|████████████████████████████████████████████████▍ | 815M/1.33G [00:19<00:13, 42.2MB/s]

60%|████████████████████████████████████████████████▊ | 820M/1.33G [00:19<00:14, 39.0MB/s]

61%|█████████████████████████████████████████████████ | 826M/1.33G [00:19<00:14, 39.2MB/s]

61%|█████████████████████████████████████████████████▌ | 833M/1.33G [00:19<00:13, 41.1MB/s]

62%|█████████████████████████████████████████████████▉ | 839M/1.33G [00:19<00:11, 45.9MB/s]

62%|██████████████████████████████████████████████████▏ | 844M/1.33G [00:19<00:12, 42.6MB/s]

62%|██████████████████████████████████████████████████▌ | 850M/1.33G [00:20<00:11, 47.0MB/s]

63%|██████████████████████████████████████████████████▊ | 855M/1.33G [00:20<00:12, 43.6MB/s]

63%|███████████████████████████████████████████████████▏ | 861M/1.33G [00:20<00:11, 47.7MB/s]

64%|███████████████████████████████████████████████████▍ | 866M/1.33G [00:20<00:11, 44.0MB/s]

64%|███████████████████████████████████████████████████▊ | 872M/1.33G [00:20<00:10, 48.2MB/s]

64%|████████████████████████████████████████████████████▏ | 877M/1.33G [00:20<00:11, 44.4MB/s]

65%|████████████████████████████████████████████████████▌ | 883M/1.33G [00:20<00:10, 48.3MB/s]

65%|████████████████████████████████████████████████████▊ | 888M/1.33G [00:20<00:11, 44.4MB/s]

66%|█████████████████████████████████████████████████████▏ | 894M/1.33G [00:21<00:10, 48.4MB/s]

66%|█████████████████████████████████████████████████████▍ | 899M/1.33G [00:21<00:10, 44.4MB/s]

66%|█████████████████████████████████████████████████████▊ | 905M/1.33G [00:21<00:09, 48.6MB/s]

67%|██████████████████████████████████████████████████████ | 910M/1.33G [00:21<00:10, 44.5MB/s]

67%|██████████████████████████████████████████████████████▍ | 916M/1.33G [00:21<00:09, 48.6MB/s]

68%|██████████████████████████████████████████████████████▊ | 921M/1.33G [00:21<00:10, 44.5MB/s]

68%|███████████████████████████████████████████████████████ | 927M/1.33G [00:21<00:09, 48.2MB/s]

68%|███████████████████████████████████████████████████████▍ | 932M/1.33G [00:21<00:10, 44.7MB/s]

69%|███████████████████████████████████████████████████████▊ | 938M/1.33G [00:22<00:09, 48.5MB/s]

69%|████████████████████████████████████████████████████████ | 943M/1.33G [00:22<00:09, 44.7MB/s]

70%|████████████████████████████████████████████████████████▍ | 949M/1.33G [00:22<00:08, 48.3MB/s]

70%|████████████████████████████████████████████████████████▋ | 954M/1.33G [00:22<00:09, 44.8MB/s]

70%|█████████████████████████████████████████████████████████ | 960M/1.33G [00:22<00:08, 48.2MB/s]

71%|█████████████████████████████████████████████████████████▍ | 965M/1.33G [00:22<00:09, 43.5MB/s]

71%|█████████████████████████████████████████████████████████▋ | 971M/1.33G [00:22<00:09, 41.9MB/s]

72%|██████████████████████████████████████████████████████████ | 977M/1.33G [00:22<00:08, 46.6MB/s]

72%|██████████████████████████████████████████████████████████▍ | 982M/1.33G [00:23<00:09, 42.8MB/s]

73%|██████████████████████████████████████████████████████████▋ | 988M/1.33G [00:23<00:08, 47.1MB/s]

73%|███████████████████████████████████████████████████████████ | 993M/1.33G [00:23<00:08, 43.6MB/s]

73%|███████████████████████████████████████████████████████████▍ | 999M/1.33G [00:23<00:07, 48.0MB/s]

74%|██████████████████████████████████████████████████████████▉ | 0.98G/1.33G [00:23<00:08, 44.1MB/s]

74%|███████████████████████████████████████████████████████████▎ | 0.99G/1.33G [00:23<00:07, 48.2MB/s]

75%|███████████████████████████████████████████████████████████▌ | 0.99G/1.33G [00:23<00:08, 44.3MB/s]

75%|███████████████████████████████████████████████████████████▉ | 1.00G/1.33G [00:23<00:07, 48.4MB/s]

75%|████████████████████████████████████████████████████████████▎ | 1.00G/1.33G [00:24<00:07, 44.4MB/s]

76%|████████████████████████████████████████████████████████████▌ | 1.01G/1.33G [00:24<00:07, 48.4MB/s]

76%|████████████████████████████████████████████████████████████▉ | 1.01G/1.33G [00:24<00:07, 44.1MB/s]

77%|█████████████████████████████████████████████████████████████▏ | 1.02G/1.33G [00:24<00:07, 47.7MB/s]

77%|█████████████████████████████████████████████████████████████▌ | 1.02G/1.33G [00:24<00:07, 44.5MB/s]

77%|█████████████████████████████████████████████████████████████▉ | 1.03G/1.33G [00:24<00:06, 47.9MB/s]

78%|██████████████████████████████████████████████████████████████▏ | 1.03G/1.33G [00:24<00:07, 44.7MB/s]

78%|██████████████████████████████████████████████████████████████▍ | 1.04G/1.33G [00:24<00:06, 46.5MB/s]

78%|██████████████████████████████████████████████████████████████▊ | 1.04G/1.33G [00:25<00:06, 44.3MB/s]

79%|███████████████████████████████████████████████████████████████ | 1.05G/1.33G [00:25<00:06, 45.7MB/s]

79%|███████████████████████████████████████████████████████████████▍ | 1.05G/1.33G [00:25<00:06, 45.1MB/s]

80%|███████████████████████████████████████████████████████████████▋ | 1.06G/1.33G [00:25<00:06, 46.2MB/s]

80%|████████████████████████████████████████████████████████████████ | 1.07G/1.33G [00:25<00:06, 45.6MB/s]

80%|████████████████████████████████████████████████████████████████▎ | 1.07G/1.33G [00:25<00:05, 46.6MB/s]

81%|████████████████████████████████████████████████████████████████▋ | 1.08G/1.33G [00:25<00:05, 45.9MB/s]

81%|█████████████████████████████████████████████████████████████████ | 1.08G/1.33G [00:25<00:05, 46.8MB/s]

82%|█████████████████████████████████████████████████████████████████▎ | 1.09G/1.33G [00:26<00:05, 45.8MB/s]

82%|█████████████████████████████████████████████████████████████████▋ | 1.09G/1.33G [00:26<00:05, 46.8MB/s]

83%|██████████████████████████████████████████████████████████████████ | 1.10G/1.33G [00:26<00:05, 46.0MB/s]

83%|██████████████████████████████████████████████████████████████████▎ | 1.10G/1.33G [00:26<00:05, 46.9MB/s]

83%|██████████████████████████████████████████████████████████████████▋ | 1.11G/1.33G [00:26<00:05, 46.1MB/s]

84%|██████████████████████████████████████████████████████████████████▉ | 1.11G/1.33G [00:26<00:04, 46.8MB/s]

84%|███████████████████████████████████████████████████████████████████▏ | 1.12G/1.33G [00:26<00:04, 48.1MB/s]

84%|███████████████████████████████████████████████████████████████████▌ | 1.12G/1.33G [00:26<00:04, 45.4MB/s]

85%|███████████████████████████████████████████████████████████████████▊ | 1.13G/1.33G [00:26<00:04, 47.1MB/s]

85%|████████████████████████████████████████████████████████████████████ | 1.13G/1.33G [00:27<00:04, 44.6MB/s]

86%|████████████████████████████████████████████████████████████████████▍ | 1.14G/1.33G [00:27<00:04, 46.0MB/s]

86%|████████████████████████████████████████████████████████████████████▊ | 1.14G/1.33G [00:27<00:04, 45.3MB/s]

86%|█████████████████████████████████████████████████████████████████████ | 1.15G/1.33G [00:27<00:04, 42.6MB/s]

87%|█████████████████████████████████████████████████████████████████████▍ | 1.15G/1.33G [00:27<00:04, 41.5MB/s]

87%|█████████████████████████████████████████████████████████████████████▊ | 1.16G/1.33G [00:27<00:04, 42.8MB/s]

88%|██████████████████████████████████████████████████████████████████████▏ | 1.17G/1.33G [00:27<00:03, 49.0MB/s]

88%|██████████████████████████████████████████████████████████████████████▌ | 1.17G/1.33G [00:28<00:03, 44.4MB/s]

89%|██████████████████████████████████████████████████████████████████████▉ | 1.18G/1.33G [00:28<00:03, 44.6MB/s]

89%|███████████████████████████████████████████████████████████████████████▎ | 1.19G/1.33G [00:28<00:03, 44.8MB/s]

90%|███████████████████████████████████████████████████████████████████████▊ | 1.19G/1.33G [00:28<00:03, 45.0MB/s]

90%|████████████████████████████████████████████████████████████████████████ | 1.20G/1.33G [00:28<00:02, 48.4MB/s]

91%|████████████████████████████████████████████████████████████████████████▍ | 1.20G/1.33G [00:28<00:03, 44.6MB/s]

91%|████████████████████████████████████████████████████████████████████████▊ | 1.21G/1.33G [00:28<00:02, 47.7MB/s]

91%|█████████████████████████████████████████████████████████████████████████ | 1.21G/1.33G [00:29<00:02, 44.0MB/s]

92%|█████████████████████████████████████████████████████████████████████████▎ | 1.22G/1.33G [00:29<00:02, 40.9MB/s]

92%|█████████████████████████████████████████████████████████████████████████▊ | 1.23G/1.33G [00:29<00:02, 47.2MB/s]

93%|██████████████████████████████████████████████████████████████████████████ | 1.23G/1.33G [00:29<00:02, 43.9MB/s]

93%|██████████████████████████████████████████████████████████████████████████▍ | 1.24G/1.33G [00:29<00:02, 42.7MB/s]

94%|██████████████████████████████████████████████████████████████████████████▊ | 1.24G/1.33G [00:29<00:01, 48.4MB/s]

94%|███████████████████████████████████████████████████████████████████████████ | 1.25G/1.33G [00:29<00:01, 44.8MB/s]

94%|███████████████████████████████████████████████████████████████████████████▍ | 1.25G/1.33G [00:29<00:01, 48.6MB/s]

95%|███████████████████████████████████████████████████████████████████████████▊ | 1.26G/1.33G [00:30<00:01, 44.7MB/s]

95%|████████████████████████████████████████████████████████████████████████████ | 1.26G/1.33G [00:30<00:01, 39.9MB/s]

95%|████████████████████████████████████████████████████████████████████████████▎ | 1.27G/1.33G [00:30<00:01, 37.3MB/s]

96%|████████████████████████████████████████████████████████████████████████████▊ | 1.28G/1.33G [00:30<00:01, 39.8MB/s]

96%|█████████████████████████████████████████████████████████████████████████████ | 1.28G/1.33G [00:30<00:01, 44.8MB/s]

97%|█████████████████████████████████████████████████████████████████████████████▍ | 1.29G/1.33G [00:30<00:01, 41.8MB/s]

97%|█████████████████████████████████████████████████████████████████████████████▊ | 1.29G/1.33G [00:30<00:00, 46.7MB/s]

98%|██████████████████████████████████████████████████████████████████████████████ | 1.30G/1.33G [00:31<00:00, 43.1MB/s]

98%|██████████████████████████████████████████████████████████████████████████████▍ | 1.30G/1.33G [00:31<00:00, 47.8MB/s]

98%|██████████████████████████████████████████████████████████████████████████████▋ | 1.31G/1.33G [00:31<00:00, 43.7MB/s]

99%|██████████████████████████████████████████████████████████████████████████████▉ | 1.31G/1.33G [00:31<00:00, 45.4MB/s]

99%|███████████████████████████████████████████████████████████████████████████████▎| 1.32G/1.33G [00:31<00:00, 44.4MB/s]

100%|███████████████████████████████████████████████████████████████████████████████▋| 1.32G/1.33G [00:31<00:00, 46.4MB/s]

100%|███████████████████████████████████████████████████████████████████████████████▉| 1.33G/1.33G [00:31<00:00, 45.0MB/s]

100%|████████████████████████████████████████████████████████████████████████████████| 1.33G/1.33G [00:31<00:00, 44.8MB/s]


Imports

os.environ["KERAS_BACKEND"] = "jax"  # tensorflow, torch, jax


import keras
import numpy as np
import pandas as pd
import tensorflow as tf
from keras import ops
from matplotlib import pyplot as plt
from medicai.callbacks import SlidingWindowInferenceCallback
from medicai.losses import BinaryDiceCELoss
from medicai.metrics import BinaryDiceMetric
from medicai.models import SwinUNETR
from medicai.transforms import (
    Compose,
    CropForeground,
    NormalizeIntensity,
    RandFlip,
    RandShiftIntensity,
    RandSpatialCrop,
    TensorBundle,
)
from medicai.utils.inference import SlidingWindowInference

# enable mixed precision
keras.mixed_precision.set_global_policy("mixed_float16")

# reproducibility
keras.utils.set_random_seed(101)

print(
    f"keras backend: {keras.config.backend()}\n"
    f"keras version: {keras.version()}\n"
    f"tensorflow version: {tf.__version__}\n"
)
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
E0000 00:00:1770049564.251136    5118 cuda_dnn.cc:8579] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
E0000 00:00:1770049564.258875    5118 cuda_blas.cc:1407] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
W0000 00:00:1770049564.277625    5118 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.
W0000 00:00:1770049564.277649    5118 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.
W0000 00:00:1770049564.277651    5118 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.
W0000 00:00:1770049564.277654    5118 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.

keras backend: jax
keras version: 3.13.2
tensorflow version: 2.19.1

Create Multi-label Brain Tumor Labels

The BraTS segmentation task involves multiple tumor sub-regions, and it is formulated as a multi-label segmentation problem. The label combinations are used to define the following clinical regions of interest:

- Tumor Core (TC): label = 1 or 4
- Whole Tumor (WT): label = 1 or 2 or 4
- Enhancing Tumor (ET): label = 4

These region-wise groupings allow for evaluation across different tumor structures relevant for clinical assessment and treatment planning. A sample view is shown below, figure taken from BraTS-benchmark paper.


Managing Multi-Label Outputs with TensorBundle

To organize and manage these multi-label segmentation targets, we will implement a custom transformation using TensorBundle from medicai. The TensorBundle is a lightweight container class designed to hold:

  • A dictionary of tensors (e.g., images, labels)
  • Optional metadata associated with those tensors (e.g., affine matrices, spacing, original shapes)

This design allows data and metadata to be passed together through the transformation pipeline in a structured and consistent way. Each medicai transformation expects inputs to be organized as key:value pairs, for example:

meta = {"affine": affine}
data = {"image": image, "label": label}

Using TensorBundle makes it easier to apply complex medical imaging transformations while preserving spatial and anatomical information throughout preprocessing and model inference.

class ConvertToMultiChannelBasedOnBratsClasses:
    """
    Convert labels to multi channels based on BRATS classes using TensorFlow.

    Label definitions:
    - 1: necrotic and non-enhancing tumor core
    - 2: peritumoral edema
    - 4: GD-enhancing tumor

    Output channels:
    - Channel 0 (TC): Tumor core (labels 1 or 4)
    - Channel 1 (WT): Whole tumor (labels 1, 2, or 4)
    - Channel 2 (ET): Enhancing tumor (label 4)
    """

    def __init__(self, keys):
        self.keys = keys

    def __call__(self, inputs):
        if isinstance(inputs, dict):
            inputs = TensorBundle(inputs)

        for key in self.keys:
            data = inputs[key]

            # TC: label == 1 or 4
            tc = tf.logical_or(tf.equal(data, 1), tf.equal(data, 4))

            # WT: label == 1 or 2 or 4
            wt = tf.logical_or(tc, tf.equal(data, 2))

            # ET: label == 4
            et = tf.equal(data, 4)

            stacked = tf.stack(
                [
                    tf.cast(tc, tf.float32),
                    tf.cast(wt, tf.float32),
                    tf.cast(et, tf.float32),
                ],
                axis=-1,
            )

            inputs[key] = stacked
        return inputs

Transformation

Each medicai transformation expects the input to have the shape (depth, height, width, channel). The original .nii (and converted .tfrecord) format contains the input shape of (height, width, depth). To make it compatible with medicai, we need to re-arrange the shape axes.

def rearrange_shape(sample):
    # unpack sample
    image = sample["image"]
    label = sample["label"]
    affine = sample["affine"]

    # special case
    image = tf.transpose(image, perm=[2, 1, 0, 3])  # whdc -> dhwc
    label = tf.transpose(label, perm=[2, 1, 0])  # whd -> dhw
    cols = tf.gather(affine, [2, 1, 0], axis=1)  # (whd) -> (dhw)
    affine = tf.concat([cols, affine[:, 3:]], axis=1)

    # update sample with new / updated tensor
    sample["image"] = image
    sample["label"] = label
    sample["affine"] = affine
    return sample

Each transformation class of medicai expects input as either a dictionary or a TensorBundle object, as discussed earlier. When a dictionary of input data (along with metadata) is passed, it is automatically wrapped into a TensorBundle instance. The examples below demonstrate how transformations are used in this way.

num_classes = 3
epochs = 4
input_shape = (96, 96, 96, 4)


def train_transformation(sample):
    meta = {"affine": sample["affine"]}
    data = {"image": sample["image"], "label": sample["label"]}

    pipeline = Compose(
        [
            ConvertToMultiChannelBasedOnBratsClasses(keys=["label"]),
            CropForeground(
                keys=("image", "label"),
                source_key="image",
                k_divisible=input_shape[:3],
            ),
            RandSpatialCrop(
                keys=["image", "label"], roi_size=input_shape[:3], random_size=False
            ),
            RandFlip(keys=["image", "label"], spatial_axis=[0], prob=0.5),
            RandFlip(keys=["image", "label"], spatial_axis=[1], prob=0.5),
            RandFlip(keys=["image", "label"], spatial_axis=[2], prob=0.5),
            NormalizeIntensity(keys=["image"], nonzero=True, channel_wise=True),
            RandShiftIntensity(keys=["image"], offsets=0.10, prob=1.0),
        ]
    )
    result = pipeline(data, meta)
    return result["image"], result["label"]


def val_transformation(sample):
    meta = {"affine": sample["affine"]}
    data = {"image": sample["image"], "label": sample["label"]}

    pipeline = Compose(
        [
            ConvertToMultiChannelBasedOnBratsClasses(keys=["label"]),
            NormalizeIntensity(keys=["image"], nonzero=True, channel_wise=True),
        ]
    )
    result = pipeline(data, meta)
    return result["image"], result["label"]

The tfrecord parser

def parse_tfrecord_fn(example_proto):
    feature_description = {
        # Image raw data
        "flair_raw": tf.io.FixedLenFeature([], tf.string),
        "t1_raw": tf.io.FixedLenFeature([], tf.string),
        "t1ce_raw": tf.io.FixedLenFeature([], tf.string),
        "t2_raw": tf.io.FixedLenFeature([], tf.string),
        "label_raw": tf.io.FixedLenFeature([], tf.string),
        # Image shape
        "flair_shape": tf.io.FixedLenFeature([3], tf.int64),
        "t1_shape": tf.io.FixedLenFeature([3], tf.int64),
        "t1ce_shape": tf.io.FixedLenFeature([3], tf.int64),
        "t2_shape": tf.io.FixedLenFeature([3], tf.int64),
        "label_shape": tf.io.FixedLenFeature([3], tf.int64),
        # Affine matrices (4x4 = 16 values)
        "flair_affine": tf.io.FixedLenFeature([16], tf.float32),
        "t1_affine": tf.io.FixedLenFeature([16], tf.float32),
        "t1ce_affine": tf.io.FixedLenFeature([16], tf.float32),
        "t2_affine": tf.io.FixedLenFeature([16], tf.float32),
        "label_affine": tf.io.FixedLenFeature([16], tf.float32),
        # Voxel Spacing (pixdim)
        "flair_pixdim": tf.io.FixedLenFeature([8], tf.float32),
        "t1_pixdim": tf.io.FixedLenFeature([8], tf.float32),
        "t1ce_pixdim": tf.io.FixedLenFeature([8], tf.float32),
        "t2_pixdim": tf.io.FixedLenFeature([8], tf.float32),
        "label_pixdim": tf.io.FixedLenFeature([8], tf.float32),
        # Filenames
        "flair_filename": tf.io.FixedLenFeature([], tf.string),
        "t1_filename": tf.io.FixedLenFeature([], tf.string),
        "t1ce_filename": tf.io.FixedLenFeature([], tf.string),
        "t2_filename": tf.io.FixedLenFeature([], tf.string),
        "label_filename": tf.io.FixedLenFeature([], tf.string),
    }

    example = tf.io.parse_single_example(example_proto, feature_description)

    # Decode image and label data
    flair = tf.io.decode_raw(example["flair_raw"], tf.float32)
    t1 = tf.io.decode_raw(example["t1_raw"], tf.float32)
    t1ce = tf.io.decode_raw(example["t1ce_raw"], tf.float32)
    t2 = tf.io.decode_raw(example["t2_raw"], tf.float32)
    label = tf.io.decode_raw(example["label_raw"], tf.float32)

    # Reshape to original dimensions
    flair = tf.reshape(flair, example["flair_shape"])
    t1 = tf.reshape(t1, example["t1_shape"])
    t1ce = tf.reshape(t1ce, example["t1ce_shape"])
    t2 = tf.reshape(t2, example["t2_shape"])
    label = tf.reshape(label, example["label_shape"])

    # Decode affine matrices
    flair_affine = tf.reshape(example["flair_affine"], (4, 4))
    t1_affine = tf.reshape(example["t1_affine"], (4, 4))
    t1ce_affine = tf.reshape(example["t1ce_affine"], (4, 4))
    t2_affine = tf.reshape(example["t2_affine"], (4, 4))
    label_affine = tf.reshape(example["label_affine"], (4, 4))

    # add channel axis
    flair = flair[..., None]
    t1 = t1[..., None]
    t1ce = t1ce[..., None]
    t2 = t2[..., None]
    image = tf.concat([flair, t1, t1ce, t2], axis=-1)

    return {
        "image": image,
        "label": label,
        "affine": flair_affine,  # Since affine is the same for all
    }

Dataloader

def load_tfrecord_dataset(tfrecord_datalist, batch_size=1, shuffle=True):
    dataset = tf.data.TFRecordDataset(tfrecord_datalist)
    dataset = dataset.shuffle(buffer_size=100) if shuffle else dataset
    dataset = dataset.map(parse_tfrecord_fn, num_parallel_calls=tf.data.AUTOTUNE)
    dataset = dataset.map(rearrange_shape, num_parallel_calls=tf.data.AUTOTUNE)
    if shuffle:
        dataset = dataset.map(train_transformation, num_parallel_calls=tf.data.AUTOTUNE)
    else:
        dataset = dataset.map(val_transformation, num_parallel_calls=tf.data.AUTOTUNE)
    dataset = dataset.batch(batch_size).prefetch(tf.data.AUTOTUNE)
    return dataset

The training batch size can be set to more than 1 depending on the environment and available resources. However, we intentionally keep the validation batch size as 1 to handle variable-sized samples more flexibly. While padded or ragged batches are alternative options, a batch size of 1 ensures simplicity and consistency during evaluation, especially for 3D medical data.

tfrecord_pattern = "brats2020_subset/training_shard_*.tfrec"
datalist = sorted(
    tf.io.gfile.glob(tfrecord_pattern),
    key=lambda x: int(x.split("_")[-1].split(".")[0]),
)

train_datalist = datalist[:-1]
val_datalist = datalist[-1:]
print(len(train_datalist), len(val_datalist))

train_ds = load_tfrecord_dataset(train_datalist, batch_size=1, shuffle=True)
val_ds = load_tfrecord_dataset(val_datalist, batch_size=1, shuffle=False)
2 1

I0000 00:00:1770049567.629599    5118 gpu_device.cc:2019] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 13764 MB memory:  -> device: 0, name: Tesla T4, pci bus id: 0000:00:04.0, compute capability: 7.5

WARNING:tensorflow:From /home/jupyter/py311/lib/python3.11/site-packages/tensorflow/python/util/deprecation.py:660: calling map_fn_v2 (from tensorflow.python.ops.map_fn) with dtype is deprecated and will be removed in a future version.
Instructions for updating:
Use fn_output_signature instead

sanity check: Fetch a single validation sample to inspect its shape and values.

val_x, val_y = next(iter(val_ds))
test_image = val_x.numpy().squeeze()
test_mask = val_y.numpy().squeeze()
print(test_image.shape, test_mask.shape, np.unique(test_mask))
print(test_image.min(), test_image.max())
(155, 240, 240, 4) (155, 240, 240, 3) [0. 1.]
-3.8107734 12.865826

sanity check: Visualize the middle slice of the image and its corresponding label.

slice_no = test_image.shape[0] // 2

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 6))
ax1.imshow(test_image[slice_no], cmap="gray")
ax1.set_title(f"Image shape: {test_image.shape}")
ax2.imshow(test_mask[slice_no])
ax2.set_title(f"Label shape: {test_mask.shape}")
plt.show()
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Got range [-3.8107734..10.728726].

png

sanity check: Visualize sample image and label channels at middle slice index.

print(f"image shape: {test_image.shape}")
plt.figure("image", (24, 6))
for i in range(4):
    plt.subplot(1, 4, i + 1)
    plt.title(f"image channel {i}")
    plt.imshow(test_image[slice_no, :, :, i], cmap="gray")
plt.show()


print(f"label shape: {test_mask.shape}")
plt.figure("label", (18, 6))
for i in range(3):
    plt.subplot(1, 3, i + 1)
    plt.title(f"label channel {i}")
    plt.imshow(test_mask[slice_no, :, :, i])
plt.show()
image shape: (155, 240, 240, 4)

png

label shape: (155, 240, 240, 3)

png


Model

We will be using the 3D model architecture Swin UNEt TRansformers, i.e., SwinUNETR. It was used in the BraTS 2021 segmentation challenge by NVIDIA. The model was among the top-performing methods. It uses a Swin Transformer encoder to extract features at five different resolutions. A CNN-based decoder is connected to each resolution using skip connections.

The BraTS dataset provides four input modalities: flair, t1, t1ce, and t2 and three multi-label outputs: tumor-core, whole-tumor, and enhancing-tumor. Accordingly, we will initiate the model with 4 input channels and 3 output channels.

# # check available models
# medicai.models.list_models()
model = SwinUNETR(
    encoder_name="swin_tiny_v2",
    input_shape=input_shape,
    num_classes=num_classes,
    classifier_activation=None,
)

model.compile(
    optimizer=keras.optimizers.AdamW(
        learning_rate=1e-4,
        weight_decay=1e-5,
    ),
    loss=BinaryDiceCELoss(
        from_logits=True,
        num_classes=num_classes,
    ),
    metrics=[
        BinaryDiceMetric(
            from_logits=True,
            ignore_empty=True,
            num_classes=num_classes,
            name="dice",
        ),
        BinaryDiceMetric(
            from_logits=True,
            ignore_empty=True,
            target_class_ids=[0],
            num_classes=num_classes,
            name="dice_tc",
        ),
        BinaryDiceMetric(
            from_logits=True,
            ignore_empty=True,
            target_class_ids=[1],
            num_classes=num_classes,
            name="dice_wt",
        ),
        BinaryDiceMetric(
            from_logits=True,
            ignore_empty=True,
            target_class_ids=[2],
            num_classes=num_classes,
            name="dice_et",
        ),
    ],
)

# ALERT: This `instance_describe` attributes only available in medicai (not in core keras)
try:
    print(model.instance_describe())
except AttributeError:
    pass
Instance of SwinUNETR
  • input_shape: (96, 96, 96, 4)
  • encoder: SwinTinyV2(
    • input_shape: (96, 96, 96, 4)
    • include_rescaling: False
    • patch_size: (2, 2, 2)
    • window_size: (7, 7, 7)
    • dropout: 0.0
    • downsampling_strategy: 'swin_unetr_like'
    • name: 'SwinTinyV23D'
    )
  • encoder_name: 'swin_tiny_v2'
  • num_classes: 3
  • patch_size: 2
  • window_size: 7
  • classifier_activation: linear
  • feature_size: 48
  • res_block: True
  • norm_name: instance

Callback

We will be using sliding window inference callback from medicai to perform validation at certain interval or epoch during training. Based on the number of epoch size, we should set interval accordingly. For example, if epoch is set 15 and we want to evaluate model on validation set every 5 epoch, then we should set interval to 5.

swi_callback_metric = BinaryDiceMetric(
    from_logits=True,
    ignore_empty=True,
    num_classes=num_classes,
    name="val_dice",
)

swi_callback = SlidingWindowInferenceCallback(
    model,
    dataset=val_ds,
    metrics=swi_callback_metric,
    num_classes=num_classes,
    interval=2,
    overlap=0.5,
    roi_size=input_shape[:3],
    sw_batch_size=4,
    mode="gaussian",
    save_path="brats.model.weights.h5",
)

Training

Set more epoch for better optimization.

history = model.fit(train_ds, epochs=epochs, callbacks=[swi_callback])
Epoch 1/4

20/20 ━━━━━━━━━━━━━━━━━━━━ 148s 462ms/step - dice: 0.1189 - dice_et: 0.0788 - dice_tc: 0.1141 - dice_wt: 0.1637 - loss: 1.6568

Epoch 2/4

20/20 ━━━━━━━━━━━━━━━━━━━━ 0s 459ms/step - dice: 0.2062 - dice_et: 0.1818 - dice_tc: 0.1996 - dice_wt: 0.2372 - loss: 1.4219

Epoch 2: Running inference...

Total patch 48: 0%| | 0/12 [00:00<?, ?it/s]

Total patch 48: 8%|█████▊ | 1/12 [01:25<15:36, 85.17s/it]

Total patch 48: 17%|███████████▋ | 2/12 [01:25<05:53, 35.38s/it]

Total patch 48: 25%|█████████████████▌ | 3/12 [01:26<02:55, 19.47s/it]

Total patch 48: 33%|███████████████████████▎ | 4/12 [01:26<01:36, 12.00s/it]

Total patch 48: 42%|█████████████████████████████▏ | 5/12 [01:27<00:55, 7.87s/it]

Total patch 48: 50%|███████████████████████████████████ | 6/12 [01:27<00:32, 5.38s/it]

Total patch 48: 58%|████████████████████████████████████████▊ | 7/12 [01:28<00:19, 3.80s/it]

Total patch 48: 67%|██████████████████████████████████████████████▋ | 8/12 [01:28<00:11, 2.77s/it]

Total patch 48: 75%|████████████████████████████████████████████████████▌ | 9/12 [01:29<00:06, 2.07s/it]

Total patch 48: 83%|█████████████████████████████████████████████████████████▌ | 10/12 [01:30<00:03, 1.60s/it]

Total patch 48: 92%|███████████████████████████████████████████████████████████████▎ | 11/12 [01:30<00:01, 1.28s/it]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [01:31<00:00, 1.05s/it]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [01:31<00:00, 7.60s/it]

Total patch 48: 0%| | 0/12 [00:00<?, ?it/s]

Total patch 48: 8%|█████▊ | 1/12 [00:00<00:06, 1.78it/s]

Total patch 48: 17%|███████████▋ | 2/12 [00:01<00:05, 1.78it/s]

Total patch 48: 25%|█████████████████▌ | 3/12 [00:01<00:05, 1.77it/s]

Total patch 48: 33%|███████████████████████▎ | 4/12 [00:02<00:04, 1.77it/s]

Total patch 48: 42%|█████████████████████████████▏ | 5/12 [00:02<00:03, 1.78it/s]

Total patch 48: 50%|███████████████████████████████████ | 6/12 [00:03<00:03, 1.78it/s]

Total patch 48: 58%|████████████████████████████████████████▊ | 7/12 [00:03<00:02, 1.78it/s]

Total patch 48: 67%|██████████████████████████████████████████████▋ | 8/12 [00:04<00:02, 1.78it/s]

Total patch 48: 75%|████████████████████████████████████████████████████▌ | 9/12 [00:05<00:01, 1.79it/s]

Total patch 48: 83%|█████████████████████████████████████████████████████████▌ | 10/12 [00:05<00:01, 1.79it/s]

Total patch 48: 92%|███████████████████████████████████████████████████████████████▎ | 11/12 [00:06<00:00, 1.80it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.81it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.79it/s]

Total patch 48: 0%| | 0/12 [00:00<?, ?it/s]

Total patch 48: 8%|█████▊ | 1/12 [00:00<00:06, 1.74it/s]

Total patch 48: 17%|███████████▋ | 2/12 [00:01<00:05, 1.76it/s]

Total patch 48: 25%|█████████████████▌ | 3/12 [00:01<00:05, 1.78it/s]

Total patch 48: 33%|███████████████████████▎ | 4/12 [00:02<00:04, 1.78it/s]

Total patch 48: 42%|█████████████████████████████▏ | 5/12 [00:02<00:03, 1.79it/s]

Total patch 48: 50%|███████████████████████████████████ | 6/12 [00:03<00:03, 1.80it/s]

Total patch 48: 58%|████████████████████████████████████████▊ | 7/12 [00:03<00:02, 1.81it/s]

Total patch 48: 67%|██████████████████████████████████████████████▋ | 8/12 [00:04<00:02, 1.81it/s]

Total patch 48: 75%|████████████████████████████████████████████████████▌ | 9/12 [00:05<00:01, 1.82it/s]

Total patch 48: 83%|█████████████████████████████████████████████████████████▌ | 10/12 [00:05<00:01, 1.81it/s]

Total patch 48: 92%|███████████████████████████████████████████████████████████████▎ | 11/12 [00:06<00:00, 1.81it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.81it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.80it/s]

Total patch 48: 0%| | 0/12 [00:00<?, ?it/s]

Total patch 48: 8%|█████▊ | 1/12 [00:00<00:06, 1.74it/s]

Total patch 48: 17%|███████████▋ | 2/12 [00:01<00:05, 1.77it/s]

Total patch 48: 25%|█████████████████▌ | 3/12 [00:01<00:05, 1.77it/s]

Total patch 48: 33%|███████████████████████▎ | 4/12 [00:02<00:04, 1.77it/s]

Total patch 48: 42%|█████████████████████████████▏ | 5/12 [00:02<00:03, 1.78it/s]

Total patch 48: 50%|███████████████████████████████████ | 6/12 [00:03<00:03, 1.78it/s]

Total patch 48: 58%|████████████████████████████████████████▊ | 7/12 [00:03<00:02, 1.78it/s]

Total patch 48: 67%|██████████████████████████████████████████████▋ | 8/12 [00:04<00:02, 1.79it/s]

Total patch 48: 75%|████████████████████████████████████████████████████▌ | 9/12 [00:05<00:01, 1.79it/s]

Total patch 48: 83%|█████████████████████████████████████████████████████████▌ | 10/12 [00:05<00:01, 1.80it/s]

Total patch 48: 92%|███████████████████████████████████████████████████████████████▎ | 11/12 [00:06<00:00, 1.80it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.81it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.79it/s]

Total patch 48: 0%| | 0/12 [00:00<?, ?it/s]

Total patch 48: 8%|█████▊ | 1/12 [00:00<00:06, 1.77it/s]

Total patch 48: 17%|███████████▋ | 2/12 [00:01<00:05, 1.78it/s]

Total patch 48: 25%|█████████████████▌ | 3/12 [00:01<00:05, 1.77it/s]

Total patch 48: 33%|███████████████████████▎ | 4/12 [00:02<00:04, 1.77it/s]

Total patch 48: 42%|█████████████████████████████▏ | 5/12 [00:02<00:03, 1.77it/s]

Total patch 48: 50%|███████████████████████████████████ | 6/12 [00:03<00:03, 1.78it/s]

Total patch 48: 58%|████████████████████████████████████████▊ | 7/12 [00:03<00:02, 1.78it/s]

Total patch 48: 67%|██████████████████████████████████████████████▋ | 8/12 [00:04<00:02, 1.78it/s]

Total patch 48: 75%|████████████████████████████████████████████████████▌ | 9/12 [00:05<00:01, 1.79it/s]

Total patch 48: 83%|█████████████████████████████████████████████████████████▌ | 10/12 [00:05<00:01, 1.79it/s]

Total patch 48: 92%|███████████████████████████████████████████████████████████████▎ | 11/12 [00:06<00:00, 1.80it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.80it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.79it/s]

Total patch 48: 0%| | 0/12 [00:00<?, ?it/s]

Total patch 48: 8%|█████▊ | 1/12 [00:00<00:06, 1.74it/s]

Total patch 48: 17%|███████████▋ | 2/12 [00:01<00:05, 1.76it/s]

Total patch 48: 25%|█████████████████▌ | 3/12 [00:01<00:05, 1.76it/s]

Total patch 48: 33%|███████████████████████▎ | 4/12 [00:02<00:04, 1.76it/s]

Total patch 48: 42%|█████████████████████████████▏ | 5/12 [00:02<00:03, 1.77it/s]

Total patch 48: 50%|███████████████████████████████████ | 6/12 [00:03<00:03, 1.79it/s]

Total patch 48: 58%|████████████████████████████████████████▊ | 7/12 [00:03<00:02, 1.79it/s]

Total patch 48: 67%|██████████████████████████████████████████████▋ | 8/12 [00:04<00:02, 1.79it/s]

Total patch 48: 75%|████████████████████████████████████████████████████▌ | 9/12 [00:05<00:01, 1.79it/s]

Total patch 48: 83%|█████████████████████████████████████████████████████████▌ | 10/12 [00:05<00:01, 1.79it/s]

Total patch 48: 92%|███████████████████████████████████████████████████████████████▎ | 11/12 [00:06<00:00, 1.79it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.80it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.78it/s]

Total patch 48: 0%| | 0/12 [00:00<?, ?it/s]

Total patch 48: 8%|█████▊ | 1/12 [00:00<00:06, 1.74it/s]

Total patch 48: 17%|███████████▋ | 2/12 [00:01<00:05, 1.75it/s]

Total patch 48: 25%|█████████████████▌ | 3/12 [00:01<00:05, 1.75it/s]

Total patch 48: 33%|███████████████████████▎ | 4/12 [00:02<00:04, 1.76it/s]

Total patch 48: 42%|█████████████████████████████▏ | 5/12 [00:02<00:03, 1.76it/s]

Total patch 48: 50%|███████████████████████████████████ | 6/12 [00:03<00:03, 1.77it/s]

Total patch 48: 58%|████████████████████████████████████████▊ | 7/12 [00:03<00:02, 1.77it/s]

Total patch 48: 67%|██████████████████████████████████████████████▋ | 8/12 [00:04<00:02, 1.78it/s]

Total patch 48: 75%|████████████████████████████████████████████████████▌ | 9/12 [00:05<00:01, 1.78it/s]

Total patch 48: 83%|█████████████████████████████████████████████████████████▌ | 10/12 [00:05<00:01, 1.78it/s]

Total patch 48: 92%|███████████████████████████████████████████████████████████████▎ | 11/12 [00:06<00:00, 1.78it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.78it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.77it/s]

Total patch 48: 0%| | 0/12 [00:00<?, ?it/s]

Total patch 48: 8%|█████▊ | 1/12 [00:00<00:06, 1.74it/s]

Total patch 48: 17%|███████████▋ | 2/12 [00:01<00:05, 1.75it/s]

Total patch 48: 25%|█████████████████▌ | 3/12 [00:01<00:05, 1.76it/s]

Total patch 48: 33%|███████████████████████▎ | 4/12 [00:02<00:04, 1.77it/s]

Total patch 48: 42%|█████████████████████████████▏ | 5/12 [00:02<00:03, 1.77it/s]

Total patch 48: 50%|███████████████████████████████████ | 6/12 [00:03<00:03, 1.77it/s]

Total patch 48: 58%|████████████████████████████████████████▊ | 7/12 [00:03<00:02, 1.79it/s]

Total patch 48: 67%|██████████████████████████████████████████████▋ | 8/12 [00:04<00:02, 1.79it/s]

Total patch 48: 75%|████████████████████████████████████████████████████▌ | 9/12 [00:05<00:01, 1.81it/s]

Total patch 48: 83%|█████████████████████████████████████████████████████████▌ | 10/12 [00:05<00:01, 1.81it/s]

Total patch 48: 92%|███████████████████████████████████████████████████████████████▎ | 11/12 [00:06<00:00, 1.82it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.82it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.80it/s]

Epoch 2: Score = 0.1062

New best score! Model saved to brats.model.weights.h5
20/20 ━━━━━━━━━━━━━━━━━━━━ 159s 8s/step - dice: 0.2022 - dice_et: 0.1714 - dice_tc: 0.1672 - dice_wt: 0.2679 - loss: 1.3864

Epoch 3/4

20/20 ━━━━━━━━━━━━━━━━━━━━ 14s 440ms/step - dice: 0.4578 - dice_et: 0.4743 - dice_tc: 0.3668 - dice_wt: 0.5322 - loss: 1.2493

Epoch 4/4

20/20 ━━━━━━━━━━━━━━━━━━━━ 0s 427ms/step - dice: 0.4439 - dice_et: 0.4352 - dice_tc: 0.3364 - dice_wt: 0.5422 - loss: 1.2091

Epoch 4: Running inference...

Total patch 48: 0%| | 0/12 [00:00<?, ?it/s]

Total patch 48: 8%|█████▊ | 1/12 [00:00<00:07, 1.43it/s]

Total patch 48: 17%|███████████▋ | 2/12 [00:01<00:07, 1.42it/s]

Total patch 48: 25%|█████████████████▌ | 3/12 [00:01<00:05, 1.55it/s]

Total patch 48: 33%|███████████████████████▎ | 4/12 [00:02<00:04, 1.65it/s]

Total patch 48: 42%|█████████████████████████████▏ | 5/12 [00:03<00:04, 1.71it/s]

Total patch 48: 50%|███████████████████████████████████ | 6/12 [00:03<00:03, 1.75it/s]

Total patch 48: 58%|████████████████████████████████████████▊ | 7/12 [00:04<00:02, 1.77it/s]

Total patch 48: 67%|██████████████████████████████████████████████▋ | 8/12 [00:04<00:02, 1.79it/s]

Total patch 48: 75%|████████████████████████████████████████████████████▌ | 9/12 [00:05<00:01, 1.81it/s]

Total patch 48: 83%|█████████████████████████████████████████████████████████▌ | 10/12 [00:05<00:01, 1.81it/s]

Total patch 48: 92%|███████████████████████████████████████████████████████████████▎ | 11/12 [00:06<00:00, 1.82it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.82it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.74it/s]

Total patch 48: 0%| | 0/12 [00:00<?, ?it/s]

Total patch 48: 8%|█████▊ | 1/12 [00:00<00:06, 1.78it/s]

Total patch 48: 17%|███████████▋ | 2/12 [00:01<00:05, 1.79it/s]

Total patch 48: 25%|█████████████████▌ | 3/12 [00:01<00:05, 1.79it/s]

Total patch 48: 33%|███████████████████████▎ | 4/12 [00:02<00:04, 1.80it/s]

Total patch 48: 42%|█████████████████████████████▏ | 5/12 [00:02<00:03, 1.81it/s]

Total patch 48: 50%|███████████████████████████████████ | 6/12 [00:03<00:03, 1.80it/s]

Total patch 48: 58%|████████████████████████████████████████▊ | 7/12 [00:03<00:02, 1.80it/s]

Total patch 48: 67%|██████████████████████████████████████████████▋ | 8/12 [00:04<00:02, 1.81it/s]

Total patch 48: 75%|████████████████████████████████████████████████████▌ | 9/12 [00:04<00:01, 1.82it/s]

Total patch 48: 83%|█████████████████████████████████████████████████████████▌ | 10/12 [00:05<00:01, 1.82it/s]

Total patch 48: 92%|███████████████████████████████████████████████████████████████▎ | 11/12 [00:06<00:00, 1.83it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.83it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.81it/s]

Total patch 48: 0%| | 0/12 [00:00<?, ?it/s]

Total patch 48: 8%|█████▊ | 1/12 [00:00<00:06, 1.81it/s]

Total patch 48: 17%|███████████▋ | 2/12 [00:01<00:05, 1.81it/s]

Total patch 48: 25%|█████████████████▌ | 3/12 [00:01<00:04, 1.80it/s]

Total patch 48: 33%|███████████████████████▎ | 4/12 [00:02<00:04, 1.80it/s]

Total patch 48: 42%|█████████████████████████████▏ | 5/12 [00:02<00:03, 1.80it/s]

Total patch 48: 50%|███████████████████████████████████ | 6/12 [00:03<00:03, 1.80it/s]

Total patch 48: 58%|████████████████████████████████████████▊ | 7/12 [00:03<00:02, 1.80it/s]

Total patch 48: 67%|██████████████████████████████████████████████▋ | 8/12 [00:04<00:02, 1.81it/s]

Total patch 48: 75%|████████████████████████████████████████████████████▌ | 9/12 [00:04<00:01, 1.81it/s]

Total patch 48: 83%|█████████████████████████████████████████████████████████▌ | 10/12 [00:05<00:01, 1.82it/s]

Total patch 48: 92%|███████████████████████████████████████████████████████████████▎ | 11/12 [00:06<00:00, 1.82it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.82it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.81it/s]

Total patch 48: 0%| | 0/12 [00:00<?, ?it/s]

Total patch 48: 8%|█████▊ | 1/12 [00:00<00:06, 1.77it/s]

Total patch 48: 17%|███████████▋ | 2/12 [00:01<00:05, 1.78it/s]

Total patch 48: 25%|█████████████████▌ | 3/12 [00:01<00:05, 1.79it/s]

Total patch 48: 33%|███████████████████████▎ | 4/12 [00:02<00:04, 1.79it/s]

Total patch 48: 42%|█████████████████████████████▏ | 5/12 [00:02<00:03, 1.80it/s]

Total patch 48: 50%|███████████████████████████████████ | 6/12 [00:03<00:03, 1.81it/s]

Total patch 48: 58%|████████████████████████████████████████▊ | 7/12 [00:03<00:02, 1.80it/s]

Total patch 48: 67%|██████████████████████████████████████████████▋ | 8/12 [00:04<00:02, 1.80it/s]

Total patch 48: 75%|████████████████████████████████████████████████████▌ | 9/12 [00:05<00:01, 1.81it/s]

Total patch 48: 83%|█████████████████████████████████████████████████████████▌ | 10/12 [00:05<00:01, 1.81it/s]

Total patch 48: 92%|███████████████████████████████████████████████████████████████▎ | 11/12 [00:06<00:00, 1.81it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.80it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.80it/s]

Total patch 48: 0%| | 0/12 [00:00<?, ?it/s]

Total patch 48: 8%|█████▊ | 1/12 [00:00<00:06, 1.76it/s]

Total patch 48: 17%|███████████▋ | 2/12 [00:01<00:05, 1.76it/s]

Total patch 48: 25%|█████████████████▌ | 3/12 [00:01<00:05, 1.76it/s]

Total patch 48: 33%|███████████████████████▎ | 4/12 [00:02<00:04, 1.76it/s]

Total patch 48: 42%|█████████████████████████████▏ | 5/12 [00:02<00:03, 1.76it/s]

Total patch 48: 50%|███████████████████████████████████ | 6/12 [00:03<00:03, 1.77it/s]

Total patch 48: 58%|████████████████████████████████████████▊ | 7/12 [00:03<00:02, 1.77it/s]

Total patch 48: 67%|██████████████████████████████████████████████▋ | 8/12 [00:04<00:02, 1.79it/s]

Total patch 48: 75%|████████████████████████████████████████████████████▌ | 9/12 [00:05<00:01, 1.79it/s]

Total patch 48: 83%|█████████████████████████████████████████████████████████▌ | 10/12 [00:05<00:01, 1.79it/s]

Total patch 48: 92%|███████████████████████████████████████████████████████████████▎ | 11/12 [00:06<00:00, 1.79it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.79it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.78it/s]

Total patch 48: 0%| | 0/12 [00:00<?, ?it/s]

Total patch 48: 8%|█████▊ | 1/12 [00:00<00:06, 1.77it/s]

Total patch 48: 17%|███████████▋ | 2/12 [00:01<00:05, 1.78it/s]

Total patch 48: 25%|█████████████████▌ | 3/12 [00:01<00:05, 1.77it/s]

Total patch 48: 33%|███████████████████████▎ | 4/12 [00:02<00:04, 1.78it/s]

Total patch 48: 42%|█████████████████████████████▏ | 5/12 [00:02<00:03, 1.78it/s]

Total patch 48: 50%|███████████████████████████████████ | 6/12 [00:03<00:03, 1.78it/s]

Total patch 48: 58%|████████████████████████████████████████▊ | 7/12 [00:03<00:02, 1.78it/s]

Total patch 48: 67%|██████████████████████████████████████████████▋ | 8/12 [00:04<00:02, 1.78it/s]

Total patch 48: 75%|████████████████████████████████████████████████████▌ | 9/12 [00:05<00:01, 1.79it/s]

Total patch 48: 83%|█████████████████████████████████████████████████████████▌ | 10/12 [00:05<00:01, 1.79it/s]

Total patch 48: 92%|███████████████████████████████████████████████████████████████▎ | 11/12 [00:06<00:00, 1.79it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.79it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.78it/s]

Total patch 48: 0%| | 0/12 [00:00<?, ?it/s]

Total patch 48: 8%|█████▊ | 1/12 [00:00<00:06, 1.76it/s]

Total patch 48: 17%|███████████▋ | 2/12 [00:01<00:05, 1.77it/s]

Total patch 48: 25%|█████████████████▌ | 3/12 [00:01<00:05, 1.76it/s]

Total patch 48: 33%|███████████████████████▎ | 4/12 [00:02<00:04, 1.77it/s]

Total patch 48: 42%|█████████████████████████████▏ | 5/12 [00:02<00:03, 1.77it/s]

Total patch 48: 50%|███████████████████████████████████ | 6/12 [00:03<00:03, 1.77it/s]

Total patch 48: 58%|████████████████████████████████████████▊ | 7/12 [00:03<00:02, 1.77it/s]

Total patch 48: 67%|██████████████████████████████████████████████▋ | 8/12 [00:04<00:02, 1.78it/s]

Total patch 48: 75%|████████████████████████████████████████████████████▌ | 9/12 [00:05<00:01, 1.79it/s]

Total patch 48: 83%|█████████████████████████████████████████████████████████▌ | 10/12 [00:05<00:01, 1.79it/s]

Total patch 48: 92%|███████████████████████████████████████████████████████████████▎ | 11/12 [00:06<00:00, 1.79it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.80it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.78it/s]

Total patch 48: 0%| | 0/12 [00:00<?, ?it/s]

Total patch 48: 8%|█████▊ | 1/12 [00:00<00:06, 1.80it/s]

Total patch 48: 17%|███████████▋ | 2/12 [00:01<00:05, 1.80it/s]

Total patch 48: 25%|█████████████████▌ | 3/12 [00:01<00:05, 1.80it/s]

Total patch 48: 33%|███████████████████████▎ | 4/12 [00:02<00:04, 1.80it/s]

Total patch 48: 42%|█████████████████████████████▏ | 5/12 [00:02<00:03, 1.80it/s]

Total patch 48: 50%|███████████████████████████████████ | 6/12 [00:03<00:03, 1.80it/s]

Total patch 48: 58%|████████████████████████████████████████▊ | 7/12 [00:03<00:02, 1.79it/s]

Total patch 48: 67%|██████████████████████████████████████████████▋ | 8/12 [00:04<00:02, 1.79it/s]

Total patch 48: 75%|████████████████████████████████████████████████████▌ | 9/12 [00:05<00:01, 1.79it/s]

Total patch 48: 83%|█████████████████████████████████████████████████████████▌ | 10/12 [00:05<00:01, 1.79it/s]

Total patch 48: 92%|███████████████████████████████████████████████████████████████▎ | 11/12 [00:06<00:00, 1.80it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.81it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.80it/s]

Epoch 4: Score = 0.2053

New best score! Model saved to brats.model.weights.h5
20/20 ━━━━━━━━━━━━━━━━━━━━ 78s 4s/step - dice: 0.4848 - dice_et: 0.4880 - dice_tc: 0.4305 - dice_wt: 0.5359 - loss: 1.1825

Let’s take a quick look at how our model performed during training. We will first print the available metrics recorded in the training history, save them to a CSV file for future reference, and then visualize them to better understand the model’s learning progress over epochs.

def plot_training_history(history_df):
    metrics = history_df.columns
    n_metrics = len(metrics)

    n_rows = 2
    n_cols = (n_metrics + 1) // 2  # ceiling division for columns

    plt.figure(figsize=(5 * n_cols, 5 * n_rows))

    for idx, metric in enumerate(metrics):
        plt.subplot(n_rows, n_cols, idx + 1)
        plt.plot(history_df[metric], label=metric, marker="o")
        plt.title(metric)
        plt.xlabel("Epoch")
        plt.ylabel("Value")
        plt.grid(True)
        plt.legend()

    plt.tight_layout()
    plt.show()


print(model.history.history.keys())
his_csv = pd.DataFrame(model.history.history)
his_csv.to_csv("brats.history.csv")
plot_training_history(his_csv)
dict_keys(['dice', 'dice_et', 'dice_tc', 'dice_wt', 'loss'])

png


Evaluation

In this Kaggle notebook (version 5), we trained the model on the entire dataset for approximately 30 epochs. The resulting weights will be used for further evaluation. Note that the validation set used in both here and Kaggle notebook are the same: training_shard_36.tfrec, which contains 8 samples.

model_weight = kagglehub.model_download(
    "ipythonx/bratsmodel/keras/default", path="brats.model.weights.h5"
)
print("\nPath to model files:", model_weight)

model.load_weights(model_weight)
Path to model files: /home/jupyter/.cache/kagglehub/models/ipythonx/bratsmodel/keras/default/3/brats.model.weights.h5

In this section, we perform sliding window inference on the validation dataset and compute Dice scores for overall segmentation quality as well as specific tumor subregions: - Tumor Core (TC) - Whole Tumor (WT) - Enhancing Tumor (ET)

swi = SlidingWindowInference(
    model,
    num_classes=num_classes,
    roi_size=input_shape[:3],
    sw_batch_size=4,
    overlap=0.5,
    mode="gaussian",
)

dice = BinaryDiceMetric(
    from_logits=True,
    ignore_empty=True,
    num_classes=num_classes,
    name="dice",
)
dice_tc = BinaryDiceMetric(
    from_logits=True,
    ignore_empty=True,
    target_class_ids=[0],
    num_classes=num_classes,
    name="dice_tc",
)
dice_wt = BinaryDiceMetric(
    from_logits=True,
    ignore_empty=True,
    target_class_ids=[1],
    num_classes=num_classes,
    name="dice_wt",
)
dice_et = BinaryDiceMetric(
    from_logits=True,
    ignore_empty=True,
    target_class_ids=[2],
    num_classes=num_classes,
    name="dice_et",
)

Due to the variable size, and larger size of the validation data, we iterate over the validation dataloader. The sliding window inference handles input patches and computes the predictions for each batch.

dice.reset_state()
dice_tc.reset_state()
dice_wt.reset_state()
dice_et.reset_state()

for sample in val_ds:
    x, y = sample
    output = swi(x)
    dice.update_state(y, output)
    dice_tc.update_state(y, output)
    dice_wt.update_state(y, output)
    dice_et.update_state(y, output)

dice_score = float(ops.convert_to_numpy(dice.result()))
dice_score_tc = float(ops.convert_to_numpy(dice_tc.result()))
dice_score_wt = float(ops.convert_to_numpy(dice_wt.result()))
dice_score_et = float(ops.convert_to_numpy(dice_et.result()))

print(f"Dice Score: {dice_score:.4f}")
print(f"Dice Score on tumor core (TC): {dice_score_tc:.4f}")
print(f"Dice Score on whole tumor (WT): {dice_score_wt:.4f}")
print(f"Dice Score on enhancing tumor (ET): {dice_score_et:.4f}")

Total patch 48: 0%| | 0/12 [00:00<?, ?it/s]

Total patch 48: 8%|█████▊ | 1/12 [00:00<00:08, 1.23it/s]

Total patch 48: 17%|███████████▋ | 2/12 [00:01<00:07, 1.36it/s]

Total patch 48: 25%|█████████████████▌ | 3/12 [00:02<00:05, 1.51it/s]

Total patch 48: 33%|███████████████████████▎ | 4/12 [00:02<00:05, 1.60it/s]

Total patch 48: 42%|█████████████████████████████▏ | 5/12 [00:03<00:04, 1.65it/s]

Total patch 48: 50%|███████████████████████████████████ | 6/12 [00:03<00:03, 1.69it/s]

Total patch 48: 58%|████████████████████████████████████████▊ | 7/12 [00:04<00:02, 1.71it/s]

Total patch 48: 67%|██████████████████████████████████████████████▋ | 8/12 [00:04<00:02, 1.74it/s]

Total patch 48: 75%|████████████████████████████████████████████████████▌ | 9/12 [00:05<00:01, 1.75it/s]

Total patch 48: 83%|█████████████████████████████████████████████████████████▌ | 10/12 [00:06<00:01, 1.77it/s]

Total patch 48: 92%|███████████████████████████████████████████████████████████████▎ | 11/12 [00:06<00:00, 1.78it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:07<00:00, 1.78it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:07<00:00, 1.68it/s]

Total patch 48: 0%| | 0/12 [00:00<?, ?it/s]

Total patch 48: 8%|█████▊ | 1/12 [00:00<00:06, 1.75it/s]

Total patch 48: 17%|███████████▋ | 2/12 [00:01<00:05, 1.78it/s]

Total patch 48: 25%|█████████████████▌ | 3/12 [00:01<00:05, 1.76it/s]

Total patch 48: 33%|███████████████████████▎ | 4/12 [00:02<00:04, 1.76it/s]

Total patch 48: 42%|█████████████████████████████▏ | 5/12 [00:02<00:03, 1.76it/s]

Total patch 48: 50%|███████████████████████████████████ | 6/12 [00:03<00:03, 1.77it/s]

Total patch 48: 58%|████████████████████████████████████████▊ | 7/12 [00:03<00:02, 1.77it/s]

Total patch 48: 67%|██████████████████████████████████████████████▋ | 8/12 [00:04<00:02, 1.77it/s]

Total patch 48: 75%|████████████████████████████████████████████████████▌ | 9/12 [00:05<00:01, 1.78it/s]

Total patch 48: 83%|█████████████████████████████████████████████████████████▌ | 10/12 [00:05<00:01, 1.78it/s]

Total patch 48: 92%|███████████████████████████████████████████████████████████████▎ | 11/12 [00:06<00:00, 1.78it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.78it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.77it/s]

Total patch 48: 0%| | 0/12 [00:00<?, ?it/s]

Total patch 48: 8%|█████▊ | 1/12 [00:00<00:06, 1.70it/s]

Total patch 48: 17%|███████████▋ | 2/12 [00:01<00:05, 1.71it/s]

Total patch 48: 25%|█████████████████▌ | 3/12 [00:01<00:05, 1.72it/s]

Total patch 48: 33%|███████████████████████▎ | 4/12 [00:02<00:04, 1.72it/s]

Total patch 48: 42%|█████████████████████████████▏ | 5/12 [00:02<00:04, 1.73it/s]

Total patch 48: 50%|███████████████████████████████████ | 6/12 [00:03<00:03, 1.73it/s]

Total patch 48: 58%|████████████████████████████████████████▊ | 7/12 [00:04<00:02, 1.73it/s]

Total patch 48: 67%|██████████████████████████████████████████████▋ | 8/12 [00:04<00:02, 1.74it/s]

Total patch 48: 75%|████████████████████████████████████████████████████▌ | 9/12 [00:05<00:01, 1.74it/s]

Total patch 48: 83%|█████████████████████████████████████████████████████████▌ | 10/12 [00:05<00:01, 1.75it/s]

Total patch 48: 92%|███████████████████████████████████████████████████████████████▎ | 11/12 [00:06<00:00, 1.77it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.79it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.75it/s]

Total patch 48: 0%| | 0/12 [00:00<?, ?it/s]

Total patch 48: 8%|█████▊ | 1/12 [00:00<00:06, 1.76it/s]

Total patch 48: 17%|███████████▋ | 2/12 [00:01<00:05, 1.77it/s]

Total patch 48: 25%|█████████████████▌ | 3/12 [00:01<00:05, 1.78it/s]

Total patch 48: 33%|███████████████████████▎ | 4/12 [00:02<00:04, 1.77it/s]

Total patch 48: 42%|█████████████████████████████▏ | 5/12 [00:02<00:03, 1.76it/s]

Total patch 48: 50%|███████████████████████████████████ | 6/12 [00:03<00:03, 1.76it/s]

Total patch 48: 58%|████████████████████████████████████████▊ | 7/12 [00:03<00:02, 1.76it/s]

Total patch 48: 67%|██████████████████████████████████████████████▋ | 8/12 [00:04<00:02, 1.75it/s]

Total patch 48: 75%|████████████████████████████████████████████████████▌ | 9/12 [00:05<00:01, 1.75it/s]

Total patch 48: 83%|█████████████████████████████████████████████████████████▌ | 10/12 [00:05<00:01, 1.75it/s]

Total patch 48: 92%|███████████████████████████████████████████████████████████████▎ | 11/12 [00:06<00:00, 1.75it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.75it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.76it/s]

Total patch 48: 0%| | 0/12 [00:00<?, ?it/s]

Total patch 48: 8%|█████▊ | 1/12 [00:00<00:06, 1.74it/s]

Total patch 48: 17%|███████████▋ | 2/12 [00:01<00:05, 1.78it/s]

Total patch 48: 25%|█████████████████▌ | 3/12 [00:01<00:05, 1.78it/s]

Total patch 48: 33%|███████████████████████▎ | 4/12 [00:02<00:04, 1.78it/s]

Total patch 48: 42%|█████████████████████████████▏ | 5/12 [00:02<00:03, 1.77it/s]

Total patch 48: 50%|███████████████████████████████████ | 6/12 [00:03<00:03, 1.76it/s]

Total patch 48: 58%|████████████████████████████████████████▊ | 7/12 [00:03<00:02, 1.75it/s]

Total patch 48: 67%|██████████████████████████████████████████████▋ | 8/12 [00:04<00:02, 1.75it/s]

Total patch 48: 75%|████████████████████████████████████████████████████▌ | 9/12 [00:05<00:01, 1.75it/s]

Total patch 48: 83%|█████████████████████████████████████████████████████████▌ | 10/12 [00:05<00:01, 1.75it/s]

Total patch 48: 92%|███████████████████████████████████████████████████████████████▎ | 11/12 [00:06<00:00, 1.76it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.77it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.76it/s]

Total patch 48: 0%| | 0/12 [00:00<?, ?it/s]

Total patch 48: 8%|█████▊ | 1/12 [00:00<00:06, 1.70it/s]

Total patch 48: 17%|███████████▋ | 2/12 [00:01<00:05, 1.72it/s]

Total patch 48: 25%|█████████████████▌ | 3/12 [00:01<00:05, 1.73it/s]

Total patch 48: 33%|███████████████████████▎ | 4/12 [00:02<00:04, 1.73it/s]

Total patch 48: 42%|█████████████████████████████▏ | 5/12 [00:02<00:04, 1.72it/s]

Total patch 48: 50%|███████████████████████████████████ | 6/12 [00:03<00:03, 1.73it/s]

Total patch 48: 58%|████████████████████████████████████████▊ | 7/12 [00:04<00:02, 1.72it/s]

Total patch 48: 67%|██████████████████████████████████████████████▋ | 8/12 [00:04<00:02, 1.73it/s]

Total patch 48: 75%|████████████████████████████████████████████████████▌ | 9/12 [00:05<00:01, 1.73it/s]

Total patch 48: 83%|█████████████████████████████████████████████████████████▌ | 10/12 [00:05<00:01, 1.74it/s]

Total patch 48: 92%|███████████████████████████████████████████████████████████████▎ | 11/12 [00:06<00:00, 1.75it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.76it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.74it/s]

Total patch 48: 0%| | 0/12 [00:00<?, ?it/s]

Total patch 48: 8%|█████▊ | 1/12 [00:00<00:06, 1.71it/s]

Total patch 48: 17%|███████████▋ | 2/12 [00:01<00:05, 1.77it/s]

Total patch 48: 25%|█████████████████▌ | 3/12 [00:01<00:05, 1.79it/s]

Total patch 48: 33%|███████████████████████▎ | 4/12 [00:02<00:04, 1.78it/s]

Total patch 48: 42%|█████████████████████████████▏ | 5/12 [00:02<00:03, 1.78it/s]

Total patch 48: 50%|███████████████████████████████████ | 6/12 [00:03<00:03, 1.76it/s]

Total patch 48: 58%|████████████████████████████████████████▊ | 7/12 [00:03<00:02, 1.75it/s]

Total patch 48: 67%|██████████████████████████████████████████████▋ | 8/12 [00:04<00:02, 1.74it/s]

Total patch 48: 75%|████████████████████████████████████████████████████▌ | 9/12 [00:05<00:01, 1.75it/s]

Total patch 48: 83%|█████████████████████████████████████████████████████████▌ | 10/12 [00:05<00:01, 1.75it/s]

Total patch 48: 92%|███████████████████████████████████████████████████████████████▎ | 11/12 [00:06<00:00, 1.75it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.75it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.75it/s]

Total patch 48: 0%| | 0/12 [00:00<?, ?it/s]

Total patch 48: 8%|█████▊ | 1/12 [00:00<00:06, 1.69it/s]

Total patch 48: 17%|███████████▋ | 2/12 [00:01<00:05, 1.71it/s]

Total patch 48: 25%|█████████████████▌ | 3/12 [00:01<00:05, 1.71it/s]

Total patch 48: 33%|███████████████████████▎ | 4/12 [00:02<00:04, 1.71it/s]

Total patch 48: 42%|█████████████████████████████▏ | 5/12 [00:02<00:04, 1.71it/s]

Total patch 48: 50%|███████████████████████████████████ | 6/12 [00:03<00:03, 1.72it/s]

Total patch 48: 58%|████████████████████████████████████████▊ | 7/12 [00:04<00:02, 1.72it/s]

Total patch 48: 67%|██████████████████████████████████████████████▋ | 8/12 [00:04<00:02, 1.72it/s]

Total patch 48: 75%|████████████████████████████████████████████████████▌ | 9/12 [00:05<00:01, 1.73it/s]

Total patch 48: 83%|█████████████████████████████████████████████████████████▌ | 10/12 [00:05<00:01, 1.74it/s]

Total patch 48: 92%|███████████████████████████████████████████████████████████████▎ | 11/12 [00:06<00:00, 1.74it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.74it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.73it/s]

Dice Score: 0.7939
Dice Score on tumor core (TC): 0.6186
Dice Score on whole tumor (WT): 0.9184
Dice Score on enhancing tumor (ET): 0.8446

Analyse and Visualize

Let's analyse the model predictions and visualize them. First, we will implement the test transformation pipeline. This is same as validation transformation.

def test_transformation(sample):
    return val_transformation(sample)

Let's load the tfrecord file and check its properties.

index = 0
dataset = tf.data.TFRecordDataset(val_datalist[index])
dataset = dataset.map(parse_tfrecord_fn, num_parallel_calls=tf.data.AUTOTUNE)
dataset = dataset.map(rearrange_shape, num_parallel_calls=tf.data.AUTOTUNE)

sample = next(iter(dataset))
orig_image = sample["image"]
orig_label = sample["label"]
orig_affine = sample["affine"]
print(orig_image.shape, orig_label.shape, orig_affine.shape, np.unique(orig_label))
(155, 240, 240, 4) (155, 240, 240) (4, 4) [0. 1. 2. 4.]

Run the transformation to prepare the inputs.

pre_image, pre_label = test_transformation(sample)
print(pre_image.shape, pre_label.shape)
(155, 240, 240, 4) (155, 240, 240, 3)

Pass the preprocessed sample to the inference object, ensuring that a batch axis is added to the input beforehand.

y_pred = swi(pre_image[None, ...])
print(y_pred.shape)

Total patch 48: 0%| | 0/12 [00:00<?, ?it/s]

Total patch 48: 8%|█████▊ | 1/12 [00:00<00:06, 1.58it/s]

Total patch 48: 17%|███████████▋ | 2/12 [00:01<00:05, 1.69it/s]

Total patch 48: 25%|█████████████████▌ | 3/12 [00:01<00:05, 1.71it/s]

Total patch 48: 33%|███████████████████████▎ | 4/12 [00:02<00:04, 1.73it/s]

Total patch 48: 42%|█████████████████████████████▏ | 5/12 [00:02<00:04, 1.74it/s]

Total patch 48: 50%|███████████████████████████████████ | 6/12 [00:03<00:03, 1.74it/s]

Total patch 48: 58%|████████████████████████████████████████▊ | 7/12 [00:04<00:02, 1.75it/s]

Total patch 48: 67%|██████████████████████████████████████████████▋ | 8/12 [00:04<00:02, 1.76it/s]

Total patch 48: 75%|████████████████████████████████████████████████████▌ | 9/12 [00:05<00:01, 1.76it/s]

Total patch 48: 83%|█████████████████████████████████████████████████████████▌ | 10/12 [00:05<00:01, 1.77it/s]

Total patch 48: 92%|███████████████████████████████████████████████████████████████▎ | 11/12 [00:06<00:00, 1.77it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.78it/s]

Total patch 48: 100%|█████████████████████████████████████████████████████████████████████| 12/12 [00:06<00:00, 1.75it/s]

(1, 155, 240, 240, 3)

After running inference, we remove the batch dimension and apply a sigmoid activation to obtain class probabilities. We then threshold the probabilities at 0.5 to generate the final binary segmentation map.

y_pred_logits = y_pred.squeeze(axis=0)
y_pred_prob = ops.convert_to_numpy(ops.sigmoid(y_pred_logits))
segment = (y_pred_prob > 0.5).astype(int)
print(segment.shape, np.unique(segment))
(155, 240, 240, 3) [0 1]

We compare the ground truth (pre_label) and the predicted segmentation (segment) for each tumor sub-region. Each sub-plot shows a specific channel corresponding to a tumor type: TC, WT, and ET. Here we visualize the 80th axial slice across the three channels.

label_map = {0: "TC", 1: "WT", 2: "ET"}

plt.figure(figsize=(16, 4))
for i in range(pre_label.shape[-1]):
    plt.subplot(1, 3, i + 1)
    plt.title(f"label channel {label_map[i]}")
    plt.imshow(pre_label[80, :, :, i])
plt.show()

plt.figure(figsize=(16, 4))
for i in range(3):
    plt.subplot(1, 3, i + 1)
    plt.title(f"pred channel {label_map[i]}")
    plt.imshow(segment[80, :, :, i])
plt.show()

png

png

The predicted output is a multi-channel binary map, where each channel corresponds to a specific tumor region. To visualize it against the original ground truth, we convert it into a single-channel label map. Here we assign: - Label 1 for Tumor Core (TC) - Label 2 for Whole Tumor (WT) - Label 4 for Enhancing Tumor (ET) The label values are chosen to match typical conventions used in medical segmentation benchmarks like BraTS.

prediction = np.zeros(
    (segment.shape[0], segment.shape[1], segment.shape[2]), dtype="float32"
)
prediction[segment[..., 1] == 1] = 2
prediction[segment[..., 0] == 1] = 1
prediction[segment[..., 2] == 1] = 4

print("label ", orig_label.shape, np.unique(orig_label))
print("predicted ", prediction.shape, np.unique(prediction))
label  (155, 240, 240) [0. 1. 2. 4.]
predicted  (155, 240, 240) [0. 1. 2. 4.]

Let's begin by examining the original input slices from the MRI scan. The input contains four channels corresponding to different MRI modalities: - FLAIR - T1 - T1CE (T1 with contrast enhancement) - T2 We display the same slice number across all modalities for comparison.

slice_map = {0: "flair", 1: "t1", 2: "t1ce", 3: "t2"}
slice_num = 75

plt.figure(figsize=(16, 4))
for i in range(orig_image.shape[-1]):
    plt.subplot(1, 4, i + 1)
    plt.title(f"Original channel: {slice_map[i]}")
    plt.imshow(orig_image[slice_num, :, :, i], cmap="gray")

plt.tight_layout()
plt.show()

png

Next, we compare this input with the ground truth label and the predicted segmentation on the same slice. This provides visual insight into how well the model has localized and segmented the tumor regions.

num_channels = orig_image.shape[-1]
plt.figure("image", (15, 15))

# plotting image, label and prediction
plt.subplot(3, num_channels, num_channels + 1)
plt.title("image")
plt.imshow(orig_image[slice_num, :, :, 0], cmap="gray")

plt.subplot(3, num_channels, num_channels + 2)
plt.title("label")
plt.imshow(orig_label[slice_num, :, :])

plt.subplot(3, num_channels, num_channels + 3)
plt.title("prediction")
plt.imshow(prediction[slice_num, :, :])

plt.tight_layout()
plt.show()

png


Additional Resources