Ejemplo n.º 1
0
def build_program(main_prog, startup_prog, args, data_args):
    image_shape = [3, data_args.resize_h, data_args.resize_w]
    if 'coco' in data_args.dataset:
        num_classes = 91
    elif 'pascalvoc' in data_args.dataset:
        num_classes = 21

    with fluid.program_guard(main_prog, startup_prog):
        py_reader = fluid.layers.py_reader(
            capacity=64,
            shapes=[[-1] + image_shape, [-1, 4], [-1, 1], [-1, 1]],
            lod_levels=[0, 1, 1, 1],
            dtypes=["float32", "float32", "int32", "int32"],
            use_double_buffer=True)
        with fluid.unique_name.guard():
            image, gt_box, gt_label, difficult = fluid.layers.read_file(
                py_reader)
            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)
            with fluid.program_guard(main_prog):
                map = fluid.metrics.DetectionMAP(nmsed_out,
                                                 gt_label,
                                                 gt_box,
                                                 difficult,
                                                 num_classes,
                                                 overlap_threshold=0.5,
                                                 evaluate_difficult=False,
                                                 ap_version=args.ap_version)
    return py_reader, map
Ejemplo n.º 2
0
def build_program(main_prog, startup_prog, train_params, is_train):
    image_shape = train_params['image_shape']
    class_num = train_params['class_num']
    ap_version = train_params['ap_version']
    outs = []
    with fluid.program_guard(main_prog, startup_prog):
        py_reader = fluid.layers.py_reader(
            capacity=64,
            shapes=[[-1] + image_shape, [-1, 4], [-1, 1], [-1, 1]],
            lod_levels=[0, 1, 1, 1],
            dtypes=["float32", "float32", "int32", "int32"],
            use_double_buffer=True)
        with fluid.unique_name.guard():
            image, gt_box, gt_label, difficult = fluid.layers.read_file(
                py_reader)
            locs, confs, box, box_var = build_mobilenet_ssd(
                image, class_num, image_shape)
            if is_train:
                with fluid.unique_name.guard("train"):
                    loss = fluid.layers.ssd_loss(locs, confs, gt_box, gt_label,
                                                 box, box_var)
                    loss = fluid.layers.reduce_sum(loss)
                    optimizer = optimizer_setting(train_params)
                    optimizer.minimize(loss)
                outs = [py_reader, loss]
            else:
                with fluid.unique_name.guard("inference"):
                    nmsed_out = fluid.layers.detection_output(
                        locs, confs, box, box_var, nms_threshold=0.45)

                    gt_label = fluid.layers.cast(x=gt_label,
                                                 dtype=gt_box.dtype)
                    if difficult:
                        difficult = fluid.layers.cast(x=difficult,
                                                      dtype=gt_box.dtype)
                        gt_label = fluid.layers.reshape(gt_label, [-1, 1])
                        difficult = fluid.layers.reshape(difficult, [-1, 1])
                        label = fluid.layers.concat(
                            [gt_label, difficult, gt_box], axis=1)
                    else:
                        label = fluid.layers.concat([gt_label, gt_box], axis=1)

                    map_var = fluid.layers.detection.detection_map(
                        nmsed_out,
                        label,
                        class_num,
                        background_label=0,
                        overlap_threshold=0.5,
                        evaluate_difficult=False,
                        ap_version=ap_version)

                # nmsed_out and image is used to save mode for inference
                outs = [py_reader, map_var, nmsed_out, image]
    return outs
Ejemplo n.º 3
0
def infer(args, data_args, image_path, model_dir):
    image_shape = [3, data_args.resize_h, data_args.resize_w]
    num_classes = 7
    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)

    #print('################')
    def work(image_path):
        #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, image_path)()
        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)
        return getList(image_path, nmsed_out_v, args.confs_threshold,
                       label_list)

    return work
Ejemplo n.º 4
0
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)
Ejemplo n.º 5
0
def eval(args, data_args, test_list, batch_size, model_dir=None):
    image_shape = [3, data_args.resize_h, data_args.resize_w]
    num_classes = 91

    image = fluid.layers.data(name='image', shape=image_shape, dtype='float32')
    gt_box = fluid.layers.data(name='gt_box',
                               shape=[4],
                               dtype='float32',
                               lod_level=1)
    gt_label = fluid.layers.data(name='gt_label',
                                 shape=[1],
                                 dtype='int32',
                                 lod_level=1)
    gt_iscrowd = fluid.layers.data(name='gt_iscrowd',
                                   shape=[1],
                                   dtype='int32',
                                   lod_level=1)
    gt_image_info = fluid.layers.data(name='gt_image_id',
                                      shape=[3],
                                      dtype='int32')

    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)
    loss = fluid.layers.ssd_loss(locs, confs, gt_box, gt_label, box, box_var)
    loss = fluid.layers.reduce_sum(loss)

    place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace()
    exe = fluid.Executor(place)
    exe.run(fluid.default_startup_program())
    # 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_reader = reader.test(data_args, test_list, batch_size)
    feeder = fluid.DataFeeder(
        place=place,
        feed_list=[image, gt_box, gt_label, gt_iscrowd, gt_image_info])

    def get_dt_res(nmsed_out_v, data):
        dts_res = []
        lod = nmsed_out_v[0].lod()[0]
        nmsed_out_v = np.array(nmsed_out_v[0])
        real_batch_size = min(batch_size, len(data))
        assert (len(lod) == real_batch_size + 1), \
        "Error Lod Tensor offset dimension. Lod({}) vs. batch_size({})".format(len(lod), batch_size)
        k = 0
        for i in range(real_batch_size):
            dt_num_this_img = lod[i + 1] - lod[i]
            image_id = int(data[i][4][0])
            image_width = int(data[i][4][1])
            image_height = int(data[i][4][2])
            for j in range(dt_num_this_img):
                dt = nmsed_out_v[k]
                k = k + 1
                category_id, score, xmin, ymin, xmax, ymax = dt.tolist()
                xmin = max(min(xmin, 1.0), 0.0) * image_width
                ymin = max(min(ymin, 1.0), 0.0) * image_height
                xmax = max(min(xmax, 1.0), 0.0) * image_width
                ymax = max(min(ymax, 1.0), 0.0) * image_height
                w = xmax - xmin
                h = ymax - ymin
                bbox = [xmin, ymin, w, h]
                dt_res = {
                    'image_id': image_id,
                    'category_id': category_id,
                    'bbox': bbox,
                    'score': score
                }
                dts_res.append(dt_res)
        return dts_res

    def test():
        dts_res = []

        for batch_id, data in enumerate(test_reader()):
            nmsed_out_v = exe.run(fluid.default_main_program(),
                                  feed=feeder.feed(data),
                                  fetch_list=[nmsed_out],
                                  return_numpy=False)
            if batch_id % 20 == 0:
                print("Batch {0}".format(batch_id))
            dts_res += get_dt_res(nmsed_out_v, data)

        with io.open("detection_result.json", 'w') as outfile:
            encode_func = unicode if six.PY2 else str
            outfile.write(encode_func(json.dumps(dts_res)))
        print("start evaluate using coco api")
        cocoGt = COCO(os.path.join(data_args.data_dir, test_list))
        cocoDt = cocoGt.loadRes("detection_result.json")
        cocoEval = COCOeval(cocoGt, cocoDt, "bbox")
        cocoEval.evaluate()
        cocoEval.accumulate()
        cocoEval.summarize()

    test()
Ejemplo n.º 6
0
import os
import paddle
import paddle.fluid as fluid
from mobilenet_ssd import build_mobilenet_ssd

image_shape=[3,300,300]
num_classes=7
label_list='./data/Mydata/label_list'
model_dir='./model/best_model'

image=fluid.layers.data(name='img',shape=image_shape,dtype='float32')
locs,confs,box,box_var=build_mobilenet_ssd(image,num_classes,image_shape)
out=fluid.layers.detection_output(locs,confs,box,box_var,nms_threshold=0.45)
place=fluid.CPUPlace()
exe=fluid.Executor(place)

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)

fluid.io.save_inference_model(dirname='./cppmodel',feeded_var_names=['img'],target_vars=[out],executor=exe,model_filename='model',params_filename='param',program_only=False)