fractal-analytics-platform / fractal-tasks-core

Main tasks for the Fractal analytics platform
https://fractal-analytics-platform.github.io/fractal-tasks-core/
BSD 3-Clause "New" or "Revised" License
11 stars 5 forks source link

Expose additional cellpose options in the task #649

Closed jluethi closed 4 months ago

jluethi commented 5 months ago

Some users need advanced options when running Cellpose tasks. We should expose more of them (& figure out how to separate between regular options and advanced options).

A few options that have come up that would be good to expose: Normalization/Quantile rescaling. This leads to issues in sparse images when not turned off otherwise.

Main parameters I'd like to expose:

Other parameters that we could also expose, just to have all options available:

Parameters we shouldn't currently expose:

Open questions Can we provide other rescaling strategies? One idea is letting the user provide the rescaling quantiles (e.g. so they could set them to 0.0, 0.99999). The Cellpose API doesn't seem to provide one. One approach would be that Fractal itself performs some form of rescaling before passing the image to the Cellpose API. Cellpose rescales the image to be a float32 between 0 & 1. The open questions would be: What statistics are used to determine this rescaling values. Fixed-values, quantiles or statistics of a whole dataset? (cc @fstur for discussions we've been having on this recently)

All these options are in the model.eval call: https://github.com/MouseLand/cellpose/blob/4f5661983c3787efa443bbbd3f60256f4fd8bf53/cellpose/models.py#L109

And would need to be added here in Fractal: https://github.com/fractal-analytics-platform/fractal-tasks-core/blob/e5b8a0cefba076c3b6a0f3a1d0ce8909b4547d5c/fractal_tasks_core/tasks/cellpose_segmentation.py#L116

jluethi commented 5 months ago

Only exposing the options does not deal with the sparsity issue we were having. In my tests, setting normalize = False with our existing (underexposed) 16bit images just leads to Cellpose not finding any objects.

I suspect we'd really have to implement rescaling of the data outside of cellpose to make this work. Will take a bit of experimentation on whether it makes sense to pass float32 normalized datasets to cellpose or not. And if that's the correct way, would need to think about the best way to expose this in the task

jluethi commented 5 months ago

See this issue in the Cellpose repo for more context: https://github.com/MouseLand/cellpose/issues/795

The hard-set normalization to either nothing (basically always seems to result in an empty segmentation) or 1st to 99th percentile (can be too harsh on sparse images) has been shown to be an issue by others.

I'm currently working on an option to the Fractal task to normalize the images pass to the cellpose model beforehand. That seems to do the trick. I'll investigate a bit more in https://github.com/fractal-analytics-platform/fractal-tasks-core/pull/650