PaddlePaddle / PaddleHub

Awesome pre-trained models toolkit based on PaddlePaddle. (400+ models including Image, Text, Audio, Video and Cross-Modal with Easy Inference & Serving)【安全加固,暂停交互,请耐心等待】
https://www.paddlepaddle.org.cn/hub
Apache License 2.0
12.72k stars 2.08k forks source link

Paddle2.0.0版本之后,paddle默认使用动态图模式,请问静态图在2.0.0之后的版本上要如何运行? #1576

Open tgniye opened 3 years ago

tgniye commented 3 years ago

版本信息: PaddlePaddle2.1.2 PaddleHub2.0.4 Python3.7.4 环境: Windows使用的AIStudio

相关代码:

import paddle

from paddlehub.finetune.trainer import Trainer

# optimizer = paddle.optimizer.Adam(learning_rate=0.001, parameters=model.parameters())

trainer = Trainer(model, None, checkpoint_dir='img_classification_ckpt')

利用AIStudio进行 finetune 训练时, trainer = Trainer(model, None, checkpoint_dir='img_classification_ckpt') 报错AttributeError: 'ResNet50vdAnimals' object has no attribute 'name';

详细报错如下: ---------------------------------------------------------------------------AttributeError Traceback (most recent call last) in 7 8 ----> 9 trainer = Trainer(model, None, checkpoint_dir='img_classification_ckpt') 10 11 trainer.train(flowers, epochs=100, batch_size=32, eval_dataset=flowers_validate, save_interval=1) /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddlehub/finetune/trainer.py in init(self, model, optimizer, use_gpu, use_vdl, checkpoint_dir, compare_metrics, **kwargs) 67 68 if not isinstance(self.model, paddle.nn.Layer): ---> 69 raise TypeError('The model {} is not a paddle.nn.Layer object.'.format(self.model.name)) 70 71 if self.local_rank == 0 and not os.path.exists(self.checkpoint_dir): /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddlehub/module/module.py in getattribute(self, attr) 87 # execution of the static graph model to fail, so compatibility protection is required. 88 def getattribute(self, attr): ---> 89 _attr = object.getattribute(self, attr) 90 91 # If the acquired attribute is a built-in property of the object, skip it. AttributeError: 'ResNet50vdAnimals' object has no attribute 'name'

查看PaddleHubAPI后发现2.0.0版本后Paddle默认使用动态图,会导致静态图模型执行失败; ResNet50vdAnimals模型传入后提示不属于paddle.nn.Layer, 请问动态图在2.0.0之后的版本上要如何运行?

# After the 2.0.0rc version, paddle uses the dynamic graph mode by default, which will cause the
# execution of the static graph model to fail, so compatibility protection is required.
def __getattribute__(self, attr):
    _attr = object.__getattribute__(self, attr)

    # If the acquired attribute is a built-in property of the object, skip it.
    if re.match('__.*__', attr):
        return _attr
    # If the module is a dynamic graph model, skip it.
    elif isinstance(self, paddle.nn.Layer):
        return _attr
    # If the acquired attribute is not a class method, skip it.
    elif not inspect.ismethod(_attr):
        return _attr

    return paddle_utils.run_in_static_mode(_attr)
haoyuying commented 3 years ago

ResNet50vdAnimals这个模型目前是静态图版本,已经不支持2.0以上版本hub的训练,建议使用resnet50_vd_imagenet_ssld,具体教程如下:https://www.paddlepaddle.org.cn/hubdetail?name=resnet50_vd_imagenet_ssld&en_category=ImageClassification