-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update training scripts * Add README for unet training
- Loading branch information
1 parent
87688cd
commit 170e449
Showing
5 changed files
with
137 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# 3D U-Net Training for Cochlea Data | ||
|
||
This folder contains the scripts for training a 3D U-Net for cell segmentation in the cochlea data. | ||
It contains two relevant scripts: | ||
- `check_training_data.py`, which visualizes the training data and annotations in napari. | ||
- `train_distance_unet.py`, which trains the 3D U-Net. | ||
|
||
Both scripts accept the argument `-i /path/to/data`, to specify the root folder with the training data. For example, run `python train_distance_unet.py -i /path/to/data` for training. The scripts will consider all tif files in the sub-folders of the root folder for training. | ||
They will load the **image data** according to the following rules: | ||
- Files with the ending `_annotations.tif` or `_cp_masks.tif` will not be considered as image data. | ||
- The other files will be considered as image data, if a corresponding file with ending `_annotations.tif` can be found. If it cannot be found the file will be excluded; the scripts will print the name of all files being excluded. | ||
|
||
The training script will save the trained model in `checkpoints/cochlea_distance_unet_<CURRENT_DATE>`, e.g. `checkpoints/cochlea_distance_unet_20250115`. | ||
For further options for the scripts run `python check_training_data.py -h` / `python train_distance_unet.py -h`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,57 @@ | ||
import argparse | ||
import os | ||
from glob import glob | ||
|
||
import imageio.v3 as imageio | ||
import napari | ||
import numpy as np | ||
|
||
root = "/home/pape/Work/data/moser/lightsheet" | ||
from train_distance_unet import get_image_and_label_paths | ||
from tqdm import tqdm | ||
|
||
# Root folder on my laptop. | ||
# This is just for convenience, so that I don't have to pass | ||
# the root argument during development. | ||
ROOT_CP = "/home/pape/Work/data/moser/lightsheet" | ||
|
||
def check_visually(check_downsampled=False): | ||
if check_downsampled: | ||
images = sorted(glob(os.path.join(root, "images_s2", "*.tif"))) | ||
masks = sorted(glob(os.path.join(root, "masks_s2", "*.tif"))) | ||
else: | ||
images = sorted(glob(os.path.join(root, "images", "*.tif"))) | ||
masks = sorted(glob(os.path.join(root, "masks", "*.tif"))) | ||
assert len(images) == len(masks) | ||
|
||
for im, mask in zip(images, masks): | ||
print(im) | ||
def check_visually(images, labels): | ||
for im, label in tqdm(zip(images, labels), total=len(images)): | ||
|
||
vol = imageio.imread(im) | ||
seg = imageio.imread(mask).astype("uint32") | ||
seg = imageio.imread(label).astype("uint32") | ||
|
||
v = napari.Viewer() | ||
v.add_image(vol) | ||
v.add_labels(seg) | ||
v.add_image(vol, name="pv-channel") | ||
v.add_labels(seg, name="annotations") | ||
folder, name = os.path.split(im) | ||
folder = os.path.basename(folder) | ||
v.title = f"{folder}/{name}" | ||
napari.run() | ||
|
||
|
||
def check_labels(): | ||
masks = sorted(glob(os.path.join(root, "masks", "*.tif"))) | ||
for mask_path in masks: | ||
labels = imageio.imread(mask_path) | ||
def check_labels(images, labels): | ||
for label_path in labels: | ||
labels = imageio.imread(label_path) | ||
n_labels = len(np.unique(labels)) | ||
print(mask_path, n_labels) | ||
print(label_path, n_labels) | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
"--root", "-i", help="The root folder with the annotated training crops.", | ||
default=ROOT_CP, | ||
) | ||
parser.add_argument("--check_labels", "-l", action="store_true") | ||
args = parser.parse_args() | ||
root = args.root | ||
|
||
images, labels = get_image_and_label_paths(root) | ||
|
||
check_visually(images, labels) | ||
if args.check_labels: | ||
check_labels(images, labels) | ||
|
||
|
||
if __name__ == "__main__": | ||
check_visually(True) | ||
# check_labels() | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters