open-mmlab / mmdetection

OpenMMLab Detection Toolbox and Benchmark
https://mmdetection.readthedocs.io
Apache License 2.0
29.61k stars 9.47k forks source link

AttributeError: 'Namespace' object has no attribute 'config_file' #10792

Open zyl75391 opened 1 year ago

zyl75391 commented 1 year ago

Thanks for your error report and we appreciate it a lot.

Checklist

  1. I have searched related issues but cannot get the expected help.
  2. I have read the FAQ documentation but cannot get the expected help.
  3. The bug has not been fixed in the latest version.

Describe the bug A clear and concise description of what the bug is.

Reproduction

  1. What command or script did you run? python tools/deployment/mmdet2torchserve.py rtmdet_tiny_8xb32-300e_coco.py \

    rtmdet_tiny_8xb32-300e_coco_20220902_112414-78e30dcc.pth \ --output-folder out

    line 67, in mmdet2torchserve manifest = ModelExportUtils.generate_manifest_json(args) File "/root/miniconda3/envs/3.10/lib/python3.10/site-packages/model_archiver/model_packaging_utils.py", line 129, in generate_manifest_json model = ModelExportUtils.generate_model(args) File "/root/miniconda3/envs/3.10/lib/python3.10/site-packages/model_archiver/model_packaging_utils.py", line 117, in generate_model config_file=modelargs.config_file, AttributeError: 'Namespace' object has no attribute 'config_file'

hhaAndroid commented 1 year ago

@zyl75391 Please checkout to dev-3.x branch

zyl75391 commented 1 year ago

图片 生成后会直接退出

zyl75391 commented 1 year ago

如果使用config目录下的config 可以打包但是启动server后会直接退出,使用下载的config 会报错 NameError: name 'false' is not defined 包括false trun 的大小写和 null没有定义

taharh commented 1 year ago

@zyl75391 I'm running into the same issue with rtmdet_ins_l, did you find any workaround?

pecanjk commented 1 year ago

图片 生成后会直接退出

解决了么?

Brendan-Richards commented 1 year ago

I'm running into the same AttributeError: 'Namespace' object has no attribute 'config_file' as OP when trying to create a mar bundle with the mmdet2torchserve.py script. I tried editing the script so it passes my model config through which allows it to generate a mar bundle. But when I try to run my model in torchserve I'm getting the same java error as @pecanjk and @pecanjk

dantecsm commented 1 year ago

图片 生成后会直接退出

same here, any solution?

roshambo919 commented 1 year ago

You need to pass in a YAML config file to the model archiver. See https://github.com/pytorch/serve/blob/master/model-archiver/README.md#creating-a-model-archive for more details. They give a sample config file.

You can add this in the mmdet2torchserve.py file.

taharh commented 1 year ago

The solution is to pass the YAML config file of your model to the torch-model-archiver in the --extra-files argument and don't forget to update the _base_ path in the YAML file according to your execution env.

Example: torch-model-archiver --model-name rtmdetins --version 1.0 --serialized-file best_coco_bbox_mAP_epoch_20.pth --extra-files custom_configs/rtmdet-ins_l_8xb32-300e_coco.py --handler handler.py --export-path model_store/

spankr commented 10 months ago

As a person new to PyTorch & OpenMMLab, I came across this same issue when working with MMRotate and google brought me here when trying to solve the issue.

The important piece is that the call to generate the .mar, within the OpenMMLab script, is broken with newer versions of torch-model-archiver. ( mmdet2torchserve.py for you and mmrotate2torchserve.py for me)

At the time of this post, the instantiation of Namespace is missing an argument, for 'config_file' in mmrotate2torchserve.py:

        args = Namespace(
            **{
                'model_file': f'{tmpdir}/config.py',
                'serialized_file': checkpoint_file,
                'handler': f'{Path(__file__).parent}/mmrotate_handler.py',
                'model_name': model_name or Path(checkpoint_file).stem,
                'version': model_version,
                'export_path': output_folder,
                'force': force,
                'requirements_file': None,
                'config_file': None, # <-- This is the line/argument that is missing. If no config yaml is needed, use 'None'
                'extra_files': None,
                'runtime': 'python',
                'archive_format': 'default'
            })
        manifest = ModelExportUtils.generate_manifest_json(args)
        package_model(args, manifest)

Note: The latest mmdet2torchserve.py populates 'config_file' with the config python script, which I think is incorrect as it should only be used to populate 'model_file'. It should still fix this error from occurring.

If a config yaml is required, then modify your *2torchserve.py script to pass in a config yaml value and set that 'config_file' value to it instead of None.

My biggest blocker with the advice above is the terminology. Models come with two files:

  1. Config script (e.g. oriented_rcnn_r50_fpn_1x_dota_le90.py) - populates 'model_file'
  2. Model/Weights file (e.g. oriented_rcnn_r50_fpn_1x_dota_le90-6d2b2ce0.pth) - populates 'serialized_file'

What got me is that when archiving, there is a 3rd, "optional" document: a config yaml. It didn't register that there are two (2) "config files".

The link for an example is above, provided by @roshambo919 but it can be pretty simple:

# TS frontend parameters
# See all supported parameters: https://github.com/pytorch/serve/blob/master/frontend/archive/src/main/java/org/pytorch/serve/archive/model/ModelConfig.java#L14 
minWorkers: 1
maxWorkers: 1

Again, it is optional on whether it exists (or not) inside the .mar file.

Another note: If you are new, learning (like me), or going through some of the provided/pre-generated models, using torch-model-archiver to generate your .mar file isn't going to work because the *2torchserve.py "flattens" and renames the configuration script to "config.py":

    config = Config.fromfile(config_file)

    with TemporaryDirectory() as tmpdir:
        config.dump(f'{tmpdir}/config.py')