Cellpose-SAM: superhuman generalization for cellular segmentation#
Marius Pachitariu, Michael Rariden, Carsen Stringer
This notebook shows how to process your own 2D or 3D images, saved on Google Drive.
This notebook is adapted from the notebook by Pradeep Rajasekhar, inspired by the ZeroCostDL4Mic notebook series.
Make sure you have GPU access enabled by going to Runtime -> Change Runtime Type -> Hardware accelerator and selecting GPU#
Mount your google drive#
If you have some images to train on, mount your drive. Alternatively scroll down and download the example images.
Run this cell to connect your Google Drive to colab:
Click on the URL.
Sign in your Google Account.
You will either have to:
copy the authorisation code and enter it into box below OR
in the new google colab, you can just click “Allow” and it should connect.
from google.colab import drive
drive.mount('/content/drive')
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Cell In[1], line 1
----> 1 from google.colab import drive
2 drive.mount('/content/drive')
ModuleNotFoundError: No module named 'google'
Then click on “Folder” icon on the Left, press the refresh button. Your Google Drive folder should now be available here as “gdrive”.
Click on the triangle icon and it will allow you to access whole drive. Navigate to the folder containing your images. Once you are there, click on the three dots on the right of the folder and select “Copy Path”
Copy and paste this path in the dir string below
Install Cellpose-SAM#
!pip install git+https://www.github.com/mouseland/cellpose.git
Check GPU and instantiate model - will download weights.
import numpy as np
from cellpose import models, core, io, plot
from pathlib import Path
from tqdm import trange
import matplotlib.pyplot as plt
io.logger_setup() # run this to get printing of progress
#Check if colab notebook instance has GPU access
if core.use_gpu()==False:
raise ImportError("No GPU access, change your runtime")
model = models.CellposeModel(gpu=True)
Input directory with your images (if you have them, otherwise use sample images):
# *** change to your google drive folder path ***
train_dir = "/content/gdrive/MyDrive/PATH-TO-FILES/"
if not Path(train_dir).exists():
raise FileNotFoundError("directory does not exist")
test_dir = None # optionally you can specify a directory with test files
# *** change to your mask extension ***
masks_ext = "_seg.npy"
# ^ assumes images from Cellpose GUI, if labels are tiffs, then "_masks.tif"
# list all files
files = [f for f in Path(train_dir).glob("*") if "_masks" not in f.name and "_flows" not in f.name and "_seg" not in f.name]
if(len(files)==0):
raise FileNotFoundError("no files found, did you specify the correct folder and extension?")
else:
print(f"{len(files)} files in folder:")
for f in files:
print(f.name)
Sample images (optional)#
You can use our sample images instead of mounting your google drive
from natsort import natsorted
from cellpose import utils
from pathlib import Path
url = "https://drive.google.com/uc?id=1HXpLczf7TPCdI1yZY5KV3EkdWzRrgvhQ"
utils.download_url_to_file(url, "human_in_the_loop.zip")
!unzip human_in_the_loop
train_dir = "human_in_the_loop/train/"
test_dir = "human_in_the_loop/test/"
masks_ext = "_seg.npy"
Train new model#
from cellpose import train
model_name = "new_model"
# default training params
n_epochs = 100
learning_rate = 1e-5
weight_decay = 0.1
batch_size = 1
# get files
output = io.load_train_test_data(train_dir, test_dir, mask_filter=masks_ext)
train_data, train_labels, _, test_data, test_labels, _ = output
# (not passing test data into function to speed up training)
new_model_path, train_losses, test_losses = train.train_seg(model.net,
train_data=train_data,
train_labels=train_labels,
batch_size=batch_size,
n_epochs=n_epochs,
learning_rate=learning_rate,
weight_decay=weight_decay,
nimg_per_epoch=max(2, len(train_data)), # can change this
model_name=model_name)
Evaluate on test data (optional)#
If you have test data, check performance
from cellpose import metrics
model = models.CellposeModel(gpu=True,
pretrained_model=new_model_path)
# run model on test images
masks = model.eval(test_data, batch_size=32)[0]
# check performance using ground truth labels
ap = metrics.average_precision(test_labels, masks)[0]
print('')
print(f'>>> average precision at iou threshold 0.5 = {ap[:,0].mean():.3f}')
plot masks
plt.figure(figsize=(12,8), dpi=150)
for k,im in enumerate(test_data):
img = im.copy()
plt.subplot(3,len(test_data), k+1)
img = np.vstack((img, np.zeros_like(img)[:1]))
img = img.transpose(1,2,0)
plt.imshow(img)
plt.axis('off')
if k==0:
plt.title('image')
plt.subplot(3,len(test_data), len(test_data) + k+1)
plt.imshow(masks[k])
plt.axis('off')
if k==0:
plt.title('predicted labels')
plt.subplot(3,len(test_data), 2*len(test_data) + k+1)
plt.imshow(test_labels[k])
plt.axis('off')
if k==0:
plt.title('true labels')
plt.tight_layout()