We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
运行检测模型卷积通道剪裁示例时报错
当我运行python compress.py \
    -s yolov3_mobilenet_v1_slim.yaml \
    -c ../../configs/yolov3_mobilenet_v1.yml \
    -o max_iters=20 \
    num_classes=4 \
    YoloTrainFeed.batch_size=32 \
    pretrain_weights=/home/aistudio/PaddleDetection/output/yolov3_mobilenet_v1/best_model \
    -d "/home/aistudio/work/coco"
这个命令时一直报错以下错误，经定位是eval_utils.py将gt_box的值转换成im_id异常，异常信息如下


loading annotations into memory...
Done (t=0.01s)
creating index...
index created!
[ 16. 115. 218. 374.]
Traceback (most recent call last):
  File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/contrib/slim/core/compressor.py", line 593, in run
    self._eval(context)
  File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/contrib/slim/core/compressor.py", line 542, in _eval
    func(self.eval_graph.program, self.scope))
  File "compress.py", line 207, in eval_func
    FLAGS.output_eval)
  File "../../ppdet/utils/eval_utils.py", line 205, in eval_results
    is_bbox_normalized=is_bbox_normalized)
  File "../../ppdet/utils/coco_eval.py", line 86, in bbox_eval
    results, clsid2catid, is_bbox_normalized=is_bbox_normalized)
  File "../../ppdet/utils/coco_eval.py", line 215, in bbox2out
    im_id = int(im_ids[i][0])
TypeError: only size-1 arrays can be converted to Python scalars
2019-11-20 20:32:00,491-ERROR: None
2019-11-20 20:32:00,491-ERROR: None
2019-11-20 20:32:01,633-INFO: epoch:1; batch_id:0; odict_keys(['loss', 'lr']) = [117.678, 0.0]


后面通过排查发现compress.py 将gt_box的值当成im_id出入导致报错，
在第79行新增outs.append(data['im_id']) 解决问题

![QQ图片20191120204048](https://user-images.githubusercontent.com/56528375/69239822-0e5b6900-0bd6-11ea-9fa2-ee8d95eb183d.png)


--- 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/compress.py`
Content:
```
1 # Copyright (c) 2019 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
20 import time
21 import multiprocessing
22 import numpy as np
23 import sys
24 sys.path.append("../../")
25 from paddle.fluid.contrib.slim import Compressor
26 
27 
28 def set_paddle_flags(**kwargs):
29     for key, value in kwargs.items():
30         if os.environ.get(key, None) is None:
31             os.environ[key] = str(value)
32 
33 
34 # NOTE(paddle-dev): All of these flags should be set before
35 # `import paddle`. Otherwise, it would not take any effect.
36 set_paddle_flags(
37     FLAGS_eager_delete_tensor_gb=0,  # enable GC to save memory
38 )
39 
40 from paddle import fluid
41 from ppdet.core.workspace import load_config, merge_config, create
42 from ppdet.data.data_feed import create_reader
43 from ppdet.utils.eval_utils import parse_fetches, eval_results
44 from ppdet.utils.cli import ArgsParser
45 from ppdet.utils.check import check_gpu
46 import ppdet.utils.checkpoint as checkpoint
47 from ppdet.modeling.model_input import create_feed
48 
49 import logging
50 FORMAT = '%(asctime)s-%(levelname)s: %(message)s'
51 logging.basicConfig(level=logging.INFO, format=FORMAT)
52 logger = logging.getLogger(__name__)
53 
54 
55 def eval_run(exe, compile_program, reader, keys, values, cls, test_feed):
56     """
57     Run evaluation program, return program outputs.
58     """
59     iter_id = 0
60     results = []
61     if len(cls) != 0:
62         values = []
63         for i in range(len(cls)):
64             _, accum_map = cls[i].get_map_var()
65             cls[i].reset(exe)
66             values.append(accum_map)
67 
68     images_num = 0
69     start_time = time.time()
70     has_bbox = 'bbox' in keys
71     for data in reader():
72         data = test_feed.feed(data)
73         feed_data = {'image': data['image'], 'im_size': data['im_size']}
74         outs = exe.run(compile_program,
75                        feed=feed_data,
76                        fetch_list=[values[0]],
77                        return_numpy=False)
78         outs.append(data['gt_box'])
79         outs.append(data['gt_label'])
80         outs.append(data['is_difficult'])
81         res = {
82             k: (np.array(v), v.recursive_sequence_lengths())
83             for k, v in zip(keys, outs)
84         }
85         results.append(res)
86         if iter_id % 100 == 0:
87             logger.info('Test iter {}'.format(iter_id))
88         iter_id += 1
89         images_num += len(res['bbox'][1][0]) if has_bbox else 1
90     logger.info('Test finish iter {}'.format(iter_id))
91 
92     end_time = time.time()
93     fps = images_num / (end_time - start_time)
94     if has_bbox:
95         logger.info('Total number of images: {}, inference time: {} fps.'.
96                     format(images_num, fps))
97     else:
98         logger.info('Total iteration: {}, inference time: {} batch/s.'.format(
99             images_num, fps))
100 
101     return results
102 
103 
104 def main():
105     cfg = load_config(FLAGS.config)
106     if 'architecture' in cfg:
107         main_arch = cfg.architecture
108     else:
109         raise ValueError("'architecture' not specified in config file.")
110 
111     merge_config(FLAGS.opt)
112     if 'log_iter' not in cfg:
113         cfg.log_iter = 20
114 
115     # check if set use_gpu=True in paddlepaddle cpu version
116     check_gpu(cfg.use_gpu)
117 
118     if cfg.use_gpu:
119         devices_num = fluid.core.get_cuda_device_count()
120     else:
121         devices_num = int(
122             os.environ.get('CPU_NUM', multiprocessing.cpu_count()))
123 
124     if 'train_feed' not in cfg:
125         train_feed = create(main_arch + 'TrainFeed')
126     else:
127         train_feed = create(cfg.train_feed)
128 
129     if 'eval_feed' not in cfg:
130         eval_feed = create(main_arch + 'EvalFeed')
131     else:
132         eval_feed = create(cfg.eval_feed)
133 
134     place = fluid.CUDAPlace(0) if cfg.use_gpu else fluid.CPUPlace()
135     exe = fluid.Executor(place)
136 
137     lr_builder = create('LearningRate')
138     optim_builder = create('OptimizerBuilder')
139 
140     # build program
141     startup_prog = fluid.Program()
142     train_prog = fluid.Program()
143     with fluid.program_guard(train_prog, startup_prog):
144         with fluid.unique_name.guard():
145             model = create(main_arch)
146             _, feed_vars = create_feed(train_feed, True)
147             train_fetches = model.train(feed_vars)
148             loss = train_fetches['loss']
149             lr = lr_builder()
150             optimizer = optim_builder(lr)
151             optimizer.minimize(loss)
152 
153     train_reader = create_reader(train_feed, cfg.max_iters, FLAGS.dataset_dir)
154 
155     # parse train fetches
156     train_keys, train_values, _ = parse_fetches(train_fetches)
157     train_keys.append("lr")
158     train_values.append(lr.name)
159 
160     train_fetch_list = []
161     for k, v in zip(train_keys, train_values):
162         train_fetch_list.append((k, v))
163 
164     eval_prog = fluid.Program()
165     with fluid.program_guard(eval_prog, startup_prog):
166         with fluid.unique_name.guard():
167             model = create(main_arch)
168             _, test_feed_vars = create_feed(eval_feed, True)
169             fetches = model.eval(test_feed_vars)
170 
171     eval_prog = eval_prog.clone(True)
172 
173     eval_reader = create_reader(eval_feed, args_path=FLAGS.dataset_dir)
174     test_data_feed = fluid.DataFeeder(test_feed_vars.values(), place)
175 
176     # parse eval fetches
177     extra_keys = []
178     if cfg.metric == 'COCO':
179         extra_keys = ['im_info', 'im_id', 'im_shape']
180     if cfg.metric == 'VOC':
181         extra_keys = ['gt_box', 'gt_label', 'is_difficult']
182     eval_keys, eval_values, eval_cls = parse_fetches(fetches, eval_prog,
183                                                      extra_keys)
184     eval_fetch_list = []
185     for k, v in zip(eval_keys, eval_values):
186         eval_fetch_list.append((k, v))
187 
188     exe.run(startup_prog)
189     checkpoint.load_params(exe, train_prog, cfg.pretrain_weights)
190 
191     best_box_ap_list = []
192 
193     def eval_func(program, scope):
194 
195         #place = fluid.CPUPlace()
196         #exe = fluid.Executor(place)
197         results = eval_run(exe, program, eval_reader, eval_keys, eval_values,
198                            eval_cls, test_data_feed)
199 
200         resolution = None
201         if 'mask' in results[0]:
202             resolution = model.mask_head.resolution
203         box_ap_stats = eval_results(results, eval_feed, cfg.metric,
204                                     cfg.num_classes, resolution, False,
205                                     FLAGS.output_eval)
206         if len(best_box_ap_list) == 0:
207             best_box_ap_list.append(box_ap_stats[0])
208         elif box_ap_stats[0] > best_box_ap_list[0]:
209             best_box_ap_list[0] = box_ap_stats[0]
210         logger.info("Best test box ap: {}".format(best_box_ap_list[0]))
211         return best_box_ap_list[0]
212 
213     test_feed = [('image', test_feed_vars['image'].name),
214                  ('im_size', test_feed_vars['im_size'].name)]
215 
216     com = Compressor(
217         place,
218         fluid.global_scope(),
219         train_prog,
220         train_reader=train_reader,
221         train_feed_list=[(key, value.name) for key, value in feed_vars.items()],
222         train_fetch_list=train_fetch_list,
223         eval_program=eval_prog,
224         eval_reader=eval_reader,
225         eval_feed_list=test_feed,
226         eval_func={'map': eval_func},
227         eval_fetch_list=[eval_fetch_list[0]],
228         save_eval_model=True,
229         prune_infer_model=[["image", "im_size"], ["multiclass_nms_0.tmp_0"]],
230         train_optimizer=None)
231     com.config(FLAGS.slim_file)
232     com.run()
233 
234 
235 if __name__ == '__main__':
236     parser = ArgsParser()
237     parser.add_argument(
238         "-s",
239         "--slim_file",
240         default=None,
241         type=str,
242         help="Config file of PaddleSlim.")
243     parser.add_argument(
244         "--output_eval",
245         default=None,
246         type=str,
247         help="Evaluation directory, default is current directory.")
248     parser.add_argument(
249         "-d",
250         "--dataset_dir",
251         default=None,
252         type=str,
253         help="Dataset path, same as DataFeed.dataset.dataset_dir")
254     FLAGS = parser.parse_args()
255     main()
256
```
--- 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/compress.py b/slim/prune/compress.py
--- a/slim/prune/compress.py
+++ b/slim/prune/compress.py
@@ -52,7 +52,7 @@
 logger = logging.getLogger(__name__)
 
 
-def eval_run(exe, compile_program, reader, keys, values, cls, test_feed):
+def eval_run(exe, compile_program, reader, keys, values, cls, test_feed, cfg):
     """
     Run evaluation program, return program outputs.
     """
@@ -75,9 +75,16 @@
                        feed=feed_data,
                        fetch_list=[values[0]],
                        return_numpy=False)
-        outs.append(data['gt_box'])
-        outs.append(data['gt_label'])
-        outs.append(data['is_difficult'])
+
+        if cfg.metric == 'VOC':
+            outs.append(data['gt_box'])
+            outs.append(data['gt_label'])
+            outs.append(data['is_difficult'])
+        elif cfg.metric == 'COCO':
+            outs.append(data['im_info'])
+            outs.append(data['im_id'])
+            outs.append(data['im_shape'])
+
         res = {
             k: (np.array(v), v.recursive_sequence_lengths())
             for k, v in zip(keys, outs)
@@ -195,7 +202,7 @@
         #place = fluid.CPUPlace()
         #exe = fluid.Executor(place)
         results = eval_run(exe, program, eval_reader, eval_keys, eval_values,
-                           eval_cls, test_data_feed)
+                           eval_cls, test_data_feed, cfg)
 
         resolution = None
         if 'mask' in results[0]:

{"golden_diff": "diff --git a/slim/prune/compress.py b/slim/prune/compress.py\n--- a/slim/prune/compress.py\n+++ b/slim/prune/compress.py\n@@ -52,7 +52,7 @@\n logger = logging.getLogger(__name__)\n \n \n-def eval_run(exe, compile_program, reader, keys, values, cls, test_feed):\n+def eval_run(exe, compile_program, reader, keys, values, cls, test_feed, cfg):\n     \"\"\"\n     Run evaluation program, return program outputs.\n     \"\"\"\n@@ -75,9 +75,16 @@\n                        feed=feed_data,\n                        fetch_list=[values[0]],\n                        return_numpy=False)\n-        outs.append(data['gt_box'])\n-        outs.append(data['gt_label'])\n-        outs.append(data['is_difficult'])\n+\n+        if cfg.metric == 'VOC':\n+            outs.append(data['gt_box'])\n+            outs.append(data['gt_label'])\n+            outs.append(data['is_difficult'])\n+        elif cfg.metric == 'COCO':\n+            outs.append(data['im_info'])\n+            outs.append(data['im_id'])\n+            outs.append(data['im_shape'])\n+\n         res = {\n             k: (np.array(v), v.recursive_sequence_lengths())\n             for k, v in zip(keys, outs)\n@@ -195,7 +202,7 @@\n         #place = fluid.CPUPlace()\n         #exe = fluid.Executor(place)\n         results = eval_run(exe, program, eval_reader, eval_keys, eval_values,\n-                           eval_cls, test_data_feed)\n+                           eval_cls, test_data_feed, cfg)\n \n         resolution = None\n         if 'mask' in results[0]:\n", "issue": "\u8fd0\u884c\u68c0\u6d4b\u6a21\u578b\u5377\u79ef\u901a\u9053\u526a\u88c1\u793a\u4f8b\u65f6\u62a5\u9519\n\u5f53\u6211\u8fd0\u884cpython compress.py \\\r\n    -s yolov3_mobilenet_v1_slim.yaml \\\r\n    -c ../../configs/yolov3_mobilenet_v1.yml \\\r\n    -o max_iters=20 \\\r\n    num_classes=4 \\\r\n    YoloTrainFeed.batch_size=32 \\\r\n    pretrain_weights=/home/aistudio/PaddleDetection/output/yolov3_mobilenet_v1/best_model \\\r\n    -d \"/home/aistudio/work/coco\"\r\n\u8fd9\u4e2a\u547d\u4ee4\u65f6\u4e00\u76f4\u62a5\u9519\u4ee5\u4e0b\u9519\u8bef\uff0c\u7ecf\u5b9a\u4f4d\u662feval_utils.py\u5c06gt_box\u7684\u503c\u8f6c\u6362\u6210im_id\u5f02\u5e38\uff0c\u5f02\u5e38\u4fe1\u606f\u5982\u4e0b\r\n\r\n\r\nloading annotations into memory...\r\nDone (t=0.01s)\r\ncreating index...\r\nindex created!\r\n[ 16. 115. 218. 374.]\r\nTraceback (most recent call last):\r\n  File \"/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/contrib/slim/core/compressor.py\", line 593, in run\r\n    self._eval(context)\r\n  File \"/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/contrib/slim/core/compressor.py\", line 542, in _eval\r\n    func(self.eval_graph.program, self.scope))\r\n  File \"compress.py\", line 207, in eval_func\r\n    FLAGS.output_eval)\r\n  File \"../../ppdet/utils/eval_utils.py\", line 205, in eval_results\r\n    is_bbox_normalized=is_bbox_normalized)\r\n  File \"../../ppdet/utils/coco_eval.py\", line 86, in bbox_eval\r\n    results, clsid2catid, is_bbox_normalized=is_bbox_normalized)\r\n  File \"../../ppdet/utils/coco_eval.py\", line 215, in bbox2out\r\n    im_id = int(im_ids[i][0])\r\nTypeError: only size-1 arrays can be converted to Python scalars\r\n2019-11-20 20:32:00,491-ERROR: None\r\n2019-11-20 20:32:00,491-ERROR: None\r\n2019-11-20 20:32:01,633-INFO: epoch:1; batch_id:0; odict_keys(['loss', 'lr']) = [117.678, 0.0]\r\n\r\n\r\n\u540e\u9762\u901a\u8fc7\u6392\u67e5\u53d1\u73b0compress.py \u5c06gt_box\u7684\u503c\u5f53\u6210im_id\u51fa\u5165\u5bfc\u81f4\u62a5\u9519\uff0c\r\n\u5728\u7b2c79\u884c\u65b0\u589eouts.append(data['im_id']) \u89e3\u51b3\u95ee\u9898\r\n\r\n![QQ\u56fe\u724720191120204048](https://user-images.githubusercontent.com/56528375/69239822-0e5b6900-0bd6-11ea-9fa2-ee8d95eb183d.png)\r\n\n", "before_files": [{"content": "# Copyright (c) 2019 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\nimport time\nimport multiprocessing\nimport numpy as np\nimport sys\nsys.path.append(\"../../\")\nfrom paddle.fluid.contrib.slim import Compressor\n\n\ndef set_paddle_flags(**kwargs):\n    for key, value in kwargs.items():\n        if os.environ.get(key, None) is None:\n            os.environ[key] = str(value)\n\n\n# NOTE(paddle-dev): All of these flags should be set before\n# `import paddle`. Otherwise, it would not take any effect.\nset_paddle_flags(\n    FLAGS_eager_delete_tensor_gb=0,  # enable GC to save memory\n)\n\nfrom paddle import fluid\nfrom ppdet.core.workspace import load_config, merge_config, create\nfrom ppdet.data.data_feed import create_reader\nfrom ppdet.utils.eval_utils import parse_fetches, eval_results\nfrom ppdet.utils.cli import ArgsParser\nfrom ppdet.utils.check import check_gpu\nimport ppdet.utils.checkpoint as checkpoint\nfrom ppdet.modeling.model_input import create_feed\n\nimport logging\nFORMAT = '%(asctime)s-%(levelname)s: %(message)s'\nlogging.basicConfig(level=logging.INFO, format=FORMAT)\nlogger = logging.getLogger(__name__)\n\n\ndef eval_run(exe, compile_program, reader, keys, values, cls, test_feed):\n    \"\"\"\n    Run evaluation program, return program outputs.\n    \"\"\"\n    iter_id = 0\n    results = []\n    if len(cls) != 0:\n        values = []\n        for i in range(len(cls)):\n            _, accum_map = cls[i].get_map_var()\n            cls[i].reset(exe)\n            values.append(accum_map)\n\n    images_num = 0\n    start_time = time.time()\n    has_bbox = 'bbox' in keys\n    for data in reader():\n        data = test_feed.feed(data)\n        feed_data = {'image': data['image'], 'im_size': data['im_size']}\n        outs = exe.run(compile_program,\n                       feed=feed_data,\n                       fetch_list=[values[0]],\n                       return_numpy=False)\n        outs.append(data['gt_box'])\n        outs.append(data['gt_label'])\n        outs.append(data['is_difficult'])\n        res = {\n            k: (np.array(v), v.recursive_sequence_lengths())\n            for k, v in zip(keys, outs)\n        }\n        results.append(res)\n        if iter_id % 100 == 0:\n            logger.info('Test iter {}'.format(iter_id))\n        iter_id += 1\n        images_num += len(res['bbox'][1][0]) if has_bbox else 1\n    logger.info('Test finish iter {}'.format(iter_id))\n\n    end_time = time.time()\n    fps = images_num / (end_time - start_time)\n    if has_bbox:\n        logger.info('Total number of images: {}, inference time: {} fps.'.\n                    format(images_num, fps))\n    else:\n        logger.info('Total iteration: {}, inference time: {} batch/s.'.format(\n            images_num, fps))\n\n    return results\n\n\ndef main():\n    cfg = load_config(FLAGS.config)\n    if 'architecture' in cfg:\n        main_arch = cfg.architecture\n    else:\n        raise ValueError(\"'architecture' not specified in config file.\")\n\n    merge_config(FLAGS.opt)\n    if 'log_iter' not in cfg:\n        cfg.log_iter = 20\n\n    # check if set use_gpu=True in paddlepaddle cpu version\n    check_gpu(cfg.use_gpu)\n\n    if cfg.use_gpu:\n        devices_num = fluid.core.get_cuda_device_count()\n    else:\n        devices_num = int(\n            os.environ.get('CPU_NUM', multiprocessing.cpu_count()))\n\n    if 'train_feed' not in cfg:\n        train_feed = create(main_arch + 'TrainFeed')\n    else:\n        train_feed = create(cfg.train_feed)\n\n    if 'eval_feed' not in cfg:\n        eval_feed = create(main_arch + 'EvalFeed')\n    else:\n        eval_feed = create(cfg.eval_feed)\n\n    place = fluid.CUDAPlace(0) if cfg.use_gpu else fluid.CPUPlace()\n    exe = fluid.Executor(place)\n\n    lr_builder = create('LearningRate')\n    optim_builder = create('OptimizerBuilder')\n\n    # build program\n    startup_prog = fluid.Program()\n    train_prog = fluid.Program()\n    with fluid.program_guard(train_prog, startup_prog):\n        with fluid.unique_name.guard():\n            model = create(main_arch)\n            _, feed_vars = create_feed(train_feed, True)\n            train_fetches = model.train(feed_vars)\n            loss = train_fetches['loss']\n            lr = lr_builder()\n            optimizer = optim_builder(lr)\n            optimizer.minimize(loss)\n\n    train_reader = create_reader(train_feed, cfg.max_iters, FLAGS.dataset_dir)\n\n    # parse train fetches\n    train_keys, train_values, _ = parse_fetches(train_fetches)\n    train_keys.append(\"lr\")\n    train_values.append(lr.name)\n\n    train_fetch_list = []\n    for k, v in zip(train_keys, train_values):\n        train_fetch_list.append((k, v))\n\n    eval_prog = fluid.Program()\n    with fluid.program_guard(eval_prog, startup_prog):\n        with fluid.unique_name.guard():\n            model = create(main_arch)\n            _, test_feed_vars = create_feed(eval_feed, True)\n            fetches = model.eval(test_feed_vars)\n\n    eval_prog = eval_prog.clone(True)\n\n    eval_reader = create_reader(eval_feed, args_path=FLAGS.dataset_dir)\n    test_data_feed = fluid.DataFeeder(test_feed_vars.values(), place)\n\n    # parse eval fetches\n    extra_keys = []\n    if cfg.metric == 'COCO':\n        extra_keys = ['im_info', 'im_id', 'im_shape']\n    if cfg.metric == 'VOC':\n        extra_keys = ['gt_box', 'gt_label', 'is_difficult']\n    eval_keys, eval_values, eval_cls = parse_fetches(fetches, eval_prog,\n                                                     extra_keys)\n    eval_fetch_list = []\n    for k, v in zip(eval_keys, eval_values):\n        eval_fetch_list.append((k, v))\n\n    exe.run(startup_prog)\n    checkpoint.load_params(exe, train_prog, cfg.pretrain_weights)\n\n    best_box_ap_list = []\n\n    def eval_func(program, scope):\n\n        #place = fluid.CPUPlace()\n        #exe = fluid.Executor(place)\n        results = eval_run(exe, program, eval_reader, eval_keys, eval_values,\n                           eval_cls, test_data_feed)\n\n        resolution = None\n        if 'mask' in results[0]:\n            resolution = model.mask_head.resolution\n        box_ap_stats = eval_results(results, eval_feed, cfg.metric,\n                                    cfg.num_classes, resolution, False,\n                                    FLAGS.output_eval)\n        if len(best_box_ap_list) == 0:\n            best_box_ap_list.append(box_ap_stats[0])\n        elif box_ap_stats[0] > best_box_ap_list[0]:\n            best_box_ap_list[0] = box_ap_stats[0]\n        logger.info(\"Best test box ap: {}\".format(best_box_ap_list[0]))\n        return best_box_ap_list[0]\n\n    test_feed = [('image', test_feed_vars['image'].name),\n                 ('im_size', test_feed_vars['im_size'].name)]\n\n    com = Compressor(\n        place,\n        fluid.global_scope(),\n        train_prog,\n        train_reader=train_reader,\n        train_feed_list=[(key, value.name) for key, value in feed_vars.items()],\n        train_fetch_list=train_fetch_list,\n        eval_program=eval_prog,\n        eval_reader=eval_reader,\n        eval_feed_list=test_feed,\n        eval_func={'map': eval_func},\n        eval_fetch_list=[eval_fetch_list[0]],\n        save_eval_model=True,\n        prune_infer_model=[[\"image\", \"im_size\"], [\"multiclass_nms_0.tmp_0\"]],\n        train_optimizer=None)\n    com.config(FLAGS.slim_file)\n    com.run()\n\n\nif __name__ == '__main__':\n    parser = ArgsParser()\n    parser.add_argument(\n        \"-s\",\n        \"--slim_file\",\n        default=None,\n        type=str,\n        help=\"Config file of PaddleSlim.\")\n    parser.add_argument(\n        \"--output_eval\",\n        default=None,\n        type=str,\n        help=\"Evaluation directory, default is current directory.\")\n    parser.add_argument(\n        \"-d\",\n        \"--dataset_dir\",\n        default=None,\n        type=str,\n        help=\"Dataset path, same as DataFeed.dataset.dataset_dir\")\n    FLAGS = parser.parse_args()\n    main()\n", "path": "slim/prune/compress.py"}], "after_files": [{"content": "# Copyright (c) 2019 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\nimport time\nimport multiprocessing\nimport numpy as np\nimport sys\nsys.path.append(\"../../\")\nfrom paddle.fluid.contrib.slim import Compressor\n\n\ndef set_paddle_flags(**kwargs):\n    for key, value in kwargs.items():\n        if os.environ.get(key, None) is None:\n            os.environ[key] = str(value)\n\n\n# NOTE(paddle-dev): All of these flags should be set before\n# `import paddle`. Otherwise, it would not take any effect.\nset_paddle_flags(\n    FLAGS_eager_delete_tensor_gb=0,  # enable GC to save memory\n)\n\nfrom paddle import fluid\nfrom ppdet.core.workspace import load_config, merge_config, create\nfrom ppdet.data.data_feed import create_reader\nfrom ppdet.utils.eval_utils import parse_fetches, eval_results\nfrom ppdet.utils.cli import ArgsParser\nfrom ppdet.utils.check import check_gpu\nimport ppdet.utils.checkpoint as checkpoint\nfrom ppdet.modeling.model_input import create_feed\n\nimport logging\nFORMAT = '%(asctime)s-%(levelname)s: %(message)s'\nlogging.basicConfig(level=logging.INFO, format=FORMAT)\nlogger = logging.getLogger(__name__)\n\n\ndef eval_run(exe, compile_program, reader, keys, values, cls, test_feed, cfg):\n    \"\"\"\n    Run evaluation program, return program outputs.\n    \"\"\"\n    iter_id = 0\n    results = []\n    if len(cls) != 0:\n        values = []\n        for i in range(len(cls)):\n            _, accum_map = cls[i].get_map_var()\n            cls[i].reset(exe)\n            values.append(accum_map)\n\n    images_num = 0\n    start_time = time.time()\n    has_bbox = 'bbox' in keys\n    for data in reader():\n        data = test_feed.feed(data)\n        feed_data = {'image': data['image'], 'im_size': data['im_size']}\n        outs = exe.run(compile_program,\n                       feed=feed_data,\n                       fetch_list=[values[0]],\n                       return_numpy=False)\n\n        if cfg.metric == 'VOC':\n            outs.append(data['gt_box'])\n            outs.append(data['gt_label'])\n            outs.append(data['is_difficult'])\n        elif cfg.metric == 'COCO':\n            outs.append(data['im_info'])\n            outs.append(data['im_id'])\n            outs.append(data['im_shape'])\n\n        res = {\n            k: (np.array(v), v.recursive_sequence_lengths())\n            for k, v in zip(keys, outs)\n        }\n        results.append(res)\n        if iter_id % 100 == 0:\n            logger.info('Test iter {}'.format(iter_id))\n        iter_id += 1\n        images_num += len(res['bbox'][1][0]) if has_bbox else 1\n    logger.info('Test finish iter {}'.format(iter_id))\n\n    end_time = time.time()\n    fps = images_num / (end_time - start_time)\n    if has_bbox:\n        logger.info('Total number of images: {}, inference time: {} fps.'.\n                    format(images_num, fps))\n    else:\n        logger.info('Total iteration: {}, inference time: {} batch/s.'.format(\n            images_num, fps))\n\n    return results\n\n\ndef main():\n    cfg = load_config(FLAGS.config)\n    if 'architecture' in cfg:\n        main_arch = cfg.architecture\n    else:\n        raise ValueError(\"'architecture' not specified in config file.\")\n\n    merge_config(FLAGS.opt)\n    if 'log_iter' not in cfg:\n        cfg.log_iter = 20\n\n    # check if set use_gpu=True in paddlepaddle cpu version\n    check_gpu(cfg.use_gpu)\n\n    if cfg.use_gpu:\n        devices_num = fluid.core.get_cuda_device_count()\n    else:\n        devices_num = int(\n            os.environ.get('CPU_NUM', multiprocessing.cpu_count()))\n\n    if 'train_feed' not in cfg:\n        train_feed = create(main_arch + 'TrainFeed')\n    else:\n        train_feed = create(cfg.train_feed)\n\n    if 'eval_feed' not in cfg:\n        eval_feed = create(main_arch + 'EvalFeed')\n    else:\n        eval_feed = create(cfg.eval_feed)\n\n    place = fluid.CUDAPlace(0) if cfg.use_gpu else fluid.CPUPlace()\n    exe = fluid.Executor(place)\n\n    lr_builder = create('LearningRate')\n    optim_builder = create('OptimizerBuilder')\n\n    # build program\n    startup_prog = fluid.Program()\n    train_prog = fluid.Program()\n    with fluid.program_guard(train_prog, startup_prog):\n        with fluid.unique_name.guard():\n            model = create(main_arch)\n            _, feed_vars = create_feed(train_feed, True)\n            train_fetches = model.train(feed_vars)\n            loss = train_fetches['loss']\n            lr = lr_builder()\n            optimizer = optim_builder(lr)\n            optimizer.minimize(loss)\n\n    train_reader = create_reader(train_feed, cfg.max_iters, FLAGS.dataset_dir)\n\n    # parse train fetches\n    train_keys, train_values, _ = parse_fetches(train_fetches)\n    train_keys.append(\"lr\")\n    train_values.append(lr.name)\n\n    train_fetch_list = []\n    for k, v in zip(train_keys, train_values):\n        train_fetch_list.append((k, v))\n\n    eval_prog = fluid.Program()\n    with fluid.program_guard(eval_prog, startup_prog):\n        with fluid.unique_name.guard():\n            model = create(main_arch)\n            _, test_feed_vars = create_feed(eval_feed, True)\n            fetches = model.eval(test_feed_vars)\n\n    eval_prog = eval_prog.clone(True)\n\n    eval_reader = create_reader(eval_feed, args_path=FLAGS.dataset_dir)\n    test_data_feed = fluid.DataFeeder(test_feed_vars.values(), place)\n\n    # parse eval fetches\n    extra_keys = []\n    if cfg.metric == 'COCO':\n        extra_keys = ['im_info', 'im_id', 'im_shape']\n    if cfg.metric == 'VOC':\n        extra_keys = ['gt_box', 'gt_label', 'is_difficult']\n    eval_keys, eval_values, eval_cls = parse_fetches(fetches, eval_prog,\n                                                     extra_keys)\n    eval_fetch_list = []\n    for k, v in zip(eval_keys, eval_values):\n        eval_fetch_list.append((k, v))\n\n    exe.run(startup_prog)\n    checkpoint.load_params(exe, train_prog, cfg.pretrain_weights)\n\n    best_box_ap_list = []\n\n    def eval_func(program, scope):\n\n        #place = fluid.CPUPlace()\n        #exe = fluid.Executor(place)\n        results = eval_run(exe, program, eval_reader, eval_keys, eval_values,\n                           eval_cls, test_data_feed, cfg)\n\n        resolution = None\n        if 'mask' in results[0]:\n            resolution = model.mask_head.resolution\n        box_ap_stats = eval_results(results, eval_feed, cfg.metric,\n                                    cfg.num_classes, resolution, False,\n                                    FLAGS.output_eval)\n        if len(best_box_ap_list) == 0:\n            best_box_ap_list.append(box_ap_stats[0])\n        elif box_ap_stats[0] > best_box_ap_list[0]:\n            best_box_ap_list[0] = box_ap_stats[0]\n        logger.info(\"Best test box ap: {}\".format(best_box_ap_list[0]))\n        return best_box_ap_list[0]\n\n    test_feed = [('image', test_feed_vars['image'].name),\n                 ('im_size', test_feed_vars['im_size'].name)]\n\n    com = Compressor(\n        place,\n        fluid.global_scope(),\n        train_prog,\n        train_reader=train_reader,\n        train_feed_list=[(key, value.name) for key, value in feed_vars.items()],\n        train_fetch_list=train_fetch_list,\n        eval_program=eval_prog,\n        eval_reader=eval_reader,\n        eval_feed_list=test_feed,\n        eval_func={'map': eval_func},\n        eval_fetch_list=[eval_fetch_list[0]],\n        save_eval_model=True,\n        prune_infer_model=[[\"image\", \"im_size\"], [\"multiclass_nms_0.tmp_0\"]],\n        train_optimizer=None)\n    com.config(FLAGS.slim_file)\n    com.run()\n\n\nif __name__ == '__main__':\n    parser = ArgsParser()\n    parser.add_argument(\n        \"-s\",\n        \"--slim_file\",\n        default=None,\n        type=str,\n        help=\"Config file of PaddleSlim.\")\n    parser.add_argument(\n        \"--output_eval\",\n        default=None,\n        type=str,\n        help=\"Evaluation directory, default is current directory.\")\n    parser.add_argument(\n        \"-d\",\n        \"--dataset_dir\",\n        default=None,\n        type=str,\n        help=\"Dataset path, same as DataFeed.dataset.dataset_dir\")\n    FLAGS = parser.parse_args()\n    main()\n", "path": "slim/prune/compress.py"}]}