-
Notifications
You must be signed in to change notification settings - Fork 697
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Nic Ma <nma@nvidia.com>
- Loading branch information
Showing
9 changed files
with
203 additions
and
19 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
modules/model_package/spleen_segmentation/commands/inference_v2.sh
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,4 @@ | ||
python ../scripts/inference.py | ||
--base_config ../configs/inference.json | ||
--config ../configs/inference_v2.json | ||
--meta ../configs/metadata.json |
3 changes: 3 additions & 0 deletions
3
modules/model_package/spleen_segmentation/commands/trtinfer.sh
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,3 @@ | ||
python ../scripts/inference.py | ||
--config ../configs/trtinfer.json | ||
--meta ../configs/metadata.json |
2 changes: 1 addition & 1 deletion
2
modules/model_package/spleen_segmentation/configs/inference.json
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
23 changes: 23 additions & 0 deletions
23
modules/model_package/spleen_segmentation/configs/inference_v2.json
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,23 @@ | ||
{ | ||
"amp": false, | ||
"network": { | ||
"name": "UNet", | ||
"args": { | ||
"spatial_dims": 3, | ||
"in_channels": 1, | ||
"out_channels": 2, | ||
"channels": [32, 64, 128, 256, 512], | ||
"strides": [2, 2, 2, 2], | ||
"num_res_units": 2, | ||
"norm": "group" | ||
} | ||
}, | ||
"inferer": { | ||
"name": "SlidingWindowInferer", | ||
"args": { | ||
"roi_size": [96, 96, 96], | ||
"sw_batch_size": 4, | ||
"overlap": 0.6 | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
modules/model_package/spleen_segmentation/configs/trtinfer.json
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,19 @@ | ||
{ | ||
"preprocessing": { | ||
"ref": { | ||
"path": "../inference.json/preprocessing" | ||
} | ||
}, | ||
"dataset": { | ||
"ref": { | ||
"path": "../inference.json/dataset" | ||
} | ||
}, | ||
"model": "#load_trt_model(...)", | ||
"dataloader": { | ||
"name": "DALIpipeline" | ||
}, | ||
"inferer": { | ||
"name": "TensorRTInferer" | ||
} | ||
} |
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
67 changes: 67 additions & 0 deletions
67
modules/model_package/spleen_segmentation/scripts/inference_v2.py
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,67 @@ | ||
|
||
# Copyright 2020 - 2021 MONAI Consortium | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import argparse | ||
import json | ||
|
||
import torch | ||
from monai.apps import ConfigParser | ||
from monai.data import decollate_batch | ||
from monai.inferers import Inferer | ||
from monai.transforms import Transform | ||
from monai.utils.enums import CommonKeys | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('--base_config', '-c', type=str, help='file path of base config', required=False) | ||
parser.add_argument('--config', '-c', type=str, help='config file to override base config', required=True) | ||
parser.add_argument('--meta', '-e', type=str, help='file path of the meta data') | ||
args = parser.parse_args() | ||
|
||
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | ||
configs = {} | ||
|
||
# load meta data | ||
with open(args.meta, "r") as f: | ||
configs.update(json.load(f)) | ||
# load base config file, can override meta data in config | ||
with open(args.base_config, "r") as f: | ||
configs.update(json.load(f)) | ||
# load config file, add or override the content of base config | ||
with open(args.config, "r") as f: | ||
configs.update(json.load(f)) | ||
|
||
model: torch.nn.Module = None | ||
dataloader: torch.utils.data.DataLoader = None | ||
inferer: Inferer = None | ||
postprocessing: Transform = None | ||
# TODO: parse inference config file and construct instances | ||
config_parser = ConfigParser(configs) | ||
# instantialize the components immediately | ||
model = config_parser.get_instance("model").to(device) | ||
dataloader = config_parser.get_instance("dataloader") | ||
inferer = config_parser.get_instance("inferer") | ||
postprocessing = config_parser.get_instance("postprocessing") | ||
|
||
model.eval() | ||
with torch.no_grad(): | ||
for d in dataloader: | ||
images = d[CommonKeys.IMAGE].to(device) | ||
# define sliding window size and batch size for windows inference | ||
d[CommonKeys.PRED] = inferer(inputs=images, predictor=model) | ||
# decollate the batch data into a list of dictionaries, then execute postprocessing transforms | ||
[postprocessing(i) for i in decollate_batch(d)] | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
59 changes: 59 additions & 0 deletions
59
modules/model_package/spleen_segmentation/scripts/trtinfer.py
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,59 @@ | ||
|
||
# Copyright 2020 - 2021 MONAI Consortium | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import argparse | ||
import json | ||
|
||
import torch | ||
from monai.apps import ConfigParser | ||
from monai.data import decollate_batch | ||
from monai.transforms import Transform | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('--config', '-c', type=str, help='config file that defines components', required=True) | ||
parser.add_argument('--meta', '-e', type=str, help='file path of the meta data') | ||
args = parser.parse_args() | ||
|
||
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | ||
configs = {} | ||
|
||
# load meta data | ||
with open(args.meta, "r") as f: | ||
configs.update(json.load(f)) | ||
# load config file, can override meta data in config | ||
with open(args.config, "r") as f: | ||
configs.update(json.load(f)) | ||
|
||
# fake code to simulate TensorRT and DALI logic | ||
model: TRTModel = None | ||
dataloader: DALIpipeline = None | ||
inferer: TRTInfer = None | ||
postprocessing: Transform = None | ||
# TODO: parse inference config file and construct instances | ||
config_parser = ConfigParser(configs) | ||
|
||
# instantialize the components immediately | ||
model = config_parser.get_instance("model").to(device) | ||
dataloader = config_parser.get_instance("dataloader") | ||
inferer = config_parser.get_instance("inferer") | ||
postprocessing = config_parser.get_instance("postprocessing") | ||
|
||
# simuluate TensorRT and DALI logic | ||
for d in dataloader: | ||
r = inferer(inputs=d, predictor=model) | ||
[postprocessing(i) for i in decollate_batch(r)] | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |