--- title: Code-based tutorial for semantic and instance segmentation on the MoNuSeg dataset keywords: fastai sidebar: home_sidebar nb_path: "nbs/tutorial_monuseg.ipynb" ---
from https://monuseg.grand-challenge.org: Training data containing 30 images and around 22,000 nuclear boundary annotations has been released to the public previously as a dataset article in IEEE Transactions on Medical imaging in 2017.
How to download and preprocess the data can be found in the corresponding Notebook.
!pip install -Uqq deepflash2
import numpy as np
from deepflash2.all import *
from pathlib import Path
Prior to training and predicting directorys need to be specified and parameters need to be set. For convenience exissting Google Drive folders can be used.
# Connect to drive
try:
from google.colab import drive
drive.mount('/gdrive')
except:
print('Google Drive is not available.')
SEED = 0 # We used seeds [0,1,2] in our experiemnts
OUTPUT_PATH = Path("/content/predictions") # Save predictions here
MODEL_PATH = Path("/content/models") # Save models here
TRAINED_MODEL_PATH= Path('/gdrive/MyDrive/deepflash2-paper/models/')
DATA_PATH = Path('/gdrive/MyDrive/deepflash2-paper/data')
#################### Parameters ####################
DATASET = 'monuseg'
mask_directory='masks_preprocessed'
# Datasets have different numbers of classes - 2 in case of monuseg
num_classes = 2
# Diameters are calculated using the median sizes from the respective training sets - 21 in case of monuseg
diameter = 21
# Create deepflash2 config class
cfg = Config(random_state=SEED,
num_classes=num_classes, scale= 1.)
EnsembleLearner
train_data_path = DATA_PATH/DATASET/'train'
ensemble_path = MODEL_PATH/DATASET/f'{SEED+1}'
el = EnsembleLearner(image_dir='images',
mask_dir=mask_directory,
config=cfg,
path=train_data_path,
ensemble_path=ensemble_path)
el.ds.show_data(max_n=2)
el.fit_ensemble()
test_data_path = DATA_PATH/DATASET/'test'
# Use the trained model from our paper
ensemble_name = f'{DATASET}_ensemble_{SEED+1}.pt'
ensemble_trained_dir = Path("/content/trained_models")/DATASET
ensemble_trained_dir.mkdir(exist_ok=True, parents=True)
ensemble_trained_path = ensemble_trained_dir/ensemble_name
!wget -O {ensemble_trained_path.as_posix()} https://github.com/matjesg/deepflash2/releases/download/model_library/{ensemble_name}
# Uncomment to use your own trained model from the section above
# ensemble_trained_path = ensemble_path
# Save the predictions here
prediction_path = OUTPUT_PATH/DATASET/f'{SEED+1}'
cfg.instance_labels = True # Test masks are saved as instance labels
ep = EnsemblePredictor('images',
'masks', # Uncomment if no masks are avaible
path=test_data_path,
config=cfg,
ensemble_path=ensemble_trained_path)
Predict, save, and show semantic segmentation masks
_ = ep.get_ensemble_results(export_dir=prediction_path)
_ = ep.score_ensemble_results()
ep.show_ensemble_results(files=['TCGA-2Z-A9J9-01A-01-TS1.tif', 'TCGA-44-2665-01B-06-BS6.tif'])
Predict, save, and show instance segmentation masks
ep.config.cellpose_diameter=diameter
_ = ep.get_cellpose_results(export_dir=prediction_path)
_ = ep.score_cellpose_results()
ep.show_cellpose_results(files=['TCGA-2Z-A9J9-01A-01-TS1.tif', 'TCGA-44-2665-01B-06-BS6.tif'])
Save and show results table
ep.df_ens.to_csv(prediction_path/'uncertainty_scores.csv', index=False)
display(ep.df_ens[['file', 'dice_score', 'mAP_class1', 'uncertainty_score']])