PaddlePaddle / Paddle

PArallel Distributed Deep LEarning: Machine Learning Framework from Industrial Practice (『飞桨』核心框架,深度学习&机器学习高性能单机、分布式训练和跨平台部署)
http://www.paddlepaddle.org/
Apache License 2.0
21.66k stars 5.44k forks source link

Inadequate Validation for 'scale' Parameter in Model APIs. #64034

Open Zoeeeeey opened 2 weeks ago

Zoeeeeey commented 2 weeks ago

bug描述 Describe the Bug

在一些提供构建模型的API中,设计了参数scale作为调整模型通道数的放缩因子。

因此,scale应该是一个非负数才使得放缩有意义。

但部分scale缺少参数验证,当输入负值时,程序仍然能够运行、输出。

这些API包括:

paddle.vision.models.MobileNetV2
paddle.vision.models.MobileNetV3Large
paddle.vision.models.MobileNetV3Small
paddle.vision.models.mobilenet_v2
paddle.vision.models.mobilenet_v3_large
paddle.vision.models.mobilenet_v3_small

以下简单的代码可以复现上述问题。 In MobileNetV3Large:

import paddle
from paddle.vision.models import MobileNetV3Large

# Build model
model = MobileNetV3Large(scale=-2.0)
x = paddle.rand([1, 3, 224, 224])
out = model(x)

print(out.shape)

Got:

[1, 1000]

Expected

# In `MobileNetV1`:
AssertionError: Expected every dim's size to be larger than 0, but the size of the 0-th dim is -64

# In `ShuffleNetV2`:
NotImplementedError: This scale size:[-2.0] is not implemented!

其他补充信息 Additional Supplementary Information

No response

dyning commented 1 week ago

对于一个具体模型,scale与网络结构非常相关,是有固定取值范围的,任意的scale,即使大于0,也可能导致网络不通。具体模型合适的scale范围,可以参看https://github.com/PaddlePaddle/PaddleClas/blob/release/2.5/docs/zh_CN/models/ImageNet1k/model_list.md 模型库中的模型名称,比如mobilenetv2的系列模型有6个,MobileNetV2_x0_25、MobileNetV2_x0_5、MobileNetV2_x0_75、MobileNetV2、MobileNetV2_x1_5、MobileNetV2_x2_0,则对应的scale取值为0.25,0.5,0.75,1.0,1.5,2.0