About the Example MoNuSeg Dataset
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.
- The train dataset (images and annotations) can be downloaded from https://drive. google.com/file/d/1ZgqFJomqQGNnsx7w7QBzQQMVA16lbVCA/view
- Test set images with additional 7000 nuclear boundary annotations are available here MoNuSeg 2018 Testing data. Please cite the following papers if you use the training and testing datasets of this challenge: The test dataset can be downloaded from https://drive.google.com/file/d/1NKkSQ5T0ZNQ8aUhh0a8Dt2YKYCQXIViw/view
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.)
Data preprocessing
- Initialize
EnsembleLearner
- Plot images and masks to show if they are correctly loaded
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
- Calculate similarity scores (Dice score) on the test set
- Only show two example files
_ = 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
- Calculate similarity scores (Dice score) on the test set
- Only show two example files
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']])