-
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
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
需要补充单测来通过 PR-CI-Coverage |
@luotao1 意思是提供一个这样的测试用例? import paddle
m = paddle.vision.models.resnet18(pretrained=True)
# 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()])
|
python/paddle/vision/models/utils.py
Outdated
@@ -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 | |||
|
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
python/paddle/vision/models/utils.py
Outdated
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 comment
The 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 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
字典变量用来指示哪些层的输出需要返回,返回之后的名字叫什么
python/paddle/vision/models/utils.py
Outdated
Examples: | ||
.. code-block:: python | ||
|
||
>>> m = paddle.vision.models.resnet18(pretrained=True) |
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.
测试代码需要加import paddle,保证代码可以单独运行
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.
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.
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
示例代码跑不过,把>>>
都去掉吧
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 😆
add `import paddle`
请在提交的代码前使用pre-commit 做下格式检查
|
…develop Nothing important.
还请下添加单测~ |
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.
"/home/Paddle/build/python/paddle/fluid/tests/unittests/test_IntermediateLayerGetter.py", line 18, in <module>
2022-11-25 02:39:14 from paddle.vision.models import IntermediateLayerGetter
2022-11-25 02:39:14 ImportError: cannot import name 'IntermediateLayerGetter' from 'paddle.vision.models'
现在CI上流水线都挂在这个单测上,请先修一下。需要在 utils.py里加一个__all__
来加上这个新的API
PR-CI-Py3 和 PR-CI-Windows-OPENBLAS 上都是 CPU 环境,单测默认时间是15s,目前是超时的。 Paddle/python/paddle/fluid/tests/unittests/CMakeLists.txt Lines 1026 to 1030 in 89b5fc7
|
您好,打扰了,这是需要QA的同学的许可吧,我需要给他们发邮件吗?
|
单测时间太长了,特别是windows这条要跑15分钟:
|
@paddle.no_grad() | ||
def test_inter_result(self): | ||
|
||
inp = paddle.randn([32, 3, 224, 224]) |
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.
输入数据减少是否会降低单测时间?
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.
嗯嗯是的,在AIstudio CPU环境测试,由75s下降至6s 🥳🥳🥳
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.
目前的单测时间
- 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
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.
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
终于算是卡着边界通过了😂😂😂
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.
LGTM
@jeff41404 的意见: |
我们和torchvision保持一致吧,把utils.py改成_utils.py 注意示例代码也做下相应修改 |
@jerrywgz
请问 |
因为这个API只在内部使用,可以一同迁移 |
打扰了,还是有一个单测没有通过,请问是我操作不对吗?还是我需要给RD和TPM发邮件得到许可呢 ?
|
import paddle | ||
import paddle.nn as nn | ||
|
||
__all__ = ["IntermediateLayerGetter"] |
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.
打扰了,还是有一个单测没有通过,请问是我操作不对吗?还是我需要给RD和TPM发邮件得到许可呢 ?
因为是非公开API,你试试去掉21行后:
- 流水线是否能通过
- 你能否使用paddle.vision.models._utils.IntermediateLayerGetter
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
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.
LGTM
PR types
New features
PR changes
APIs
Describe
Add
IntermediateLayerGetter
just astorchvision.models._utils.IntermediateLayerGetter