-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Add IntermediateLayerGetter #47908
Changes from 3 commits
d12605e
39b0f2c
bc9bd78
cc210b0
31bd115
1ca8277
970ecbe
27f7655
89b5fc7
17293f9
4e8fcaf
e33237b
fe1850c
2670de7
cf9e020
09d254e
37a6ad1
dc0ccc8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,9 @@ | |
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import paddle.nn as nn | ||
from collections import OrderedDict | ||
from typing import Dict | ||
|
||
def _make_divisible(v, divisor=8, min_value=None): | ||
""" | ||
|
@@ -30,3 +33,63 @@ 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 | ||
|
||
>>> m = paddle.vision.models.resnet18(pretrained=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 测试代码需要加import paddle,保证代码可以单独运行 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
https://xly.bce.baidu.com/paddlepaddle/paddle/newipipe/detail/7275600/job/20709077 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里的layers表示return layer前所有layer?能否给出示例代码 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 是的
之后执行:
由于继承自 所以在 如果某一层的输出,是我们想要的,即
|
||
|
||
super(IntermediateLayerGetter, self).__init__(layers) | ||
self.return_layers = orig_return_layers | ||
|
||
def forward(self, x): | ||
out = OrderedDict() | ||
for name, module in self.items(): | ||
x = module(x) | ||
if name in self.return_layers: | ||
out_name = self.return_layers[name] | ||
out[out_name] = x | ||
return out |
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.
#43611 可以参考这个pr增加单测
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