예제 #1
0
def infer():
    place = fluid.CUDAPlace(0) if cfg.use_gpu else fluid.CPUPlace()
    exe = fluid.Executor(place)
    class_nums = cfg.class_num
    model = model_builder.RRPN(add_conv_body_func=resnet.ResNet(),
                               add_roi_box_head_func=resnet.ResNetC5(),
                               use_pyreader=False,
                               mode='infer')
    startup_prog = fluid.Program()
    infer_prog = fluid.Program()
    with fluid.program_guard(infer_prog, startup_prog):
        with fluid.unique_name.guard():
            model.build_model()
            pred_boxes = model.eval_bbox_out()
    infer_prog = infer_prog.clone(True)
    exe.run(startup_prog)
    fluid.load(infer_prog, cfg.pretrained_model, exe)
    infer_reader = reader.infer(cfg.image_path)
    data_loader = model.data_loader
    data_loader.set_sample_list_generator(infer_reader, places=place)
    fetch_list = [pred_boxes]
    imgs = os.listdir(cfg.image_path)
    imgs.sort()

    for i, data in enumerate(data_loader()):
        result = exe.run(infer_prog,
                         fetch_list=[v.name for v in fetch_list],
                         feed=data,
                         return_numpy=False)
        nmsed_out = result[0]
        im_info = np.array(data[0]['im_info'])[0]
        im_scale = im_info[2]
        outs = np.array(nmsed_out)
        draw_bounding_box_on_image(cfg.image_path, imgs[i], outs, im_scale,
                                   cfg.draw_threshold)
예제 #2
0
def infer():

    data_path = DatasetPath('val')
    test_list = data_path.get_file_list()

    cocoGt = COCO(test_list)
    num_id_to_cat_id_map = {i + 1: v for i, v in enumerate(cocoGt.getCatIds())}
    category_ids = cocoGt.getCatIds()
    label_list = {
        item['id']: item['name']
        for item in cocoGt.loadCats(category_ids)
    }
    label_list[0] = ['background']
    image_shape = [3, cfg.TEST.max_size, cfg.TEST.max_size]
    class_nums = cfg.class_num

    model = model_builder.RCNN(
        add_conv_body_func=resnet.add_ResNet50_conv4_body,
        add_roi_box_head_func=resnet.add_ResNet_roi_conv5_head,
        use_pyreader=False,
        is_train=False)
    model.build_model(image_shape)
    pred_boxes = model.eval_bbox_out()
    if cfg.MASK_ON:
        masks = model.eval_mask_out()
    place = fluid.CUDAPlace(0) if cfg.use_gpu else fluid.CPUPlace()
    exe = fluid.Executor(place)
    # yapf: disable
    if cfg.pretrained_model:
        def if_exist(var):
            return os.path.exists(os.path.join(cfg.pretrained_model, var.name))
        fluid.io.load_vars(exe, cfg.pretrained_model, predicate=if_exist)
    # yapf: enable
    infer_reader = reader.infer()
    feeder = fluid.DataFeeder(place=place, feed_list=model.feeds())

    dts_res = []
    segms_res = []
    if cfg.MASK_ON:
        fetch_list = [pred_boxes, masks]
    else:
        fetch_list = [pred_boxes]
    data = next(infer_reader())
    im_info = [data[0][1]]
    result = exe.run(fetch_list=[v.name for v in fetch_list],
                     feed=feeder.feed(data),
                     return_numpy=False)
    pred_boxes_v = result[0]
    if cfg.MASK_ON:
        masks_v = result[1]
    new_lod = pred_boxes_v.lod()
    nmsed_out = pred_boxes_v
    path = os.path.join(cfg.image_path, cfg.image_name)
    image = None
    if cfg.MASK_ON:
        segms_out = segm_results(nmsed_out, masks_v, im_info)
        image = draw_mask_on_image(path, segms_out, cfg.draw_threshold)

    draw_bounding_box_on_image(path, nmsed_out, cfg.draw_threshold, label_list,
                               num_id_to_cat_id_map, image)
예제 #3
0
def infer(args):
    # parameters from arguments
    seg_num = args.seg_num
    class_dim = args.class_dim
    num_layers = args.num_layers
    test_model = args.test_model

    if test_model == None:
        print('Please specify the test model ...')
        return

    image_shape = [int(m) for m in args.image_shape.split(",")]
    image_shape = [seg_num] + image_shape

    # model definition
    model = TSN_ResNet(layers=num_layers, seg_num=seg_num)
    image = fluid.layers.data(name='image', shape=image_shape, dtype='float32')

    out = model.net(input=image, class_dim=class_dim)

    # for test
    inference_program = fluid.default_main_program().clone(for_test=True)

    if args.with_mem_opt:
        fluid.memory_optimize(fluid.default_main_program())

    place = fluid.CUDAPlace(0)
    exe = fluid.Executor(place)
    exe.run(fluid.default_startup_program())

    def is_parameter(var):
        if isinstance(var, Parameter):
            return isinstance(var, Parameter)

    if test_model is not None:
        vars = filter(is_parameter, inference_program.list_vars())
        fluid.io.load_vars(exe, test_model, vars=vars)

    # reader
    test_reader = paddle.batch(reader.infer(seg_num), batch_size=1)
    feeder = fluid.DataFeeder(place=place, feed_list=[image])

    fetch_list = [out.name]

    # test
    TOPK = 1
    for batch_id, data in enumerate(test_reader()):
        data, vid = data[0]
        data = [[data]]
        result = exe.run(inference_program,
                         fetch_list=fetch_list,
                         feed=feeder.feed(data))
        result = result[0][0]
        pred_label = np.argsort(result)[::-1][:TOPK]
        print("Test sample: {0}, score: {1}, class {2}".format(
            vid, result[pred_label], pred_label))
        sys.stdout.flush()
예제 #4
0
파일: infer.py 프로젝트: ZC119/count
def infer(args, data_args, image_path, model_dir):
    image_shape = [3, data_args.resize_h, data_args.resize_w]

    num_classes = 2
    label_list = data_args.label_list

    image = fluid.layers.data(name='image', shape=image_shape, dtype='float32')
    locs, confs, box, box_var = mobile_net(num_classes, image, image_shape)
    nmsed_out = fluid.layers.detection_output(locs,
                                              confs,
                                              box,
                                              box_var,
                                              nms_threshold=args.nms_threshold)

    place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace()
    exe = fluid.Executor(place)
    # yapf: disable
    if model_dir:
        def if_exist(var):
            return os.path.exists(os.path.join(model_dir, var.name))
        fluid.io.load_vars(exe, model_dir, predicate=if_exist)
    # yapf: enable
    test_images = os.listdir(image_path)

    test_images.remove('label_list')

    test_images = sorted(test_images, key=lambda x: int(x[:-4]))

    for test_image in test_images:
        test_path = os.path.join(image_path, test_image)

        infer_reader = reader.infer(data_args, test_path)
        feeder = fluid.DataFeeder(place=place, feed_list=[image])

        data = infer_reader()

        # switch network to test mode (i.e. batch norm test mode)
        test_program = fluid.default_main_program().clone(for_test=True)
        nmsed_out_v, = exe.run(test_program,
                               feed=feeder.feed([[data]]),
                               fetch_list=[nmsed_out],
                               return_numpy=False)
        nmsed_out_v = np.array(nmsed_out_v)

        count = 0
        for dt in nmsed_out_v:
            category_id, score, xmin, ymin, xmax, ymax = dt.tolist()
            if score < args.confs_threshold:
                continue
            count += 1

        #draw_bounding_box_on_image(test_path, nmsed_out_v, args.confs_threshold,
        #                       label_list)

        with open('result.csv', 'a') as f:
            test_path = test_path.split('/')[-1]
            f.write(test_path[:-4] + ',' + str(count) + '\n')
예제 #5
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)
예제 #6
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)
예제 #8
0
파일: infer.py 프로젝트: walloollaw/models
def infer(args, data_args, image_path, model_dir):
    image_shape = [3, data_args.resize_h, data_args.resize_w]
    if 'coco' in data_args.dataset:
        num_classes = 91
        # cocoapi
        from pycocotools.coco import COCO
        from pycocotools.cocoeval import COCOeval
        label_fpath = os.path.join(data_dir, label_file)
        coco = COCO(label_fpath)
        category_ids = coco.getCatIds()
        label_list = {
            item['id']: item['name']
            for item in coco.loadCats(category_ids)
        }
        label_list[0] = ['background']
    elif 'pascalvoc' in data_args.dataset:
        num_classes = 21
        label_list = data_args.label_list

    image = fluid.layers.data(name='image', shape=image_shape, dtype='float32')
    locs, confs, box, box_var = build_mobilenet_ssd(image, num_classes,
                                                    image_shape)
    nmsed_out = fluid.layers.detection_output(locs,
                                              confs,
                                              box,
                                              box_var,
                                              nms_threshold=args.nms_threshold)

    place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace()
    exe = fluid.Executor(place)
    # yapf: disable
    if model_dir:
        def if_exist(var):
            return os.path.exists(os.path.join(model_dir, var.name))
        fluid.io.load_vars(exe, model_dir, predicate=if_exist)
    # yapf: enable
    infer_reader = reader.infer(data_args, image_path)
    feeder = fluid.DataFeeder(place=place, feed_list=[image])

    data = infer_reader()

    # switch network to test mode (i.e. batch norm test mode)
    test_program = fluid.default_main_program().clone(for_test=True)
    nmsed_out_v, = exe.run(test_program,
                           feed=feeder.feed([[data]]),
                           fetch_list=[nmsed_out],
                           return_numpy=False)
    nmsed_out_v = np.array(nmsed_out_v)
    draw_bounding_box_on_image(image_path, nmsed_out_v, args.confs_threshold,
                               label_list)
예제 #9
0
파일: infer.py 프로젝트: walloollaw/models
def infer(args):
    # parameters from arguments
    model_name = args.model
    pretrained_model = args.pretrained_model
    with_memory_optimization = args.with_mem_opt
    image_shape = [int(m) for m in args.image_shape.split(",")]

    assert model_name in model_list, "{} is not in lists: {}".format(
        args.model, model_list)

    image = fluid.layers.data(name='image', shape=image_shape, dtype='float32')

    # model definition
    model = models.__dict__[model_name]()
    out = model.net(input=image, embedding_size=args.embedding_size)

    test_program = fluid.default_main_program().clone(for_test=True)

    if with_memory_optimization:
        fluid.memory_optimize(fluid.default_main_program())

    place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace()
    exe = fluid.Executor(place)
    exe.run(fluid.default_startup_program())

    if pretrained_model:

        def if_exist(var):
            return os.path.exists(os.path.join(pretrained_model, var.name))

        fluid.io.load_vars(exe, pretrained_model, predicate=if_exist)

    infer_reader = paddle.batch(reader.infer(args),
                                batch_size=args.batch_size,
                                drop_last=False)
    feeder = fluid.DataFeeder(place=place, feed_list=[image])

    fetch_list = [out.name]

    for batch_id, data in enumerate(infer_reader()):
        result = exe.run(test_program,
                         fetch_list=fetch_list,
                         feed=feeder.feed(data))
        result = result[0][0].reshape(-1)
        print("Test-{0}-feature: {1}".format(batch_id, result[:5]))
        sys.stdout.flush()
예제 #10
0
    def work(img):
        #nonlocal reader
        nonlocal feeder
        nonlocal test_program
        nonlocal data_args
        nonlocal nmsed_out
        nonlocal exe
        nonlocal args
        nonlocal label_list

        data=reader.infer(data_args,img)()
        nmsed_out_v, = exe.run(test_program,
                               feed=feeder.feed([[data]]),
                               fetch_list=[nmsed_out],
                               return_numpy=False)
        nmsed_out_v = np.array(nmsed_out_v)
       # draw_bounding_box_on_image(img, nmsed_out_v, args.confs_threshold, label_list)
        return getList(img, nmsed_out_v,args.confs_threshold, label_list)
예제 #11
0
def infer(eval_file_list, save_path, data_args, batch_size, model_path,
          threshold):
    detect_out = vgg_ssd_net.net_conf(mode="infer")

    assert os.path.isfile(model_path), "Invalid model."
    parameters = paddle.parameters.Parameters.from_tar(gzip.open(model_path))

    inferer = paddle.inference.Inference(
        output_layer=detect_out, parameters=parameters)

    data_reader = reader.infer(data_args, eval_file_list)
    all_fname_list = [
        line.strip().split()[0] for line in open(eval_file_list).readlines()
    ]

    test_data = []
    fname_list = []
    img_w = []
    img_h = []
    idx = 0
    with open(save_path, "w") as fout:
        for idx, img in enumerate(data_reader()):
            if idx > 50: break
            test_data.append([img])
            fname_list.append(all_fname_list[idx])
            w, h = Image.open(
                os.path.join(data_args.data_dir, "resized_images",
                             fname_list[-1])).size
            img_w.append(w)
            img_h.append(h)
            if len(test_data) == batch_size:
                ret_res = _infer(inferer, test_data, threshold)
                if ret_res is None: continue
                save_batch_res(ret_res, img_w, img_h, fname_list, fout)
                test_data = []
                fname_list = []
                img_w = []
                img_h = []

        if len(test_data) > 0:
            ret_res = _infer(inferer, test_data, threshold)
            save_batch_res(ret_res, img_w, img_h, fname_list, fout)
예제 #12
0
def infer(args, data_args):
    model_dir = args.init_model
    image_path = args.image_path
    confs_threshold = args.confs_threshold

    place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace()
    exe = fluid.Executor(place)
    [inference_program, feed , fetch] = fluid.io.load_inference_model(
        dirname=model_dir,
        executor=exe,
        model_filename='__model__')

    #print(np.array(fluid.global_scope().find_var('conv2d_20.w_0').get_tensor()))
    #print(np.max(np.array(fluid.global_scope().find_var('conv2d_20.w_0').get_tensor())))
    infer_reader = reader.infer(data_args, image_path)
    data = infer_reader()
    data = data.reshape((1,) + data.shape)
    outs = exe.run(inference_program,
                   feed={feed[0]: data},
                   fetch_list=fetch,
                   return_numpy=False)
    out = np.array(outs[0])
    draw_bounding_box_on_image(image_path, out, confs_threshold,
                               data_args.label_list)
예제 #13
0
def infer():

    try:
        from pycocotools.coco import COCO
        from pycocotools.cocoeval import COCOeval, Params

        data_path = DatasetPath('val')
        test_list = data_path.get_file_list()
        coco_api = COCO(test_list)
        cid = coco_api.getCatIds()
        cat_id_to_num_id_map = {
            v: i + 1
            for i, v in enumerate(coco_api.getCatIds())
        }
        category_ids = coco_api.getCatIds()
        labels_map = {
            cat_id_to_num_id_map[item['id']]: item['name']
            for item in coco_api.loadCats(category_ids)
        }
        labels_map[0] = 'background'
    except:
        print("The COCO dataset or COCO API is not exist, use the default "
              "mapping of class index and real category name on COCO17.")
        assert cfg.dataset == 'coco2017'
        labels_map = coco17_labels()

    image_shape = [3, cfg.TEST.max_size, cfg.TEST.max_size]
    class_nums = cfg.class_num

    model = model_builder.RCNN(
        add_conv_body_func=resnet.add_ResNet50_conv4_body,
        add_roi_box_head_func=resnet.add_ResNet_roi_conv5_head,
        use_pyreader=False,
        mode='infer')
    model.build_model(image_shape)
    pred_boxes = model.eval_bbox_out()
    if cfg.MASK_ON:
        masks = model.eval_mask_out()
    place = fluid.CUDAPlace(0) if cfg.use_gpu else fluid.CPUPlace()
    exe = fluid.Executor(place)
    # yapf: disable
    if not os.path.exists(cfg.pretrained_model):
        raise ValueError("Model path [%s] does not exist." % (cfg.pretrained_model))

    def if_exist(var):
        return os.path.exists(os.path.join(cfg.pretrained_model, var.name))
    fluid.io.load_vars(exe, cfg.pretrained_model, predicate=if_exist)
    # yapf: enable
    infer_reader = reader.infer(cfg.image_path)
    feeder = fluid.DataFeeder(place=place, feed_list=model.feeds())

    dts_res = []
    segms_res = []
    if cfg.MASK_ON:
        fetch_list = [pred_boxes, masks]
    else:
        fetch_list = [pred_boxes]
    data = next(infer_reader())
    im_info = [data[0][1]]
    result = exe.run(fetch_list=[v.name for v in fetch_list],
                     feed=feeder.feed(data),
                     return_numpy=False)
    pred_boxes_v = result[0]
    if cfg.MASK_ON:
        masks_v = result[1]
    new_lod = pred_boxes_v.lod()
    nmsed_out = pred_boxes_v
    image = None
    if cfg.MASK_ON:
        segms_out = segm_results(nmsed_out, masks_v, im_info)
        image = draw_mask_on_image(cfg.image_path, segms_out,
                                   cfg.draw_threshold)

    draw_bounding_box_on_image(cfg.image_path, nmsed_out, cfg.draw_threshold,
                               labels_map, image)