Skip to content
Snippets Groups Projects
Commit 9b8e5e81 authored by Son Phan's avatar Son Phan
Browse files

add course materials

parent 628cbd49
No related branches found
No related tags found
No related merge requests found
Showing
with 1873 additions and 0 deletions
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
*.pyc
*.~*.py
*.py.swm
*.py.swn
*.py.swp
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.DS_Store
# personal stuff
Part1Unet/models
Part1Unet/logs
Part1Unet/unet_model.png
Part2Cellpose/models
# Unet
Install unet env in conda
```
setconda
conda env create -f unet_env.yml
```
Launch the jupyter notebook
```
setconda
conda activate qlife25-unet
setenv LD_LIBRARY_PATH "${CONDA_PREFIX}/lib:${LD_LIBRARY_PATH}"
jupyter notebook
```
File added
File added
File added
This diff is collapsed.
name: qlife25-unet
channels:
- conda-forge
dependencies:
- python=3.10
- numpy=1.23.5
- cudatoolkit=11.2
- cudnn=8.1.0
- pip:
- tensorflow-gpu==2.10.0
- keras==2.10.0
- notebook==6.1.5
- scikit-image==0.19.3
- matplotlib==3.7.1
- pydot==3.0.4
"""
data_loader.py
This script contains functions for handling various data processing tasks for the
Cell-Segmentation-UNet project. It includes functions for unzipping datasets, retrieving data paths,
preprocessing images and masks, and loading datasets into a suitable format for training and testing.
Functions:
- unzip_to_original_subfolders: Unzips all files within a directory to their respective subfolders.
- get_data_paths: Retrieves paths to folders named by unique IDs within a specified directory.
- preprocess_image: Processes an image file to fit the input requirements of the model.
- preprocess_mask: Processes mask files associated with each image for segmentation tasks.
- load_dataset: Loads and preprocesses the dataset from a specified directory for model training.
Author: Mahdi Habibi
"""
import os
import random
import numpy as np
import matplotlib.pyplot as plt
from skimage.io import imread
from skimage.transform import resize
def unzip_to_original_subfolders(root_directory):
"""
Unzip all files within a directory to their respective subfolders.
Parameters:
- root_directory (str): Path to the root directory containing zip files.
"""
# Walking through the root directory to find all files
for dirpath, _, filenames in os.walk(root_directory):
for filename in filenames:
file_path = os.path.join(dirpath, filename)
# Extract the file name without the extension to create a subfolder
folder_name = os.path.splitext(filename)[0]
destination_folder = os.path.join(dirpath, folder_name)
# Unzipping to the created subfolder
os.system(f'unzip -q {file_path} -d {destination_folder}')
# remove the zip file after extracting
os.remove(file_path)
def get_data_paths(path):
"""
Retrieve paths to the folders named by IDs within the specified training path.
Parameters:
- path (str): Path to the data directory.
Returns:
- list: A list of paths to the individual data folders.
"""
return next(os.walk(path))[1]
def preprocess_image(id_, path, img_height, img_width, img_channels):
"""
Preprocess an image file to fit the input requirements of the model.
Parameters:
- id_ (str): Unique identifier for the image.
- path (str): Path to the data directory.
- img_height (int): Desired image height.
- img_width (int): Desired image width.
- img_channels (int): Number of image channels.
Returns:
- ndarray: Processed image array.
"""
img_file_path = os.path.join(path, id_, "images", id_ + ".png")
img = imread(img_file_path)[:, :, :img_channels]
img = resize(img, (img_height, img_width), mode='constant', preserve_range=True)
return img
def preprocess_mask(id_, path, img_height, img_width):
"""
Process mask files associated with each image for segmentation tasks.
Parameters:
- id_ (str): Unique identifier for the image.
- path (str): Path to the data directory.
- img_height (int): Desired mask height.
- img_width (int): Desired mask width.
Returns:
- ndarray: Combined mask array for the given image.
"""
mask_file_dir = os.path.join(path, id_, "masks")
all_masks = os.listdir(mask_file_dir)
mask = np.zeros((img_height, img_width, 1), dtype=np.bool)
for mask_file in all_masks:
mask_path = os.path.join(mask_file_dir, mask_file)
mask_ = imread(mask_path)
mask_ = np.expand_dims(resize(mask_, (img_height, img_width), mode='constant', preserve_range=True), axis=-1)
mask = np.maximum(mask, mask_)
return mask
def load_train_dataset(train_path, img_height, img_width, img_channels):
"""
Loads and preprocesses the dataset from the specified directory for model training.
Parameters:
- train_path (str): Path to the training data directory.
- img_height (int): Desired image height.
- img_width (int): Desired image width.
- img_channels (int): Number of image channels.
Returns:
- tuple: Tuple containing arrays for training images and corresponding masks.
"""
train_ids = get_data_paths(train_path)
X_train = np.zeros((len(train_ids), img_height, img_width, img_channels), dtype=np.uint8)
Y_train = np.zeros((len(train_ids), img_height, img_width, 1), dtype=np.bool)
for n, id_ in enumerate(train_ids):
X_train[n] = preprocess_image(id_, train_path, img_height, img_width, img_channels)
Y_train[n] = preprocess_mask(id_, train_path, img_height, img_width)
return X_train, Y_train
def load_test_dataset(test_path, img_height, img_width, img_channels):
"""
Loads and preprocesses the dataset from the specified directory for model training.
Parameters:
- test_path (str): Path to the test data directory.
- img_height (int): Desired image height.
- img_width (int): Desired image width.
- img_channels (int): Number of image channels.
Returns:
- tuple: Tuple containing arrays for training images and corresponding masks.
"""
test_ids = get_data_paths(test_path)
X_test = np.zeros((len(test_ids), img_height, img_width, img_channels), dtype=np.uint8)
for n, id_ in enumerate(test_ids):
X_test[n] = preprocess_image(id_, test_path, img_height, img_width, img_channels)
return X_test
def display_sample_grid(X, Y, num_samples=3):
"""Display a grid of microscopy images and their associated masks."""
fig, axs = plt.subplots(2, num_samples, figsize=(12, 8))
for i in range(num_samples):
idx = random.randint(0, len(X) - 1)
# Display original image
axs[0, i].imshow(X[idx])
axs[0, i].set_title(f'Original Image {idx}')
axs[0, i].axis('off') # Turn off axis
# Display corresponding mask
axs[1, i].imshow(np.squeeze(Y[idx]))
axs[1, i].set_title(f'Mask Image {idx}')
axs[1, i].axis('off') # Turn off axis
plt.tight_layout()
plt.show()
def display_train_prediction(X_data, Y_data, predictions, predictions_thr,_ix=None):
"""
Display a random sample consisting of the original image, ground truth mask, and predicted mask.
Parameters:
- X_data: Array of original images.
- Y_data: Array of ground truth masks.
- predictions: Array of predicted masks by the model.
The function randomly selects an image from the dataset, displays the original image,
the actual mask (ground truth), and the mask predicted by the model. This visual comparison
helps in understanding the model's segmentation accuracy on a qualitative level.
"""
# Randomly select an index from the dataset
if _ix is None:
ix = random.randint(0, len(predictions) - 1)
else:
ix = _ix
# Plot original image, ground truth mask, and predicted mask
fig, ax = plt.subplots(1, 4, figsize=(16, 8))
ax[0].imshow(X_data[ix])
ax[0].title.set_text(f'Original Image {ix}')
ax[1].imshow(np.squeeze(Y_data[ix]))
ax[1].title.set_text('Ground Truth Mask')
ax[2].imshow(np.squeeze(predictions[ix]))
ax[2].title.set_text('Predicted Probability')
ax[3].imshow(np.squeeze(predictions_thr[ix]))
ax[3].title.set_text('Predicted Mask')
plt.tight_layout()
plt.show()
def display_test_prediction(X_data, predictions, predictions_thr,_ix=None):
"""
Display a random sample consisting of the original image, ground truth mask, and predicted mask.
Parameters:
- X_data: Array of original images.
- predictions: Array of predicted masks by the model.
The function randomly selects an image from the dataset, displays the original image,
the actual mask (ground truth), and the mask predicted by the model. This visual comparison
helps in understanding the model's segmentation accuracy on a qualitative level.
"""
# Randomly select an index from the dataset
if _ix is None:
ix = random.randint(0, len(predictions) - 1)
else:
ix = _ix
# Plot original image, ground truth mask, and predicted mask
fig, ax = plt.subplots(1, 3, figsize=(16, 8))
ax[0].imshow(X_data[ix])
ax[0].title.set_text(f'Original Image {ix}')
ax[1].imshow(np.squeeze(predictions[ix]))
ax[1].title.set_text('Predicted Probability')
ax[2].imshow(np.squeeze(predictions_thr[ix]))
ax[2].title.set_text('Predicted Mask')
plt.tight_layout()
plt.show()
# Cellpose
Run the following commands to install cellpose env in conda
```
setconda
conda create -n qlife25-cellpose python=3.10 -y
conda activate qlife-cellpose
conda install pytorch==1.12.0 cudatoolkit=11.3 -c pytorch
pip uninstall torch
pip install 'cellpose[gui]'
pip install notebook==6.1.5 scikit-image matplotlib
```
Launch the jupyter notebook
```
setconda
conda activate qlife25-cellpose
jupyter notebook
```
\ No newline at end of file
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment