eval.py 3.2 KB
Newer Older
G
Guanghua Yu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# Copyright (c) 2022 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.

import os
import sys
import numpy as np
import argparse
G
Guanghua Yu 已提交
19
from tqdm import tqdm
G
Guanghua Yu 已提交
20
import paddle
21
from paddleslim.common import load_config as load_slim_config
22
from paddleslim.common import load_onnx_model
G
Guanghua Yu 已提交
23 24
from post_process import YOLOv6PostProcess, coco_metric
from dataset import COCOValDataset
G
Guanghua Yu 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48


def argsparser():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        '--config_path',
        type=str,
        default=None,
        help="path of compression strategy config.",
        required=True)
    parser.add_argument(
        '--devices',
        type=str,
        default='gpu',
        help="which device used to compress.")

    return parser


def eval():

    place = paddle.CUDAPlace(0) if FLAGS.devices == 'gpu' else paddle.CPUPlace()
    exe = paddle.static.Executor(place)

49 50
    val_program, feed_target_names, fetch_targets = load_onnx_model(
        global_config["model_dir"])
G
Guanghua Yu 已提交
51

G
Guanghua Yu 已提交
52 53 54 55 56 57 58 59 60 61 62 63
    bboxes_list, bbox_nums_list, image_id_list = [], [], []
    with tqdm(
            total=len(val_loader),
            bar_format='Evaluation stage, Run batch:|{bar}| {n_fmt}/{total_fmt}',
            ncols=80) as t:
        for data in val_loader:
            data_all = {k: np.array(v) for k, v in data.items()}
            outs = exe.run(val_program,
                           feed={feed_target_names[0]: data_all['image']},
                           fetch_list=fetch_targets,
                           return_numpy=False)
            res = {}
G
Guanghua Yu 已提交
64 65 66
            postprocess = YOLOv6PostProcess(
                score_threshold=0.001, nms_threshold=0.65, multi_label=True)
            res = postprocess(np.array(outs[0]), data_all['scale_factor'])
G
Guanghua Yu 已提交
67 68 69 70 71 72
            bboxes_list.append(res['bbox'])
            bbox_nums_list.append(res['bbox_num'])
            image_id_list.append(np.array(data_all['im_id']))
            t.update()

    coco_metric(anno_file, bboxes_list, bbox_nums_list, image_id_list)
G
Guanghua Yu 已提交
73 74 75 76 77 78 79 80


def main():
    global global_config
    all_config = load_slim_config(FLAGS.config_path)
    global_config = all_config["Global"]

    global val_loader
G
Guanghua Yu 已提交
81 82 83 84 85 86 87
    dataset = COCOValDataset(
        dataset_dir=global_config['dataset_dir'],
        image_dir=global_config['val_image_dir'],
        anno_path=global_config['val_anno_path'])
    global anno_file
    anno_file = dataset.ann_file
    val_loader = paddle.io.DataLoader(dataset, batch_size=1)
G
Guanghua Yu 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100

    eval()


if __name__ == '__main__':
    paddle.enable_static()
    parser = argsparser()
    FLAGS = parser.parse_args()

    assert FLAGS.devices in ['cpu', 'gpu', 'xpu', 'npu']
    paddle.set_device(FLAGS.devices)

    main()