Skip to main content

mn-DINO: [add description of the project later]

Project description

mn-DINO

Detecting micronuclei in images using DINOv2 backbone

Refer to tutorial notebook for real examples

Install package

pip install mndino

Load the model

import torch
from mndino import mnmodel
from huggingface_hub import hf_hub_download

model_path = hf_hub_download(repo_id="yifanren/mn-DINO", filename="latest.pth")
device = "cuda" if torch.cuda.is_available() else "cpu"
model = mnmodel.MicronucleiModel(device=device)
model.load(model_path)

Make predictions

import skimage
import numpy as np

STEP = 32 # recommended value
PREDICTION_BATCH = 4
THRESHOLD = 0.5

im = skimage.io.imread(your_image_path)
im = np.array((im - np.min(im))/(np.max(im) - np.min(im)), dtype="float32") # normalize image
probabilities = model.predict(im, stride=1, step=STEP, batch_size=PREDICTION_BATCH)

mn_predictions = probabilities[0,:,:] > THRESHOLD
nuclei_predictions = probabilities[1,:,:] > THRESHOLD

Evaluation

import skimage
from mndino import evaluation

mn_gt = skimage.io.imread(your_annotated_image_path)
evaluation.segmentation_report(imid='My_Image', predictions=mn_predictions, gt=mn_gt, intersection_ratio=0.1)

Reproducing mndino Training & Evaluation Experiments

git clone git@github.com:CaicedoLab/micronuclei-detection.git
cd micronuclei-detection

Training

python3 training_model.py --path '/scr/data/annotated_mn_datasets/' --gpu 0 --epochs 20 --batch_size 4 --loss_fn 'combined' --lr 1e-5 --scale 1.0 --gaussian

Making Predictions on Test Set

python3 prediction.py --path '/scr/data/annotated_mn_datasets/' --test_set  --gpu 0 --step 32 --batch_size 4 --prob_threshold 0.5 --iou_threshold 0.1 --scale 1

Turn on --test_set if user wants to evaluate on test set, turn it off to select validation set.
Turn on --wandb_mode if user wants to show loss on Weights and Biases

Train your own specialist model

  • Expected file extension of training images and nuclei masks is .tif, the corresponding training masks is .png. Following values are tunable if retraining on non-micronucleus subcellular datasets.
  • Combined loss = 0.8 * subcellular loss + 0.2 * nuclei loss.
device = f"cuda:{gpu}" if torch.cuda.is_available() else 'cpu'
model = mnmodel.MicronucleiModel(
    device=device,
    data_dir=DIRECTORY,
    patch_size=256,
    scale_factor=1.0,
    gaussian=True,
    oversample=False # oversample option is only applied to the micronuclei dataset presented in manuscript
)

model.train(epochs=20, 
            batch_size=4, 
            learning_rate=1e-5, 
            loss_fn='combined',
            weight_decay=1e-6,
            wandb_mode=False
)

model.save(outdir=OUTPUT_DIR, model_name=MODEL_NAME)

Reproducing Baseline experiments

MNFinder Evaluation

# Run reformant mnfinder images script
python3 mnfinder_prediction.py --test_path '/scr/data/annotated_mn_datasets/test/images/' --save_path '/scr/data/annotated_mn_datasets/mnfinder_predictions/' --wandb_mode

# Retraining Cellpose3
python3 cellpose_prediction.py --gpu 0 --train_path '/scr/data/annotated_mn_datasets/train/images/' --save_path '/scr/data/annotated_mn_datasets/cellpose_predictions/' --finetune --wandb_mode

use frozen microSAM (better performance)

python3 reformat_microsam_images.py --load_path '/scr/yren/annotated_mn_datasets/' --save_path '/scr/yren/microsam_data/'

python3 microsam_prediction.py --gpu 0 --train_path '/scr/data/microsam_data/train/' --pred_path '/scr/data/annotated_mn_datasets/test/images/' --save_path '/scr/yren/microsam_data/microsam_predictions/' --frozen --wandb_mode

Turn on --frozen if user wants to use frozen microSAM backbone to make predictions

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mndino-0.0.0.2.tar.gz (20.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mndino-0.0.0.2-py3-none-any.whl (22.8 kB view details)

Uploaded Python 3

File details

Details for the file mndino-0.0.0.2.tar.gz.

File metadata

  • Download URL: mndino-0.0.0.2.tar.gz
  • Upload date:
  • Size: 20.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for mndino-0.0.0.2.tar.gz
Algorithm Hash digest
SHA256 59917355055ab8d841978925fb715c198f8daaada4551f7586435717a339c4b0
MD5 ecc887bf3ba475686c16f8de215eb8c3
BLAKE2b-256 dc0e0acf42edf07883041d154a8d3d401ddb68544a45b486651be8f5fdf18b76

See more details on using hashes here.

File details

Details for the file mndino-0.0.0.2-py3-none-any.whl.

File metadata

  • Download URL: mndino-0.0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 22.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for mndino-0.0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e931fd465be8cffcb1720b8dcf3a7e949c6824fb9d4d2452c4f8f7299de98c27
MD5 175ce106c9fb72b18ccd21aa1f37a9c4
BLAKE2b-256 22e4752b38719d5c015ede54543865c5bcfc4b2fb22a1f80996250c7f9d15958

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page