eval_utils.py 9.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import logging
import numpy as np
21
import os
22
import time
23 24 25

import paddle.fluid as fluid

26
from .voc_eval import bbox_eval as voc_bbox_eval
M
Manuel Garcia 已提交
27
from .post_process import mstest_box_post_process, mstest_mask_post_process
28

29
__all__ = ['parse_fetches', 'eval_run', 'eval_results', 'json_eval_results']
30 31 32 33 34 35 36 37 38 39 40 41 42 43

logger = logging.getLogger(__name__)


def parse_fetches(fetches, prog=None, extra_keys=None):
    """
    Parse fetch variable infos from model fetches,
    values for fetch_list and keys for stat
    """
    keys, values = [], []
    cls = []
    for k, v in fetches.items():
        if hasattr(v, 'name'):
            keys.append(k)
44
            #v.persistable = True
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
            values.append(v.name)
        else:
            cls.append(v)

    if prog is not None and extra_keys is not None:
        for k in extra_keys:
            try:
                v = fluid.framework._get_var(k, prog)
                keys.append(k)
                values.append(v.name)
            except Exception:
                pass

    return keys, values, cls


W
wangguanzhong 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
def length2lod(length_lod):
    offset_lod = [0]
    for i in length_lod:
        offset_lod.append(offset_lod[-1] + i)
    return [offset_lod]


def get_sub_feed(input, place):
    new_dict = {}
    res_feed = {}
    key_name = ['bbox', 'im_info', 'im_id', 'im_shape', 'bbox_flip']
    for k in key_name:
        if k in input.keys():
            new_dict[k] = input[k]
    for k in input.keys():
        if 'image' in k:
            new_dict[k] = input[k]
    for k, v in new_dict.items():
        data_t = fluid.LoDTensor()
        data_t.set(v[0], place)
        if 'bbox' in k:
            lod = length2lod(v[1][0])
            data_t.set_lod(lod)
        res_feed[k] = data_t
    return res_feed


def clean_res(result, keep_name_list):
    clean_result = {}
    for k in result.keys():
        if k in keep_name_list:
            clean_result[k] = result[k]
    result.clear()
    return clean_result


G
Guanghua Yu 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
def get_masks(result):
    import pycocotools.mask as mask_util
    if result is None:
        return {}
    seg_pred = result['segm'][0].astype(np.uint8)
    cate_label = result['cate_label'][0].astype(np.int)
    cate_score = result['cate_score'][0].astype(np.float)
    num_ins = seg_pred.shape[0]
    masks = []
    for idx in range(num_ins - 1):
        cur_mask = seg_pred[idx, ...]
        rle = mask_util.encode(
            np.array(
                cur_mask[:, :, np.newaxis], order='F'))[0]
        rst = (rle, cate_score[idx])
        masks.append([cate_label[idx], rst])
    return masks


W
wangguanzhong 已提交
116 117
def eval_run(exe,
             compile_program,
W
wangguanzhong 已提交
118
             loader,
W
wangguanzhong 已提交
119 120 121 122 123 124
             keys,
             values,
             cls,
             cfg=None,
             sub_prog=None,
             sub_keys=None,
W
wangguanzhong 已提交
125 126
             sub_values=None,
             resolution=None):
127 128 129 130 131 132 133 134 135 136 137 138
    """
    Run evaluation program, return program outputs.
    """
    iter_id = 0
    results = []
    if len(cls) != 0:
        values = []
        for i in range(len(cls)):
            _, accum_map = cls[i].get_map_var()
            cls[i].reset(exe)
            values.append(accum_map)

139 140 141 142
    images_num = 0
    start_time = time.time()
    has_bbox = 'bbox' in keys

143
    try:
W
wangguanzhong 已提交
144
        loader.start()
145 146 147 148 149 150 151 152
        while True:
            outs = exe.run(compile_program,
                           fetch_list=values,
                           return_numpy=False)
            res = {
                k: (np.array(v), v.recursive_sequence_lengths())
                for k, v in zip(keys, outs)
            }
W
wangguanzhong 已提交
153 154 155 156
            multi_scale_test = getattr(cfg, 'MultiScaleTEST', None)
            mask_multi_scale_test = multi_scale_test and 'Mask' in cfg.architecture

            if multi_scale_test:
W
wangguanzhong 已提交
157 158
                post_res = mstest_box_post_process(res, multi_scale_test,
                                                   cfg.num_classes)
W
wangguanzhong 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
                res.update(post_res)
            if mask_multi_scale_test:
                place = fluid.CUDAPlace(0) if cfg.use_gpu else fluid.CPUPlace()
                sub_feed = get_sub_feed(res, place)
                sub_prog_outs = exe.run(sub_prog,
                                        feed=sub_feed,
                                        fetch_list=sub_values,
                                        return_numpy=False)
                sub_prog_res = {
                    k: (np.array(v), v.recursive_sequence_lengths())
                    for k, v in zip(sub_keys, sub_prog_outs)
                }
                post_res = mstest_mask_post_process(sub_prog_res, cfg)
                res.update(post_res)
            if multi_scale_test:
                res = clean_res(
                    res, ['im_info', 'bbox', 'im_id', 'im_shape', 'mask'])
W
wangguanzhong 已提交
176 177 178
            if 'mask' in res:
                from ppdet.utils.post_process import mask_encode
                res['mask'] = mask_encode(res, resolution)
W
wangguanzhong 已提交
179 180 181 182
            post_config = getattr(cfg, 'PostProcess', None)
            if 'Corner' in cfg.architecture and post_config is not None:
                from ppdet.utils.post_process import corner_post_process
                corner_post_process(res, post_config, cfg.num_classes)
W
wangguanzhong 已提交
183 184
            if 'TTFNet' in cfg.architecture:
                res['bbox'][1].append([len(res['bbox'][0])])
G
Guanghua Yu 已提交
185 186
            if 'segm' in res:
                res['segm'] = get_masks(res)
187 188 189 190
            results.append(res)
            if iter_id % 100 == 0:
                logger.info('Test iter {}'.format(iter_id))
            iter_id += 1
G
Guanghua Yu 已提交
191
            if 'bbox' not in res or len(res['bbox'][1]) == 0:
W
wangguanzhong 已提交
192
                has_bbox = False
193
            images_num += len(res['bbox'][1][0]) if has_bbox else 1
194
    except (StopIteration, fluid.core.EOFException):
W
wangguanzhong 已提交
195
        loader.reset()
196 197
    logger.info('Test finish iter {}'.format(iter_id))

198 199 200 201 202 203 204 205 206
    end_time = time.time()
    fps = images_num / (end_time - start_time)
    if has_bbox:
        logger.info('Total number of images: {}, inference time: {} fps.'.
                    format(images_num, fps))
    else:
        logger.info('Total iteration: {}, inference time: {} batch/s.'.format(
            images_num, fps))

207 208 209
    return results


W
wangguanzhong 已提交
210 211
def eval_results(results,
                 metric,
212
                 num_classes,
W
wangguanzhong 已提交
213 214
                 resolution=None,
                 is_bbox_normalized=False,
215
                 output_directory=None,
216
                 map_type='11point',
W
wangguanzhong 已提交
217 218
                 dataset=None,
                 save_only=False):
219
    """Evaluation for evaluation program results"""
220
    box_ap_stats = []
221
    if metric == 'COCO':
G
Guanghua Yu 已提交
222
        from ppdet.utils.coco_eval import proposal_eval, bbox_eval, mask_eval, segm_eval
223 224
        anno_file = dataset.get_anno()
        with_background = dataset.with_background
225 226
        if 'proposal' in results[0]:
            output = 'proposal.json'
227 228
            if output_directory:
                output = os.path.join(output_directory, 'proposal.json')
229 230 231
            proposal_eval(results, anno_file, output)
        if 'bbox' in results[0]:
            output = 'bbox.json'
232 233
            if output_directory:
                output = os.path.join(output_directory, 'bbox.json')
234

235 236 237 238 239
            box_ap_stats = bbox_eval(
                results,
                anno_file,
                output,
                with_background,
W
wangguanzhong 已提交
240 241
                is_bbox_normalized=is_bbox_normalized,
                save_only=save_only)
242

243 244
        if 'mask' in results[0]:
            output = 'mask.json'
245 246
            if output_directory:
                output = os.path.join(output_directory, 'mask.json')
W
wangguanzhong 已提交
247 248
            mask_eval(
                results, anno_file, output, resolution, save_only=save_only)
G
Guanghua Yu 已提交
249 250 251 252 253 254 255 256
        if 'segm' in results[0]:
            output = 'segm.json'
            if output_directory:
                output = os.path.join(output_directory, output)
            mask_ap_stats = segm_eval(
                results, anno_file, output, save_only=save_only)
            if len(box_ap_stats) == 0:
                box_ap_stats = mask_ap_stats
257
    else:
258 259 260
        if 'accum_map' in results[-1]:
            res = np.mean(results[-1]['accum_map'][0])
            logger.info('mAP: {:.2f}'.format(res * 100.))
261
            box_ap_stats.append(res * 100.)
262
        elif 'bbox' in results[0]:
263
            box_ap = voc_bbox_eval(
264 265
                results,
                num_classes,
266 267
                is_bbox_normalized=is_bbox_normalized,
                map_type=map_type)
268 269
            box_ap_stats.append(box_ap)
    return box_ap_stats
270

271

272
def json_eval_results(metric, json_directory=None, dataset=None):
273 274 275 276 277
    """
    cocoapi eval with already exists proposal.json, bbox.json or mask.json
    """
    assert metric == 'COCO'
    from ppdet.utils.coco_eval import cocoapi_eval
278
    anno_file = dataset.get_anno()
279 280
    json_file_list = ['proposal.json', 'bbox.json', 'mask.json']
    if json_directory:
281 282 283
        assert os.path.exists(
            json_directory), "The json directory:{} does not exist".format(
                json_directory)
284 285 286 287 288 289 290 291 292
        for k, v in enumerate(json_file_list):
            json_file_list[k] = os.path.join(str(json_directory), v)

    coco_eval_style = ['proposal', 'bbox', 'segm']
    for i, v_json in enumerate(json_file_list):
        if os.path.exists(v_json):
            cocoapi_eval(v_json, coco_eval_style[i], anno_file=anno_file)
        else:
            logger.info("{} not exists!".format(v_json))