We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
训练-裁剪-训练-导出报错
PaddleDetection是2.0版本
项目是在平台上运行的
使用PaddleDetection训练好yolov3_mobilenet_v3模型后进行模型裁剪
裁剪完评估完成后导出报错
这是我执行的脚本
`
!python slim/prune/export_model.py \
-c configs/yolov3_mobilenet_v3.yml \
--pruned_params "yolo_block.0.0.0.conv.weights,yolo_block.0.0.1.conv.weights,yolo_block.0.1.0.conv.weights,yolo_block.0.1.1.conv.weights,yolo_block.0.2.conv.weights,yolo_block.0.tip.conv.weights,yolo_block.1.0.0.conv.weights,yolo_block.1.0.1.conv.weights,yolo_block.1.1.0.conv.weights,yolo_block.1.1.1.conv.weights,yolo_block.1.2.conv.weights,yolo_block.1.tip.conv.weights,yolo_block.2.0.0.conv.weights,yolo_block.2.0.1.conv.weights,yolo_block.2.1.0.conv.weights,yolo_block.2.1.1.conv.weights,yolo_block.2.2.conv.weights,yolo_block.2.tip.conv.weights" \
--pruned_ratios="0.7150126596733395,0.8177442961035291,0.8274278897456334,0.8373393786362668,0.7956892620674756,0.8445719578292334,0.9,0.9,0.9,0.9,0.9,0.9,0.9,0.9,0.9,0.9,0.9,0.9" \
-o weights=output/yolov3_mobilenet_v3/model_final
`

以下是报错信息，

```
[03-04 10:10:58 MainThread @logger.py:242] Argv: slim/prune/export_model.py -c configs/yolov3_mobilenet_v3.yml --pruned_params yolo_block.0.0.0.conv.weights,yolo_block.0.0.1.conv.weights,yolo_block.0.1.0.conv.weights,yolo_block.0.1.1.conv.weights,yolo_block.0.2.conv.weights,yolo_block.0.tip.conv.weights,yolo_block.1.0.0.conv.weights,yolo_block.1.0.1.conv.weights,yolo_block.1.1.0.conv.weights,yolo_block.1.1.1.conv.weights,yolo_block.1.2.conv.weights,yolo_block.1.tip.conv.weights,yolo_block.2.0.0.conv.weights,yolo_block.2.0.1.conv.weights,yolo_block.2.1.0.conv.weights,yolo_block.2.1.1.conv.weights,yolo_block.2.2.conv.weights,yolo_block.2.tip.conv.weights --pruned_ratios=0.7150126596733395,0.8177442961035291,0.8274278897456334,0.8373393786362668,0.7956892620674756,0.8445719578292334,0.9,0.9,0.9,0.9,0.9,0.9,0.9,0.9,0.9,0.9,0.9,0.9 -o weights=output/yolov3_mobilenet_v3/model_final
[03-04 10:10:58 MainThread @utils.py:79] WRN paddlepaddle version: 2.0.0. The dynamic graph version of PARL is under development, not fully tested and supported
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/parl/remote/communication.py:38: DeprecationWarning: 'pyarrow.default_serialization_context' is deprecated as of 2.0.0 and will be removed in a future version. Use pickle or the pyarrow IPC functionality instead.
  context = pyarrow.default_serialization_context()
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/pandas/core/tools/datetimes.py:3: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import MutableMapping
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/rcsetup.py:20: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import Iterable, Mapping
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/colors.py:53: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import Sized
2021-03-04 10:11:00,126-INFO: pruned params: ['yolo_block.0.0.0.conv.weights', 'yolo_block.0.0.1.conv.weights', 'yolo_block.0.1.0.conv.weights', 'yolo_block.0.1.1.conv.weights', 'yolo_block.0.2.conv.weights', 'yolo_block.0.tip.conv.weights', 'yolo_block.1.0.0.conv.weights', 'yolo_block.1.0.1.conv.weights', 'yolo_block.1.1.0.conv.weights', 'yolo_block.1.1.1.conv.weights', 'yolo_block.1.2.conv.weights', 'yolo_block.1.tip.conv.weights', 'yolo_block.2.0.0.conv.weights', 'yolo_block.2.0.1.conv.weights', 'yolo_block.2.1.0.conv.weights', 'yolo_block.2.1.1.conv.weights', 'yolo_block.2.2.conv.weights', 'yolo_block.2.tip.conv.weights']
2021-03-04 10:11:00,126-INFO: pruned ratios: [0.7150126596733395, 0.8177442961035291, 0.8274278897456334, 0.8373393786362668, 0.7956892620674756, 0.8445719578292334, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9]
2021-03-04 10:11:00,169-INFO: pruning: yolo_block.0.0.0.conv.weights
Traceback (most recent call last):
  File "slim/prune/export_model.py", line 123, in <module>
    main()
  File "slim/prune/export_model.py", line 88, in main
    only_graph=True)
  File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddleslim/prune/pruner.py", line 112, in prune
    g = self._transform(self.idx_selector(scores, ratio))
  File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddleslim/prune/idx_selector.py", line 57, in default_idx_selector
    0]  # sort channels by the first convolution's score
IndexError: list index out of range
```

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `slim/prune/export_model.py`
Content:
```
1 # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 
15 from __future__ import absolute_import
16 from __future__ import division
17 from __future__ import print_function
18 
19 import os, sys
20 # add python path of PadleDetection to sys.path
21 parent_path = os.path.abspath(os.path.join(__file__, *(['..'] * 3)))
22 if parent_path not in sys.path:
23     sys.path.append(parent_path)
24 
25 import paddle
26 from paddle import fluid
27 
28 from ppdet.core.workspace import load_config, merge_config, create
29 from ppdet.utils.cli import ArgsParser
30 import ppdet.utils.checkpoint as checkpoint
31 from ppdet.utils.export_utils import save_infer_model, dump_infer_config
32 from ppdet.utils.check import check_config, check_version, enable_static_mode
33 from paddleslim.prune import Pruner
34 from paddleslim.analysis import flops
35 
36 import logging
37 FORMAT = '%(asctime)s-%(levelname)s: %(message)s'
38 logging.basicConfig(level=logging.INFO, format=FORMAT)
39 logger = logging.getLogger(__name__)
40 
41 
42 def main():
43     cfg = load_config(FLAGS.config)
44     merge_config(FLAGS.opt)
45     check_config(cfg)
46     check_version()
47 
48     main_arch = cfg.architecture
49 
50     # Use CPU for exporting inference model instead of GPU
51     place = fluid.CPUPlace()
52     exe = fluid.Executor(place)
53 
54     model = create(main_arch)
55 
56     startup_prog = fluid.Program()
57     infer_prog = fluid.Program()
58     with fluid.program_guard(infer_prog, startup_prog):
59         with fluid.unique_name.guard():
60             inputs_def = cfg['TestReader']['inputs_def']
61             inputs_def['use_dataloader'] = False
62             feed_vars, _ = model.build_inputs(**inputs_def)
63             test_fetches = model.test(feed_vars)
64     infer_prog = infer_prog.clone(True)
65 
66     pruned_params = FLAGS.pruned_params
67     assert (
68         FLAGS.pruned_params is not None
69     ), "FLAGS.pruned_params is empty!!! Please set it by '--pruned_params' option."
70     pruned_params = FLAGS.pruned_params.strip().split(",")
71     logger.info("pruned params: {}".format(pruned_params))
72     pruned_ratios = [float(n) for n in FLAGS.pruned_ratios.strip().split(",")]
73     logger.info("pruned ratios: {}".format(pruned_ratios))
74     assert (len(pruned_params) == len(pruned_ratios)
75             ), "The length of pruned params and pruned ratios should be equal."
76     assert (pruned_ratios > [0] * len(pruned_ratios) and
77             pruned_ratios < [1] * len(pruned_ratios)
78             ), "The elements of pruned ratios should be in range (0, 1)."
79 
80     base_flops = flops(infer_prog)
81     pruner = Pruner()
82     infer_prog, _, _ = pruner.prune(
83         infer_prog,
84         fluid.global_scope(),
85         params=pruned_params,
86         ratios=pruned_ratios,
87         place=place,
88         only_graph=True)
89     pruned_flops = flops(infer_prog)
90     logger.info("pruned FLOPS: {}".format(
91         float(base_flops - pruned_flops) / base_flops))
92 
93     exe.run(startup_prog)
94     checkpoint.load_checkpoint(exe, infer_prog, cfg.weights)
95 
96     dump_infer_config(FLAGS, cfg)
97     save_infer_model(FLAGS, exe, feed_vars, test_fetches, infer_prog)
98 
99 
100 if __name__ == '__main__':
101     enable_static_mode()
102     parser = ArgsParser()
103     parser.add_argument(
104         "--output_dir",
105         type=str,
106         default="output",
107         help="Directory for storing the output model files.")
108 
109     parser.add_argument(
110         "-p",
111         "--pruned_params",
112         default=None,
113         type=str,
114         help="The parameters to be pruned when calculating sensitivities.")
115     parser.add_argument(
116         "--pruned_ratios",
117         default=None,
118         type=str,
119         help="The ratios pruned iteratively for each parameter when calculating sensitivities."
120     )
121 
122     FLAGS = parser.parse_args()
123     main()
124
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/slim/prune/export_model.py b/slim/prune/export_model.py
--- a/slim/prune/export_model.py
+++ b/slim/prune/export_model.py
@@ -63,6 +63,9 @@
             test_fetches = model.test(feed_vars)
     infer_prog = infer_prog.clone(True)
 
+    exe.run(startup_prog)
+    checkpoint.load_checkpoint(exe, infer_prog, cfg.weights)
+
     pruned_params = FLAGS.pruned_params
     assert (
         FLAGS.pruned_params is not None
@@ -90,13 +93,9 @@
     logger.info("pruned FLOPS: {}".format(
         float(base_flops - pruned_flops) / base_flops))
 
-    exe.run(startup_prog)
-    checkpoint.load_checkpoint(exe, infer_prog, cfg.weights)
-
     dump_infer_config(FLAGS, cfg)
     save_infer_model(FLAGS, exe, feed_vars, test_fetches, infer_prog)
 
-
 if __name__ == '__main__':
     enable_static_mode()
     parser = ArgsParser()

{"golden_diff": "diff --git a/slim/prune/export_model.py b/slim/prune/export_model.py\n--- a/slim/prune/export_model.py\n+++ b/slim/prune/export_model.py\n@@ -63,6 +63,9 @@\n             test_fetches = model.test(feed_vars)\n     infer_prog = infer_prog.clone(True)\n \n+    exe.run(startup_prog)\n+    checkpoint.load_checkpoint(exe, infer_prog, cfg.weights)\n+\n     pruned_params = FLAGS.pruned_params\n     assert (\n         FLAGS.pruned_params is not None\n@@ -90,13 +93,9 @@\n     logger.info(\"pruned FLOPS: {}\".format(\n         float(base_flops - pruned_flops) / base_flops))\n \n-    exe.run(startup_prog)\n-    checkpoint.load_checkpoint(exe, infer_prog, cfg.weights)\n-\n     dump_infer_config(FLAGS, cfg)\n     save_infer_model(FLAGS, exe, feed_vars, test_fetches, infer_prog)\n \n-\n if __name__ == '__main__':\n     enable_static_mode()\n     parser = ArgsParser()\n", "issue": "\u8bad\u7ec3-\u88c1\u526a-\u8bad\u7ec3-\u5bfc\u51fa\u62a5\u9519\nPaddleDetection\u662f2.0\u7248\u672c\r\n\u9879\u76ee\u662f\u5728\u5e73\u53f0\u4e0a\u8fd0\u884c\u7684\r\n\u4f7f\u7528PaddleDetection\u8bad\u7ec3\u597dyolov3_mobilenet_v3\u6a21\u578b\u540e\u8fdb\u884c\u6a21\u578b\u88c1\u526a\r\n\u88c1\u526a\u5b8c\u8bc4\u4f30\u5b8c\u6210\u540e\u5bfc\u51fa\u62a5\u9519\r\n\u8fd9\u662f\u6211\u6267\u884c\u7684\u811a\u672c\r\n`\r\n!python slim/prune/export_model.py \\\r\n-c configs/yolov3_mobilenet_v3.yml \\\r\n--pruned_params \"yolo_block.0.0.0.conv.weights,yolo_block.0.0.1.conv.weights,yolo_block.0.1.0.conv.weights,yolo_block.0.1.1.conv.weights,yolo_block.0.2.conv.weights,yolo_block.0.tip.conv.weights,yolo_block.1.0.0.conv.weights,yolo_block.1.0.1.conv.weights,yolo_block.1.1.0.conv.weights,yolo_block.1.1.1.conv.weights,yolo_block.1.2.conv.weights,yolo_block.1.tip.conv.weights,yolo_block.2.0.0.conv.weights,yolo_block.2.0.1.conv.weights,yolo_block.2.1.0.conv.weights,yolo_block.2.1.1.conv.weights,yolo_block.2.2.conv.weights,yolo_block.2.tip.conv.weights\" \\\r\n--pruned_ratios=\"0.7150126596733395,0.8177442961035291,0.8274278897456334,0.8373393786362668,0.7956892620674756,0.8445719578292334,0.9,0.9,0.9,0.9,0.9,0.9,0.9,0.9,0.9,0.9,0.9,0.9\" \\\r\n-o weights=output/yolov3_mobilenet_v3/model_final\r\n`\r\n\r\n\u4ee5\u4e0b\u662f\u62a5\u9519\u4fe1\u606f\uff0c\r\n\r\n```\r\n[03-04 10:10:58 MainThread @logger.py:242] Argv: slim/prune/export_model.py -c configs/yolov3_mobilenet_v3.yml --pruned_params yolo_block.0.0.0.conv.weights,yolo_block.0.0.1.conv.weights,yolo_block.0.1.0.conv.weights,yolo_block.0.1.1.conv.weights,yolo_block.0.2.conv.weights,yolo_block.0.tip.conv.weights,yolo_block.1.0.0.conv.weights,yolo_block.1.0.1.conv.weights,yolo_block.1.1.0.conv.weights,yolo_block.1.1.1.conv.weights,yolo_block.1.2.conv.weights,yolo_block.1.tip.conv.weights,yolo_block.2.0.0.conv.weights,yolo_block.2.0.1.conv.weights,yolo_block.2.1.0.conv.weights,yolo_block.2.1.1.conv.weights,yolo_block.2.2.conv.weights,yolo_block.2.tip.conv.weights --pruned_ratios=0.7150126596733395,0.8177442961035291,0.8274278897456334,0.8373393786362668,0.7956892620674756,0.8445719578292334,0.9,0.9,0.9,0.9,0.9,0.9,0.9,0.9,0.9,0.9,0.9,0.9 -o weights=output/yolov3_mobilenet_v3/model_final\r\n[03-04 10:10:58 MainThread @utils.py:79] WRN paddlepaddle version: 2.0.0. The dynamic graph version of PARL is under development, not fully tested and supported\r\n/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/parl/remote/communication.py:38: DeprecationWarning: 'pyarrow.default_serialization_context' is deprecated as of 2.0.0 and will be removed in a future version. Use pickle or the pyarrow IPC functionality instead.\r\n  context = pyarrow.default_serialization_context()\r\n/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/pandas/core/tools/datetimes.py:3: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working\r\n  from collections import MutableMapping\r\n/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/rcsetup.py:20: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working\r\n  from collections import Iterable, Mapping\r\n/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/colors.py:53: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working\r\n  from collections import Sized\r\n2021-03-04 10:11:00,126-INFO: pruned params: ['yolo_block.0.0.0.conv.weights', 'yolo_block.0.0.1.conv.weights', 'yolo_block.0.1.0.conv.weights', 'yolo_block.0.1.1.conv.weights', 'yolo_block.0.2.conv.weights', 'yolo_block.0.tip.conv.weights', 'yolo_block.1.0.0.conv.weights', 'yolo_block.1.0.1.conv.weights', 'yolo_block.1.1.0.conv.weights', 'yolo_block.1.1.1.conv.weights', 'yolo_block.1.2.conv.weights', 'yolo_block.1.tip.conv.weights', 'yolo_block.2.0.0.conv.weights', 'yolo_block.2.0.1.conv.weights', 'yolo_block.2.1.0.conv.weights', 'yolo_block.2.1.1.conv.weights', 'yolo_block.2.2.conv.weights', 'yolo_block.2.tip.conv.weights']\r\n2021-03-04 10:11:00,126-INFO: pruned ratios: [0.7150126596733395, 0.8177442961035291, 0.8274278897456334, 0.8373393786362668, 0.7956892620674756, 0.8445719578292334, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9]\r\n2021-03-04 10:11:00,169-INFO: pruning: yolo_block.0.0.0.conv.weights\r\nTraceback (most recent call last):\r\n  File \"slim/prune/export_model.py\", line 123, in <module>\r\n    main()\r\n  File \"slim/prune/export_model.py\", line 88, in main\r\n    only_graph=True)\r\n  File \"/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddleslim/prune/pruner.py\", line 112, in prune\r\n    g = self._transform(self.idx_selector(scores, ratio))\r\n  File \"/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddleslim/prune/idx_selector.py\", line 57, in default_idx_selector\r\n    0]  # sort channels by the first convolution's score\r\nIndexError: list index out of range\r\n```\n", "before_files": [{"content": "# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport os, sys\n# add python path of PadleDetection to sys.path\nparent_path = os.path.abspath(os.path.join(__file__, *(['..'] * 3)))\nif parent_path not in sys.path:\n    sys.path.append(parent_path)\n\nimport paddle\nfrom paddle import fluid\n\nfrom ppdet.core.workspace import load_config, merge_config, create\nfrom ppdet.utils.cli import ArgsParser\nimport ppdet.utils.checkpoint as checkpoint\nfrom ppdet.utils.export_utils import save_infer_model, dump_infer_config\nfrom ppdet.utils.check import check_config, check_version, enable_static_mode\nfrom paddleslim.prune import Pruner\nfrom paddleslim.analysis import flops\n\nimport logging\nFORMAT = '%(asctime)s-%(levelname)s: %(message)s'\nlogging.basicConfig(level=logging.INFO, format=FORMAT)\nlogger = logging.getLogger(__name__)\n\n\ndef main():\n    cfg = load_config(FLAGS.config)\n    merge_config(FLAGS.opt)\n    check_config(cfg)\n    check_version()\n\n    main_arch = cfg.architecture\n\n    # Use CPU for exporting inference model instead of GPU\n    place = fluid.CPUPlace()\n    exe = fluid.Executor(place)\n\n    model = create(main_arch)\n\n    startup_prog = fluid.Program()\n    infer_prog = fluid.Program()\n    with fluid.program_guard(infer_prog, startup_prog):\n        with fluid.unique_name.guard():\n            inputs_def = cfg['TestReader']['inputs_def']\n            inputs_def['use_dataloader'] = False\n            feed_vars, _ = model.build_inputs(**inputs_def)\n            test_fetches = model.test(feed_vars)\n    infer_prog = infer_prog.clone(True)\n\n    pruned_params = FLAGS.pruned_params\n    assert (\n        FLAGS.pruned_params is not None\n    ), \"FLAGS.pruned_params is empty!!! Please set it by '--pruned_params' option.\"\n    pruned_params = FLAGS.pruned_params.strip().split(\",\")\n    logger.info(\"pruned params: {}\".format(pruned_params))\n    pruned_ratios = [float(n) for n in FLAGS.pruned_ratios.strip().split(\",\")]\n    logger.info(\"pruned ratios: {}\".format(pruned_ratios))\n    assert (len(pruned_params) == len(pruned_ratios)\n            ), \"The length of pruned params and pruned ratios should be equal.\"\n    assert (pruned_ratios > [0] * len(pruned_ratios) and\n            pruned_ratios < [1] * len(pruned_ratios)\n            ), \"The elements of pruned ratios should be in range (0, 1).\"\n\n    base_flops = flops(infer_prog)\n    pruner = Pruner()\n    infer_prog, _, _ = pruner.prune(\n        infer_prog,\n        fluid.global_scope(),\n        params=pruned_params,\n        ratios=pruned_ratios,\n        place=place,\n        only_graph=True)\n    pruned_flops = flops(infer_prog)\n    logger.info(\"pruned FLOPS: {}\".format(\n        float(base_flops - pruned_flops) / base_flops))\n\n    exe.run(startup_prog)\n    checkpoint.load_checkpoint(exe, infer_prog, cfg.weights)\n\n    dump_infer_config(FLAGS, cfg)\n    save_infer_model(FLAGS, exe, feed_vars, test_fetches, infer_prog)\n\n\nif __name__ == '__main__':\n    enable_static_mode()\n    parser = ArgsParser()\n    parser.add_argument(\n        \"--output_dir\",\n        type=str,\n        default=\"output\",\n        help=\"Directory for storing the output model files.\")\n\n    parser.add_argument(\n        \"-p\",\n        \"--pruned_params\",\n        default=None,\n        type=str,\n        help=\"The parameters to be pruned when calculating sensitivities.\")\n    parser.add_argument(\n        \"--pruned_ratios\",\n        default=None,\n        type=str,\n        help=\"The ratios pruned iteratively for each parameter when calculating sensitivities.\"\n    )\n\n    FLAGS = parser.parse_args()\n    main()\n", "path": "slim/prune/export_model.py"}], "after_files": [{"content": "# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport os, sys\n# add python path of PadleDetection to sys.path\nparent_path = os.path.abspath(os.path.join(__file__, *(['..'] * 3)))\nif parent_path not in sys.path:\n    sys.path.append(parent_path)\n\nimport paddle\nfrom paddle import fluid\n\nfrom ppdet.core.workspace import load_config, merge_config, create\nfrom ppdet.utils.cli import ArgsParser\nimport ppdet.utils.checkpoint as checkpoint\nfrom ppdet.utils.export_utils import save_infer_model, dump_infer_config\nfrom ppdet.utils.check import check_config, check_version, enable_static_mode\nfrom paddleslim.prune import Pruner\nfrom paddleslim.analysis import flops\n\nimport logging\nFORMAT = '%(asctime)s-%(levelname)s: %(message)s'\nlogging.basicConfig(level=logging.INFO, format=FORMAT)\nlogger = logging.getLogger(__name__)\n\n\ndef main():\n    cfg = load_config(FLAGS.config)\n    merge_config(FLAGS.opt)\n    check_config(cfg)\n    check_version()\n\n    main_arch = cfg.architecture\n\n    # Use CPU for exporting inference model instead of GPU\n    place = fluid.CPUPlace()\n    exe = fluid.Executor(place)\n\n    model = create(main_arch)\n\n    startup_prog = fluid.Program()\n    infer_prog = fluid.Program()\n    with fluid.program_guard(infer_prog, startup_prog):\n        with fluid.unique_name.guard():\n            inputs_def = cfg['TestReader']['inputs_def']\n            inputs_def['use_dataloader'] = False\n            feed_vars, _ = model.build_inputs(**inputs_def)\n            test_fetches = model.test(feed_vars)\n    infer_prog = infer_prog.clone(True)\n\n    exe.run(startup_prog)\n    checkpoint.load_checkpoint(exe, infer_prog, cfg.weights)\n\n    pruned_params = FLAGS.pruned_params\n    assert (\n        FLAGS.pruned_params is not None\n    ), \"FLAGS.pruned_params is empty!!! Please set it by '--pruned_params' option.\"\n    pruned_params = FLAGS.pruned_params.strip().split(\",\")\n    logger.info(\"pruned params: {}\".format(pruned_params))\n    pruned_ratios = [float(n) for n in FLAGS.pruned_ratios.strip().split(\",\")]\n    logger.info(\"pruned ratios: {}\".format(pruned_ratios))\n    assert (len(pruned_params) == len(pruned_ratios)\n            ), \"The length of pruned params and pruned ratios should be equal.\"\n    assert (pruned_ratios > [0] * len(pruned_ratios) and\n            pruned_ratios < [1] * len(pruned_ratios)\n            ), \"The elements of pruned ratios should be in range (0, 1).\"\n\n    base_flops = flops(infer_prog)\n    pruner = Pruner()\n    infer_prog, _, _ = pruner.prune(\n        infer_prog,\n        fluid.global_scope(),\n        params=pruned_params,\n        ratios=pruned_ratios,\n        place=place,\n        only_graph=True)\n    pruned_flops = flops(infer_prog)\n    logger.info(\"pruned FLOPS: {}\".format(\n        float(base_flops - pruned_flops) / base_flops))\n\n    dump_infer_config(FLAGS, cfg)\n    save_infer_model(FLAGS, exe, feed_vars, test_fetches, infer_prog)\n\nif __name__ == '__main__':\n    enable_static_mode()\n    parser = ArgsParser()\n    parser.add_argument(\n        \"--output_dir\",\n        type=str,\n        default=\"output\",\n        help=\"Directory for storing the output model files.\")\n\n    parser.add_argument(\n        \"-p\",\n        \"--pruned_params\",\n        default=None,\n        type=str,\n        help=\"The parameters to be pruned when calculating sensitivities.\")\n    parser.add_argument(\n        \"--pruned_ratios\",\n        default=None,\n        type=str,\n        help=\"The ratios pruned iteratively for each parameter when calculating sensitivities.\"\n    )\n\n    FLAGS = parser.parse_args()\n    main()\n", "path": "slim/prune/export_model.py"}]}