Skip to content

Commit bb99996

Browse files
committed
add cellpose-planer plugin
1 parent e37df0b commit bb99996

File tree

2 files changed

+197
-1
lines changed

2 files changed

+197
-1
lines changed
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
# cellpose-planer
2+
**Path:** https://github.com/Image-Py/cellpose-planer
3+
4+
**Version:** 0.1
5+
6+
**Author:** YXDragon, Y.Dong
7+
8+
**Email:** yxdragon@imagepy.org
9+
10+
**Keyword:** cellpose, segment, unet
11+
12+
**Description:** cellpose on planer framework for imagepy.
13+
14+
15+
[Cellpose](https://github.com/MouseLand/cellpose) is a generalist algorithm for cellular segmentation, Which written by Carsen Stringer and Marius Pachitariu.
16+
17+
[Planer](https://github.com/Image-Py/planer) is a light-weight CNN framework implemented in pure Numpy-like interface. It can run only with Numpy. Or change different backends. (Cupy accelerated with CUDA, ClPy accelerated with OpenCL).
18+
19+
So Cellpose-Planer is the **cellpose** models on **planer** framework. We generate onnx from torch models, then deduce it to planer model.
20+
21+
**We just use cellpose's models, but we rewrite all the pre-after processing and render algorithm, So the result is not same as the official one**
22+
23+
## Features
24+
* cellpose-planer is very light, only depend on [Numpy](https://github.com/numpy/numpy) and Scipy.
25+
* cellpose-planer can be accelerated with [Cupy](https://github.com/cupy/cupy).
26+
* without ui, with out object or class, pure function oriented designed.
27+
* optimize cellpose 's pre-after processing and render algorithm, having a better performance and result.
28+
29+
## Install
30+
**pip install cellpose-planer**
31+
32+
Option: *pip install cupy-cuda101* on envidia gpu, install cuda and cupy would get a large acceleration.
33+
34+
# Usage
35+
```python
36+
import cellpose_planer as cellpp
37+
from skimage.data import coins
38+
39+
img = coins()
40+
x = img.astype(np.float32)/255
41+
42+
net = cellpp.load_model('cyto_0')
43+
flowpb, style = cellpp.get_flow(net, x, size=480)
44+
lab = cellpp.flow2msk(flowpb, level=0.2)
45+
46+
flowpb = cellpp.asnumpy(flowpb)
47+
lab = cellpp.asnumpy(lab)
48+
cellpp.show(img, flowpb, lab)
49+
```
50+
![demo](https://user-images.githubusercontent.com/24822467/111028247-4d549580-83aa-11eb-9bf4-2cb87332530e.png)
51+
52+
## first time: search and download models
53+
search the models you need, and download them. (just one time)
54+
```python
55+
>>> import cellpose_planer as cellpp
56+
>>> cellpp.search_models()
57+
cyto_0 : --
58+
cyto_1 : --
59+
cyto_2 : --
60+
cyto_3 : --
61+
nuclei_0 : --
62+
nuclei_1 : --
63+
nuclei_2 : --
64+
nuclei_3 : --
65+
66+
>>> cellpp.download(['cyto_0', 'cyto_1', 'cyto_2', 'cyto_3'])
67+
download cyto_0 from http://release.imagepy.org/cellpose-planer/cyto_0.npy
68+
100%|█████████████████████████████████████| 100/100 [00:10<00:00, 2.37it/s]
69+
download cyto_1 from http://release.imagepy.org/cellpose-planer/cyto_1.npy
70+
100%|█████████████████████████████████████| 100/100 [00:10<00:00, 2.37it/s]
71+
download cyto_2 from http://release.imagepy.org/cellpose-planer/cyto_2.npy
72+
100%|█████████████████████████████████████| 100/100 [00:10<00:00, 2.37it/s]
73+
download cyto_3 from http://release.imagepy.org/cellpose-planer/cyto_3.npy
74+
100%|█████████████████████████████████████| 100/100 [00:10<00:00, 2.37it/s]
75+
76+
>>> cellpp.list_models()
77+
['cyto_0', 'cyto_1', 'cyto_2', 'cyto_3']
78+
```
79+
80+
## 1. load models
81+
you can load one model or more, when multi models, you would get a mean output.
82+
```python
83+
nets = cellpp.load_model('cyto_0')
84+
nets = cellpp.load_model(['cyto_0', 'cyto_1', 'cyto_2', 'cyto_3'])
85+
```
86+
87+
## 2. get flow image
88+
**def get_flow(nets, img, cn=[0,0], sample=1, size=512, tile=True, work=1, callback=progress)**
89+
90+
* *nets:* the nets loaded upon.
91+
92+
* *img:* the image to process
93+
94+
* *cn:* the cytoplasm and nucleus channels
95+
96+
* *sample:* if not 1, we scale it. (only avalible when tile==True)
97+
98+
* *size:* when tile==True, this is the tile size, when tile==False, we scale the image to size.
99+
100+
* *tile:* if True, method try to process image in tiles. else resize the image.
101+
102+
* *work:* open multi-thread to process the image. (GPU not recommend)
103+
```python
104+
flowpb, style = cellpp.get_flow(net, coins(), [0,0], work=4)
105+
```
106+
107+
## 3. flow to mask
108+
**def flow2msk(flowpb, level=0.5, grad=0.5, area=None, volume=None)**
109+
110+
* *flowpb:* get_flow 's output
111+
112+
* *level:* below level means background, where water can not flow. So level decide the outline.
113+
114+
* *grad:* if the flow gradient is smaller than this value, we set it 0. became a watershed. bigger gradient threshold could suppress the over-segmentation. especially in narrow-long area.
115+
116+
* *area:* at end of the flow process, every watershed should be small enough. (<area), default is 0 (auto).
117+
118+
* *volume:* and in small area, must contian a lot of water. (>volume), default is 0 (auto).
119+
120+
```python
121+
msk = cellpp.flow2msk(flowpb, level=0.5, grad=0.5, area=None, volume=None)
122+
```
123+
## 4. render
124+
cellpose-planer implements some render styles.
125+
```python
126+
import cellpose_planer as cellpp
127+
128+
# get edge from label msask
129+
edge = cellpp.msk2edge(lab)
130+
# get build flow as hsv 2 rgb
131+
hsv = cellpp.flow2hsv(flow)
132+
# 5 colors render (different in neighborhood)
133+
rgb = cellpp.rgb_mask(img, lab)
134+
# draw edge as red line
135+
line = cellpp.draw_edge(img, lab)
136+
```
137+
![cell](https://user-images.githubusercontent.com/24822467/111029250-93acf300-83b0-11eb-9e83-41bc0cf045dd.png)
138+
139+
## 5. backend and performance
140+
Planer can run with numpy or cupy backend, by default, cellpose-planer try to use cupy backend, if failed, use numpy backend. But we can change the backend manually. (if you switch backend, the net loaded befor would be useless, reload them pleanse)
141+
```python
142+
import cellpose-planer as cellpp
143+
144+
# use numpy and scipy as backend
145+
import numpy as np
146+
import scipy.ndimage as ndimg
147+
cellpp.engine(np, ndimg)
148+
149+
# use cupy and cupy.scipy as backend
150+
import cupy as cp
151+
import cupyx.scipy.ndimage as cpimg
152+
cellpp.engine(cp, cpimg)
153+
```
154+
here we time a 1024x1024 image on I7 CPU and 2070 GPU.
155+
```
156+
user switch engine: numpy
157+
net cost: 11.590
158+
flow cost: 0.0797
159+
160+
user switch engine: cupy
161+
net cost: 0.0139
162+
flow cost: 0.009
163+
```
164+
165+
# Model deducing and releasing
166+
Planer only has forward, so we need train the models in torch. then deduc it in planer.
167+
168+
## deduce from torch
169+
```python
170+
# train in cellpose with torch, and export torch as onnx file.
171+
from planer import onnx2planer
172+
onnx2planer(xxx.onnx)
173+
```
174+
then you would get a json file (graph structure), and a npy file (weights).
175+
176+
## model releasing
177+
if you want to share your model in cellpose-planer, just upload the json and npy file generated upon to any public container, then append a record in the **models list** tabel below, and give a pull request.
178+
*infact, when we call cellpp.search_models, cellpp pull the text below and parse them.*
179+
180+
## models list
181+
| model name | auther | description | url |
182+
| --- | --- | --- | --- |
183+
| cyto_0 | carsen-stringer | [for cell cyto segmentation](http://www.cellpose.org/) | [download](http://release.imagepy.org/cellpose-planer/cyto_0.npy) |
184+
| cyto_1 | carsen-stringer | [for cell cyto segmentation](http://www.cellpose.org/) | [download](http://release.imagepy.org/cellpose-planer/cyto_1.npy) |
185+
| cyto_2 | carsen-stringer | [for cell cyto segmentation](http://www.cellpose.org/) | [download](http://release.imagepy.org/cellpose-planer/cyto_2.npy) |
186+
| cyto_3 | carsen-stringer | [for cell cyto segmentation](http://www.cellpose.org/) | [download](http://release.imagepy.org/cellpose-planer/cyto_3.npy) |
187+
| nuclei_0 | carsen-stringer | [for cell nuclear segmentation](http://www.cellpose.org/) | [download](http://release.imagepy.org/cellpose-planer/nuclei_0.npy) |
188+
| nuclei_1 | carsen-stringer | [for cell nuclear segmentation](http://www.cellpose.org/) | [download](http://release.imagepy.org/cellpose-planer/nuclei_1.npy) |
189+
| nuclei_2 | carsen-stringer | [for cell nuclear segmentation](http://www.cellpose.org/) | [download](http://release.imagepy.org/cellpose-planer/nuclei_2.npy) |
190+
| nuclei_3 | carsen-stringer | [for cell nuclear segmentation](http://www.cellpose.org/) | [download](http://release.imagepy.org/cellpose-planer/nuclei_3.npy) |
191+
192+
*cellpp.search_models function pull the text below and parse them, welcom to release your models here!*
193+
194+
## Use cellpose-planer as ImagePy plugins
195+
cellpose-planer can also start as ImagePy's plugins. supporting interactive and bat processing.
196+
![image](https://user-images.githubusercontent.com/24822467/111069844-ce339000-8483-11eb-9dce-caa8f6ab80af.png)

sciwx/text/mdutil.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
def md2html(mdstr, css=None):
55
exts = ['markdown.extensions.extra', 'markdown.extensions.codehilite',
6-
'markdown.extensions.tables','markdown.extensions.toc', 'mdx_math']
6+
'markdown.extensions.tables','markdown.extensions.toc']#, 'mdx_math']
77
html = '''
88
<html lang="zh-cn">
99
<head>

0 commit comments

Comments
 (0)