Skip to content

Commit 72790aa

Browse files
committed
add LV segmentation
1 parent 7075154 commit 72790aa

File tree

16 files changed

+691
-0
lines changed

16 files changed

+691
-0
lines changed

ves_seg/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Large vesicle segmentation
2+
3+
This module contains code for training and infering on a new dataset, as well as pretrained model checkpoints and a sample for validation. For setup, install [Pytorch Connectomics](https://connectomics.readthedocs.io/en/latest/notes/installation.html), [em_util](https://github.com/PytorchConnectomics/em_util), [neuroglancer](https://pypi.org/project/neuroglancer), and [cloud-volume](https://pypi.org/project/cloud-volume).
4+
5+
## Training
6+
Download your data. This model was built using the Pytorch Connectomics package, and thus all configuration for training is done within the yaml files within `configs`. Refer to the file `configs/00_base.yaml` as a template. Most fields can be left untouched, but to adapt to your dataset, change the following parameters at the very minimum:
7+
8+
* `DATASET.INPUT_PATH`: points towards directory containing training data. PyTC's dataloader will automatically seperate this into training/validation partitions.
9+
* `DATASET.IMAGE_NAME`: points to a single image or a list of images. Lists can be represented using standard YAML notation or by concatenating lists of images with `@` as a delimiter
10+
* `DATASET.OUTPUT_PATH`: points to an output directory that will contain model checkpoints
11+
12+
After editing the config file, you can execute training with:
13+
14+
`python scripts/main.py --config-base configs/<base.yaml> --config-file configs/bcd_config.yaml`
15+
16+
To continue retraining from a checkpoint, use:
17+
18+
`python scripts/main.py --config-base configs/<base.yaml> --config-file configs/bcd_config.yaml --checkpoint <path-to-checkpoint> SOLVER.ITERATION_RESTART True`
19+
20+
## Inference
21+
22+
Inference tools are available in `tools/process.py`. To do inference, in a Python shell or file, run:
23+
24+
`> do_inference(<im_path>, <pred_path>, [<base_config.yaml>, <bcd_config.yaml>], <checkpoint_path>)`
25+
26+
To also get adapted rand, precison, and recall metrics, run:
27+
28+
`> infer_and_stats([<im_path1>, ...], [<pred_path1>, ...], [<mask_path1, ...>], [<base_config.yaml>, <bcd_config.yaml>], <checkpoint_path>),
29+
`
30+
31+
Our final model checkpoint is provided at `outputs/checkpoint_1000000.pth.tar`.
32+
33+
## Visualiztion
34+
35+
Visualing predictions is done with neuroglancer. To load a volume and its segmentation, modify the file `scripts/ng.py`, then run `python -i scripts/ng.py`. A link to view neuroglancer will open. If running on a remote cluster, please note that port forwarding will be necessary to view on your machine. The `screnshot()` function can be used to take high-resolution screenshots of the neuroglancer representation.
36+
37+
Further documentation for both functions is contained in `tools/process.py`
38+
39+
## Sample
40+
41+
A small sample is prepared in the `sample` directory. `11-5` shows a small inter-neuron region with an assortment of large vesicles. Neuroglancer scripts that display the CLAHE-enhanced images, neuron mask, ground truth, and predictions with the final model checkpoints are prepared in `sample/11-5_ng.py`.

ves_seg/configs/00_base.yaml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
SYSTEM:
2+
NUM_GPUS: 1
3+
NUM_CPUS: 16
4+
MODEL:
5+
ARCHITECTURE: unet_3d
6+
BLOCK_TYPE: residual_se
7+
INPUT_SIZE: [9, 65, 65]
8+
OUTPUT_SIZE: [9, 65, 65]
9+
NORM_MODE: gn
10+
IN_PLANES: 1
11+
MIXED_PRECESION: False
12+
FILTERS: [32, 64, 96, 128, 160]
13+
ISOTROPY: [False, False, False, True, True]
14+
DATASET:
15+
INPUT_PATH: input_dir
16+
IMAGE_NAME: train_img1.h5@train_img2.h5@train_img3.h5
17+
LABEL_NAME: train_gt1.h5@train_gt2.h5@train_gt3.h5
18+
VALID_MASK_NAME: train_mask1.h5@train_mask2.h5@train_mask3.h5
19+
REDUCE_LABEL: False
20+
OUTPUT_PATH: output_dir
21+
PAD_SIZE: [0, 0, 0]
22+
DATA_SCALE: [1.0, 1.0, 1.0]
23+
VALID_RATIO: 0.1
24+
DISTRIBUTED: False
25+
SOLVER:
26+
LR_SCHEDULER_NAME: WarmupCosineLR
27+
BASE_LR: 0.02
28+
ITERATION_STEP: 1
29+
ITERATION_SAVE: 10000
30+
ITERATION_TOTAL: 100000
31+
SAMPLES_PER_BATCH: 64
32+
MONITOR:
33+
ITERATION_NUM: [40, 400]
34+
INFERENCE:
35+
INPUT_SIZE: [9, 65, 65]
36+
OUTPUT_SIZE: [9, 65, 65]
37+
INPUT_PATH: input_dir
38+
OUTPUT_PATH: output_dir
39+
IMAGE_NAME: infer_im.h5
40+
OUTPUT_NAME: infer_pred.h5
41+
PAD_SIZE: [0, 0, 0]
42+
AUG_MODE: "mean"
43+
AUG_NUM: 4
44+
STRIDE: [4, 32, 32]
45+
SAMPLES_PER_BATCH: 24

ves_seg/configs/01_base.yaml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
SYSTEM:
2+
NUM_GPUS: 1
3+
NUM_CPUS: 16
4+
MODEL:
5+
ARCHITECTURE: unet_3d
6+
BLOCK_TYPE: residual_se
7+
INPUT_SIZE: [9, 65, 65]
8+
OUTPUT_SIZE: [9, 65, 65]
9+
NORM_MODE: gn
10+
IN_PLANES: 1
11+
MIXED_PRECESION: False
12+
FILTERS: [32, 64, 96, 128, 160]
13+
ISOTROPY: [False, False, False, True, True]
14+
DATASET:
15+
INPUT_PATH: /mmfs1/data/linok/hydra/datasets/large_vesicle
16+
IMAGE_NAME: 01-initial/vol0_clahe.h5@01-initial/vol1_clahe.h5@01-initial/vol2_clahe.h5
17+
LABEL_NAME: 01-initial/vol0_ves.h5@01-initial/vol1_ves.h5@01-initial/vol2_ves.h5
18+
VALID_MASK_NAME: 01-initial/vol0_mask.h5@01-initial/vol1_mask.h5@01-initial/vol2_mask.h5
19+
REDUCE_LABEL: False
20+
OUTPUT_PATH: /mmfs1/data/linok/hydra/outputs/large_vesicle/01-initial
21+
PAD_SIZE: [0, 0, 0]
22+
DATA_SCALE: [1.0, 1.0, 1.0]
23+
VALID_RATIO: 0.1
24+
DISTRIBUTED: False
25+
SOLVER:
26+
LR_SCHEDULER_NAME: WarmupCosineLR
27+
BASE_LR: 0.02
28+
ITERATION_STEP: 1
29+
ITERATION_SAVE: 10000
30+
ITERATION_TOTAL: 100000
31+
SAMPLES_PER_BATCH: 64
32+
MONITOR:
33+
ITERATION_NUM: [40, 400]
34+
INFERENCE:
35+
INPUT_SIZE: [9, 65, 65]
36+
OUTPUT_SIZE: [9, 65, 65]
37+
INPUT_PATH: /data/projects/weilab/dataset/hydra/big_vesicle/v0-intial_dataset
38+
OUTPUT_PATH: outputs/large_vesicle/initial_volumes/results
39+
IMAGE_NAME: vol0_im.h5
40+
OUTPUT_NAME: vol0_inf.h5
41+
PAD_SIZE: [0, 0, 0]
42+
AUG_MODE: "mean"
43+
AUG_NUM: 4
44+
STRIDE: [4, 32, 32]
45+
SAMPLES_PER_BATCH: 24

ves_seg/configs/02_base.yaml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
SYSTEM:
2+
NUM_GPUS: 1
3+
NUM_CPUS: 16
4+
MODEL:
5+
ARCHITECTURE: unet_3d
6+
BLOCK_TYPE: residual_se
7+
INPUT_SIZE: [9, 65, 65]
8+
OUTPUT_SIZE: [9, 65, 65]
9+
NORM_MODE: gn
10+
IN_PLANES: 1
11+
MIXED_PRECESION: False
12+
FILTERS: [32, 64, 96, 128, 160]
13+
ISOTROPY: [False, False, False, True, True]
14+
DATASET:
15+
INPUT_PATH: /mmfs1/data/linok/hydra/datasets/large_vesicle
16+
IMAGE_NAME: 01-initial/vol0_clahe.h5@01-initial/vol1_clahe.h5@01-initial/vol2_clahe.h5@03-finetune/vol_10_im0_clahe.h5@03-finetune/vol_10_im1_clahe.h5@03-finetune/vol_11_im0_clahe.h5@03-finetune/vol_11_im1_clahe.h5@03-finetune/vol_11_im2_clahe.h5@03-finetune/vol_12_im0_clahe.h5@03-finetune/vol_12_im1_clahe.h5@03-finetune/vol_12_im2_clahe.h5@03-finetune/vol_12_im3_clahe.h5@03-finetune/vol_12_im4_clahe.h5@03-finetune/vol_12_im5_clahe.h5@03-finetune/vol_13_im0_clahe.h5@03-finetune/vol_13_im1_clahe.h5@03-finetune/vol_13_im2_clahe.h5@03-finetune/vol_14_im0_clahe.h5@03-finetune/vol_14_im1_clahe.h5@03-finetune/vol_14_im2_clahe.h5@03-finetune/vol_14_im3_clahe.h5@03-finetune/vol_14_im4_clahe.h5@03-finetune/vol_15_im_clahe.h5
17+
LABEL_NAME: 01-initial/vol0_ves.h5@01-initial/vol1_ves.h5@01-initial/vol2_ves.h5@03-finetune/vol_10_ves0.h5@03-finetune/vol_10_ves1.h5@03-finetune/vol_11_ves0.h5@03-finetune/vol_11_ves1.h5@03-finetune/vol_11_ves2.h5@03-finetune/vol_12_ves0.h5@03-finetune/vol_12_ves1.h5@03-finetune/vol_12_ves2.h5@03-finetune/vol_12_ves3.h5@03-finetune/vol_12_ves4.h5@03-finetune/vol_12_ves5.h5@03-finetune/vol_13_ves0.h5@03-finetune/vol_13_ves1.h5@03-finetune/vol_13_ves2.h5@03-finetune/vol_14_ves0.h5@03-finetune/vol_14_ves1.h5@03-finetune/vol_14_ves2.h5@03-finetune/vol_14_ves3.h5@03-finetune/vol_14_ves4.h5@03-finetune/vol_15_ves.h5
18+
VALID_MASK_NAME: 01-initial/vol0_mask.h5@01-initial/vol1_mask.h5@01-initial/vol2_mask.h5@03-finetune/vol_10_mask0.h5@03-finetune/vol_10_mask1.h5@03-finetune/vol_11_mask0.h5@03-finetune/vol_11_mask1.h5@03-finetune/vol_11_mask2.h5@03-finetune/vol_12_mask0.h5@03-finetune/vol_12_mask1.h5@03-finetune/vol_12_mask2.h5@03-finetune/vol_12_mask3.h5@03-finetune/vol_12_mask4.h5@03-finetune/vol_12_mask5.h5@03-finetune/vol_13_mask0.h5@03-finetune/vol_13_mask1.h5@03-finetune/vol_13_mask2.h5@03-finetune/vol_14_mask0.h5@03-finetune/vol_14_mask1.h5@03-finetune/vol_14_mask2.h5@03-finetune/vol_14_mask3.h5@03-finetune/vol_14_mask4.h5@03-finetune/vol_15_mask.h5
19+
REDUCE_LABEL: False
20+
OUTPUT_PATH: /mmfs1/data/linok/hydra/outputs/large_vesicle/03-finetune
21+
PAD_SIZE: [0, 0, 0]
22+
DATA_SCALE: [1.0, 1.0, 1.0]
23+
VALID_RATIO: 0.1
24+
DISTRIBUTED: False
25+
SOLVER:
26+
LR_SCHEDULER_NAME: WarmupCosineLR
27+
BASE_LR: 0.02
28+
ITERATION_STEP: 1
29+
ITERATION_SAVE: 10000
30+
ITERATION_TOTAL: 300000
31+
SAMPLES_PER_BATCH: 64
32+
MONITOR:
33+
ITERATION_NUM: [40, 400]
34+
INFERENCE:
35+
INPUT_SIZE: [9, 65, 65]
36+
OUTPUT_SIZE: [9, 65, 65]
37+
INPUT_PATH: /data/projects/weilab/dataset/hydra/big_vesicle/v0-intial_dataset
38+
OUTPUT_PATH: outputs/large_vesicle/initial_volumes/results
39+
IMAGE_NAME: vol0_im.h5
40+
OUTPUT_NAME: vol0_inf.h5
41+
PAD_SIZE: [0, 0, 0]
42+
AUG_MODE: "mean"
43+
AUG_NUM: 4
44+
STRIDE: [4, 32, 32]
45+
SAMPLES_PER_BATCH: 24

ves_seg/configs/03_base.yaml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
SYSTEM:
2+
NUM_GPUS: 1
3+
NUM_CPUS: 64
4+
DISTRIBUTED: True
5+
PARALLEL: 'DDP'
6+
MODEL:
7+
ARCHITECTURE: unet_3d
8+
BLOCK_TYPE: residual_se
9+
INPUT_SIZE: [9, 65, 65]
10+
OUTPUT_SIZE: [9, 65, 65]
11+
NORM_MODE: gn
12+
IN_PLANES: 1
13+
MIXED_PRECESION: False
14+
FILTERS: [32, 64, 96, 128]
15+
ISOTROPY: [False, False, False, True]
16+
DATASET:
17+
INPUT_PATH: /mmfs1/data/linok/hydra/datasets/large_vesicle
18+
IMAGE_NAME: 01-initial/vol0_clahe.h5@01-initial/vol1_clahe.h5@01-initial/vol2_clahe.h5@03-finetune/vol_10_im0_clahe.h5@03-finetune/vol_10_im1_clahe.h5@03-finetune/vol_11_im0_clahe.h5@03-finetune/vol_11_im1_clahe.h5@03-finetune/vol_11_im2_clahe.h5@03-finetune/vol_12_im0_clahe.h5@03-finetune/vol_12_im1_clahe.h5@03-finetune/vol_12_im2_clahe.h5@03-finetune/vol_12_im3_clahe.h5@03-finetune/vol_12_im4_clahe.h5@03-finetune/vol_12_im5_clahe.h5@03-finetune/vol_13_im0_clahe.h5@03-finetune/vol_13_im1_clahe.h5@03-finetune/vol_13_im2_clahe.h5@03-finetune/vol_14_im0_clahe.h5@03-finetune/vol_14_im1_clahe.h5@03-finetune/vol_14_im2_clahe.h5@03-finetune/vol_14_im3_clahe.h5@03-finetune/vol_14_im4_clahe.h5@03-finetune/vol_15_im_clahe.h5@05-tile_0-188/6-12_clahe1.h5@05-tile_0-188/6-12_clahe2.h5@05-tile_0-188/6-12_clahe4.h5@05-tile_0-188/6-13_clahe1.h5@05-tile_0-188/6-13_clahe2.h5@05-tile_0-188/6-13_clahe3.h5@05-tile_0-188/6-13_clahe4.h5@05-tile_0-188/6-13_clahe5.h5@05-tile_0-188/6-13_clahe6.h5@05-tile_0-188/6-13_clahe7.h5
19+
LABEL_NAME: 01-initial/vol0_ves.h5@01-initial/vol1_ves.h5@01-initial/vol2_ves.h5@03-finetune/vol_10_ves0.h5@03-finetune/vol_10_ves1.h5@03-finetune/vol_11_ves0.h5@03-finetune/vol_11_ves1.h5@03-finetune/vol_11_ves2.h5@03-finetune/vol_12_ves0.h5@03-finetune/vol_12_ves1.h5@03-finetune/vol_12_ves2.h5@03-finetune/vol_12_ves3.h5@03-finetune/vol_12_ves4.h5@03-finetune/vol_12_ves5.h5@03-finetune/vol_13_ves0.h5@03-finetune/vol_13_ves1.h5@03-finetune/vol_13_ves2.h5@03-finetune/vol_14_ves0.h5@03-finetune/vol_14_ves1.h5@03-finetune/vol_14_ves2.h5@03-finetune/vol_14_ves3.h5@03-finetune/vol_14_ves4.h5@03-finetune/vol_15_ves.h5@05-tile_0-188/6-12_ves1.h5@05-tile_0-188/6-12_ves2.h5@05-tile_0-188/6-12_ves4.h5@05-tile_0-188/6-13_ves1.h5@05-tile_0-188/6-13_ves2.h5@05-tile_0-188/6-13_ves3.h5@05-tile_0-188/6-13_ves4.h5@05-tile_0-188/6-13_ves5.h5@05-tile_0-188/6-13_ves6.h5@05-tile_0-188/6-13_ves7.h5
20+
VALID_MASK_NAME: 01-initial/vol0_mask.h5@01-initial/vol1_mask.h5@01-initial/vol2_mask.h5@03-finetune/vol_10_mask0.h5@03-finetune/vol_10_mask1.h5@03-finetune/vol_11_mask0.h5@03-finetune/vol_11_mask1.h5@03-finetune/vol_11_mask2.h5@03-finetune/vol_12_mask0.h5@03-finetune/vol_12_mask1.h5@03-finetune/vol_12_mask2.h5@03-finetune/vol_12_mask3.h5@03-finetune/vol_12_mask4.h5@03-finetune/vol_12_mask5.h5@03-finetune/vol_13_mask0.h5@03-finetune/vol_13_mask1.h5@03-finetune/vol_13_mask2.h5@03-finetune/vol_14_mask0.h5@03-finetune/vol_14_mask1.h5@03-finetune/vol_14_mask2.h5@03-finetune/vol_14_mask3.h5@03-finetune/vol_14_mask4.h5@03-finetune/vol_15_mask.h5@05-tile_0-188/6-12_mask1.h5@05-tile_0-188/6-12_mask2.h5@05-tile_0-188/6-12_mask4.h5@05-tile_0-188/6-13_mask1.h5@05-tile_0-188/6-13_mask2.h5@05-tile_0-188/6-13_mask3.h5@05-tile_0-188/6-13_mask4.h5@05-tile_0-188/6-13_mask5.h5@05-tile_0-188/6-13_mask6.h5@05-tile_0-188/6-13_mask7.h5
21+
REDUCE_LABEL: False
22+
OUTPUT_PATH: /mmfs1/data/linok/hydra/outputs/large_vesicle/04-more_data
23+
PAD_SIZE: [0, 0, 0]
24+
DATA_SCALE: [1.0, 1.0, 1.0]
25+
VALID_RATIO: 0.1
26+
DISTRIBUTED: False
27+
#REJECT_SAMPLING:
28+
# NUM_TRIAL: 5
29+
SOLVER:
30+
LR_SCHEDULER_NAME: WarmupCosineLR
31+
BASE_LR: 0.02
32+
ITERATION_STEP: 1
33+
ITERATION_SAVE: 10000
34+
ITERATION_TOTAL: 1000000
35+
SAMPLES_PER_BATCH: 96
36+
MONITOR:
37+
ITERATION_NUM: [40, 400]
38+
INFERENCE:
39+
INPUT_SIZE: [9, 65, 65]
40+
OUTPUT_SIZE: [9, 65, 65]
41+
INPUT_PATH: /data/projects/weilab/dataset/hydra/big_vesicle/v0-intial_dataset
42+
OUTPUT_PATH: outputs/large_vesicle/initial_volumes/results
43+
IMAGE_NAME: vol0_im.h5
44+
OUTPUT_NAME: vol0_inf.h5
45+
PAD_SIZE: [0, 0, 0]
46+
AUG_MODE: "mean"
47+
AUG_NUM: 4
48+
STRIDE: [4, 32, 32]
49+
SAMPLES_PER_BATCH: 24

ves_seg/configs/bcd_config.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
MODEL:
2+
OUT_PLANES: 3
3+
TARGET_OPT: ['0','4-0-1', '5-3d-1-0-1.0']
4+
LOSS_OPTION:
5+
- - WeightedBCEWithLogitsLoss
6+
- DiceLoss
7+
- - WeightedBCEWithLogitsLoss
8+
- DiceLoss
9+
- - WeightedMSE
10+
LOSS_WEIGHT: [[1.0, 0.5], [1.0, 0.5], [5.0]]
11+
WEIGHT_OPT: [['1', '0'], ['1', '0'], ['0']]
12+
OUTPUT_ACT: [['none', 'sigmoid'], ['none', 'sigmoid'], ['tanh']]
13+
INFERENCE:
14+
OUTPUT_ACT: ['sigmoid', 'sigmoid', 'tanh']
12.7 MB
Binary file not shown.

ves_seg/sample/11-5_clahe.h5

48 MB
Binary file not shown.

ves_seg/sample/11-5_mask.h5

662 KB
Binary file not shown.

ves_seg/sample/11-5_pred.h5

533 KB
Binary file not shown.

0 commit comments

Comments
 (0)