muchlisinadi commited on
Commit
54f43fd
·
1 Parent(s): 9cb2b92
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ import torch
3
+ from monai.bundle import ConfigParser
4
+ import gradio as gr
5
+ import pickle
6
+ import torchvision.transforms as T
7
+ import numpy as np
8
+ import random
9
+
10
+ parser = ConfigParser()
11
+ parser.read_config(f="configs/inference.json")
12
+ parser.read_meta(f="configs/metadata.json")
13
+
14
+ inference = parser.get_parsed_content("inferer")
15
+ # loader = parser.get_parsed_content("dataloader")
16
+ network = parser.get_parsed_content("network_def")
17
+ preprocess = parser.get_parsed_content("preprocessing")
18
+ postprocess = parser.get_parsed_content("postprocessing")
19
+
20
+ state_dict = torch.load("models/model.pt")
21
+ network.load_state_dict(state_dict, strict=True)
22
+
23
+ label2color = {0: (0, 0, 0),
24
+ 1: (225, 24, 69), # RED
25
+ 2: (135, 233, 17), # GREEN
26
+ 3: (0, 87, 233), # BLUE
27
+ 4: (242, 202, 25), # YELLOW
28
+ 5: (137, 49, 239),} # PURPLE
29
+
30
+ example_files = list(Path("sample_data").glob("*.png"))
31
+
32
+ def visualize_instance_seg_mask(mask):
33
+ image = np.zeros((mask.shape[0], mask.shape[1], 3))
34
+ labels = np.unique(mask)
35
+ for i in range(image.shape[0]):
36
+ for j in range(image.shape[1]):
37
+ image[i, j, :] = label2color[mask[i, j]]
38
+ image = image / 255
39
+ return image
40
+
41
+ def query_image(img, progress=gr.Progress(track_tqdm=True)):
42
+ data = {"image": img}
43
+ batch = preprocess(data)
44
+
45
+ # with open('filename.pickle', 'rb') as handle:
46
+ # pred = pickle.load(handle)
47
+ # batch["pred"] = pred
48
+
49
+ network.eval()
50
+ with torch.no_grad():
51
+ pred = inference(batch['image'].unsqueeze(dim=0), network)
52
+
53
+ batch["pred"] = pred
54
+ for k,v in batch["pred"].items():
55
+ batch["pred"][k] = v.squeeze(dim=0)
56
+
57
+ batch = postprocess(batch)
58
+
59
+ result = visualize_instance_seg_mask(batch["type_map"].squeeze())
60
+
61
+ # Combine image
62
+ result = batch["image"].permute(1, 2, 0).cpu().numpy() * 0.5 + result * 0.5
63
+
64
+ # Solve rotating problem
65
+ result = np.fliplr(result)
66
+ result = np.rot90(result, k=1)
67
+
68
+ return result
69
+
70
+ demo = gr.Interface(
71
+ query_image,
72
+ inputs=[gr.Image(type="filepath")],
73
+ outputs="image",
74
+ title="Medical Image Classification with MONAI - Pathology Nuclei Segmentation Classification",
75
+ description = "Please upload an image to see segmentation capabilities of this model",
76
+ examples=example_files
77
+ )
78
+
79
+ demo.queue(concurrency_count=20).launch()
configs/evaluate.json ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "network_def": {
3
+ "_target_": "HoVerNet",
4
+ "mode": "@hovernet_mode",
5
+ "adapt_standard_resnet": true,
6
+ "in_channels": 3,
7
+ "out_classes": 5
8
+ },
9
+ "validate#handlers": [
10
+ {
11
+ "_target_": "CheckpointLoader",
12
+ "load_path": "$os.path.join(@bundle_root, 'models', 'model.pt')",
13
+ "load_dict": {
14
+ "model": "@network"
15
+ }
16
+ },
17
+ {
18
+ "_target_": "StatsHandler",
19
+ "iteration_log": false
20
+ },
21
+ {
22
+ "_target_": "MetricsSaver",
23
+ "save_dir": "@output_dir",
24
+ "metrics": [
25
+ "val_mean_dice"
26
+ ],
27
+ "metric_details": [
28
+ "val_mean_dice"
29
+ ],
30
+ "batch_transform": "$monai.handlers.from_engine(['image_meta_dict'])",
31
+ "summary_ops": "*"
32
+ }
33
+ ],
34
+ "evaluating": [
35
+ "$setattr(torch.backends.cudnn, 'benchmark', True)",
36
+ "$@validate#evaluator.run()"
37
+ ]
38
+ }
configs/inference-Copy1.json ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "imports": [
3
+ "$import glob",
4
+ "$import os"
5
+ ],
6
+ "bundle_root": "$os.getcwd()",
7
+ "output_dir": ".",
8
+ "dataset_dir": "CoNSeP/Test/Images",
9
+ "num_cpus": 6,
10
+ "batch_size": 1,
11
+ "sw_batch_size": 16,
12
+ "hovernet_mode": "fast",
13
+ "patch_size": 256,
14
+ "out_size": 164,
15
+ "device": "$torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')",
16
+ "network_def": {
17
+ "_target_": "HoVerNet",
18
+ "mode": "@hovernet_mode",
19
+ "adapt_standard_resnet": true,
20
+ "in_channels": 3,
21
+ "out_classes": 5
22
+ },
23
+ "network": "$@network_def.to(@device)",
24
+ "preprocessing": {
25
+ "_target_": "Compose",
26
+ "transforms": [
27
+ {
28
+ "_target_": "LoadImaged",
29
+ "keys": "image",
30
+ "reader": "$monai.data.PILReader",
31
+ "converter": "$lambda x: x.convert('RGB')"
32
+ },
33
+ {
34
+ "_target_": "EnsureChannelFirstd",
35
+ "keys": "image"
36
+ },
37
+ {
38
+ "_target_": "CastToTyped",
39
+ "keys": "image",
40
+ "dtype": "float32"
41
+ },
42
+ {
43
+ "_target_": "ScaleIntensityRanged",
44
+ "keys": "image",
45
+ "a_min": 0.0,
46
+ "a_max": 255.0,
47
+ "b_min": 0.0,
48
+ "b_max": 1.0,
49
+ "clip": true
50
+ }
51
+ ]
52
+ },
53
+ "data_list": "$[{'image': image} for image in glob.glob(os.path.join(@dataset_dir, '*.png'))]",
54
+ "dataset": {
55
+ "_target_": "Dataset",
56
+ "data": "@data_list",
57
+ "transform": "@preprocessing"
58
+ },
59
+ "dataloader": {
60
+ "_target_": "DataLoader",
61
+ "dataset": "@dataset",
62
+ "batch_size": "@batch_size",
63
+ "shuffle": false,
64
+ "num_workers": "@num_cpus",
65
+ "pin_memory": true
66
+ },
67
+ "inferer": {
68
+ "_target_": "SlidingWindowHoVerNetInferer",
69
+ "roi_size": "@patch_size",
70
+ "sw_batch_size": "@sw_batch_size",
71
+ "overlap": "$1.0 - float(@out_size) / float(@patch_size)",
72
+ "padding_mode": "constant",
73
+ "cval": 0,
74
+ "progress": true,
75
+ "extra_input_padding": "$((@patch_size - @out_size) // 2,) * 4"
76
+ },
77
+ "postprocessing": {
78
+ "_target_": "Compose",
79
+ "transforms": [
80
+ {
81
+ "_target_": "FlattenSubKeysd",
82
+ "keys": "pred",
83
+ "sub_keys": [
84
+ "horizontal_vertical",
85
+ "nucleus_prediction",
86
+ "type_prediction"
87
+ ],
88
+ "delete_keys": true
89
+ },
90
+ {
91
+ "_target_": "HoVerNetInstanceMapPostProcessingd",
92
+ "sobel_kernel_size": 21,
93
+ "marker_threshold": 0.4,
94
+ "marker_radius": 2
95
+ },
96
+ {
97
+ "_target_": "HoVerNetNuclearTypePostProcessingd"
98
+ },
99
+ {
100
+ "_target_": "FromMetaTensord",
101
+ "keys": [
102
+ "image"
103
+ ]
104
+ }
105
+ ]
106
+ },
107
+ "handlers": [
108
+ {
109
+ "_target_": "CheckpointLoader",
110
+ "load_path": "$os.path.join(@bundle_root, 'models', 'model.pt')",
111
+ "map_location": "@device",
112
+ "load_dict": {
113
+ "model": "@network"
114
+ }
115
+ }
116
+ ],
117
+ "evaluator": {
118
+ "_target_": "SupervisedEvaluator",
119
+ "device": "@device",
120
+ "val_data_loader": "@dataloader",
121
+ "val_handlers": "@handlers",
122
+ "network": "@network",
123
+ "postprocessing": "@postprocessing",
124
+ "inferer": "@inferer",
125
+ "amp": true
126
+ },
127
+ "evaluating": [
128
+ "$setattr(torch.backends.cudnn, 'benchmark', True)",
129
130
+ ]
131
+ }
configs/inference.json ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "imports": [
3
+ "$import glob",
4
+ "$import os"
5
+ ],
6
+ "bundle_root": "$os.getcwd()",
7
+ "output_dir": ".",
8
+ "dataset_dir": "CoNSeP/Test/Images",
9
+ "num_cpus": 2,
10
+ "batch_size": 1,
11
+ "sw_batch_size": 16,
12
+ "hovernet_mode": "fast",
13
+ "patch_size": 256,
14
+ "out_size": 164,
15
+ "device": "cpu",
16
+ "network_def": {
17
+ "_target_": "HoVerNet",
18
+ "mode": "@hovernet_mode",
19
+ "adapt_standard_resnet": true,
20
+ "in_channels": 3,
21
+ "out_classes": 5
22
+ },
23
+ "network": "$@network_def.to(@device)",
24
+ "preprocessing": {
25
+ "_target_": "Compose",
26
+ "transforms": [
27
+ {
28
+ "_target_": "LoadImaged",
29
+ "keys": "image",
30
+ "reader": "$monai.data.PILReader",
31
+ "converter": "$lambda x: x.convert('RGB')"
32
+ },
33
+ {
34
+ "_target_": "EnsureChannelFirstd",
35
+ "keys": "image"
36
+ },
37
+ {
38
+ "_target_": "CastToTyped",
39
+ "keys": "image",
40
+ "dtype": "float32"
41
+ },
42
+ {
43
+ "_target_": "ScaleIntensityRanged",
44
+ "keys": "image",
45
+ "a_min": 0.0,
46
+ "a_max": 255.0,
47
+ "b_min": 0.0,
48
+ "b_max": 1.0,
49
+ "clip": true
50
+ }
51
+ ]
52
+ },
53
+ "data_list": "$[{'image': image} for image in glob.glob(os.path.join(@dataset_dir, '*.png'))]",
54
+ "dataset": {
55
+ "_target_": "Dataset",
56
+ "data": "@data_list",
57
+ "transform": "@preprocessing"
58
+ },
59
+ "dataloader": {
60
+ "_target_": "DataLoader",
61
+ "dataset": "@dataset",
62
+ "batch_size": "@batch_size",
63
+ "shuffle": false,
64
+ "num_workers": "@num_cpus",
65
+ "pin_memory": true
66
+ },
67
+ "inferer": {
68
+ "_target_": "SlidingWindowHoVerNetInferer",
69
+ "roi_size": "@patch_size",
70
+ "sw_batch_size": "@sw_batch_size",
71
+ "overlap": "$1.0 - float(@out_size) / float(@patch_size)",
72
+ "padding_mode": "constant",
73
+ "cval": 0,
74
+ "progress": true,
75
+ "extra_input_padding": "$((@patch_size - @out_size) // 2,) * 4"
76
+ },
77
+ "postprocessing": {
78
+ "_target_": "Compose",
79
+ "transforms": [
80
+ {
81
+ "_target_": "FlattenSubKeysd",
82
+ "keys": "pred",
83
+ "sub_keys": [
84
+ "horizontal_vertical",
85
+ "nucleus_prediction",
86
+ "type_prediction"
87
+ ],
88
+ "delete_keys": true
89
+ },
90
+ {
91
+ "_target_": "HoVerNetInstanceMapPostProcessingd",
92
+ "sobel_kernel_size": 21,
93
+ "marker_threshold": 0.4,
94
+ "marker_radius": 2
95
+ },
96
+ {
97
+ "_target_": "HoVerNetNuclearTypePostProcessingd"
98
+ },
99
+ {
100
+ "_target_": "FromMetaTensord",
101
+ "keys": [
102
+ "image"
103
+ ]
104
+ }
105
+ ]
106
+ },
107
+ "handlers": [
108
+ {
109
+ "_target_": "CheckpointLoader",
110
+ "load_path": "$os.path.join(@bundle_root, 'models', 'model.pt')",
111
+ "map_location": "@device",
112
+ "load_dict": {
113
+ "model": "@network"
114
+ }
115
+ }
116
+ ],
117
+ "evaluator": {
118
+ "_target_": "SupervisedEvaluator",
119
+ "device": "@device",
120
+ "val_data_loader": "@dataloader",
121
+ "val_handlers": "@handlers",
122
+ "network": "@network",
123
+ "postprocessing": "@postprocessing",
124
+ "inferer": "@inferer",
125
+ "amp": true
126
+ },
127
+ "evaluating": [
128
+ "$setattr(torch.backends.cudnn, 'benchmark', True)",
129
130
+ ]
131
+ }
configs/logging.conf ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [loggers]
2
+ keys=root
3
+
4
+ [handlers]
5
+ keys=consoleHandler
6
+
7
+ [formatters]
8
+ keys=fullFormatter
9
+
10
+ [logger_root]
11
+ level=INFO
12
+ handlers=consoleHandler
13
+
14
+ [handler_consoleHandler]
15
+ class=StreamHandler
16
+ level=INFO
17
+ formatter=fullFormatter
18
+ args=(sys.stdout,)
19
+
20
+ [formatter_fullFormatter]
21
+ format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
configs/metadata.json ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "schema": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/meta_schema_hovernet_20221124.json",
3
+ "version": "0.1.3",
4
+ "changelog": {
5
+ "0.1.3": "add name tag",
6
+ "0.1.2": "update the workflow figure",
7
+ "0.1.1": "update to use monai 1.1.0",
8
+ "0.1.0": "complete the model package"
9
+ },
10
+ "monai_version": "1.1.0",
11
+ "pytorch_version": "1.13.0",
12
+ "numpy_version": "1.22.2",
13
+ "optional_packages_version": {
14
+ "scikit-image": "0.19.3",
15
+ "scipy": "1.8.1",
16
+ "tqdm": "4.64.1",
17
+ "pillow": "9.0.1"
18
+ },
19
+ "name": "Nuclear segmentation and classification",
20
+ "task": "Nuclear segmentation and classification",
21
+ "description": "A simultaneous segmentation and classification of nuclei within multitissue histology images based on CoNSeP data",
22
+ "authors": "MONAI team",
23
+ "copyright": "Copyright (c) MONAI Consortium",
24
+ "data_source": "https://warwick.ac.uk/fac/cross_fac/tia/data/hovernet/",
25
+ "data_type": "numpy",
26
+ "image_classes": "RGB image with intensity between 0 and 255",
27
+ "label_classes": "a dictionary contains binary nuclear segmentation, hover map and pixel-level classification",
28
+ "pred_classes": "a dictionary contains scalar probability for binary nuclear segmentation, hover map and pixel-level classification",
29
+ "eval_metrics": {
30
+ "Binary Dice": 0.8293,
31
+ "PQ": 0.4936,
32
+ "F1d": 0.748
33
+ },
34
+ "intended_use": "This is an example, not to be used for diagnostic purposes",
35
+ "references": [
36
+ "Simon Graham. 'HoVer-Net: Simultaneous Segmentation and Classification of Nuclei in Multi-Tissue Histology Images.' Medical Image Analysis, 2019. https://arxiv.org/abs/1812.06499"
37
+ ],
38
+ "network_data_format": {
39
+ "inputs": {
40
+ "image": {
41
+ "type": "image",
42
+ "format": "magnitude",
43
+ "num_channels": 3,
44
+ "spatial_shape": [
45
+ "256",
46
+ "256"
47
+ ],
48
+ "dtype": "float32",
49
+ "value_range": [
50
+ 0,
51
+ 255
52
+ ],
53
+ "is_patch_data": true,
54
+ "channel_def": {
55
+ "0": "image"
56
+ }
57
+ }
58
+ },
59
+ "outputs": {
60
+ "nucleus_prediction": {
61
+ "type": "probability",
62
+ "format": "segmentation",
63
+ "num_channels": 3,
64
+ "spatial_shape": [
65
+ "164",
66
+ "164"
67
+ ],
68
+ "dtype": "float32",
69
+ "value_range": [
70
+ 0,
71
+ 1
72
+ ],
73
+ "is_patch_data": true,
74
+ "channel_def": {
75
+ "0": "background",
76
+ "1": "nuclei"
77
+ }
78
+ },
79
+ "horizontal_vertical": {
80
+ "type": "probability",
81
+ "format": "regression",
82
+ "num_channels": 2,
83
+ "spatial_shape": [
84
+ "164",
85
+ "164"
86
+ ],
87
+ "dtype": "float32",
88
+ "value_range": [
89
+ 0,
90
+ 1
91
+ ],
92
+ "is_patch_data": true,
93
+ "channel_def": {
94
+ "0": "horizontal distances map",
95
+ "1": "vertical distances map"
96
+ }
97
+ },
98
+ "type_prediction": {
99
+ "type": "probability",
100
+ "format": "classification",
101
+ "num_channels": 2,
102
+ "spatial_shape": [
103
+ "164",
104
+ "164"
105
+ ],
106
+ "dtype": "float32",
107
+ "value_range": [
108
+ 0,
109
+ 1
110
+ ],
111
+ "is_patch_data": true,
112
+ "channel_def": {
113
+ "0": "background",
114
+ "1": "type of nucleus for each pixel"
115
+ }
116
+ }
117
+ }
118
+ }
119
+ }
configs/multi_gpu_train.json ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "device": "$torch.device(f'cuda:{dist.get_rank()}')",
3
+ "network": {
4
+ "_target_": "torch.nn.parallel.DistributedDataParallel",
5
+ "module": "$@network_def.to(@device)",
6
+ "device_ids": [
7
+ "@device"
8
+ ]
9
+ },
10
+ "train#sampler": {
11
+ "_target_": "DistributedSampler",
12
+ "dataset": "@train#dataset",
13
+ "even_divisible": true,
14
+ "shuffle": true
15
+ },
16
+ "train#dataloader#sampler": "@train#sampler",
17
+ "train#dataloader#shuffle": false,
18
+ "train#trainer#train_handlers": "$@train#train_handlers[: -2 if dist.get_rank() > 0 else None]",
19
+ "validate#sampler": {
20
+ "_target_": "DistributedSampler",
21
+ "dataset": "@validate#dataset",
22
+ "even_divisible": false,
23
+ "shuffle": false
24
+ },
25
+ "validate#dataloader#sampler": "@validate#sampler",
26
+ "validate#evaluator#val_handlers": "$None if dist.get_rank() > 0 else @validate#handlers",
27
+ "training": [
28
+ "$import torch.distributed as dist",
29
+ "$dist.init_process_group(backend='nccl')",
30
+ "$torch.cuda.set_device(@device)",
31
+ "$monai.utils.set_determinism(seed=321)",
32
+ "$setattr(torch.backends.cudnn, 'benchmark', True)",
33
+ "$@train#trainer.run()",
34
+ "$dist.destroy_process_group()"
35
+ ]
36
+ }
configs/train.json ADDED
@@ -0,0 +1,525 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "imports": [
3
+ "$import glob",
4
+ "$import os",
5
+ "$import skimage"
6
+ ],
7
+ "bundle_root": "$os.getcwd()",
8
+ "ckpt_dir_stage0": "$os.path.join(@bundle_root, 'models', 'stage0')",
9
+ "ckpt_dir_stage1": "$os.path.join(@bundle_root, 'models')",
10
+ "ckpt_path_stage0": "$os.path.join(@ckpt_dir_stage0, 'model.pt')",
11
+ "output_dir": "$os.path.join(@bundle_root, 'eval')",
12
+ "dataset_dir": "/workspace/Data/Pathology/CoNSeP/Prepared/",
13
+ "train_images": "$list(sorted(glob.glob(@dataset_dir + '/Train/*image.npy')))",
14
+ "val_images": "$list(sorted(glob.glob(@dataset_dir + '/Test/*image.npy')))",
15
+ "train_inst_map": "$list(sorted(glob.glob(@dataset_dir + '/Train/*inst_map.npy')))",
16
+ "val_inst_map": "$list(sorted(glob.glob(@dataset_dir + '/Test/*inst_map.npy')))",
17
+ "train_type_map": "$list(sorted(glob.glob(@dataset_dir + '/Train/*type_map.npy')))",
18
+ "val_type_map": "$list(sorted(glob.glob(@dataset_dir + '/Test/*type_map.npy')))",
19
+ "device": "$torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')",
20
+ "stage": 0,
21
+ "epochs": 50,
22
+ "val_interval": 1,
23
+ "learning_rate": 0.0001,
24
+ "amp": true,
25
+ "hovernet_mode": "fast",
26
+ "patch_size": 256,
27
+ "out_size": 164,
28
+ "ckpt_dir": "$@ckpt_dir_stage0 if @stage == 0 else @ckpt_dir_stage1",
29
+ "network_def": {
30
+ "_target_": "HoVerNet",
31
+ "mode": "@hovernet_mode",
32
+ "in_channels": 3,
33
+ "out_classes": 5,
34
+ "adapt_standard_resnet": true,
35
+ "pretrained_url": "$None",
36
+ "freeze_encoder": true
37
+ },
38
+ "network": "$@network_def.to(@device)",
39
+ "loss": {
40
+ "_target_": "HoVerNetLoss",
41
+ "lambda_hv_mse": 1.0
42
+ },
43
+ "optimizer": {
44
+ "_target_": "torch.optim.Adam",
45
+ "params": "$filter(lambda p: p.requires_grad, @network.parameters())",
46
+ "lr": "@learning_rate",
47
+ "weight_decay": 1e-05
48
+ },
49
+ "lr_scheduler": {
50
+ "_target_": "torch.optim.lr_scheduler.StepLR",
51
+ "optimizer": "@optimizer",
52
+ "step_size": 25
53
+ },
54
+ "train": {
55
+ "preprocessing_transforms": [
56
+ {
57
+ "_target_": "LoadImaged",
58
+ "keys": [
59
+ "image",
60
+ "label_inst",
61
+ "label_type"
62
+ ]
63
+ },
64
+ {
65
+ "_target_": "EnsureChannelFirstd",
66
+ "keys": [
67
+ "image",
68
+ "label_inst",
69
+ "label_type"
70
+ ],
71
+ "channel_dim": -1
72
+ },
73
+ {
74
+ "_target_": "Lambdad",
75
+ "keys": "label_inst",
76
+ "func": "$lambda x: skimage.measure.label(x)"
77
+ },
78
+ {
79
+ "_target_": "RandAffined",
80
+ "keys": [
81
+ "image",
82
+ "label_inst",
83
+ "label_type"
84
+ ],
85
+ "prob": 1.0,
86
+ "rotate_range": [
87
+ "$np.pi"
88
+ ],
89
+ "scale_range": [
90
+ [
91
+ -0.2,
92
+ 0.2
93
+ ],
94
+ [
95
+ -0.2,
96
+ 0.2
97
+ ]
98
+ ],
99
+ "shear_range": [
100
+ [
101
+ -0.05,
102
+ 0.05
103
+ ],
104
+ [
105
+ -0.05,
106
+ 0.05
107
+ ]
108
+ ],
109
+ "translate_range": [
110
+ [
111
+ -6,
112
+ 6
113
+ ],
114
+ [
115
+ -6,
116
+ 6
117
+ ]
118
+ ],
119
+ "padding_mode": "zeros",
120
+ "mode": "nearest"
121
+ },
122
+ {
123
+ "_target_": "CenterSpatialCropd",
124
+ "keys": [
125
+ "image"
126
+ ],
127
+ "roi_size": [
128
+ "@patch_size",
129
+ "@patch_size"
130
+ ]
131
+ },
132
+ {
133
+ "_target_": "RandFlipd",
134
+ "keys": [
135
+ "image",
136
+ "label_inst",
137
+ "label_type"
138
+ ],
139
+ "prob": 0.5,
140
+ "spatial_axis": 0
141
+ },
142
+ {
143
+ "_target_": "RandFlipd",
144
+ "keys": [
145
+ "image",
146
+ "label_inst",
147
+ "label_type"
148
+ ],
149
+ "prob": 0.5,
150
+ "spatial_axis": 1
151
+ },
152
+ {
153
+ "_target_": "OneOf",
154
+ "transforms": [
155
+ {
156
+ "_target_": "RandGaussianSmoothd",
157
+ "keys": [
158
+ "image"
159
+ ],
160
+ "sigma_x": [
161
+ 0.1,
162
+ 1.1
163
+ ],
164
+ "sigma_y": [
165
+ 0.1,
166
+ 1.1
167
+ ],
168
+ "prob": 1.0
169
+ },
170
+ {
171
+ "_target_": "MedianSmoothd",
172
+ "keys": [
173
+ "image"
174
+ ],
175
+ "radius": 1
176
+ },
177
+ {
178
+ "_target_": "RandGaussianNoised",
179
+ "keys": [
180
+ "image"
181
+ ],
182
+ "std": 0.05,
183
+ "prob": 1.0
184
+ }
185
+ ]
186
+ },
187
+ {
188
+ "_target_": "CastToTyped",
189
+ "keys": "image",
190
+ "dtype": "$np.uint8"
191
+ },
192
+ {
193
+ "_target_": "TorchVisiond",
194
+ "keys": "image",
195
+ "name": "ColorJitter",
196
+ "brightness": [
197
+ 0.9,
198
+ 1.0
199
+ ],
200
+ "contrast": [
201
+ 0.95,
202
+ 1.1
203
+ ],
204
+ "saturation": [
205
+ 0.8,
206
+ 1.2
207
+ ],
208
+ "hue": [
209
+ -0.04,
210
+ 0.04
211
+ ]
212
+ },
213
+ {
214
+ "_target_": "AsDiscreted",
215
+ "keys": "label_type",
216
+ "to_onehot": 5
217
+ },
218
+ {
219
+ "_target_": "ScaleIntensityRanged",
220
+ "keys": "image",
221
+ "a_min": 0.0,
222
+ "a_max": 255.0,
223
+ "b_min": 0.0,
224
+ "b_max": 1.0,
225
+ "clip": true
226
+ },
227
+ {
228
+ "_target_": "CastToTyped",
229
+ "keys": "label_inst",
230
+ "dtype": "$torch.int"
231
+ },
232
+ {
233
+ "_target_": "ComputeHoVerMapsd",
234
+ "keys": "label_inst"
235
+ },
236
+ {
237
+ "_target_": "Lambdad",
238
+ "keys": "label_inst",
239
+ "func": "$lambda x: x > 0",
240
+ "overwrite": "label"
241
+ },
242
+ {
243
+ "_target_": "CenterSpatialCropd",
244
+ "keys": [
245
+ "label",
246
+ "hover_label_inst",
247
+ "label_inst",
248
+ "label_type"
249
+ ],
250
+ "roi_size": [
251
+ "@out_size",
252
+ "@out_size"
253
+ ]
254
+ },
255
+ {
256
+ "_target_": "AsDiscreted",
257
+ "keys": "label",
258
+ "to_onehot": 2
259
+ },
260
+ {
261
+ "_target_": "CastToTyped",
262
+ "keys": [
263
+ "image",
264
+ "label_inst",
265
+ "label_type"
266
+ ],
267
+ "dtype": "$torch.float32"
268
+ }
269
+ ],
270
+ "preprocessing": {
271
+ "_target_": "Compose",
272
+ "transforms": "$@train#preprocessing_transforms"
273
+ },
274
+ "dataset": {
275
+ "_target_": "Dataset",
276
+ "data": "$[{'image': i, 'label_inst': j, 'label_type': k} for i, j, k in zip(@train_images, @train_inst_map, @train_type_map)]",
277
+ "transform": "@train#preprocessing"
278
+ },
279
+ "dataloader": {
280
+ "_target_": "DataLoader",
281
+ "dataset": "@train#dataset",
282
+ "batch_size": 16,
283
+ "shuffle": true,
284
+ "num_workers": 4
285
+ },
286
+ "inferer": {
287
+ "_target_": "SimpleInferer"
288
+ },
289
+ "postprocessing_np": {
290
+ "_target_": "Compose",
291
+ "transforms": [
292
+ {
293
+ "_target_": "Activationsd",
294
+ "keys": "nucleus_prediction",
295
+ "softmax": true
296
+ },
297
+ {
298
+ "_target_": "AsDiscreted",
299
+ "keys": "nucleus_prediction",
300
+ "argmax": true
301
+ }
302
+ ]
303
+ },
304
+ "postprocessing": {
305
+ "_target_": "Lambdad",
306
+ "keys": "pred",
307
+ "func": "$@train#postprocessing_np"
308
+ },
309
+ "handlers": [
310
+ {
311
+ "_target_": "LrScheduleHandler",
312
+ "lr_scheduler": "@lr_scheduler",
313
+ "print_lr": true
314
+ },
315
+ {
316
+ "_target_": "ValidationHandler",
317
+ "validator": "@validate#evaluator",
318
+ "epoch_level": true,
319
+ "interval": "@val_interval"
320
+ },
321
+ {
322
+ "_target_": "CheckpointSaver",
323
+ "save_dir": "@ckpt_dir",
324
+ "save_dict": {
325
+ "model": "@network"
326
+ },
327
+ "save_interval": 10,
328
+ "epoch_level": true,
329
+ "save_final": true,
330
+ "final_filename": "model.pt"
331
+ },
332
+ {
333
+ "_target_": "StatsHandler",
334
+ "tag_name": "train_loss",
335
+ "output_transform": "$monai.handlers.from_engine(['loss'], first=True)"
336
+ },
337
+ {
338
+ "_target_": "TensorBoardStatsHandler",
339
+ "log_dir": "@output_dir",
340
+ "tag_name": "train_loss",
341
+ "output_transform": "$monai.handlers.from_engine(['loss'], first=True)"
342
+ }
343
+ ],
344
+ "extra_handlers": [
345
+ {
346
+ "_target_": "CheckpointLoader",
347
+ "load_path": "$os.path.join(@ckpt_dir_stage0, 'model.pt')",
348
+ "load_dict": {
349
+ "model": "@network"
350
+ }
351
+ }
352
+ ],
353
+ "train_handlers": "$@train#extra_handlers + @train#handlers if @stage==1 else @train#handlers",
354
+ "key_metric": {
355
+ "train_mean_dice": {
356
+ "_target_": "MeanDice",
357
+ "include_background": false,
358
+ "output_transform": "$monai.apps.pathology.handlers.utils.from_engine_hovernet(keys=['pred', 'label'], nested_key='nucleus_prediction')"
359
+ }
360
+ },
361
+ "trainer": {
362
+ "_target_": "SupervisedTrainer",
363
+ "max_epochs": "@epochs",
364
+ "device": "@device",
365
+ "train_data_loader": "@train#dataloader",
366
+ "prepare_batch": "$monai.apps.pathology.engines.utils.PrepareBatchHoVerNet(extra_keys=['label_type', 'hover_label_inst'])",
367
+ "network": "@network",
368
+ "loss_function": "@loss",
369
+ "optimizer": "@optimizer",
370
+ "inferer": "@train#inferer",
371
+ "postprocessing": "@train#postprocessing",
372
+ "key_train_metric": "@train#key_metric",
373
+ "train_handlers": "@train#train_handlers",
374
+ "amp": "@amp"
375
+ }
376
+ },
377
+ "validate": {
378
+ "preprocessing_transforms": [
379
+ {
380
+ "_target_": "LoadImaged",
381
+ "keys": [
382
+ "image",
383
+ "label_inst",
384
+ "label_type"
385
+ ]
386
+ },
387
+ {
388
+ "_target_": "EnsureChannelFirstd",
389
+ "keys": [
390
+ "image",
391
+ "label_inst",
392
+ "label_type"
393
+ ],
394
+ "channel_dim": -1
395
+ },
396
+ {
397
+ "_target_": "Lambdad",
398
+ "keys": "label_inst",
399
+ "func": "$lambda x: skimage.measure.label(x)"
400
+ },
401
+ {
402
+ "_target_": "CastToTyped",
403
+ "keys": [
404
+ "image",
405
+ "label_inst"
406
+ ],
407
+ "dtype": "$torch.int"
408
+ },
409
+ {
410
+ "_target_": "CenterSpatialCropd",
411
+ "keys": [
412
+ "image"
413
+ ],
414
+ "roi_size": [
415
+ "@patch_size",
416
+ "@patch_size"
417
+ ]
418
+ },
419
+ {
420
+ "_target_": "ScaleIntensityRanged",
421
+ "keys": "image",
422
+ "a_min": 0.0,
423
+ "a_max": 255.0,
424
+ "b_min": 0.0,
425
+ "b_max": 1.0,
426
+ "clip": true
427
+ },
428
+ {
429
+ "_target_": "ComputeHoVerMapsd",
430
+ "keys": "label_inst"
431
+ },
432
+ {
433
+ "_target_": "Lambdad",
434
+ "keys": "label_inst",
435
+ "func": "$lambda x: x > 0",
436
+ "overwrite": "label"
437
+ },
438
+ {
439
+ "_target_": "CenterSpatialCropd",
440
+ "keys": [
441
+ "label",
442
+ "hover_label_inst",
443
+ "label_inst",
444
+ "label_type"
445
+ ],
446
+ "roi_size": [
447
+ "@out_size",
448
+ "@out_size"
449
+ ]
450
+ },
451
+ {
452
+ "_target_": "CastToTyped",
453
+ "keys": [
454
+ "image",
455
+ "label_inst",
456
+ "label_type"
457
+ ],
458
+ "dtype": "$torch.float32"
459
+ }
460
+ ],
461
+ "preprocessing": {
462
+ "_target_": "Compose",
463
+ "transforms": "$@validate#preprocessing_transforms"
464
+ },
465
+ "dataset": {
466
+ "_target_": "Dataset",
467
+ "data": "$[{'image': i, 'label_inst': j, 'label_type': k} for i, j, k in zip(@val_images, @val_inst_map, @val_type_map)]",
468
+ "transform": "@validate#preprocessing"
469
+ },
470
+ "dataloader": {
471
+ "_target_": "DataLoader",
472
+ "dataset": "@validate#dataset",
473
+ "batch_size": 16,
474
+ "shuffle": false,
475
+ "num_workers": 4
476
+ },
477
+ "inferer": {
478
+ "_target_": "SimpleInferer"
479
+ },
480
+ "postprocessing": "$@train#postprocessing",
481
+ "handlers": [
482
+ {
483
+ "_target_": "StatsHandler",
484
+ "iteration_log": false
485
+ },
486
+ {
487
+ "_target_": "TensorBoardStatsHandler",
488
+ "log_dir": "@output_dir",
489
+ "iteration_log": false
490
+ },
491
+ {
492
+ "_target_": "CheckpointSaver",
493
+ "save_dir": "@ckpt_dir",
494
+ "save_dict": {
495
+ "model": "@network"
496
+ },
497
+ "save_key_metric": true
498
+ }
499
+ ],
500
+ "key_metric": {
501
+ "val_mean_dice": {
502
+ "_target_": "MeanDice",
503
+ "include_background": false,
504
+ "output_transform": "$monai.apps.pathology.handlers.utils.from_engine_hovernet(keys=['pred', 'label'], nested_key='nucleus_prediction')"
505
+ }
506
+ },
507
+ "evaluator": {
508
+ "_target_": "SupervisedEvaluator",
509
+ "device": "@device",
510
+ "val_data_loader": "@validate#dataloader",
511
+ "prepare_batch": "$monai.apps.pathology.engines.utils.PrepareBatchHoVerNet(extra_keys=['label_type', 'hover_label_inst'])",
512
+ "network": "@network",
513
+ "inferer": "@validate#inferer",
514
+ "postprocessing": "@validate#postprocessing",
515
+ "key_val_metric": "@validate#key_metric",
516
+ "val_handlers": "@validate#handlers",
517
+ "amp": "@amp"
518
+ }
519
+ },
520
+ "training": [
521
+ "$monai.utils.set_determinism(seed=321)",
522
+ "$setattr(torch.backends.cudnn, 'benchmark', True)",
523
+ "$@train#trainer.run()"
524
+ ]
525
+ }
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ monai==1.1.0
2
+ scikit-image==0.20.0