Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot load new trained model for inference #558

Closed
JunMa11 opened this issue Aug 22, 2022 · 3 comments
Closed

cannot load new trained model for inference #558

JunMa11 opened this issue Aug 22, 2022 · 3 comments

Comments

@JunMa11
Copy link

JunMa11 commented Aug 22, 2022

Dear Cellpose developers,

Train

I trained a new model with my own data by

python -m cellpose --train --use_gpu --dir /media/cellpose/imagesTr/imagesTrTif/ --chan 2 --chan2 1 --n_epochs 600 --pretrained_model None --batch_size 8 --dir_above --save_each --verbose --img_filter '_img' --mask_filter '_masks'

The trained model was saved in the image path

Inference

from cellpose import models, io
import os
join = os.path.join
import numpy as np
from tqdm import tqdm
import tifffile as tif

# model_type='cyto' or model_type='nuclei'
model_path = '/media/cellpose/imagesTr/imagesTrTif/models/cellpose.028618_epoch_599'
model = models.CellposeModel(gpu=True, model_type='cyto', pretrained_model = model_path)
print('model info:', model.pretrained_model)
print('start predicting....')

img_path = '/media/cellpose/imagesTs/images'
names = sorted(os.listdir(img_path))
seg_path = '/media/seg-images/cellpose-seg'
os.makedirs(seg_path, exist_ok=True)

chan = [2,1]
for i, name in enumerate(tqdm(names)):
    save_name = name.split('_img')[0]+'_label.tiff'
    if not os.path.isfile(join(seg_path, save_name)):
        if name.endswith('.tif') or name.endswith('.tiff'):
            img = tif.imread(join(img_path, name))
        else:
            img = io.imread(join(img_path, name))
        masks, flows, styles = model.eval(img, diameter=None, channels=chan, net_avg=False, progress=True)

However, I find the script does not load my trained model. It still loads the default model [/home/.cellpose/models/cytotorch_0]

How can I load the new trained model for inference?

@JunMa11
Copy link
Author

JunMa11 commented Aug 22, 2022

It works by replacing the default model file in .cellpose/models with the trained model.

@carsen-stringer
Copy link
Member

I've added the function io.add_model(/full/path/to/model) to add the model file to the hidden folder to use with GUI and CLI. You can also access the function by running python -m cellpose --add_model /full/path/to/model.

@carsen-stringer
Copy link
Member

carsen-stringer commented Sep 9, 2022

In your case though, it wasn't working because you were using model_type='cyto' -- the model was by default using that input, instead in the future if you don't want to add your models to the hidden folder use command

model = models.CellposeModel(gpu=True, pretrained_model = model_path)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants