Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

small updates to Colab demo #81

Merged
merged 7 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 45 additions & 21 deletions napari_cellseg3d/code_models/worker_training.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,24 +331,40 @@ def _get_data(self):
(self.data_shape, dataset) = self.get_dataset(train_transforms)

logger.debug(f"Data shape : {self.data_shape}")
self.dataloader = DataLoader(
dataset,
batch_size=self.config.batch_size,
shuffle=True,
num_workers=self.config.num_workers,
collate_fn=pad_list_data_collate,
)
try:
self.dataloader = DataLoader(
dataset,
batch_size=self.config.batch_size,
shuffle=True,
num_workers=self.config.num_workers,
collate_fn=pad_list_data_collate,
)
except ValueError:
self.dataloader = DataLoader(
dataset,
batch_size=self.config.batch_size,
num_workers=self.config.num_workers,
collate_fn=pad_list_data_collate,
)

if self.config.eval_volume_dict is not None:
eval_dataset = self.get_dataset_eval(self.config.eval_volume_dict)
logger.debug(f"Eval batch size : {self.config.eval_batch_size}")
self.eval_dataloader = DataLoader(
eval_dataset,
batch_size=self.config.eval_batch_size,
shuffle=False,
num_workers=self.config.num_workers,
collate_fn=pad_list_data_collate,
)
try:
self.eval_dataloader = DataLoader(
eval_dataset,
batch_size=self.config.eval_batch_size,
shuffle=False,
num_workers=self.config.num_workers,
collate_fn=pad_list_data_collate,
)
except ValueError:
self.eval_dataloader = DataLoader(
eval_dataset,
batch_size=self.config.eval_batch_size,
num_workers=self.config.num_workers,
collate_fn=pad_list_data_collate,
)
else:
self.eval_dataloader = None
return self.dataloader, self.eval_dataloader, self.data_shape
Expand Down Expand Up @@ -1385,13 +1401,21 @@ def get_patch_loader_func(num_samples):
data=self.val_files, transform=load_whole_images
)
logger.debug("Dataloader")
train_loader = DataLoader(
train_dataset,
batch_size=self.config.batch_size,
shuffle=True,
num_workers=self.config.num_workers,
collate_fn=pad_list_data_collate,
)
try:
train_loader = DataLoader(
train_dataset,
batch_size=self.config.batch_size,
shuffle=True,
num_workers=self.config.num_workers,
collate_fn=pad_list_data_collate,
)
except ValueError:
train_loader = DataLoader(
train_dataset,
batch_size=self.config.batch_size,
num_workers=self.config.num_workers,
collate_fn=pad_list_data_collate,
)

validation_loader = DataLoader(
validation_dataset,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,42 @@
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "view-in-github"
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<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>"
"<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>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"id": "PoYFnmmQAm-x"
},
"source": [
"# **CellSeg3D : inference demo notebook**\n",
"\n",
"---\n",
"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",
"\n",
"- 💜 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."
"- 💜 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."
]
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"id": "vJLmPrWhAm-z"
},
"source": [
"# **1. Installing dependencies**\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"id": "CUNTOWCnAm-z"
},
"source": [
"## **1.1 Installing CellSeg3D**\n",
"---"
Expand All @@ -41,7 +47,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"id": "bnFKu6uFAm-z"
},
"outputs": [],
"source": [
"#@markdown ##Install CellSeg3D and dependencies\n",
Expand All @@ -51,7 +59,9 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"id": "SfYAv60MAm-z"
},
"source": [
"## **1.2. Restart your runtime**\n",
"---\n",
Expand All @@ -66,7 +76,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"id": "Of85zlxzAm-z"
},
"outputs": [],
"source": [
"# @title Force session restart\n",
Expand All @@ -75,7 +87,9 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"id": "pSVZsebrAm-0"
},
"source": [
"## **1.3 Load key dependencies**\n",
"---"
Expand All @@ -84,7 +98,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"id": "vzm75tE_Am-0"
},
"outputs": [],
"source": [
"# @title Load libraries\n",
Expand All @@ -99,15 +115,19 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"id": "XLEJsiVNAm-0"
},
"source": [
"# **2. Inference**\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"id": "gjp4WL40Am-0"
},
"source": [
"\n",
"## **2.1. Check for GPU access**\n",
Expand All @@ -125,7 +145,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"id": "Fe8hNkOpAm-0"
},
"outputs": [],
"source": [
"#@markdown This cell verifies if GPU access is available.\n",
Expand All @@ -144,7 +166,9 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"id": "JBCyJAGsAm-0"
},
"source": [
"## **2.2 Run inference**\n",
"---"
Expand All @@ -153,12 +177,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"id": "O0jLRpARAm-0"
},
"outputs": [],
"source": [
"# @title Load demo image and inference configuration\n",
"#@markdown This cell loads a demo image and load the inference configuration.\n",
"demo_image_path = \"./CellSeg3D/examples/c5image.tif\n",
"demo_image_path = \"/content/CellSeg3D/examples/c5image.tif\"\n",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll check again before merging

"demo_image = imread(demo_image_path)\n",
"inference_config = cs3d.CONFIG\n",
"post_process_config = cs3d.PostProcessConfig()\n",
Expand All @@ -170,11 +194,11 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"id": "hIEKoyEGAm-0"
},
"outputs": [],
"source": [
"# @title Run inference on demo image\n",
"#@markdown This cell runs the inference on the demo image.\n",
"result = cs3d.inference_on_images(\n",
" demo_image,\n",
" config=inference_config,\n",
Expand All @@ -184,7 +208,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"id": "IFbmZ3_zAm-1"
},
"outputs": [],
"source": [
"# @title Post-process the result\n",
Expand All @@ -198,12 +224,14 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"id": "TMRiQ-m4Am-1"
},
"outputs": [],
"source": [
"# @title Display the result\n",
"#@markdown This cell displays the result of the inference and post-processing. Use the slider to navigate through the z-stack.\n",
"# @markdown *KNOWN ISSUE* : The colormap of the labels is not consistent between the z-stacks. \n",
"# @markdown *KNOWN ISSUE* : The colormap of the labels is not consistent between the z-stacks.\n",
"import matplotlib.pyplot as plt\n",
"import ipywidgets as widgets\n",
"from IPython.display import display\n",
Expand Down Expand Up @@ -242,7 +270,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"id": "Tw5exJ5EAm-1"
},
"outputs": [],
"source": [
"# @title Display the statistics\n",
Expand All @@ -255,7 +285,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"id": "0NhZ-YksAm-1"
},
"outputs": [],
"source": [
"# @title Plot the a 3D view, with statistics\n",
Expand Down Expand Up @@ -295,7 +327,7 @@
" )\n",
"\n",
" fig.show(renderer=\"colab\")\n",
" \n",
"\n",
"plotly_cells_stats(data)"
]
}
Expand All @@ -304,8 +336,8 @@
"accelerator": "GPU",
"colab": {
"gpuType": "T4",
"include_colab_link": true,
"provenance": []
"provenance": [],
"include_colab_link": true
},
"kernelspec": {
"display_name": "Python 3",
Expand Down
Loading