“be92338ea0ca0e8b1d30f7f879b3ef2679dba1d5”上不存在“models/rank/fnn/config.yaml”
eval_utils.py 5.3 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 23 24

import paddle.fluid as fluid

25 26
from ppdet.utils.voc_eval import bbox_eval as voc_bbox_eval

27
__all__ = ['parse_fetches', 'eval_run', 'eval_results', 'json_eval_results']
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 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

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)
            v.persistable = True
            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


def eval_run(exe, compile_program, pyreader, keys, values, cls):
    """
    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)

    try:
        pyreader.start()
        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)
            }
            results.append(res)
            if iter_id % 100 == 0:
                logger.info('Test iter {}'.format(iter_id))
            iter_id += 1
    except (StopIteration, fluid.core.EOFException):
        pyreader.reset()
    logger.info('Test finish iter {}'.format(iter_id))

    return results


W
wangguanzhong 已提交
93 94 95
def eval_results(results,
                 feed,
                 metric,
96
                 num_classes,
W
wangguanzhong 已提交
97 98
                 resolution=None,
                 is_bbox_normalized=False,
99 100
                 output_directory=None,
                 map_type='11point'):
101
    """Evaluation for evaluation program results"""
102
    box_ap_stats = []
103
    if metric == 'COCO':
104
        from ppdet.utils.coco_eval import proposal_eval, bbox_eval, mask_eval
105 106
        anno_file = getattr(feed.dataset, 'annotation', None)
        with_background = getattr(feed, 'with_background', True)
107 108
        if 'proposal' in results[0]:
            output = 'proposal.json'
109 110
            if output_directory:
                output = os.path.join(output_directory, 'proposal.json')
111 112 113
            proposal_eval(results, anno_file, output)
        if 'bbox' in results[0]:
            output = 'bbox.json'
114 115
            if output_directory:
                output = os.path.join(output_directory, 'bbox.json')
116

117
            box_ap_stats = bbox_eval(results, anno_file, output,
118 119 120
                                     with_background,
                                     is_bbox_normalized=is_bbox_normalized)

121 122
        if 'mask' in results[0]:
            output = 'mask.json'
123 124
            if output_directory:
                output = os.path.join(output_directory, 'mask.json')
125 126
            mask_eval(results, anno_file, output, resolution)
    else:
127 128 129
        if 'accum_map' in results[-1]:
            res = np.mean(results[-1]['accum_map'][0])
            logger.info('mAP: {:.2f}'.format(res * 100.))
130
            box_ap_stats.append(res * 100.)
131
        elif 'bbox' in results[0]:
132
            box_ap = voc_bbox_eval(
133 134 135
                results, num_classes, 
                is_bbox_normalized=is_bbox_normalized,
                map_type=map_type)
136 137
            box_ap_stats.append(box_ap)
    return box_ap_stats
138

139

140 141 142 143 144 145 146 147 148
def json_eval_results(feed, metric, json_directory=None):
    """
    cocoapi eval with already exists proposal.json, bbox.json or mask.json
    """
    assert metric == 'COCO'
    from ppdet.utils.coco_eval import cocoapi_eval
    anno_file = getattr(feed.dataset, 'annotation', None)
    json_file_list = ['proposal.json', 'bbox.json', 'mask.json']
    if json_directory:
149 150 151
        assert os.path.exists(
            json_directory), "The json directory:{} does not exist".format(
                json_directory)
152 153 154 155 156 157 158 159 160
        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))