-
Notifications
You must be signed in to change notification settings - Fork 1.4k
156 event handlers support arbitrary format #159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a75d891
[DLMED] add arbitrary format support for all event-handlers
Nic-Ma c7b3168
[DLMED] update StatsHandler and fix comments
Nic-Ma df66cba
[DLMED] update according to comments
Nic-Ma cdba27f
Merge branch 'master' into 156-Event-handlers-support-arbitrary-format
Nic-Ma 2d13d1d
[DLMED] fix integration test error
Nic-Ma 476e533
[DLMED] support both Tensor and Float loss value
Nic-Ma 50dadec
[DLMED] add dict based UNet inference example
Nic-Ma 6384f6f
Merge branch 'master' into 156-Event-handlers-support-arbitrary-format
wyli 2a85d9d
update docstring
wyli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,95 @@ | ||
| # Copyright 2020 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 os | ||
| import sys | ||
| import tempfile | ||
| from glob import glob | ||
|
|
||
| import nibabel as nib | ||
| import numpy as np | ||
| import torch | ||
| from ignite.engine import Engine | ||
| from torch.utils.data import DataLoader | ||
|
|
||
| # assumes the framework is found here, change as necessary | ||
| sys.path.append("..") | ||
|
|
||
| import monai | ||
| from monai.data.utils import list_data_collate | ||
| from monai.utils.sliding_window_inference import sliding_window_inference | ||
| from monai.data.synthetic import create_test_image_3d | ||
| from monai.networks.utils import predict_segmentation | ||
| from monai.networks.nets.unet import UNet | ||
| from monai.transforms.composables import LoadNiftid, AsChannelFirstd | ||
| import monai.transforms.compose as transforms | ||
| from monai.handlers.segmentation_saver import SegmentationSaver | ||
| from monai.handlers.checkpoint_loader import CheckpointLoader | ||
| from monai import config | ||
|
|
||
| config.print_config() | ||
|
|
||
| tempdir = tempfile.mkdtemp() | ||
| # tempdir = './temp' | ||
| print('generating synthetic data to {} (this may take a while)'.format(tempdir)) | ||
| for i in range(50): | ||
| im, seg = create_test_image_3d(256, 256, 256, channel_dim=-1) | ||
|
|
||
| n = nib.Nifti1Image(im, np.eye(4)) | ||
| nib.save(n, os.path.join(tempdir, 'im%i.nii.gz' % i)) | ||
|
|
||
| n = nib.Nifti1Image(seg, np.eye(4)) | ||
| nib.save(n, os.path.join(tempdir, 'seg%i.nii.gz' % i)) | ||
|
|
||
| images = sorted(glob(os.path.join(tempdir, 'im*.nii.gz'))) | ||
| segs = sorted(glob(os.path.join(tempdir, 'seg*.nii.gz'))) | ||
| val_files = [{'img': img, 'seg': seg} for img, seg in zip(images, segs)] | ||
| val_transforms = transforms.Compose([ | ||
| LoadNiftid(keys=['img', 'seg']), | ||
| AsChannelFirstd(keys=['img', 'seg'], channel_dim=-1) | ||
| ]) | ||
| val_ds = monai.data.Dataset(data=val_files, transform=val_transforms) | ||
|
|
||
| device = torch.device("cuda:0") | ||
| roi_size = (64, 64, 64) | ||
| sw_batch_size = 4 | ||
| net = UNet( | ||
| dimensions=3, | ||
| in_channels=1, | ||
| num_classes=1, | ||
| channels=(16, 32, 64, 128, 256), | ||
| strides=(2, 2, 2, 2), | ||
| num_res_units=2, | ||
| ) | ||
| net.to(device) | ||
|
|
||
|
|
||
| def _sliding_window_processor(engine, batch): | ||
| net.eval() | ||
| with torch.no_grad(): | ||
| seg_probs = sliding_window_inference(batch['img'], roi_size, sw_batch_size, lambda x: net(x)[0], device) | ||
| return predict_segmentation(seg_probs) | ||
|
|
||
|
|
||
| infer_engine = Engine(_sliding_window_processor) | ||
|
|
||
| # for the arrary data format, assume the 3rd item of batch data is the meta_data | ||
| SegmentationSaver(output_path='tempdir', output_ext='.nii.gz', output_postfix='seg', | ||
| batch_transform=lambda batch: {'filename_or_obj': batch['img.filename_or_obj'], | ||
| 'original_affine': batch['img.original_affine'], | ||
| 'affine': batch['img.affine'], | ||
| }).attach(infer_engine) | ||
| # the model was trained by "unet_segmentation_3d_array" exmple | ||
| CheckpointLoader(load_path='./runs/net_checkpoint_120.pth', load_dict={'net': net}).attach(infer_engine) | ||
|
|
||
| val_loader = DataLoader(val_ds, batch_size=1, num_workers=4, collate_fn=list_data_collate, | ||
| pin_memory=torch.cuda.is_available()) | ||
| state = infer_engine.run(val_loader) |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.