scverse / squidpy

Spatial Single Cell Analysis in Python
https://squidpy.readthedocs.io/en/stable/
BSD 3-Clause "New" or "Revised" License
440 stars 81 forks source link

CPU/GPU option of CellPose #658

Closed Qirongmao97 closed 1 year ago

Qirongmao97 commented 1 year ago

Hi,

I have a question about cell segmentation using Cellpose.

Here's my code stating no GPU while using Cellpose in cell segmentation:

def cellpose_he(img, min_size=15, flow_threshold=0.4, channel_cellpose=0):
    model = models.Cellpose(model_type='nuclei')
    res, _, _, _ = model.eval(
        img,
        channels=[channel_cellpose, 0],
        diameter=None,
        min_size=min_size,
        invert=True,
        flow_threshold=flow_threshold,
    )
    return res

def segment(image, library):
    sq.im.segment(img=image, layer=library , channel=None, flow_threshold=threshold, method=cellpose_he,  channel_cellpose=channel)
    image.save(output_dir + 'zarr/' + 'full' + library + "_segmented_Cellpose" + "_" + str(threshold) + "_" + str(channel))

output_dir = '/exports/humgen/qmao/spatial_analysis/'

library_id = ['wildtype', 'mdxbl6', 'mdx4cv', 'mdx52']
img = []

for library in library_id:
    img.append(sq.im.ImageContainer('/Visium_brain/image/VisiumBrain_{}.tif'.format(library), layer=library))

def joblib_loop():
    Parallel(n_jobs=4)(delayed(segment)(image, library_id[i]) for i, image in enumerate(img))

And I got error info like this:

Traceback (most recent call last):
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/joblib/externals/loky/process_executor.py", line 428, in _process_worker
    r = call_item()
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/joblib/externals/loky/process_executor.py", line 275, in __call__
    return self.fn(*self.args, **self.kwargs)
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/joblib/_parallel_backends.py", line 620, in __call__
    return self.func(*args, **kwargs)
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/joblib/parallel.py", line 288, in __call__
    return [func(*args, **kwargs)
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/joblib/parallel.py", line 288, in <listcomp>
    return [func(*args, **kwargs)
  File "segmentation.py", line 39, in segment
    sq.im.segment(img=image, layer=library , channel=None, flow_threshold=threshold, method=cellpose_he,  channel_cellpose=channel)
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/squidpy/im/_segment.py", line 340, in segment
    res: ImageContainer = method.segment(
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/functools.py", line 912, in _method
    return method.__get__(obj, cls)(*args, **kwargs)
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/squidpy/im/_segment.py", line 166, in _
    res: ImageContainer = img.apply(func, layer=layer, channel=channel, fn_kwargs=fn_kwargs, copy=True, **kwargs)
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/squidpy/im/_container.py", line 1248, in apply
    cont = ImageContainer(
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/squidpy/im/_container.py", line 115, in __init__
    self.compute()
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/squidpy/im/_container.py", line 1335, in compute
    self.data.load()
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/xarray/core/dataset.py", line 739, in load
    evaluated_data = da.compute(*lazy_data.values(), **kwargs)
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/dask/base.py", line 600, in compute
    results = schedule(dsk, keys, **kwargs)
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/dask/threaded.py", line 89, in get
    results = get_async(
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/dask/local.py", line 511, in get_async
    raise_exception(exc, tb)
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/dask/local.py", line 319, in reraise
    raise exc
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/dask/local.py", line 224, in execute_task
    result = _execute_task(task, data)
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/dask/core.py", line 119, in _execute_task
    return func(*(_execute_task(a, cache) for a in args))
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/dask/optimization.py", line 990, in __call__
    return core.get(self.dsk, self.outkey, dict(zip(self.inkeys, args)))
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/dask/core.py", line 149, in get
    result = _execute_task(task, cache)
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/dask/core.py", line 119, in _execute_task
    return func(*(_execute_task(a, cache) for a in args))
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/dask/utils.py", line 71, in apply
    return func(*args, **kwargs)
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/dask/array/core.py", line 523, in _pass_extra_kwargs
    return func(*args[len(keys) :], **kwargs)
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/squidpy/im/_segment.py", line 199, in _segment_chunk
    labels = self._segment(block, **kwargs).astype(_SEG_DTYPE)
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/squidpy/im/_segment.py", line 257, in _segment
    return np.asarray(self._model(arr, **kwargs))
  File "segmentation.py", line 28, in cellpose_he
    res, _, _, _ = model.eval(
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/cellpose/models.py", line 216, in eval
    diams, _ = self.sz.eval(x, channels=channels, channel_axis=channel_axis, invert=invert, batch_size=batch_size,
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/cellpose/models.py", line 925, in eval
    masks = self.cp.eval(x,
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/cellpose/models.py", line 552, in eval
    masks, styles, dP, cellprob, p = self._run_cp(x,
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/cellpose/models.py", line 652, in _run_cp
    outputs = dynamics.compute_masks(dP[:,i], cellprob[i], niter=niter, cellprob_threshold=cellprob_threshold,
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/cellpose/dynamics.py", line 731, in compute_masks
    mask = remove_bad_flow_masks(mask, dP, threshold=flow_threshold, use_gpu=use_gpu, device=device)
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/cellpose/dynamics.py", line 581, in remove_bad_flow_masks
    if masks.size * 20 > torch.cuda.mem_get_info()[0]:
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/torch/cuda/memory.py", line 594, in mem_get_info
    device = torch.cuda.current_device()
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/torch/cuda/__init__.py", line 552, in current_device
    _lazy_init()
  File "/exports/humgen/qmao/anaconda3/envs/tangram-env/lib/python3.8/site-packages/torch/cuda/__init__.py", line 229, in _lazy_init
    torch._C._cuda_init()
RuntimeError: Found no NVIDIA driver on your system. Please check that you have an NVIDIA GPU and installed a driver from http://www.nvidia.com/Download/index.aspx

The error shows something related to no GPU and I was wondering how to solve this problem with only using CPU for cell segmentation, thanks! ...

giovp commented 1 year ago

hi @Qirongmao97 , I think this is a better question for the cellpose repository, here we are just providing convenient way to use it but we can't debug/support on these type of questions.