MouseLand / cellpose

a generalist algorithm for cellular segmentation with human-in-the-loop capabilities
https://www.cellpose.org/
BSD 3-Clause "New" or "Revised" License
1.24k stars 359 forks source link

Can't train cellpose using mask #951

Open ianmanifacier opened 1 month ago

ianmanifacier commented 1 month ago

Hi, I have tried to retrain the cellpose 'cyto3' and 'livecell' using tif masks as indicated in the documentation.

0 = black = background each value between 1-255 corresponds to the mask of a single cell. frame_000.tif frame_000_masks.tif (click the link for example files)

When I launch the training set on 230 images the training process runs without error. However, after the training end I use the newly trained model located in the folder, I get worse results.

I must be doing something wrong but I don't know what.

I tried formatting the tif file in 8 and 16 bits.

Here is a preview image

The python code I used to train cellpose

import os, shutil, time,sys
import numpy as np
import matplotlib.pyplot as plt

from cellpose import io, models, train

from urllib.parse import urlparse
import skimage.io

import matplotlib as mpl
#%matplotlib inline
mpl.rcParams['figure.dpi'] = 300

from urllib.parse import urlparse
from cellpose import models, core

use_GPU = core.use_gpu()
print('>>> GPU activated? %d'%use_GPU)

io.logger_setup()

train_dir = "C:/Users/Stagiaire/Desktop/masks_cellpose/cyto3/no_resize/train_dir"
test_dir = "C:/Users/Stagiaire/Desktop/masks_cellpose/cyto3/no_resize/test_dir"
image_filter = ""
mask_filter="_masks"
output = io.load_train_test_data(train_dir, test_dir, image_filter=image_filter, mask_filter=mask_filter, look_one_level_down=False)
images, labels, image_names, test_images, test_labels, image_names_test = output

# e.g. retrain a Cellpose model
model = models.CellposeModel(gpu=use_GPU, model_type="cyto3")

model_path = train.train_seg(model.net, train_data=images, train_labels=labels, n_epochs = 2000,
                            channels=[1,0], normalize=True,
                            test_data=test_images, test_labels=test_labels)

Thank you for your help Ian

carsen-stringer commented 3 weeks ago

for retraining, you want to use the Cellpose 2 settings, which are provided in the example in the docs:

model_path = train.train_seg(model.net, train_data=images, train_labels=labels,
                            channels=[1,2], normalize=True,
                            test_data=test_images, test_labels=test_labels,
                            weight_decay=1e-4, SGD=True, learning_rate=0.1, # <-- these are the retraining params
                            n_epochs=300, model_name="my_new_model")

https://cellpose.readthedocs.io/en/latest/train.html

We found that SGD usually works best when retraining, and AdamW works better when training from scratch. We will add more documentation on this