Skip to content

Commit f87bb12

Browse files
Fix evaluation in HoVerNet tutorial (#1127)
Part of [#5774](Project-MONAI/MONAI#5774). ### Description Add `EnsureChannelFirstd` Update to argmax ### Checks <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [ ] Notebook runs automatically `./runner [-p <regex_pattern>]` Signed-off-by: KumoLiu <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 683960e commit f87bb12

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

pathology/hovernet/evaluation.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
# Copyright (c) MONAI Consortium
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
# Unless required by applicable law or agreed to in writing, software
7+
# distributed under the License is distributed on an "AS IS" BASIS,
8+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
# See the License for the specific language governing permissions and
10+
# limitations under the License.
11+
112
import os
213
import glob
314
import logging
@@ -8,7 +19,9 @@
819
from monai.engines import SupervisedEvaluator
920
from monai.transforms import (
1021
LoadImaged,
22+
EnsureChannelFirstd,
1123
Lambdad,
24+
AsDiscreted,
1225
Activationsd,
1326
Compose,
1427
CastToTyped,
@@ -54,6 +67,7 @@ def run(cfg):
5467
val_transforms = Compose(
5568
[
5669
LoadImaged(keys=["image", "label_inst", "label_type"], image_only=True),
70+
EnsureChannelFirstd(keys=["image", "label_inst", "label_type"], channel_dim=-1),
5771
Lambdad(keys="label_inst", func=lambda x: measure.label(x)),
5872
CastToTyped(keys=["image", "label_inst"], dtype=torch.int),
5973
CenterSpatialCropd(
@@ -92,14 +106,14 @@ def run(cfg):
92106
post_process_np = Compose(
93107
[
94108
Activationsd(keys=HoVerNetBranch.NP.value, softmax=True),
95-
Lambdad(keys=HoVerNetBranch.NP.value, func=lambda x: x[1:2, ...] > 0.5),
109+
AsDiscreted(keys=HoVerNetBranch.NP.value, argmax=True),
96110
]
97111
)
98112
post_process = Lambdad(keys="pred", func=post_process_np)
99113

100114
# Evaluator
101115
val_handlers = [
102-
CheckpointLoader(load_path=cfg["ckpt"], load_dict={"net": model}),
116+
CheckpointLoader(load_path=cfg["ckpt"], load_dict={"model": model}),
103117
StatsHandler(output_transform=lambda x: None),
104118
]
105119
evaluator = SupervisedEvaluator(

pathology/hovernet/inference.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1+
# Copyright (c) MONAI Consortium
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
# Unless required by applicable law or agreed to in writing, software
7+
# distributed under the License is distributed on an "AS IS" BASIS,
8+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
# See the License for the specific language governing permissions and
10+
# limitations under the License.
11+
112
import logging
213
import os
3-
import time
414
from argparse import ArgumentParser
515
from glob import glob
616

@@ -176,7 +186,7 @@ def main():
176186
parser.add_argument(
177187
"--root",
178188
type=str,
179-
default="/workspace/Data/Pathology/CoNSeP/Test/Images",
189+
default="/workspace/Data/CoNSeP/Test/Images",
180190
help="Images root dir",
181191
)
182192
parser.add_argument("--output", type=str, default="./eval/", dest="output", help="log directory")

pathology/hovernet/prepare_patches.py

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
# Copyright (c) MONAI Consortium
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
# Unless required by applicable law or agreed to in writing, software
7+
# distributed under the License is distributed on an "AS IS" BASIS,
8+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
# See the License for the specific language governing permissions and
10+
# limitations under the License.
11+
112
import glob
213
import math
314
import os

pathology/hovernet/training.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1+
# Copyright (c) MONAI Consortium
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
# Unless required by applicable law or agreed to in writing, software
7+
# distributed under the License is distributed on an "AS IS" BASIS,
8+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
# See the License for the specific language governing permissions and
10+
# limitations under the License.
11+
112
import os
213
import glob
3-
import time
414
import logging
515
import torch
616
import numpy as np

0 commit comments

Comments
 (0)