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
Add IntermediateLayerGetter #47908
Merged
Merged
Add IntermediateLayerGetter #47908
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
d12605e
IntermediateLayerGetter
DrRyanHuang 39b0f2c
Update utils.py
DrRyanHuang bc9bd78
Update utils.py
DrRyanHuang cc210b0
Update utils.py
DrRyanHuang 31bd115
pre-commit
DrRyanHuang 1ca8277
Merge branch 'develop' of https://github.com/DrRyanHuang/Paddle into …
DrRyanHuang 970ecbe
add unitest and fix linear error
DrRyanHuang 27f7655
fix can't import bug
DrRyanHuang 89b5fc7
add
DrRyanHuang 17293f9
add timeout PROPERTIES
DrRyanHuang 4e8fcaf
decrease batchsize
DrRyanHuang e33237b
del >>>
DrRyanHuang fe1850c
switch big model and del AlexNet
DrRyanHuang 2670de7
utils => _utils
DrRyanHuang cf9e020
Merge branch 'develop' into develop
DrRyanHuang 09d254e
fix docstring codestyle Error: utils => _utils
DrRyanHuang 37a6ad1
update pre-commit version
DrRyanHuang dc0ccc8
del __all__
DrRyanHuang File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
python/paddle/fluid/tests/unittests/test_IntermediateLayerGetter.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,92 @@ | ||
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. | ||
# | ||
# 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 random | ||
import unittest | ||
|
||
import paddle | ||
from paddle.vision.models._utils import IntermediateLayerGetter | ||
|
||
|
||
class TestBase: | ||
def setUp(self): | ||
|
||
self.init_model() | ||
self.model.eval() | ||
|
||
self.layer_names = [ | ||
(order, name) | ||
for order, (name, _) in enumerate(self.model.named_children()) | ||
] | ||
# choose two layer children of model randomly | ||
self.start, self.end = sorted( | ||
random.sample(self.layer_names, 2), key=lambda x: x[0] | ||
) | ||
|
||
self.return_layers_dic = {self.start[1]: "feat1", self.end[1]: "feat2"} | ||
self.new_model = IntermediateLayerGetter( | ||
self.model, self.return_layers_dic | ||
) | ||
|
||
def init_model(self): | ||
self.model = None | ||
|
||
@paddle.no_grad() | ||
def test_inter_result(self): | ||
|
||
inp = paddle.randn([1, 3, 80, 80]) | ||
inter_oup = self.new_model(inp) | ||
|
||
for layer_name, layer in self.model.named_children(): | ||
|
||
if (isinstance(layer, paddle.nn.Linear) and inp.ndim == 4) or ( | ||
len(layer.sublayers()) > 0 | ||
and isinstance(layer.sublayers()[0], paddle.nn.Linear) | ||
and inp.ndim == 4 | ||
): | ||
inp = paddle.flatten(inp, 1) | ||
|
||
inp = layer(inp) | ||
if layer_name in self.return_layers_dic: | ||
feat_name = self.return_layers_dic[layer_name] | ||
self.assertTrue((inter_oup[feat_name] == inp).all()) | ||
|
||
|
||
class TestIntermediateLayerGetterResNet18(TestBase, unittest.TestCase): | ||
def init_model(self): | ||
self.model = paddle.vision.models.resnet18(pretrained=False) | ||
|
||
|
||
class TestIntermediateLayerGetterDenseNet121(TestBase, unittest.TestCase): | ||
def init_model(self): | ||
self.model = paddle.vision.models.densenet121(pretrained=False) | ||
|
||
|
||
class TestIntermediateLayerGetterVGG11(TestBase, unittest.TestCase): | ||
def init_model(self): | ||
self.model = paddle.vision.models.vgg11(pretrained=False) | ||
|
||
|
||
class TestIntermediateLayerGetterMobileNetV3Small(TestBase, unittest.TestCase): | ||
def init_model(self): | ||
self.model = paddle.vision.models.MobileNetV3Small() | ||
|
||
|
||
class TestIntermediateLayerGetterShuffleNetV2(TestBase, unittest.TestCase): | ||
def init_model(self): | ||
self.model = paddle.vision.models.shufflenet_v2_x0_25() | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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
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,110 @@ | ||
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. | ||
# | ||
# 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 collections import OrderedDict | ||
from typing import Dict | ||
|
||
import paddle | ||
import paddle.nn as nn | ||
|
||
__all__ = ["IntermediateLayerGetter"] | ||
|
||
|
||
def _make_divisible(v, divisor=8, min_value=None): | ||
""" | ||
This function ensures that all layers have a channel number that is divisible by divisor | ||
You can also see at https://github.com/keras-team/keras/blob/8ecef127f70db723c158dbe9ed3268b3d610ab55/keras/applications/mobilenet_v2.py#L505 | ||
|
||
Args: | ||
divisor (int): The divisor for number of channels. Default: 8. | ||
min_value (int, optional): The minimum value of number of channels, if it is None, | ||
the default is divisor. Default: None. | ||
""" | ||
if min_value is None: | ||
min_value = divisor | ||
new_v = max(min_value, int(v + divisor / 2) // divisor * divisor) | ||
# Make sure that round down does not go down by more than 10%. | ||
if new_v < 0.9 * v: | ||
new_v += divisor | ||
return new_v | ||
|
||
|
||
class IntermediateLayerGetter(nn.LayerDict): | ||
""" | ||
Layer wrapper that returns intermediate layers from a model. | ||
|
||
It has a strong assumption that the layers have been registered into the model in the | ||
same order as they are used. This means that one should **not** reuse the same nn.Layer | ||
twice in the forward if you want this to work. | ||
|
||
Additionally, it is only able to query sublayer that are directly assigned to the model. | ||
So if `model` is passed, `model.feature1` can be returned, but not `model.feature1.layer2`. | ||
|
||
Args: | ||
model (nn.Layer): model on which we will extract the features | ||
return_layers (Dict[name, new_name]): a dict containing the names of the layers for | ||
which the activations will be returned as the key of the dict, and the value of the | ||
dict is the name of the returned activation (which the user can specify). | ||
|
||
Examples: | ||
.. code-block:: python | ||
|
||
import paddle | ||
m = paddle.vision.models.resnet18(pretrained=False) | ||
# extract layer1 and layer3, giving as names `feat1` and feat2` | ||
new_m = paddle.vision.models._utils.IntermediateLayerGetter(m, | ||
{'layer1': 'feat1', 'layer3': 'feat2'}) | ||
out = new_m(paddle.rand([1, 3, 224, 224])) | ||
print([(k, v.shape) for k, v in out.items()]) | ||
# [('feat1', [1, 64, 56, 56]), ('feat2', [1, 256, 14, 14])] | ||
""" | ||
|
||
__annotations__ = { | ||
"return_layers": Dict[str, str], | ||
} | ||
|
||
def __init__(self, model: nn.Layer, return_layers: Dict[str, str]) -> None: | ||
if not set(return_layers).issubset( | ||
[name for name, _ in model.named_children()] | ||
): | ||
raise ValueError("return_layers are not present in model") | ||
orig_return_layers = return_layers | ||
return_layers = {str(k): str(v) for k, v in return_layers.items()} | ||
layers = OrderedDict() | ||
for name, module in model.named_children(): | ||
layers[name] = module | ||
if name in return_layers: | ||
del return_layers[name] | ||
if not return_layers: | ||
break | ||
|
||
super(IntermediateLayerGetter, self).__init__(layers) | ||
self.return_layers = orig_return_layers | ||
|
||
def forward(self, x): | ||
out = OrderedDict() | ||
for name, module in self.items(): | ||
|
||
if (isinstance(module, nn.Linear) and x.ndim == 4) or ( | ||
len(module.sublayers()) > 0 | ||
and isinstance(module.sublayers()[0], nn.Linear) | ||
and x.ndim == 4 | ||
): | ||
x = paddle.flatten(x, 1) | ||
|
||
x = module(x) | ||
if name in self.return_layers: | ||
out_name = self.return_layers[name] | ||
out[out_name] = x | ||
return out |
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
This file was deleted.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
因为是非公开API,你试试去掉21行后:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done