Skip to content

Commit b552081

Browse files
Created using Colab
1 parent 93d3d6d commit b552081

File tree

1 file changed

+49
-35
lines changed

1 file changed

+49
-35
lines changed

notebooks/run_Cellpose-SAM.ipynb

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
{
44
"cell_type": "markdown",
55
"metadata": {
6-
"colab_type": "text",
7-
"id": "view-in-github"
6+
"id": "view-in-github",
7+
"colab_type": "text"
88
},
99
"source": [
1010
"<a href=\"https://colab.research.google.com/github/MouseLand/cellpose_dev/blob/main/notebooks/run_Cellpose-SAM.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
@@ -40,7 +40,9 @@
4040
},
4141
{
4242
"cell_type": "markdown",
43-
"metadata": {},
43+
"metadata": {
44+
"id": "VZe3EmLeDr5h"
45+
},
4446
"source": [
4547
"### Mount your google drive\n",
4648
"\n",
@@ -67,7 +69,9 @@
6769
},
6870
{
6971
"cell_type": "markdown",
70-
"metadata": {},
72+
"metadata": {
73+
"id": "l9YpMcAuDr5i"
74+
},
7175
"source": [
7276
"\n",
7377
"Then click on \"Folder\" icon on the Left, press the refresh button. Your Google Drive folder should now be available here as \"gdrive\".\n",
@@ -95,9 +99,7 @@
9599
"id": "h_iAN7cAthma"
96100
},
97101
"source": [
98-
"## **Setup Cellpose**\n",
99-
"\n",
100-
"Install Cellpose-SAM\n"
102+
"### Install Cellpose-SAM\n"
101103
]
102104
},
103105
{
@@ -114,7 +116,9 @@
114116
},
115117
{
116118
"cell_type": "markdown",
117-
"metadata": {},
119+
"metadata": {
120+
"id": "fGSKx-EiDr5k"
121+
},
118122
"source": [
119123
"Check GPU and instantiate model - will download weights."
120124
]
@@ -133,20 +137,23 @@
133137
"from pathlib import Path\n",
134138
"from tqdm import trange\n",
135139
"import matplotlib.pyplot as plt\n",
140+
"from natsort import natsorted\n",
136141
"\n",
137142
"\n",
138143
"#Check if colab notebook instance has GPU access\n",
139-
"if core.use_gpu()==False: \n",
144+
"if core.use_gpu()==False:\n",
140145
" raise ImportError(\"No GPU access, change your runtime\")\n",
141-
" \n",
146+
"\n",
142147
"io.logger_setup() # run this to get printing of progress\n",
143148
"\n",
144149
"model = models.CellposeModel(gpu=True)"
145150
]
146151
},
147152
{
148153
"cell_type": "markdown",
149-
"metadata": {},
154+
"metadata": {
155+
"id": "vOpZWZtDDr5l"
156+
},
150157
"source": [
151158
"Input directory with your images:"
152159
]
@@ -167,10 +174,10 @@
167174
" raise FileNotFoundError(\"directory does not exist\")\n",
168175
"\n",
169176
"# *** change to your image extension ***\n",
170-
"image_ext = \".tif\" \n",
177+
"image_ext = \".tif\"\n",
171178
"\n",
172179
"# list all files\n",
173-
"files = [f for f in dir.glob(\"*\"+image_ext) if \"_masks\" not in f.name and \"_flows\" not in f.name]\n",
180+
"files = natsorted([f for f in dir.glob(\"*\"+image_ext) if \"_masks\" not in f.name and \"_flows\" not in f.name])\n",
174181
"\n",
175182
"if(len(files)==0):\n",
176183
" raise FileNotFoundError(\"no image files found, did you specify the correct folder and extension?\")\n",
@@ -183,19 +190,21 @@
183190
},
184191
{
185192
"cell_type": "markdown",
186-
"metadata": {},
193+
"metadata": {
194+
"id": "eyj9hnQvDr5l"
195+
},
187196
"source": [
188197
"## Run Cellpose-SAM on one image in folder\n",
189198
"\n",
190199
"Here are some of the parameters you can change:\n",
191200
"\n",
192-
"* ***flow_threshold*** is the maximum allowed error of the flows for each mask. The default is 0.4. \n",
201+
"* ***flow_threshold*** is the maximum allowed error of the flows for each mask. The default is 0.4.\n",
193202
" * **Increase** this threshold if cellpose is not returning as many masks as you’d expect (or turn off completely with 0.0)\n",
194-
" * **Decrease** this threshold if cellpose is returning too many ill-shaped masks. \n",
203+
" * **Decrease** this threshold if cellpose is returning too many ill-shaped masks.\n",
195204
"\n",
196-
"* ***cellprob_threshold*** determines proability that a detected object is a cell. The default is 0.0. \n",
205+
"* ***cellprob_threshold*** determines proability that a detected object is a cell. The default is 0.0.\n",
197206
" * **Decrease** this threshold if cellpose is not returning as many masks as you’d expect or if masks are too small\n",
198-
" * **Increase** this threshold if cellpose is returning too many masks esp from dull/dim areas. \n",
207+
" * **Increase** this threshold if cellpose is returning too many masks esp from dull/dim areas.\n",
199208
"\n",
200209
"* ***tile_norm_blocksize*** determines the size of blocks used for normalizing the image. The default is 0, which means the entire image is normalized together.\n",
201210
" You may want to change this to 100-200 pixels if you have very inhomogeneous brightness across your image.\n",
@@ -211,16 +220,13 @@
211220
},
212221
"outputs": [],
213222
"source": [
214-
"\n",
215-
"files = [\"C:/Users/carsen/.cellpose/data/2D/gray_2D.png\"]\n",
216-
"\n",
217-
"img = io.imread(files[0])[:150, :150]\n",
223+
"img = io.imread(files[0])\n",
218224
"\n",
219225
"flow_threshold = 0.4\n",
220226
"cellprob_threshold = 0.0\n",
221227
"tile_norm_blocksize = 0\n",
222228
"\n",
223-
"masks, flows, styles = model.eval(img, batch_size=32, flow_threshold=flow_threshold, cellprob_threshold=cellprob_threshold, \n",
229+
"masks, flows, styles = model.eval(img, batch_size=32, flow_threshold=flow_threshold, cellprob_threshold=cellprob_threshold,\n",
224230
" normalize={\"tile_norm_blocksize\": tile_norm_blocksize})\n",
225231
"\n",
226232
"fig = plt.figure(figsize=(12,5))\n",
@@ -244,47 +250,55 @@
244250
{
245251
"cell_type": "code",
246252
"execution_count": null,
247-
"metadata": {},
253+
"metadata": {
254+
"id": "JgfGlUJNDr5n"
255+
},
248256
"outputs": [],
249257
"source": [
250258
"masks_ext = \".png\" if image_ext == \".png\" else \".tif\"\n",
251259
"for i in trange(len(files)):\n",
252260
" f = files[i]\n",
253261
" img = io.imread(f)\n",
254-
" masks, flows, styles = model.eval(img, batch_size=32, flow_threshold=flow_threshold, cellprob_threshold=cellprob_threshold, \n",
262+
" masks, flows, styles = model.eval(img, batch_size=32, flow_threshold=flow_threshold, cellprob_threshold=cellprob_threshold,\n",
255263
" normalize={\"tile_norm_blocksize\": tile_norm_blocksize})\n",
256-
" io.imsave(dir / (f.name + \"_masks\" + masks_ext), masks)"
264+
" io.imsave(dir / (f.stem + \"_masks\" + masks_ext), masks)"
257265
]
258266
},
259267
{
260268
"cell_type": "markdown",
261-
"metadata": {},
269+
"metadata": {
270+
"id": "ZsmgS0KJDr5n"
271+
},
262272
"source": [
263273
"if you have small images, you may want to load all of them first and then run, so that they can be batched together on the GPU"
264274
]
265275
},
266276
{
267277
"cell_type": "code",
268278
"execution_count": null,
269-
"metadata": {},
279+
"metadata": {
280+
"id": "XH-K72hGDr5n"
281+
},
270282
"outputs": [],
271283
"source": [
272284
"print(\"loading images\")\n",
273-
"imgs = [io.imread(files[i]) for i in trange(files)]\n",
285+
"imgs = [io.imread(files[i]) for i in trange(len(files))]\n",
274286
"\n",
275287
"print(\"running cellpose-SAM\")\n",
276-
"masks, flows, styles = model.eval(imgs, batch_size=32, flow_threshold=flow_threshold, cellprob_threshold=cellprob_threshold, \n",
288+
"masks, flows, styles = model.eval(imgs, batch_size=32, flow_threshold=flow_threshold, cellprob_threshold=cellprob_threshold,\n",
277289
" normalize={\"tile_norm_blocksize\": tile_norm_blocksize})\n",
278290
"\n",
279291
"print(\"saving masks\")\n",
280292
"for i in trange(len(files)):\n",
281293
" f = files[i]\n",
282-
" io.imsave(dir / (f.name + \"_masks\" + masks_ext), masks[i])"
294+
" io.imsave(dir / (f.stem + \"_masks\" + masks_ext), masks[i])"
283295
]
284296
},
285297
{
286298
"cell_type": "markdown",
287-
"metadata": {},
299+
"metadata": {
300+
"id": "9GdeH0nCDr5n"
301+
},
288302
"source": [
289303
"to save your masks for ImageJ, run the following code:"
290304
]
@@ -308,8 +322,8 @@
308322
"metadata": {
309323
"accelerator": "GPU",
310324
"colab": {
311-
"include_colab_link": true,
312-
"provenance": []
325+
"provenance": [],
326+
"include_colab_link": true
313327
},
314328
"kernelspec": {
315329
"display_name": "cellpose",
@@ -331,4 +345,4 @@
331345
},
332346
"nbformat": 4,
333347
"nbformat_minor": 0
334-
}
348+
}

0 commit comments

Comments
 (0)