PaddlePaddle / PaddleDetection

Object Detection toolkit based on PaddlePaddle. It supports object detection, instance segmentation, multiple object tracking and real-time multi-person keypoint detection.
Apache License 2.0
12.81k stars 2.89k forks source link

[Other General Issues]在flask中调用推理infer.py时出现错误ValueError,但是直接python infer.py就没有问题。 #4632

Closed yuanjim closed 2 years ago

yuanjim commented 2 years ago

Checklist:image

  1. 查找历史相关issue寻求解答/I have searched related issues but cannot get the expected help.
  2. 翻阅FAQ /I have read the FAQ documentation but cannot get the expected help.

描述问题/Describe the bug

A clear and concise description of what the bug is. flask启动后,第一次调用是正常的,但是第二次调用infer就出现如上问题了。

192.168.1.137 - - [19/Nov/2021 12:08:02] "POST /predict/ HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/opt/anaconda3/envs/pp21/lib/python3.8/site-packages/flask/app.py", line 2088, in __call__
    return self.wsgi_app(environ, start_response)
  File "/opt/anaconda3/envs/pp21/lib/python3.8/site-packages/flask/app.py", line 2073, in wsgi_app
    response = self.handle_exception(e)
  File "/opt/anaconda3/envs/pp21/lib/python3.8/site-packages/flask/app.py", line 2070, in wsgi_app
    response = self.full_dispatch_request()
  File "/opt/anaconda3/envs/pp21/lib/python3.8/site-packages/flask/app.py", line 1515, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/opt/anaconda3/envs/pp21/lib/python3.8/site-packages/flask/app.py", line 1513, in full_dispatch_request
    rv = self.dispatch_request()
  File "/opt/anaconda3/envs/pp21/lib/python3.8/site-packages/flask/app.py", line 1499, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/Users/yuanjing/jim/projects_list/0024_infrared_image_detection/flask_app.py", line 67, in get_prediction
    results = infer(save_path)
  File "/Users/yuanjing/jim/projects_list/0024_infrared_image_detection/backend/infer.py", line 170, in infer
    info = run(FLAGS, cfg, images)
  File "/Users/yuanjing/jim/projects_list/0024_infrared_image_detection/backend/infer.py", line 133, in run
    trainer = Trainer(cfg, mode='test')
  File "/Users/yuanjing/jim/projects_list/0024_infrared_image_detection/ppdet/engine/trainer.py", line 86, in __init__
    self.model = create(cfg.architecture)
  File "/Users/yuanjing/jim/projects_list/0024_infrared_image_detection/ppdet/core/workspace.py", line 238, in create
    cls_kwargs.update(cls.from_config(config, **kwargs))
  File "/Users/yuanjing/jim/projects_list/0024_infrared_image_detection/ppdet/modeling/architectures/yolo.py", line 62, in from_config
    backbone = create(cfg['backbone'])
  File "/Users/yuanjing/jim/projects_list/0024_infrared_image_detection/ppdet/core/workspace.py", line 275, in create
    return cls(**cls_kwargs)
  File "/Users/yuanjing/jim/projects_list/0024_infrared_image_detection/ppdet/modeling/backbones/mobilenet_v3.py", line 372, in __init__
    self.conv1 = ConvBNLayer(
  File "/Users/yuanjing/jim/projects_list/0024_infrared_image_detection/ppdet/modeling/backbones/mobilenet_v3.py", line 57, in __init__
    self.conv = nn.Conv2D(
  File "/opt/anaconda3/envs/pp21/lib/python3.8/site-packages/paddle/nn/layer/conv.py", line 633, in __init__
    super(Conv2D, self).__init__(
  File "/opt/anaconda3/envs/pp21/lib/python3.8/site-packages/paddle/nn/layer/conv.py", line 132, in __init__
    self.weight = self.create_parameter(
  File "/opt/anaconda3/envs/pp21/lib/python3.8/site-packages/paddle/fluid/dygraph/layers.py", line 411, in create_parameter
    return self._helper.create_parameter(temp_attr, shape, dtype, is_bias,
  File "/opt/anaconda3/envs/pp21/lib/python3.8/site-packages/paddle/fluid/layer_helper_base.py", line 364, in create_parameter
    raise ValueError(
ValueError: parameter name [conv1_weights] have be been used. In dygraph mode, the name of parameter can't be same.Please check the parameter attr value passed to self.create_parameter or constructor of dygraph Layers

复现/Reproductionimage

  1. 您使用的命令是?/What command or script did you run?
    
    #!/bin/bash
    python flask_app.py &
    python -m http.server 8888

results = infer(save_path)

-----------------------------------save_path:------------------------------ ['/Users/yuanjing/jim/projects_list/0024_infrared_image_detection/coco/val_from_flask/img4predict.jpg']


2. 您是否更改过代码或配置文件?您是否理解您所更改的内容?还请您提供所更改的部分代码。/Did you make any modifications on the code or config? Did you understand what you have modified? Please provide the codes that you modified.
直接 python infer.py --infer_dir=coco/val/ ,可以多次正常推理出结果。
3. 您使用的数据集是?/What dataset did you use?
训练没有问题。
4. 请提供您出现的报错信息及相关log。/Please provide the error messages or relevant log information.
如上的问题描述。
## 环境/Environment
1. 请提供您使用的Paddle和PaddleDetection的版本号/Please provide the version of Paddle and PaddleDetection you use:
paddle 2.1.2
paddledetection 2.1
2. 如您在使用PaddleDetection的同时还在使用其他产品,如PaddleServing、PaddleInference等,请您提供其版本号/ Please provide the version of any other related tools/products used, such as the version of PaddleServing and etc:
3. 请提供您使用的操作系统信息,如Linux/Windows/MacOS /Please provide the OS information, e.g., Linux:
macos 
4. 请问您使用的Python版本是?/ Please provide the version of Python you used.
python3.8
5. 请问您使用的CUDA/cuDNN的版本号是?/ Please provide the version of CUDA/cuDNN you used.
cpu

如果您的issue是关于安装或环境,您可以先查询[安装文档](https://github.com/PaddlePaddle/PaddleDetection/blob/release/2.1/docs/tutorials/INSTALL_cn.md)尝试解决~

If your issue looks like an installation issue / environment issue,
please first try to solve it yourself with the instructions in
https://github.com/PaddlePaddle/PaddleDetection/blob/release/2.1/docs/tutorials/INSTALL.md
qingqing01 commented 2 years ago

ValueError: parameter name [conv1_weights] have be been used. In dygraph mode, the name of parameter can't be same.Please check the parameter attr value passed to self.create_parameter or constructor of dygraph Layers

从错误来看,您使用的代码不是最新的。最新代码,没有"conv1_weights"这样的命名。

https://github.com/PaddlePaddle/PaddleDetection/blob/release/2.3/ppdet/modeling/backbones/mobilenet_v3.py#L57

yuanjim commented 2 years ago

已解决。出现上述问题的原因是在内存中重复创建了推理模型,所以将创建推理放在flask路由实例前即可。