-
Notifications
You must be signed in to change notification settings - Fork 1.4k
9 part a adding test intensity normalisation transform #33
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
Nic-Ma
merged 8 commits into
9-intensity-normalisation-transform
from
9-part-a-adding-test-intensity-normalisation-transform
Jan 21, 2020
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9d90ba0
Adding script to run unit tests and example test cases (#29)
ericspod a2fc227
initial unit tests for dice loss (#27)
wyli 4bd517a
initial unit tests for 2d/3d unet (#26)
wyli de1fbe0
14 code examples of monai input data pipeline (#24)
wyli eef2941
Create .gitlab-ci.yml (#30)
IsaacYangSLA a746342
Merge branch 'master' into 9-part-a-adding-test-intensity-normalisati…
wyli e2d69d6
tests intensity normalizer
wyli b174f89
style updates and new test cases:
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 |
|---|---|---|
|
|
@@ -103,3 +103,4 @@ venv.bak/ | |
| # mypy | ||
| .mypy_cache/ | ||
| examples/scd_lvsegs.npz | ||
| .idea/ | ||
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,12 @@ | ||
| stages: | ||
| - build | ||
|
|
||
| .base_template : &BASE | ||
| script: | ||
| - cat README.md | ||
|
|
||
| build-ci-test: | ||
| stage: build | ||
| tags: | ||
| - test | ||
| <<: *BASE |
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
|---|---|---|
|
|
@@ -12,3 +12,4 @@ pillow | |
| pandas | ||
| coverage | ||
| nibabel | ||
| parameterized | ||
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,103 @@ | ||
| #! /bin/bash | ||
| set -e | ||
| # Test script for running all tests | ||
|
|
||
|
|
||
| homedir="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | ||
| cd $homedir | ||
|
|
||
| #export PYTHONPATH="$homedir:$PYTHONPATH" | ||
|
|
||
| # configuration values | ||
| doCoverage=false | ||
| doQuickTests=false | ||
| doNetTests=false | ||
| doDryRun=false | ||
| doZooTests=false | ||
|
|
||
| # testing command to run | ||
| cmd="python" | ||
| cmdprefix="" | ||
|
|
||
|
|
||
| # parse arguments | ||
| for i in "$@" | ||
| do | ||
| case $i in | ||
| --coverage) | ||
| doCoverage=true | ||
| ;; | ||
| --quick) | ||
| doQuickTests=true | ||
| doCoverage=true | ||
| export QUICKTEST=True | ||
| ;; | ||
| --net) | ||
| doNetTests=true | ||
| ;; | ||
| --dryrun) | ||
| doDryRun=true | ||
| ;; | ||
| --zoo) | ||
| doZooTests=true | ||
| ;; | ||
| *) | ||
| echo "runtests.sh [--coverage] [--quick] [--net] [--dryrun] [--zoo]" | ||
| exit 1 | ||
| ;; | ||
| esac | ||
| done | ||
|
|
||
|
|
||
| # commands are echoed instead of run in this case | ||
| if [ "$doDryRun" = 'true' ] | ||
| then | ||
| echo "Dry run commands:" | ||
| cmdprefix="dryrun " | ||
|
|
||
| # create a dry run function which prints the command prepended with spaces for neatness | ||
| function dryrun { echo " " $* ; } | ||
| fi | ||
|
|
||
|
|
||
| # set command and clear previous coverage data | ||
| if [ "$doCoverage" = 'true' ] | ||
| then | ||
| cmd="coverage run -a --source ." | ||
| ${cmdprefix} coverage erase | ||
| fi | ||
|
|
||
|
|
||
| # # download test data if needed | ||
| # if [ ! -d testing_data ] && [ "$doDryRun" != 'true' ] | ||
| # then | ||
| # fi | ||
|
|
||
|
|
||
| # unit tests | ||
| ${cmdprefix}${cmd} -m unittest | ||
|
|
||
|
|
||
| # network training/inference/eval tests | ||
| if [ "$doNetTests" = 'true' ] | ||
| then | ||
| for i in examples/*.py | ||
| do | ||
| echo $i | ||
| ${cmdprefix}${cmd} $i | ||
| done | ||
| fi | ||
|
|
||
|
|
||
| # # run model zoo tests | ||
| # if [ "$doZooTests" = 'true' ] | ||
| # then | ||
| # fi | ||
|
|
||
|
|
||
| # report on coverage | ||
| if [ "$doCoverage" = 'true' ] | ||
| then | ||
| ${cmdprefix}coverage report --omit='*/test/*' --skip-covered -m | ||
| fi | ||
|
|
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,10 @@ | ||
| # 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. |
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,84 @@ | ||
| # 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. | ||
|
|
||
| from .utils import TorchImageTestCase2D | ||
|
|
||
| from monai.networks.layers.convolutions import Convolution, ResidualUnit | ||
|
|
||
|
|
||
| class TestConvolution2D(TorchImageTestCase2D): | ||
| def test_conv1(self): | ||
| conv = Convolution(2, self.input_channels, self.output_channels) | ||
| out = conv(self.imt) | ||
| expected_shape = (1, self.output_channels, self.im_shape[0], self.im_shape[1]) | ||
| self.assertEqual(out.shape, expected_shape) | ||
|
|
||
| def test_conv_only1(self): | ||
| conv = Convolution(2, self.input_channels, self.output_channels, conv_only=True) | ||
| out = conv(self.imt) | ||
| expected_shape = (1, self.output_channels, self.im_shape[0], self.im_shape[1]) | ||
| self.assertEqual(out.shape, expected_shape) | ||
|
|
||
| def test_stride1(self): | ||
| conv = Convolution(2, self.input_channels, self.output_channels, strides=2) | ||
| out = conv(self.imt) | ||
| expected_shape = (1, self.output_channels, self.im_shape[0] // 2, self.im_shape[1] // 2) | ||
| self.assertEqual(out.shape, expected_shape) | ||
|
|
||
| def test_dilation1(self): | ||
| conv = Convolution(2, self.input_channels, self.output_channels, dilation=3) | ||
| out = conv(self.imt) | ||
| expected_shape = (1, self.output_channels, self.im_shape[0], self.im_shape[1]) | ||
| self.assertEqual(out.shape, expected_shape) | ||
|
|
||
| def test_dropout1(self): | ||
| conv = Convolution(2, self.input_channels, self.output_channels, dropout=0.15) | ||
| out = conv(self.imt) | ||
| expected_shape = (1, self.output_channels, self.im_shape[0], self.im_shape[1]) | ||
| self.assertEqual(out.shape, expected_shape) | ||
|
|
||
| def test_transpose1(self): | ||
| conv = Convolution(2, self.input_channels, self.output_channels, is_transposed=True) | ||
| out = conv(self.imt) | ||
| expected_shape = (1, self.output_channels, self.im_shape[0], self.im_shape[1]) | ||
| self.assertEqual(out.shape, expected_shape) | ||
|
|
||
| def test_transpose2(self): | ||
| conv = Convolution(2, self.input_channels, self.output_channels, strides=2, is_transposed=True) | ||
| out = conv(self.imt) | ||
| expected_shape = (1, self.output_channels, self.im_shape[0] * 2, self.im_shape[1] * 2) | ||
| self.assertEqual(out.shape, expected_shape) | ||
|
|
||
|
|
||
| class TestResidualUnit2D(TorchImageTestCase2D): | ||
| def test_conv_only1(self): | ||
| conv = ResidualUnit(2, 1, self.output_channels) | ||
| out = conv(self.imt) | ||
| expected_shape = (1, self.output_channels, self.im_shape[0], self.im_shape[1]) | ||
| self.assertEqual(out.shape, expected_shape) | ||
|
|
||
| def test_stride1(self): | ||
| conv = ResidualUnit(2, 1, self.output_channels, strides=2) | ||
| out = conv(self.imt) | ||
| expected_shape = (1, self.output_channels, self.im_shape[0] // 2, self.im_shape[1] // 2) | ||
| self.assertEqual(out.shape, expected_shape) | ||
|
|
||
| def test_dilation1(self): | ||
| conv = ResidualUnit(2, 1, self.output_channels, dilation=3) | ||
| out = conv(self.imt) | ||
| expected_shape = (1, self.output_channels, self.im_shape[0], self.im_shape[1]) | ||
| self.assertEqual(out.shape, expected_shape) | ||
|
|
||
| def test_dropout1(self): | ||
| conv = ResidualUnit(2, 1, self.output_channels, dropout=0.15) | ||
| out = conv(self.imt) | ||
| expected_shape = (1, self.output_channels, self.im_shape[0], self.im_shape[1]) | ||
| self.assertEqual(out.shape, expected_shape) |
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,53 @@ | ||
| # 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 unittest | ||
|
|
||
| import torch | ||
| from parameterized import parameterized | ||
|
|
||
| from monai.networks.losses.dice import DiceLoss | ||
|
|
||
| TEST_CASE_1 = [ | ||
| { | ||
| 'include_background': False, | ||
| }, | ||
| { | ||
| 'pred': torch.tensor([[[[1., -1.], [-1., 1.]]]]), | ||
| 'ground': torch.tensor([[[[1., 0.], [1., 1.]]]]), | ||
| 'smooth': 1e-6, | ||
| }, | ||
| 0.307576, | ||
| ] | ||
|
|
||
| TEST_CASE_2 = [ | ||
| { | ||
| 'include_background': True, | ||
| }, | ||
| { | ||
| 'pred': torch.tensor([[[[1., -1.], [-1., 1.]]], [[[1., -1.], [-1., 1.]]]]), | ||
| 'ground': torch.tensor([[[[1., 1.], [1., 1.]]], [[[1., 0.], [1., 0.]]]]), | ||
| 'smooth': 1e-4, | ||
| }, | ||
| 0.416636, | ||
| ] | ||
|
|
||
|
|
||
| class TestDiceLoss(unittest.TestCase): | ||
|
|
||
| @parameterized.expand([TEST_CASE_1, TEST_CASE_2]) | ||
| def test_shape(self, input_param, input_data, expected_val): | ||
| result = DiceLoss(**input_param).forward(**input_data) | ||
| self.assertAlmostEqual(result.item(), expected_val, places=5) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
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,47 @@ | ||
| # 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 unittest | ||
|
|
||
| import numpy as np | ||
|
|
||
| from monai.data.transforms.intensity_normalizer import IntensityNormalizer | ||
| from tests.utils import NumpyImageTestCase2D | ||
|
|
||
|
|
||
| class IntensityNormTestCase(NumpyImageTestCase2D): | ||
|
|
||
| def test_image_normalizer_default(self): | ||
| data_key = 'image' | ||
| normalizer = IntensityNormalizer(data_key) # test a single key | ||
| normalised = normalizer({data_key: self.imt}) | ||
| expected = (self.imt - np.mean(self.imt)) / np.std(self.imt) | ||
| self.assertTrue(np.allclose(normalised[data_key], expected)) | ||
|
|
||
| def test_image_normalizer_default_1(self): | ||
| data_key = 'image' | ||
wyli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| normalizer = IntensityNormalizer([data_key]) # test list of keys | ||
| normalised = normalizer({data_key: self.imt}) | ||
| expected = (self.imt - np.mean(self.imt)) / np.std(self.imt) | ||
| self.assertTrue(np.allclose(normalised[data_key], expected)) | ||
|
|
||
| def test_image_normalizer_default_2(self): | ||
| data_keys = ['image_1', 'image_2'] | ||
| normalizer = IntensityNormalizer(data_keys) # test list of keys | ||
| normalised = normalizer(dict(zip(data_keys, (self.imt, self.seg1)))) | ||
| expected_1 = (self.imt - np.mean(self.imt)) / np.std(self.imt) | ||
| expected_2 = (self.seg1 - np.mean(self.seg1)) / np.std(self.seg1) | ||
| self.assertTrue(np.allclose(normalised[data_keys[0]], expected_1)) | ||
| self.assertTrue(np.allclose(normalised[data_keys[1]], expected_2)) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
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.