예제 #1
0
def infer():

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

    if not os.path.exists('output'):
        os.mkdir('output')

    model = YOLOv3(is_train=False)
    model.build_model()
    outputs = model.get_pred()
    input_size = cfg.input_size
    place = fluid.CUDAPlace(0) if cfg.use_gpu else fluid.CPUPlace()
    exe = fluid.Executor(place)
    # yapf: disable
    if cfg.weights:
        def if_exist(var):
            return os.path.exists(os.path.join(cfg.weights, var.name))
        fluid.io.load_vars(exe, cfg.weights, predicate=if_exist)
    # yapf: enable

    # 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)

    feeder = fluid.DataFeeder(place=place, feed_list=model.feeds())
    fetch_list = [outputs]
    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:
        infer_reader = reader.infer(input_size,
                                    os.path.join(cfg.image_path, image_name))
        label_names, _ = reader.get_label_infos()
        data = next(infer_reader())
        im_shape = data[0][2]
        outputs = exe.run(fetch_list=[v.name for v in fetch_list],
                          feed=feeder.feed(data),
                          return_numpy=False,
                          use_program_cache=True)
        bboxes = np.array(outputs[0])
        if bboxes.shape[1] != 6:
            print("No object found in {}".format(image_name))
            continue
        labels = bboxes[:, 0].astype('int32')
        scores = bboxes[:, 1].astype('float32')
        boxes = bboxes[:, 2:].astype('float32')

        path = os.path.join(cfg.image_path, image_name)
        box_utils.draw_boxes_on_image(path, boxes, scores, labels, label_names,
                                      cfg.draw_thresh)
예제 #2
0
파일: infer.py 프로젝트: zhong110020/models
def infer():

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

    if not os.path.exists('output'):
        os.mkdir('output')
    place = fluid.CUDAPlace(0) if cfg.use_gpu else fluid.CPUPlace()
    with fluid.dygraph.guard(place):

        model = YOLOv3(3, is_train=False)
        input_size = cfg.input_size
        # yapf: disable
        if cfg.weights:
            restore, _ = fluid.load_dygraph(cfg.weights)
            model.set_dict(restore)
        # yapf: enable

        # 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)

        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:
            infer_reader = reader.infer(
                input_size, os.path.join(cfg.image_path, image_name))
            label_names, _ = reader.get_label_infos()
            data = next(infer_reader())

            img_data = np.array([x[0] for x in data]).astype('float32')
            img = to_variable(img_data)

            im_shape_data = np.array([x[2] for x in data]).astype('int32')
            im_shape = to_variable(im_shape_data)

            outputs = model(img, None, None, None, None, im_shape)

            bboxes = outputs.numpy()
            if bboxes.shape[1] != 6:
                print("No object found in {}".format(image_name))
                continue
            labels = bboxes[:, 0].astype('int32')
            scores = bboxes[:, 1].astype('float32')
            boxes = bboxes[:, 2:].astype('float32')

            path = os.path.join(cfg.image_path, image_name)
            box_utils.draw_boxes_on_image(path, boxes, scores, labels,
                                          label_names, cfg.draw_thresh)
def infer():

    if not os.path.exists('output'):
        os.mkdir('output')

    model = YOLOv3(is_train=False)
    model.build_model()
    outputs = model.get_pred()
    input_size = cfg.input_size
    #place = fluid.CUDAPlace(0) if cfg.use_gpu else fluid.CPUPlace()
    place = fluid.CPUPlace()
    exe = fluid.Executor(place)
    # yapf: disable
    if cfg.weights:
        print('cfg.weights = ',cfg.weights)
        def if_exist(var):
            return os.path.exists(os.path.join(cfg.weights, var.name))
        fluid.io.load_vars(exe, cfg.weights, predicate=if_exist)
    # yapf: enable
    feeder = fluid.DataFeeder(place=place, feed_list=model.feeds())
    fetch_list = [outputs]
    image_names = []
    if cfg.image_name is not None:
        image_names.append(cfg.image_name)
        print('image_names = ', image_names)
    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:
        infer_reader = reader.infer(input_size,
                                    os.path.join(cfg.image_path, image_name))
        label_names, _ = reader.get_label_infos()
        data = next(infer_reader())
        im_shape = data[0][2]
        outputs = exe.run(fetch_list=[v.name for v in fetch_list],
                          feed=feeder.feed(data),
                          return_numpy=False)
        bboxes = np.array(outputs[0])
        if bboxes.shape[1] != 6:
            print("No object found in {}".format(image_name))
            continue
        labels = bboxes[:, 0].astype('int32')
        scores = bboxes[:, 1].astype('float32')
        boxes = bboxes[:, 2:].astype('float32')
        print(labels)
        print(scores)
        print(len(boxes))
        path = os.path.join(cfg.image_path, image_name)
        box_utils.draw_boxes_on_image(path, boxes, scores, labels, label_names,
                                      cfg.draw_thresh)
예제 #4
0
파일: eval.py 프로젝트: zhong110020/models
def eval():
    # check if set use_gpu=True in paddlepaddle cpu version
    check_gpu(cfg.use_gpu)

    if '2014' in cfg.dataset:
        test_list = 'annotations/instances_val2014.json'
    elif '2017' in cfg.dataset:
        test_list = 'annotations/instances_val2017.json'

    if cfg.debug:
        if not os.path.exists('output'):
            os.mkdir('output')

    place = fluid.CUDAPlace(0) if cfg.use_gpu else fluid.CPUPlace()
    with fluid.dygraph.guard(place):
        model = YOLOv3(3,is_train=False)
        # yapf: disable
        if cfg.weights:
            restore, _ = fluid.load_dygraph(cfg.weights)
            model.set_dict(restore)
            model.eval()

        input_size = cfg.input_size
        # batch_size for test must be 1
        test_reader = reader.test(input_size, 1)
        label_names, label_ids = reader.get_label_infos()
        if cfg.debug:
            print("Load in labels {} with ids {}".format(label_names, label_ids))

        def get_pred_result(boxes, scores, labels, im_id):
            result = []
            for box, score, label in zip(boxes, scores, labels):
                x1, y1, x2, y2 = box
                w = x2 - x1 + 1
                h = y2 - y1 + 1
                bbox = [x1, y1, w, h]

                res = {
                    'image_id': int(im_id),
                    'category_id': label_ids[int(label)],
                    'bbox': list(map(float, bbox)),
                    'score': float(score)
                }
                result.append(res)
            return result

        dts_res = []
        total_time = 0
        for iter_id, data in enumerate(test_reader()):
            start_time = time.time()

            img_data = np.array([x[0] for x in data]).astype('float32')
            img = to_variable(img_data)

            im_id_data = np.array([x[1] for x in data]).astype('int32')
            im_id = to_variable(im_id_data)

            im_shape_data = np.array([x[2] for x in data]).astype('int32')
            im_shape = to_variable(im_shape_data)

            batch_outputs = model(img, None, None, None, im_id, im_shape)
            nmsed_boxes = batch_outputs.numpy()
            if nmsed_boxes.shape[1] != 6:
                continue

            im_id = data[0][1]
            nmsed_box=nmsed_boxes
            labels = nmsed_box[:, 0]
            scores = nmsed_box[:, 1]
            boxes = nmsed_box[:, 2:6]
            dts_res += get_pred_result(boxes, scores, labels, im_id)

            end_time = time.time()
            print("batch id: {}, time: {}".format(iter_id, end_time - start_time))
            total_time += end_time - start_time

        with open("yolov3_result.json", 'w') as outfile:
            json.dump(dts_res, outfile)
        print("start evaluate detection result with coco api")
        coco = COCO(os.path.join(cfg.data_dir, test_list))
        cocoDt = coco.loadRes("yolov3_result.json")
        cocoEval = COCOeval(coco, cocoDt, 'bbox')
        cocoEval.evaluate()
        cocoEval.accumulate()
        cocoEval.summarize()
    print("evaluate done.")

    print("Time per batch: {}".format(total_time / iter_id))
예제 #5
0
def infer():

    if not os.path.exists('output_frame'):
        os.mkdir('output_frame')

    model = YOLOv3(is_train=False)
    model.build_model()
    outputs = model.get_pred()
    input_size = cfg.input_size
    place = fluid.CUDAPlace(0) if cfg.use_gpu else fluid.CPUPlace()
    exe = fluid.Executor(place)
    # yapf: disable
    if cfg.weights:
        def if_exist(var):
            return os.path.exists(os.path.join(cfg.weights, var.name))
        fluid.io.load_vars(exe, cfg.weights, predicate=if_exist)
    # yapf: enable

    # 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)

    feeder = fluid.DataFeeder(place=place, feed_list=model.feeds())
    fetch_list = [outputs]

    video_path = ""  #source
    video_dir = ""  #output path

    video_writer = FFmpegWriter(video_dir)
    cap = cv2.VideoCapture(video_path)
    i = 0
    while cap.isOpened():
        if i % 100 == 0:
            print('{}%         '.format(i / 1020 * 100) + '%d' % i)
        label_names, _ = reader.get_label_infos()
        ok, image = cap.read()
        if ok:
            infer_reader = reader.infer_m(input_size, image)
            data = next(infer_reader())
            im_shape = data[0][2]
            outputs = exe.run(fetch_list=[v.name for v in fetch_list],
                              feed=feeder.feed(data),
                              return_numpy=False)
            bboxes = np.array(outputs[0])
            if bboxes.shape[1] != 6:
                continue
            labels = bboxes[:, 0].astype('int32')
            scores = bboxes[:, 1].astype('float32')
            boxes = bboxes[:, 2:].astype('float32')

            frame = box_utils.save_as_video(image, boxes, scores, labels,
                                            label_names, cfg.draw_thresh)
            frame = frame[:, :, ::-1]
            frame = cv2.resize(frame, (492, 369))

            video_writer.writeFrame(frame)
        else:
            print("over")
            break
        i += 1
    video_writer.close()
def eval():
    if '2014' in cfg.dataset:
        test_list = 'annotations/instances_val2014.json'
    elif '2017' in cfg.dataset:
        test_list = 'annotations/instances_val2017.json'

    if cfg.debug:
        if not os.path.exists('output'):
            os.mkdir('output')

    model = YOLOv3(is_train=False)
    model.build_model()
    outputs = model.get_pred()
    place = fluid.CUDAPlace(0) if cfg.use_gpu else fluid.CPUPlace()
    exe = fluid.Executor(place)
    # yapf: disable
    if cfg.weights:
        def if_exist(var):
            return os.path.exists(os.path.join(cfg.weights, var.name))
        fluid.io.load_vars(exe, cfg.weights, predicate=if_exist)
    # yapf: enable

    # 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)

    input_size = cfg.input_size
    test_reader = reader.test(input_size, 1)
    label_names, label_ids = reader.get_label_infos()
    if cfg.debug:
        print("Load in labels {} with ids {}".format(label_names, label_ids))
    feeder = fluid.DataFeeder(place=place, feed_list=model.feeds())

    def get_pred_result(boxes, scores, labels, im_id):
        result = []
        for box, score, label in zip(boxes, scores, labels):
            x1, y1, x2, y2 = box
            w = x2 - x1 + 1
            h = y2 - y1 + 1
            bbox = [x1, y1, w, h]

            res = {
                'image_id': im_id,
                'category_id': label_ids[int(label)],
                'bbox': list(map(float, bbox)),
                'score': float(score)
            }
            result.append(res)
        return result

    dts_res = []
    fetch_list = [outputs]
    total_time = 0
    for batch_id, batch_data in enumerate(test_reader()):
        start_time = time.time()
        batch_outputs = exe.run(fetch_list=[v.name for v in fetch_list],
                                feed=feeder.feed(batch_data),
                                return_numpy=False,
                                use_program_cache=True)
        lod = batch_outputs[0].lod()[0]
        nmsed_boxes = np.array(batch_outputs[0])
        if nmsed_boxes.shape[1] != 6:
            continue
        for i in range(len(lod) - 1):
            im_id = batch_data[i][1]
            start = lod[i]
            end = lod[i + 1]
            if start == end:
                continue
            nmsed_box = nmsed_boxes[start:end, :]
            labels = nmsed_box[:, 0]
            scores = nmsed_box[:, 1]
            boxes = nmsed_box[:, 2:6]
            dts_res += get_pred_result(boxes, scores, labels, im_id)

        end_time = time.time()
        print("batch id: {}, time: {}".format(batch_id, end_time - start_time))
        total_time += end_time - start_time

    with open("yolov3_result.json", 'w') as outfile:
        json.dump(dts_res, outfile)
    print("start evaluate detection result with coco api")
    coco = COCO(os.path.join(cfg.data_dir, test_list))
    cocoDt = coco.loadRes("yolov3_result.json")
    cocoEval = COCOeval(coco, cocoDt, 'bbox')
    cocoEval.evaluate()
    cocoEval.accumulate()
    cocoEval.summarize()
    print("evaluate done.")

    print("Time per batch: {}".format(total_time / batch_id))