infer.py 3.1 KB
Newer Older
D
dengkaipeng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# 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.

D
dengkaipeng 已提交
15 16 17 18 19 20 21
import os
import time
import numpy as np
import paddle
import paddle.fluid as fluid
import box_utils
import reader
22
from utility import print_arguments, parse_args, check_gpu
D
dengkaipeng 已提交
23
from models.yolov3 import YOLOv3
D
dengkaipeng 已提交
24 25
from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval, Params
T
tink2123 已提交
26
from config import cfg
D
dengkaipeng 已提交
27 28 29 30


def infer():

31 32 33
    # check if set use_gpu=True in paddlepaddle cpu version
    check_gpu(cfg.use_gpu)

D
dengkaipeng 已提交
34 35 36
    if not os.path.exists('output'):
        os.mkdir('output')

D
dengkaipeng 已提交
37
    model = YOLOv3(is_train=False)
D
dengkaipeng 已提交
38 39
    model.build_model()
    outputs = model.get_pred()
D
dengkaipeng 已提交
40
    input_size = cfg.input_size
D
dengkaipeng 已提交
41 42 43
    place = fluid.CUDAPlace(0) if cfg.use_gpu else fluid.CPUPlace()
    exe = fluid.Executor(place)
    # yapf: disable
D
dengkaipeng 已提交
44
    if cfg.weights:
D
dengkaipeng 已提交
45
        def if_exist(var):
D
dengkaipeng 已提交
46 47
            return os.path.exists(os.path.join(cfg.weights, var.name))
        fluid.io.load_vars(exe, cfg.weights, predicate=if_exist)
D
dengkaipeng 已提交
48
    # yapf: enable
49 50 51 52 53 54 55

    # you can save inference model by following code
    # fluid.io.save_inference_model("./output/yolov3", 
    #                               feeded_var_names=['image', 'im_shape'],
    #                               target_vars=outputs,
    #                               executor=exe)

D
dengkaipeng 已提交
56
    feeder = fluid.DataFeeder(place=place, feed_list=model.feeds())
D
dengkaipeng 已提交
57
    fetch_list = [outputs]
D
dengkaipeng 已提交
58 59 60 61 62 63 64 65
    image_names = []
    if cfg.image_name is not None:
        image_names.append(cfg.image_name)
    else:
        for image_name in os.listdir(cfg.image_path):
            if image_name.split('.')[-1] in ['jpg', 'png']:
                image_names.append(image_name)
    for image_name in image_names:
u010070587's avatar
u010070587 已提交
66 67
        infer_reader = reader.infer(input_size,
                                    os.path.join(cfg.image_path, image_name))
D
dengkaipeng 已提交
68 69 70
        label_names, _ = reader.get_label_infos()
        data = next(infer_reader())
        im_shape = data[0][2]
u010070587's avatar
u010070587 已提交
71 72 73
        outputs = exe.run(fetch_list=[v.name for v in fetch_list],
                          feed=feeder.feed(data),
                          return_numpy=False)
D
dengkaipeng 已提交
74 75 76
        bboxes = np.array(outputs[0])
        if bboxes.shape[1] != 6:
            print("No object found in {}".format(image_name))
77
            continue
D
dengkaipeng 已提交
78 79 80
        labels = bboxes[:, 0].astype('int32')
        scores = bboxes[:, 1].astype('float32')
        boxes = bboxes[:, 2:].astype('float32')
D
dengkaipeng 已提交
81 82

        path = os.path.join(cfg.image_path, image_name)
u010070587's avatar
u010070587 已提交
83 84
        box_utils.draw_boxes_on_image(path, boxes, scores, labels, label_names,
                                      cfg.draw_thresh)
D
dengkaipeng 已提交
85 86 87 88 89 90


if __name__ == '__main__':
    args = parse_args()
    print_arguments(args)
    infer()