cellpose icon indicating copy to clipboard operation
cellpose copied to clipboard

[BUG] finetuned nuclei model has a model diam_mean of 30 instead of 17

Open PolarBean opened this issue 1 year ago • 1 comments

Describe the bug As described above when training a new finetuned model starting from nuclei everything appears to work correctly during training. it shows the following

2024-03-26 15:47:24,727 [INFO] training new model starting at model nuclei
2024-03-26 15:47:24,727 [INFO] training with chan = 0: gray, chan2 = 0: none
2024-03-26 15:47:24,727 [INFO] >> nuclei << model set to be used
2024-03-26 15:47:24,728 [INFO] >>>> using CPU
2024-03-26 15:47:24,853 [INFO] >>>> model diam_mean =  17.000 (ROIs rescaled to this size during training)

However when we then go to deploy that model via the command line on new data it appears to think the model was trained using a 30.00 diam mean.

2024-03-26 16:04:10,923 [INFO] >>>> loading model /run/media/harryc/4TB_external_NTFS/linux-backup/Github/img-cropper/ext-d000009_PVMouse_81265_Samp1__s023/models/nuclei_finetune
2024-03-26 16:04:11,079 [INFO] >>>> model diam_mean =  30.000 (ROIs rescaled to this size during training)
2024-03-26 16:04:11,079 [INFO] >>>> model diam_labels =  27.287 (mean diameter of training ROIs)
2024-03-26 16:04:11,079 [INFO] >>>> using diameter 30.000 for all images

I am using the latest version of cellpose cloned from the github.

PolarBean avatar Mar 26 '24 15:03 PolarBean

Changing the default diam_mean to 17. in the initialisation of CPNet fixes this (and obviously breaks all other models) https://github.com/MouseLand/cellpose/blob/509ffca33737058b0b4e2e96d506514e10620eb3/cellpose/resnet_torch.py#L186-L238

So it seems like the default is not being overwritten correctly here.

PolarBean avatar Mar 26 '24 15:03 PolarBean

on the CLI please use the --diam_mean 17. input. if this doesn't work I'll reopen the issue

carsen-stringer avatar Sep 11 '24 07:09 carsen-stringer

hmm you are right though that the diam_mean also should have been saved in the model file, can you load the model file in torch and see if the diam_mean is saved correctly?

carsen-stringer avatar Sep 11 '24 07:09 carsen-stringer

I'll have more of a look later but looking through the code here it sets diam mean to 17 only if the pretrained_model_string== 'nuclei: https://github.com/MouseLand/cellpose/blob/509ffca33737058b0b4e2e96d506514e10620eb3/cellpose/models.py#L256-L259 if we make a custom model won't this then change the pretrained_model_string?

PolarBean avatar Sep 11 '24 10:09 PolarBean