"In this notebook we'll be applying the Cellpose algorithm to segment the cells in the images. Cellpose is a deep learning algorithm that can be used to segment cells in images."
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import skimage\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
...
...
@@ -23,74 +26,134 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"- define a function that reads an image and bins it by 4.\n",
"- define a function that segments the image using cellpose\n",
"- define a function that measures the area of the cells\n",
"- loop over the images and save the results"
"This is a function to rea the third channel of an image and downscale it. Use the function to read one of the tif images in the 'images' folder and display it. \n",
"\n",
"```python\n",
"def read_and_scale_image(image_path):\n",
" image = skimage.io.imread(image_path)\n",
" image = image[:, :, 2]\n",
" image = image[::4, :: 4]\n",
" return image\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/malbert/miniconda3/envs/pyimagecourse/lib/python3.10/site-packages/cellpose/resnet_torch.py:275: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n",
"Display the masks using the following command\n",
"`plt.figure(); plt.imshow(masks)`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a folder \"segmentations\" into which you will write the nuclei segmentations (you can do this outside of jupyter).\n",
"\n",
"For each image, remove the imshow command and instead add a line that saves the segmentation masks into the previously created folder. Use the command `skimage.io.imsave(output_path, masks)` for this."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"import stackview\n",
"stackview.curtain(image, masks)"
"Check the segmentations folder and open the files in Fiji to check everything is okay."
]
}
],
...
...
%% Cell type:markdown id: tags:
# 9. Segment images using cellpose
In this notebook we'll be applying the Cellpose algorithm to segment the cells in the images. Cellpose is a deep learning algorithm that can be used to segment cells in images.
%% Cell type:code id: tags:
``` python
importos
importskimage
importnumpyasnp
importmatplotlib.pyplotasplt
fromcellposeimportmodels
```
%% Cell type:markdown id: tags:
- define a function that reads an image and bins it by 4.
- define a function that segments the image using cellpose
- define a function that measures the area of the cells
- loop over the images and save the results
This is a function to rea the third channel of an image and downscale it. Use the function to read one of the tif images in the 'images' folder and display it.
How could you improve the segmentation output? Hint: Look at the diameter.
%% Cell type:code id: tags:
``` python
model=models.Cellpose(model_type='cyto')
```
%% Output
%% Cell type:markdown id: tags:
Use the following code to create a list of all the images in the 'images' folder and print the list.
/Users/malbert/miniconda3/envs/pyimagecourse/lib/python3.10/site-packages/cellpose/resnet_torch.py:275: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.
Create a folder "segmentations" into which you will write the nuclei segmentations (you can do this outside of jupyter).
For each image, remove the imshow command and instead add a line that saves the segmentation masks into the previously created folder. Use the command `skimage.io.imsave(output_path, masks)` for this.
"In this notebook we'll be applying the Cellpose algorithm to segment the cells in the images. Cellpose is a deep learning algorithm that can be used to segment cells in images."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import skimage\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from cellpose import models"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is a function to rea the third channel of an image and downscale it. Use the function to read one of the tif images in the 'images' folder and display it. \n",
"/Users/malbert/miniconda3/envs/pyimagecourse/lib/python3.10/site-packages/cellpose/resnet_torch.py:275: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n",
"Use the following code to create a list of all the images in the 'images' folder and print the list.\n",
"\n",
"```python\n",
"image_path_list = [p for p in os.listdir('images') if p.endswith('.tif')]\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['27985_284_E10_2.tif',\n",
" '24138_196_F7_2.tif',\n",
" '50546_727_A8_2.tif',\n",
" '67703_1283_D7_3.tif',\n",
" '47549_736_E7_1.tif',\n",
" '19838_1252_F8_1.tif',\n",
" '8346_22_C1_1.tif',\n",
" '47032_977_G4_4.tif',\n",
" '37367_517_E4_2.tif',\n",
" '36268_407_B8_1.tif',\n",
" '64554_1164_A6_2.tif',\n",
" '60398_1596_E1_1.tif',\n",
" '46658_784_B12_1.tif',\n",
" '27897_273_C8_2.tif',\n",
" '36268_404_B8_2.tif']"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"image_path_list = [p for p in os.listdir('images') if p.endswith('.tif')]\n",
"image_path_list"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Restrict the list to only the first 2 images"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"image_path_list = image_path_list[:2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Write a loop that for each image file path:\n",
"- reads the image\n",
"- segments the nuclei\n",
"- displays the segmentation masks.\n",
"\n",
"Display the masks using the following command\n",
"`plt.figure(); plt.imshow(masks)`"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/malbert/miniconda3/envs/pyimagecourse/lib/python3.10/site-packages/cellpose/resnet_torch.py:275: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n",
"/Users/malbert/miniconda3/envs/pyimagecourse/lib/python3.10/site-packages/cellpose/resnet_torch.py:275: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n",
"Create a folder \"segmentations\" into which you will write the nuclei segmentations (you can do this outside of jupyter).\n",
"\n",
"For each image, remove the imshow command and instead add a line that saves the segmentation masks into the previously created folder. Use the command `skimage.io.imsave(output_path, masks)` for this."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/malbert/miniconda3/envs/pyimagecourse/lib/python3.10/site-packages/cellpose/resnet_torch.py:275: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n",
"/Users/malbert/miniconda3/envs/pyimagecourse/lib/python3.10/site-packages/skimage/_shared/utils.py:328: UserWarning: segmentations/27985_284_E10_2.tif is a low contrast image\n",
" return func(*args, **kwargs)\n",
"/Users/malbert/miniconda3/envs/pyimagecourse/lib/python3.10/site-packages/cellpose/resnet_torch.py:275: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n",
"/Users/malbert/miniconda3/envs/pyimagecourse/lib/python3.10/site-packages/skimage/_shared/utils.py:328: UserWarning: segmentations/24138_196_F7_2.tif is a low contrast image\n",
"Check the segmentations folder and open the files in Fiji to check everything is okay."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.16"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": false,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": true
}
},
"nbformat": 4,
"nbformat_minor": 4
}
%% Cell type:markdown id: tags:
# 9. Segment images using cellpose
In this notebook we'll be applying the Cellpose algorithm to segment the cells in the images. Cellpose is a deep learning algorithm that can be used to segment cells in images.
%% Cell type:code id: tags:
``` python
importos
importskimage
importnumpyasnp
importmatplotlib.pyplotasplt
fromcellposeimportmodels
```
%% Cell type:markdown id: tags:
This is a function to rea the third channel of an image and downscale it. Use the function to read one of the tif images in the 'images' folder and display it.
/Users/malbert/miniconda3/envs/pyimagecourse/lib/python3.10/site-packages/cellpose/resnet_torch.py:275: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.
/Users/malbert/miniconda3/envs/pyimagecourse/lib/python3.10/site-packages/cellpose/resnet_torch.py:275: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.
/Users/malbert/miniconda3/envs/pyimagecourse/lib/python3.10/site-packages/cellpose/resnet_torch.py:275: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.
Create a folder "segmentations" into which you will write the nuclei segmentations (you can do this outside of jupyter).
For each image, remove the imshow command and instead add a line that saves the segmentation masks into the previously created folder. Use the command `skimage.io.imsave(output_path, masks)` for this.
/Users/malbert/miniconda3/envs/pyimagecourse/lib/python3.10/site-packages/cellpose/resnet_torch.py:275: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.
/Users/malbert/miniconda3/envs/pyimagecourse/lib/python3.10/site-packages/skimage/_shared/utils.py:328: UserWarning: segmentations/27985_284_E10_2.tif is a low contrast image
return func(*args, **kwargs)
/Users/malbert/miniconda3/envs/pyimagecourse/lib/python3.10/site-packages/cellpose/resnet_torch.py:275: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.
/Users/malbert/miniconda3/envs/pyimagecourse/lib/python3.10/site-packages/skimage/_shared/utils.py:328: UserWarning: segmentations/24138_196_F7_2.tif is a low contrast image
return func(*args, **kwargs)
%% Cell type:markdown id: tags:
Check the segmentations folder and open the files in Fiji to check everything is okay.