Skip to content
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 18 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions python/paddle/fluid/tests/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,9 @@ if(WITH_GLOO)
set_tests_properties(test_parallel_dygraph_sparse_embedding_over_height_gloo
PROPERTIES TIMEOUT 120)
endif()
if(NOT WITH_GPU)
set_tests_properties(test_IntermediateLayerGetter PROPERTIES TIMEOUT 900)
endif()

if($ENV{USE_STANDALONE_EXECUTOR})
# these test will fail in some server due to PR#42149, temporarily set it use old executor.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# 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 paddle
import unittest
import random
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 = paddle.vision.models.resnet50(pretrained=False)

@paddle.no_grad()
def test_inter_result(self):

inp = paddle.randn([32, 3, 224, 224])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

输入数据减少是否会降低单测时间?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯嗯是的,在AIstudio CPU环境测试,由75s下降至6s 🥳🥳🥳

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

目前的单测时间

  • py3:12s左右
2022-11-29 23:08:58     Start 344: test_IntermediateLayerGetter
2022-11-29 23:09:11     Test #344: test_IntermediateLayerGetter .....   Passed   12.55 sec
2022-11-29 23:09:11     Start 344: test_IntermediateLayerGetter
2022-11-29 23:09:22     Test #344: test_IntermediateLayerGetter .....   Passed   11.40 sec
2022-11-29 23:09:22     Start 344: test_IntermediateLayerGetter
2022-11-29 23:09:34 1/1 Test #344: test_IntermediateLayerGetter .....   Passed   11.31 sec
  • windows-openblas: 50s左右
2022-11-29 19:00:37  325/1433 Test  #298: test_IntermediateLayerGetter ................................   Passed   51.91 sec

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

py3:

2022-11-30 14:25:47     Start 344: test_IntermediateLayerGetter
2022-11-30 14:25:58     Test #344: test_IntermediateLayerGetter .....   Passed   10.94 sec
2022-11-30 14:25:58     Start 344: test_IntermediateLayerGetter
2022-11-30 14:26:05     Test #344: test_IntermediateLayerGetter .....   Passed    7.32 sec
2022-11-30 14:26:05     Start 344: test_IntermediateLayerGetter
2022-11-30 14:26:13 1/1 Test #344: test_IntermediateLayerGetter .....   Passed    7.76 sec

windows-openblas

2022-11-30 17:17:55           Start  307: test_adam_op
2022-11-30 17:17:56  300/1433 Test  #298: test_IntermediateLayerGetter .......   Passed   14.71 sec

终于算是卡着边界通过了😂😂😂

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 TestIntermediateLayerGetterResNet50(TestBase, unittest.TestCase):
def init_model(self):
self.model = paddle.vision.models.resnet50(pretrained=False)


class TestIntermediateLayerGetterDenseNet201(TestBase, unittest.TestCase):
def init_model(self):
self.model = paddle.vision.models.densenet201(pretrained=False)


class TestIntermediateLayerGetterVGG19(TestBase, unittest.TestCase):
def init_model(self):
self.model = paddle.vision.models.vgg19(pretrained=False)


class TestIntermediateLayerGetterMobileNetV3Large(TestBase, unittest.TestCase):
def init_model(self):
self.model = paddle.vision.models.MobileNetV3Large()


class TestIntermediateLayerGetterShuffleNetV2(TestBase, unittest.TestCase):
def init_model(self):
self.model = paddle.vision.models.ShuffleNetV2()


class TestIntermediateLayerGetterAlexNet(TestBase, unittest.TestCase):
def init_model(self):
self.model = paddle.vision.models.AlexNet()


if __name__ == "__main__":
unittest.main()
1 change: 1 addition & 0 deletions python/paddle/vision/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
from .models import shufflenet_v2_x1_5 # noqa: F401
from .models import shufflenet_v2_x2_0 # noqa: F401
from .models import shufflenet_v2_swish # noqa: F401
from .models import IntermediateLayerGetter
from .transforms import BaseTransform # noqa: F401
from .transforms import Compose # noqa: F401
from .transforms import Resize # noqa: F401
Expand Down
1 change: 1 addition & 0 deletions python/paddle/vision/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
from .shufflenetv2 import shufflenet_v2_x1_5 # noqa: F401
from .shufflenetv2 import shufflenet_v2_x2_0 # noqa: F401
from .shufflenetv2 import shufflenet_v2_swish # noqa: F401
from .utils import IntermediateLayerGetter

__all__ = [ # noqa
'ResNet',
Expand Down
78 changes: 78 additions & 0 deletions python/paddle/vision/models/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import paddle
import paddle.nn as nn
from collections import OrderedDict
from typing import Dict

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#43611 可以参考这个pr增加单测

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

__all__ = ["IntermediateLayerGetter"]


def _make_divisible(v, divisor=8, min_value=None):
"""
Expand All @@ -30,3 +37,74 @@ def _make_divisible(v, divisor=8, min_value=None):
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=True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

测试代码需要加import paddle,保证代码可以单独运行

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2022-11-29 14:26:01 subprocess return code: 1
2022-11-29 14:26:01 Error Raised from Sample Code:
2022-11-29 14:26:01 stderr:   File "samplecode_temp/paddle.vision.models.utils.IntermediateLayerGetter_example.py", line 4
2022-11-29 14:26:01     >>> import paddle
2022-11-29 14:26:01      ^
2022-11-29 14:26:01 SyntaxError: invalid syntax
2022-11-29 14:26:01 
2022-11-29 14:26:01 stdout: 

https://xly.bce.baidu.com/paddlepaddle/paddle/newipipe/detail/7275600/job/20709077
示例代码跑不过,把>>>都去掉吧

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 😆

>>> # 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的layers表示return layer前所有layer?能否给出示例代码

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的

IntermediateLayerGetter 的目的就是运行 model 的前向传播,并将中间的某些层的输出拿出来

layers 之所以包括传入 model 的所有 sublayer,是因为要通过传入model 的前向传播

之后执行:

super(IntermediateLayerGetter, self).__init__(layers)

由于继承自 nn.LayerDict 则它包含的子层将被注册和添加

所以在 self.forward 中,能通过 for name, module in self.items(): 来迭代每一层操作

如果某一层的输出,是我们想要的,即if name in self.return_layers,则添加到返回out = OrderedDict()

self.return_layers 字典变量用来指示哪些层的输出需要返回,返回之后的名字叫什么


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