Skip to content

Commit 7a31d15

Browse files
MMathisLabC-Achard
andauthored
small updates to Colab demo (#81)
* Created using Colab - path update * Created using Colab * Delete notebooks/colab_inference_demo.ipynb - remove --> renamed and edited * Update dataloader for tests * Update URL --------- Co-authored-by: C-Achard <[email protected]>
1 parent 9f4a7fc commit 7a31d15

File tree

2 files changed

+108
-52
lines changed

2 files changed

+108
-52
lines changed

napari_cellseg3d/code_models/worker_training.py

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -331,24 +331,40 @@ def _get_data(self):
331331
(self.data_shape, dataset) = self.get_dataset(train_transforms)
332332

333333
logger.debug(f"Data shape : {self.data_shape}")
334-
self.dataloader = DataLoader(
335-
dataset,
336-
batch_size=self.config.batch_size,
337-
shuffle=True,
338-
num_workers=self.config.num_workers,
339-
collate_fn=pad_list_data_collate,
340-
)
334+
try:
335+
self.dataloader = DataLoader(
336+
dataset,
337+
batch_size=self.config.batch_size,
338+
shuffle=True,
339+
num_workers=self.config.num_workers,
340+
collate_fn=pad_list_data_collate,
341+
)
342+
except ValueError:
343+
self.dataloader = DataLoader(
344+
dataset,
345+
batch_size=self.config.batch_size,
346+
num_workers=self.config.num_workers,
347+
collate_fn=pad_list_data_collate,
348+
)
341349

342350
if self.config.eval_volume_dict is not None:
343351
eval_dataset = self.get_dataset_eval(self.config.eval_volume_dict)
344352
logger.debug(f"Eval batch size : {self.config.eval_batch_size}")
345-
self.eval_dataloader = DataLoader(
346-
eval_dataset,
347-
batch_size=self.config.eval_batch_size,
348-
shuffle=False,
349-
num_workers=self.config.num_workers,
350-
collate_fn=pad_list_data_collate,
351-
)
353+
try:
354+
self.eval_dataloader = DataLoader(
355+
eval_dataset,
356+
batch_size=self.config.eval_batch_size,
357+
shuffle=False,
358+
num_workers=self.config.num_workers,
359+
collate_fn=pad_list_data_collate,
360+
)
361+
except ValueError:
362+
self.eval_dataloader = DataLoader(
363+
eval_dataset,
364+
batch_size=self.config.eval_batch_size,
365+
num_workers=self.config.num_workers,
366+
collate_fn=pad_list_data_collate,
367+
)
352368
else:
353369
self.eval_dataloader = None
354370
return self.dataloader, self.eval_dataloader, self.data_shape
@@ -1385,13 +1401,21 @@ def get_patch_loader_func(num_samples):
13851401
data=self.val_files, transform=load_whole_images
13861402
)
13871403
logger.debug("Dataloader")
1388-
train_loader = DataLoader(
1389-
train_dataset,
1390-
batch_size=self.config.batch_size,
1391-
shuffle=True,
1392-
num_workers=self.config.num_workers,
1393-
collate_fn=pad_list_data_collate,
1394-
)
1404+
try:
1405+
train_loader = DataLoader(
1406+
train_dataset,
1407+
batch_size=self.config.batch_size,
1408+
shuffle=True,
1409+
num_workers=self.config.num_workers,
1410+
collate_fn=pad_list_data_collate,
1411+
)
1412+
except ValueError:
1413+
train_loader = DataLoader(
1414+
train_dataset,
1415+
batch_size=self.config.batch_size,
1416+
num_workers=self.config.num_workers,
1417+
collate_fn=pad_list_data_collate,
1418+
)
13951419

13961420
validation_loader = DataLoader(
13971421
validation_dataset,
Lines changed: 63 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,42 @@
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": [
10-
"<a href=\"https://colab.research.google.com/github/AdaptiveMotorControlLab/CellSeg3d/blob/main/notebooks/colab_inference_demo.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
10+
"<a href=\"https://colab.research.google.com/github/AdaptiveMotorControlLab/CellSeg3d/blob/main/notebooks/Colab_inference_demo.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
1111
]
1212
},
1313
{
1414
"cell_type": "markdown",
15-
"metadata": {},
15+
"metadata": {
16+
"id": "PoYFnmmQAm-x"
17+
},
1618
"source": [
1719
"# **CellSeg3D : inference demo notebook**\n",
1820
"\n",
1921
"---\n",
2022
"This notebook is part of the [CellSeg3D project](https://github.com/AdaptiveMotorControlLab/CellSeg3d) in the [Mathis Lab of Adaptive Intelligence](https://www.mackenziemathislab.org/).\n",
2123
"\n",
22-
"- 💜 The foundation of this notebook owes much to the **[ZeroCostDL4Mic](https://github.com/HenriquesLab/ZeroCostDL4Mic)** project and to the **[DeepLabCut](https://github.com/DeepLabCut/DeepLabCut)** team for bringing Colab into scientific open software."
24+
"- 💜 The foundation of this notebook owes much to the **[ZeroCostDL4Mic](https://github.com/HenriquesLab/ZeroCostDL4Mic)** project and to the **[DeepLabCut](https://github.com/DeepLabCut/DeepLabCut)** team."
2325
]
2426
},
2527
{
2628
"cell_type": "markdown",
27-
"metadata": {},
29+
"metadata": {
30+
"id": "vJLmPrWhAm-z"
31+
},
2832
"source": [
2933
"# **1. Installing dependencies**\n",
3034
"---"
3135
]
3236
},
3337
{
3438
"cell_type": "markdown",
35-
"metadata": {},
39+
"metadata": {
40+
"id": "CUNTOWCnAm-z"
41+
},
3642
"source": [
3743
"## **1.1 Installing CellSeg3D**\n",
3844
"---"
@@ -41,7 +47,9 @@
4147
{
4248
"cell_type": "code",
4349
"execution_count": null,
44-
"metadata": {},
50+
"metadata": {
51+
"id": "bnFKu6uFAm-z"
52+
},
4553
"outputs": [],
4654
"source": [
4755
"#@markdown ##Install CellSeg3D and dependencies\n",
@@ -51,7 +59,9 @@
5159
},
5260
{
5361
"cell_type": "markdown",
54-
"metadata": {},
62+
"metadata": {
63+
"id": "SfYAv60MAm-z"
64+
},
5565
"source": [
5666
"## **1.2. Restart your runtime**\n",
5767
"---\n",
@@ -66,7 +76,9 @@
6676
{
6777
"cell_type": "code",
6878
"execution_count": null,
69-
"metadata": {},
79+
"metadata": {
80+
"id": "Of85zlxzAm-z"
81+
},
7082
"outputs": [],
7183
"source": [
7284
"# @title Force session restart\n",
@@ -75,7 +87,9 @@
7587
},
7688
{
7789
"cell_type": "markdown",
78-
"metadata": {},
90+
"metadata": {
91+
"id": "pSVZsebrAm-0"
92+
},
7993
"source": [
8094
"## **1.3 Load key dependencies**\n",
8195
"---"
@@ -84,7 +98,9 @@
8498
{
8599
"cell_type": "code",
86100
"execution_count": null,
87-
"metadata": {},
101+
"metadata": {
102+
"id": "vzm75tE_Am-0"
103+
},
88104
"outputs": [],
89105
"source": [
90106
"# @title Load libraries\n",
@@ -99,15 +115,19 @@
99115
},
100116
{
101117
"cell_type": "markdown",
102-
"metadata": {},
118+
"metadata": {
119+
"id": "XLEJsiVNAm-0"
120+
},
103121
"source": [
104122
"# **2. Inference**\n",
105123
"---"
106124
]
107125
},
108126
{
109127
"cell_type": "markdown",
110-
"metadata": {},
128+
"metadata": {
129+
"id": "gjp4WL40Am-0"
130+
},
111131
"source": [
112132
"\n",
113133
"## **2.1. Check for GPU access**\n",
@@ -125,7 +145,9 @@
125145
{
126146
"cell_type": "code",
127147
"execution_count": null,
128-
"metadata": {},
148+
"metadata": {
149+
"id": "Fe8hNkOpAm-0"
150+
},
129151
"outputs": [],
130152
"source": [
131153
"#@markdown This cell verifies if GPU access is available.\n",
@@ -144,7 +166,9 @@
144166
},
145167
{
146168
"cell_type": "markdown",
147-
"metadata": {},
169+
"metadata": {
170+
"id": "JBCyJAGsAm-0"
171+
},
148172
"source": [
149173
"## **2.2 Run inference**\n",
150174
"---"
@@ -153,12 +177,12 @@
153177
{
154178
"cell_type": "code",
155179
"execution_count": null,
156-
"metadata": {},
180+
"metadata": {
181+
"id": "O0jLRpARAm-0"
182+
},
157183
"outputs": [],
158184
"source": [
159-
"# @title Load demo image and inference configuration\n",
160-
"#@markdown This cell loads a demo image and load the inference configuration.\n",
161-
"demo_image_path = \"./CellSeg3D/examples/c5image.tif\n",
185+
"demo_image_path = \"/content/CellSeg3D/examples/c5image.tif\"\n",
162186
"demo_image = imread(demo_image_path)\n",
163187
"inference_config = cs3d.CONFIG\n",
164188
"post_process_config = cs3d.PostProcessConfig()\n",
@@ -170,11 +194,11 @@
170194
{
171195
"cell_type": "code",
172196
"execution_count": null,
173-
"metadata": {},
197+
"metadata": {
198+
"id": "hIEKoyEGAm-0"
199+
},
174200
"outputs": [],
175201
"source": [
176-
"# @title Run inference on demo image\n",
177-
"#@markdown This cell runs the inference on the demo image.\n",
178202
"result = cs3d.inference_on_images(\n",
179203
" demo_image,\n",
180204
" config=inference_config,\n",
@@ -184,7 +208,9 @@
184208
{
185209
"cell_type": "code",
186210
"execution_count": null,
187-
"metadata": {},
211+
"metadata": {
212+
"id": "IFbmZ3_zAm-1"
213+
},
188214
"outputs": [],
189215
"source": [
190216
"# @title Post-process the result\n",
@@ -198,12 +224,14 @@
198224
{
199225
"cell_type": "code",
200226
"execution_count": null,
201-
"metadata": {},
227+
"metadata": {
228+
"id": "TMRiQ-m4Am-1"
229+
},
202230
"outputs": [],
203231
"source": [
204232
"# @title Display the result\n",
205233
"#@markdown This cell displays the result of the inference and post-processing. Use the slider to navigate through the z-stack.\n",
206-
"# @markdown *KNOWN ISSUE* : The colormap of the labels is not consistent between the z-stacks. \n",
234+
"# @markdown *KNOWN ISSUE* : The colormap of the labels is not consistent between the z-stacks.\n",
207235
"import matplotlib.pyplot as plt\n",
208236
"import ipywidgets as widgets\n",
209237
"from IPython.display import display\n",
@@ -242,7 +270,9 @@
242270
{
243271
"cell_type": "code",
244272
"execution_count": null,
245-
"metadata": {},
273+
"metadata": {
274+
"id": "Tw5exJ5EAm-1"
275+
},
246276
"outputs": [],
247277
"source": [
248278
"# @title Display the statistics\n",
@@ -255,7 +285,9 @@
255285
{
256286
"cell_type": "code",
257287
"execution_count": null,
258-
"metadata": {},
288+
"metadata": {
289+
"id": "0NhZ-YksAm-1"
290+
},
259291
"outputs": [],
260292
"source": [
261293
"# @title Plot the a 3D view, with statistics\n",
@@ -295,7 +327,7 @@
295327
" )\n",
296328
"\n",
297329
" fig.show(renderer=\"colab\")\n",
298-
" \n",
330+
"\n",
299331
"plotly_cells_stats(data)"
300332
]
301333
}
@@ -304,8 +336,8 @@
304336
"accelerator": "GPU",
305337
"colab": {
306338
"gpuType": "T4",
307-
"include_colab_link": true,
308-
"provenance": []
339+
"provenance": [],
340+
"include_colab_link": true
309341
},
310342
"kernelspec": {
311343
"display_name": "Python 3",

0 commit comments

Comments
 (0)