-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Fully convolutional models based on TorchVision models #1845
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
bhashemian
merged 11 commits into
Project-MONAI:master
from
bhashemian:behrooz_torch_model_fully_conv
Mar 25, 2021
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d103ba6
Implement fully convolutional version of torchvision models
bhashemian 25ea2e6
Update networks init with TorchVisionFullyConvModel
bhashemian eb0a572
Add unittests for TorchVisionFullyConvModel
bhashemian ab1868f
Add another test case
bhashemian 08e0682
Upate docs for TorchVisionFullyConvModel
bhashemian a375c3e
Make torchvision import optional
bhashemian 97cbf11
Skip the tests if torchvision not available
bhashemian 5f6e577
Merge branch 'master' into behrooz_torch_model_fully_conv
bhashemian 0bfbefe
Improve the model based on comments
bhashemian 6f45f8c
Update and add test cases
bhashemian 2d60761
Merge branch 'master' into behrooz_torch_model_fully_conv
bhashemian 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| from typing import Tuple, Union | ||
|
|
||
| import torch | ||
|
|
||
| from monai.utils import optional_import | ||
|
|
||
| models, _ = optional_import("torchvision.models") | ||
|
|
||
|
|
||
| class TorchVisionFullyConvModel(torch.nn.Module): | ||
| """ | ||
| Customize TorchVision models to replace fully connected layer by convolutional layer. | ||
|
|
||
| Args: | ||
| model_name: name of any torchvision with adaptive avg pooling and fully connected layer at the end. | ||
| - resnet18 (default) | ||
| - resnet34 | ||
| - resnet50 | ||
| - resnet101 | ||
| - resnet152 | ||
| - resnext50_32x4d | ||
| - resnext101_32x8d | ||
| - wide_resnet50_2 | ||
| - wide_resnet101_2 | ||
| n_classes: number of classes for the last classification layer. Default to 1. | ||
| pool_size: the kernel size for `AvgPool2d` to replace `AdaptiveAvgPool2d`. Default to (7, 7). | ||
| pool_stride: the stride for `AvgPool2d` to replace `AdaptiveAvgPool2d`. Default to 1. | ||
| pretrained: whether to use the imagenet pretrained weights. Default to False. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| model_name: str = "resnet18", | ||
| n_classes: int = 1, | ||
| pool_size: Union[int, Tuple[int, int]] = (7, 7), | ||
| pool_stride: Union[int, Tuple[int, int]] = 1, | ||
| pretrained: bool = False, | ||
| ): | ||
| super().__init__() | ||
| model = getattr(models, model_name)(pretrained=pretrained) | ||
| layers = list(model.children()) | ||
|
|
||
| # check if the model is compatible | ||
| if not str(layers[-1]).startswith("Linear"): | ||
| raise ValueError(f"Model ['{model_name}'] does not have a Linear layer at the end.") | ||
| if not str(layers[-2]).startswith("AdaptiveAvgPool2d"): | ||
| raise ValueError(f"Model ['{model_name}'] does not have a AdaptiveAvgPool2d layer next to the end.") | ||
|
|
||
| # remove the last Linear layer (fully connected) and the adaptive avg pooling | ||
| self.features = torch.nn.Sequential(*layers[:-2]) | ||
|
|
||
| # add 7x7 avg pooling (in place of adaptive avg pooling) | ||
| self.pool = torch.nn.AvgPool2d(kernel_size=pool_size, stride=pool_stride) | ||
|
|
||
| # add 1x1 conv (it behaves like a FC layer) | ||
| self.fc = torch.nn.Conv2d(model.fc.in_features, n_classes, kernel_size=(1, 1)) | ||
|
|
||
| def forward(self, x): | ||
| x = self.features(x) | ||
|
|
||
| # apply 2D avg pooling | ||
| x = self.pool(x) | ||
|
|
||
| # apply last 1x1 conv layer that act like a linear layer | ||
| x = self.fc(x) | ||
|
|
||
| return x | ||
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,106 @@ | ||
| # 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 unittest | ||
| from unittest import skipUnless | ||
|
|
||
| import torch | ||
| from parameterized import parameterized | ||
|
|
||
| from monai.networks import eval_mode | ||
| from monai.networks.nets import TorchVisionFullyConvModel | ||
| from monai.utils import optional_import | ||
|
|
||
| _, has_tv = optional_import("torchvision") | ||
|
|
||
| device = "cuda" if torch.cuda.is_available() else "cpu" | ||
|
|
||
| TEST_CASE_0 = [ | ||
| {"model_name": "resnet18", "n_classes": 1, "pretrained": False}, | ||
| (2, 3, 224, 224), | ||
| (2, 1, 1, 1), | ||
| ] | ||
|
|
||
| TEST_CASE_1 = [ | ||
| {"model_name": "resnet18", "n_classes": 1, "pretrained": False}, | ||
| (2, 3, 256, 256), | ||
| (2, 1, 2, 2), | ||
| ] | ||
|
|
||
| TEST_CASE_2 = [ | ||
| {"model_name": "resnet101", "n_classes": 5, "pretrained": False}, | ||
| (2, 3, 256, 256), | ||
| (2, 5, 2, 2), | ||
| ] | ||
|
|
||
| TEST_CASE_3 = [ | ||
| {"model_name": "resnet101", "n_classes": 5, "pool_size": 6, "pretrained": False}, | ||
| (2, 3, 224, 224), | ||
| (2, 5, 2, 2), | ||
| ] | ||
|
|
||
| TEST_CASE_PRETRAINED_0 = [ | ||
| {"model_name": "resnet18", "n_classes": 1, "pretrained": True}, | ||
| (2, 3, 224, 224), | ||
| (2, 1, 1, 1), | ||
| -0.010419349186122417, | ||
| ] | ||
|
|
||
| TEST_CASE_PRETRAINED_1 = [ | ||
| {"model_name": "resnet18", "n_classes": 1, "pretrained": True}, | ||
| (2, 3, 256, 256), | ||
| (2, 1, 2, 2), | ||
| -0.010419349186122417, | ||
| ] | ||
|
|
||
| TEST_CASE_PRETRAINED_2 = [ | ||
| {"model_name": "resnet18", "n_classes": 5, "pretrained": True}, | ||
| (2, 3, 256, 256), | ||
| (2, 5, 2, 2), | ||
| -0.010419349186122417, | ||
| ] | ||
|
|
||
|
|
||
| class TestTorchVisionFullyConvModel(unittest.TestCase): | ||
| @parameterized.expand( | ||
| [ | ||
| TEST_CASE_0, | ||
| TEST_CASE_1, | ||
| TEST_CASE_2, | ||
| TEST_CASE_3, | ||
| ] | ||
| ) | ||
| @skipUnless(has_tv, "Requires TorchVision.") | ||
| def test_without_pretrained(self, input_param, input_shape, expected_shape): | ||
| net = TorchVisionFullyConvModel(**input_param).to(device) | ||
| with eval_mode(net): | ||
| result = net.forward(torch.randn(input_shape).to(device)) | ||
| self.assertEqual(result.shape, expected_shape) | ||
|
|
||
| @parameterized.expand( | ||
| [ | ||
| TEST_CASE_PRETRAINED_0, | ||
| TEST_CASE_PRETRAINED_1, | ||
| TEST_CASE_PRETRAINED_2, | ||
| ] | ||
| ) | ||
| @skipUnless(has_tv, "Requires TorchVision.") | ||
| def test_with_pretrained(self, input_param, input_shape, expected_shape, expected_value): | ||
| net = TorchVisionFullyConvModel(**input_param).to(device) | ||
| with eval_mode(net): | ||
| result = net.forward(torch.randn(input_shape).to(device)) | ||
| value = next(net.parameters())[0, 0, 0, 0].item() | ||
| self.assertEqual(value, expected_value) | ||
| self.assertEqual(result.shape, expected_shape) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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.