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.4k stars 402 forks source link

TypeError: expected dtype object, got 'numpy.dtype[float64]' #426

Closed mayishazn closed 2 years ago

mayishazn commented 2 years ago

Hello,

Thank you for creating this useful tool! I do have a new issue- I have run this same notebook several times successfully. Lately, I tried it on two different computers. The first finishes running but there is no output, no progress bar, nothing new in the folders it should save to. The second outputs an error about dtype objects. Any help is appreciated!

Here is the notebook: notebook1 notebook2 notebook3

And here is the error:


TypeError Traceback (most recent call last)

in 64 # you can set the average cell `diameter` in pixels yourself (recommended) 65 # diameter can be a list or a single number for all images ---> 66 masks, flows, styles, diams = model.eval(imgs, diameter=diam, channels=channels, do_3D=False, min_size=minarea, resample=True) 67 #masks, flows, styles, diams = model.eval(imgs, diameter=130, channels=channels, do_3D=False) 68 ~\anaconda3\lib\site-packages\cellpose\models.py in eval(self, x, batch_size, channels, channel_axis, z_axis, invert, normalize, diameter, do_3D, anisotropy, net_avg, augment, tile, tile_overlap, resample, interp, cluster, flow_threshold, mask_threshold, cellprob_threshold, dist_threshold, diam_threshold, min_size, stitch_threshold, rescale, progress, omni, verbose, transparency) 283 tic = time.time() 284 models_logger.info('~~~ FINDING MASKS ~~~') --> 285 masks, flows, styles = self.cp.eval(x, 286 batch_size=batch_size, 287 invert=invert, ~\anaconda3\lib\site-packages\cellpose\models.py in eval(self, x, batch_size, channels, channel_axis, z_axis, normalize, invert, rescale, diameter, do_3D, anisotropy, net_avg, augment, tile, tile_overlap, resample, interp, cluster, flow_threshold, mask_threshold, diam_threshold, cellprob_threshold, dist_threshold, compute_masks, min_size, stitch_threshold, progress, omni, calc_trace, verbose, transparency) 567 iterator = trange(nimg, file=tqdm_out) if nimg>1 else range(nimg) 568 for i in iterator: --> 569 maski, stylei, flowi = self.eval(x[i], 570 batch_size=batch_size, 571 channels=channels[i] if (len(channels)==len(x) and ~\anaconda3\lib\site-packages\cellpose\models.py in eval(self, x, batch_size, channels, channel_axis, z_axis, normalize, invert, rescale, diameter, do_3D, anisotropy, net_avg, augment, tile, tile_overlap, resample, interp, cluster, flow_threshold, mask_threshold, diam_threshold, cellprob_threshold, dist_threshold, compute_masks, min_size, stitch_threshold, progress, omni, calc_trace, verbose, transparency) 617 self.net.collect_params().grad_req = 'null' 618 --> 619 masks, styles, dP, cellprob, p, bd, tr = self._run_cp(x, 620 compute_masks=compute_masks, 621 normalize=normalize, ~\anaconda3\lib\site-packages\cellpose\models.py in _run_cp(self, x, compute_masks, normalize, invert, rescale, net_avg, resample, augment, tile, tile_overlap, mask_threshold, diam_threshold, flow_threshold, min_size, interp, cluster, anisotropy, do_3D, stitch_threshold, omni, calc_trace, verbose) 720 for i in iterator: 721 bdi = bd[i] if bd is not None else None --> 722 outputs = dynamics.compute_masks(dP[:,i], cellprob[i], bdi, 723 niter=niter, 724 mask_threshold=mask_threshold, ~\anaconda3\lib\site-packages\cellpose\dynamics.py in compute_masks(dP, cellprob, bd, p, inds, niter, mask_threshold, diam_threshold, flow_threshold, interp, cluster, do_3D, min_size, resize, omni, calc_trace, verbose, use_gpu, device, nclasses) 1074 if mask.max()>0 and flow_threshold is not None and flow_threshold > 0: 1075 # make sure labels are unique at output of get_masks -> 1076 mask = remove_bad_flow_masks(mask, dP, threshold=flow_threshold, use_gpu=use_gpu, device=device) 1077 1078 if resize is not None: ~\anaconda3\lib\site-packages\cellpose\dynamics.py in remove_bad_flow_masks(masks, flows, threshold, use_gpu, device) 915 916 """ --> 917 merrors, _ = metrics.flow_error(masks, flows, use_gpu, device) 918 badi = 1+(merrors>threshold).nonzero()[0] 919 masks[np.isin(masks, badi)] = 0 ~\anaconda3\lib\site-packages\cellpose\metrics.py in flow_error(maski, dP_net, use_gpu, device) 278 # flows predicted from estimated masks 279 idx = -1 # flows are the last thing returned now --> 280 dP_masks = dynamics.masks_to_flows(maski, use_gpu=use_gpu, device=device)[idx] 281 # difference between predicted flows vs mask flows 282 flow_errors=np.zeros(maski.max()) ~\anaconda3\lib\site-packages\cellpose\dynamics.py in masks_to_flows(masks, use_gpu, device, dists, omni) 567 return masks, dists, T[pad:-pad,pad:-pad], mu[:,pad:-pad,pad:-pad] 568 else: # reflection not a good idea for centroid model --> 569 mu, T = masks_to_flows_device(masks, device=device) 570 return masks, dists, T, mu 571 ~\anaconda3\lib\site-packages\cellpose\dynamics.py in masks_to_flows_cpu(masks, device) 395 niter = 2*np.int32(np.ptp(x) + np.ptp(y)) 396 T = np.zeros((ly+2)*(lx+2), np.float64) --> 397 T = _extend_centers(T, y, x, ymed, xmed, np.int32(lx), np.int32(niter)) 398 T[(y+1)*lx + x+1] = np.log(1.+T[(y+1)*lx + x+1]) 399 TypeError: expected dtype object, got 'numpy.dtype[float64]'
mayishazn commented 2 years ago

Here I copy the notebook for your convenience. Maybe I overlooked something. You can change the path folder to anything appropriate:

import numpy as np import time, os, sys from urllib.parse import urlparse import skimage.io import matplotlib.pyplot as plt import matplotlib as mpl %matplotlib inline mpl.rcParams['figure.dpi'] = 300 from cellpose import utils from cellpose import plot

import re

def list_files(dir): r = [] all_data = list() for root, dirs, files in os.walk(dir): for name in files: if name.endswith('.tif'): r0=os.path.join(root,name) r.append(r0) all_data.append(skimage.io.imread(r0, as_gray=True)) return all_data, r

folders = 'C:/Users/mayis/Documents/2DCells/11062020week4/32X'

diam = 50 minarea = 100 imgs, names = list_files(folders) nimg = len(imgs)

from cellpose import models, io

model = models.Cellpose(gpu=False, model_type='cyto') channels = [0,0] # IF YOU HAVE GRAYSCALE

masks, flows, styles, diams = model.eval(imgs, diameter=diam, channels=channels, do_3D=False, min_size=minarea,... resample=True)

io.masks_flows_to_seg(imgs, masks, flows, diams, names, channels)

for file in names: name = re.sub('.tif$','_seg.npy',file) dat = np.load(name, fix_imports=True,allow_pickle=True).item()

from scipy.io import savemat
name = re.sub('npy$', 'mat', name)
savemat(name, dat)
carsen-stringer commented 2 years ago

can you please try the latest version of cellpose? pip install cellpose --upgrade ; I think this is fixed. also to show messages see the readme for details, the logging messages are no longer on by default. if the upgrade doesn't work let me know and I'll reopen the issue

theo3-14 commented 2 years ago

Hi! I'm trying to run cellpose2 for the first time and having the same issue while trying to use the model zoo:

2022-04-21 11:52:26,013 [INFO] >> LC4 << model set to be used
2022-04-21 11:52:26,014 [INFO] >>>> using CPU
2022-04-21 11:52:26,174 [INFO] >>>> model diam_mean =  30.000 (ROIs rescaled to this size during training)
NET ERROR: expected dtype object, got 'numpy.dtype[float64]'

I've tried updating using the pip install cellpose --upgrade but it's still happening. Any help is appreciated! Thanks!