-
Notifications
You must be signed in to change notification settings - Fork 1.4k
191 develop determinism integration test #195
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
7 commits
Select commit
Hold shift + click to select a range
2d420d1
[DLMED] add determinism integration test and fix bugs
Nic-Ma ecf7c1c
[DLMED] add determinism support to highlights
Nic-Ma 5e5ba19
[DLMED] add transforms to the test
Nic-Ma adab679
[DLMED] update to RandUniformPatch
Nic-Ma 9333b65
Merge branch 'master' into 191-develop-determinism-integration-test
wyli 0a0d8f0
fixes typos
wyli 1e5057b
fixes empty indicies issue in balanced sampling
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| # 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 sys | ||
|
|
||
| import numpy as np | ||
| import torch | ||
| from torch.utils.data import DataLoader, Dataset | ||
| from monai.transforms import AddChannel, Rescale, RandUniformPatch, RandRotate90 | ||
| import monai.transforms.compose as transforms | ||
| from monai.data.synthetic import create_test_image_2d | ||
| from monai.losses.dice import DiceLoss | ||
| from monai.networks.nets.unet import UNet | ||
|
|
||
|
|
||
| def run_test(batch_size=64, train_steps=100, device=torch.device("cuda:0")): | ||
|
|
||
| class _TestBatch(Dataset): | ||
|
|
||
| def __init__(self, transforms): | ||
| self.transforms = transforms | ||
|
|
||
| def __getitem__(self, _unused_id): | ||
| im, seg = create_test_image_2d(128, 128, noise_max=1, num_objs=4, num_seg_classes=1) | ||
| seed = np.random.randint(2147483647) | ||
| self.transforms.set_random_state(seed=seed) | ||
| im = self.transforms(im) | ||
| self.transforms.set_random_state(seed=seed) | ||
| seg = self.transforms(seg) | ||
| return im, seg | ||
|
|
||
| def __len__(self): | ||
| return train_steps | ||
|
|
||
| net = UNet( | ||
Nic-Ma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| dimensions=2, | ||
| in_channels=1, | ||
| out_channels=1, | ||
| channels=(4, 8, 16, 32), | ||
| strides=(2, 2, 2), | ||
| num_res_units=2, | ||
| ).to(device) | ||
|
|
||
| loss = DiceLoss(do_sigmoid=True) | ||
| opt = torch.optim.Adam(net.parameters(), 1e-2) | ||
| train_transforms = transforms.Compose([ | ||
| AddChannel(), | ||
| Rescale(), | ||
| RandUniformPatch((96, 96)), | ||
| RandRotate90() | ||
| ]) | ||
|
|
||
| src = DataLoader(_TestBatch(train_transforms), batch_size=batch_size) | ||
|
|
||
| net.train() | ||
| epoch_loss = 0 | ||
| step = 0 | ||
| for img, seg in src: | ||
| step += 1 | ||
| opt.zero_grad() | ||
| output = net(img.to(device)) | ||
| step_loss = loss(output, seg.to(device)) | ||
| step_loss.backward() | ||
| opt.step() | ||
| epoch_loss += step_loss.item() | ||
| epoch_loss /= step | ||
|
|
||
| print('Loss:', epoch_loss) | ||
| result = np.allclose(epoch_loss, 0.578675) | ||
| if result is False: | ||
| print('Loss value is wrong, expect to be 0.578675.') | ||
| return result | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| np.random.seed(0) | ||
| torch.manual_seed(0) | ||
| torch.backends.cudnn.deterministic = True | ||
| torch.backends.cudnn.benchmark = False | ||
| sys.exit(0 if run_test() is True else 1) | ||
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.