Exemple #1
0
def get_net(symbol, prefix, epoch, ctx):
    arg_params, aux_params = load_param(prefix, epoch, convert=True, ctx=ctx, process=True)

    # infer shape
    data_shape_dict = dict(DATA_SHAPES)
    arg_names, aux_names = symbol.list_arguments(), symbol.list_auxiliary_states()
    arg_shape, _, aux_shape = symbol.infer_shape(**data_shape_dict)
    arg_shape_dict = dict(zip(arg_names, arg_shape))
    aux_shape_dict = dict(zip(aux_names, aux_shape))

    # check shapes
    for k in symbol.list_arguments():
        if k in data_shape_dict or 'label' in k:
            continue
        assert k in arg_params, k + ' not initialized'
        assert arg_params[k].shape == arg_shape_dict[k], \
            'shape inconsistent for ' + k + ' inferred ' + str(arg_shape_dict[k]) + ' provided ' + str(
                arg_params[k].shape)
    for k in symbol.list_auxiliary_states():
        assert k in aux_params, k + ' not initialized'
        assert aux_params[k].shape == aux_shape_dict[k], \
            'shape inconsistent for ' + k + ' inferred ' + str(aux_shape_dict[k]) + ' provided ' + str(
                aux_params[k].shape)

    predictor = Predictor(symbol, DATA_NAMES, LABEL_NAMES, context=ctx,
                          provide_data=DATA_SHAPES, provide_label=LABEL_SHAPES,
                          arg_params=arg_params, aux_params=aux_params)
    return predictor
Exemple #2
0
def test_rcnn(network, dataset, image_set,
              dataset_path,
              ctx, prefix, epoch,
              vis, shuffle, has_rpn, proposal, max_box, thresh):
    # set config
    assert has_rpn, "only end-to-end case was checked in this project."
    config.TEST.HAS_RPN = True

    # load symbol and testing data
    sym = eval('get_' + network)(is_train=False, num_classes=config.NUM_CLASSES, num_anchors=config.NUM_ANCHORS)
    imdb = eval(dataset)(image_set, dataset_path)
    roidb = imdb.gt_roidb()
    roidb = filter_roidb(roidb)
    imdb.num_images = len(roidb)

    # get test data iter
    test_data = TestLoader(roidb, batch_size=1, shuffle=shuffle, has_rpn=has_rpn, nThreads=default.prefetch_thread_num)

    # load model
    arg_params, aux_params = load_param(prefix, epoch, convert=True, ctx=ctx, process=True)

    # infer shape
    data_shape_dict = dict(test_data.provide_data)
    arg_shape, _, aux_shape = sym.infer_shape(**data_shape_dict)
    arg_shape_dict = dict(zip(sym.list_arguments(), arg_shape))
    aux_shape_dict = dict(zip(sym.list_auxiliary_states(), aux_shape))

    # check parameters
    for k in sym.list_arguments():
        if k in data_shape_dict or 'label' in k:
            continue
        assert k in arg_params, k + ' not initialized'
        assert arg_params[k].shape == arg_shape_dict[k], \
            'shape inconsistent for ' + k + ' inferred ' + str(arg_shape_dict[k]) + ' provided ' + str(arg_params[k].shape)
    for k in sym.list_auxiliary_states():
        assert k in aux_params, k + ' not initialized'
        assert aux_params[k].shape == aux_shape_dict[k], \
            'shape inconsistent for ' + k + ' inferred ' + str(aux_shape_dict[k]) + ' provided ' + str(aux_params[k].shape)

    # decide maximum shape
    data_names = [k[0] for k in test_data.provide_data]
    label_names = None  # [k[0] for k in test_data.provide_label]
    max_data_shape = [('data', (config.NUM_IMAGES_3DCE, config.NUM_SLICES, config.MAX_SIZE, config.MAX_SIZE))]
    if not has_rpn:
        max_data_shape.append(('rois', (1, config.TEST.PROPOSAL_POST_NMS_TOP_N + 30, 5)))

    # create predictor
    predictor = Predictor(sym, data_names, label_names,
                          context=ctx, max_data_shapes=max_data_shape,
                          provide_data=test_data.provide_data, #provide_label=test_data.provide_label,
                          arg_params=arg_params, aux_params=aux_params)

    # start detection
    acc = pred_eval(predictor, test_data, imdb, vis=vis, max_box=max_box, thresh=thresh)

    return acc
def demo_maskrcnn(network, dataset, image_set, root_path, dataset_path, result_path,
              ctx, prefix, epoch,
              vis, shuffle, has_rpn, proposal, thresh):
    # set config
    if has_rpn:
        config.TEST.HAS_RPN = True

    # print config
    pprint.pprint(config)

    # load symbol and testing data
    if has_rpn:
        sym = eval('get_' + network + '_mask_test')(num_classes=config.NUM_CLASSES, num_anchors=config.NUM_ANCHORS)
        imdb = eval(dataset)(image_set, root_path, dataset_path)
        roidb = imdb.gt_roidb()
    else:
        raise NotImplementedError

    test_data = TestLoader(roidb, batch_size=1, shuffle=shuffle, has_rpn=has_rpn)

    # load model
    arg_params, aux_params = load_param(prefix, epoch, convert=True, ctx=ctx, process=True)

    # infer shape
    data_shape_dict = dict(test_data.provide_data)
    arg_shape, _, aux_shape = sym.infer_shape(**data_shape_dict)
    arg_shape_dict = dict(zip(sym.list_arguments(), arg_shape))
    aux_shape_dict = dict(zip(sym.list_auxiliary_states(), aux_shape))

    # check parameters
    for k in sym.list_arguments():
        if k in data_shape_dict or 'label' in k:
            continue
        assert k in arg_params, k + ' not initialized'
        assert arg_params[k].shape == arg_shape_dict[k], \
            'shape inconsistent for ' + k + ' inferred ' + str(arg_shape_dict[k]) + ' provided ' + str(arg_params[k].shape)
    for k in sym.list_auxiliary_states():
        assert k in aux_params, k + ' not initialized'
        assert aux_params[k].shape == aux_shape_dict[k], \
            'shape inconsistent for ' + k + ' inferred ' + str(aux_shape_dict[k]) + ' provided ' + str(aux_params[k].shape)

    # decide maximum shape
    data_names = [k[0] for k in test_data.provide_data]
    label_names = None
    max_data_shape = [('data', (1, 3, max([v[0] for v in config.SCALES]), max([v[1] for v in config.SCALES])))]
    if not has_rpn:
        max_data_shape.append(('rois', (1, config.TEST.PROPOSAL_POST_NMS_TOP_N + 30, 5)))

    # create predictor
    predictor = Predictor(sym, data_names, label_names,
                          context=ctx, max_data_shapes=max_data_shape,
                          provide_data=test_data.provide_data, provide_label=test_data.provide_label,
                          arg_params=arg_params, aux_params=aux_params)

    pred_demo_mask(predictor, test_data, imdb, roidb, result_path, vis=vis, thresh=thresh)
Exemple #4
0
def get_net(data, sym, prefix, epoch, ctx):
    # get predictor
    data = [[mx.nd.array(data[i][name]) for name in DATA_NAMES] for i in xrange(len(data))]
    max_data_shape = [('data', (1, 3, max([v[0] for v in config.SCALES]), max([v[1] for v in config.SCALES])))]
    provide_data = [[(k, v.shape) for k, v in zip(DATA_NAMES, data[i])] for i in xrange(len(data))]
    provide_label = [None for i in xrange(len(data))]
    arg_params, aux_params = load_param(prefix, epoch, process=True)
    #print DATA_NAMES, LABEL_NAMES, ctx, max_data_shape, provide_data, provide_label
    predictor = Predictor(sym, DATA_NAMES, LABEL_NAMES,
                          context=[ctx], max_data_shapes=max_data_shape,
                          provide_data=provide_data, provide_label=provide_label,
                          arg_params=arg_params, aux_params=aux_params)
    return predictor
Exemple #5
0
def get_net(symbol, prefix, epoch, ctx):
    arg_params, aux_params = load_param(prefix,
                                        epoch,
                                        convert=True,
                                        ctx=ctx,
                                        process=True)
    predictor = Predictor(symbol,
                          DATA_NAMES,
                          LABEL_NAMES,
                          context=ctx,
                          provide_data=DATA_SHAPES,
                          provide_label=LABEL_SHAPES,
                          arg_params=arg_params,
                          aux_params=aux_params)
    return predictor
Exemple #6
0
def get_net(prefix, epoch, ctx):
    arg_params, aux_params = load_param(prefix,
                                        epoch,
                                        convert=True,
                                        ctx=ctx,
                                        process=True)

    predictor = Predictor(gensym.gen_sym_infer,
                          DATA_NAMES,
                          LABEL_NAMES,
                          context=ctx,
                          max_data_shapes=dict(DATA_SHAPES),
                          provide_data=DATA_SHAPES,
                          provide_label=LABEL_SHAPES,
                          arg_params=arg_params,
                          aux_params=aux_params)
    return predictor
Exemple #7
0
def get_net(prefix, epoch, ctx):
    arg_params, aux_params = load_param(prefix,
                                        epoch,
                                        convert=True,
                                        ctx=ctx,
                                        process=True)

    # infer shape
    data_shape_dict = dict(DATA_SHAPES)

    symbol = gen_sym_infer(data_shape_dict,
                           len(ctx) if isinstance(ctx, list) else 1)
    # data = mx.symbol.Variable(name="data", shape=(1,3,600,903))
    # im_info = mx.symbol.Variable(name="im_info", shape=(1,3))
    # symbol = get_vgg_text_rpn_test(data, im_info)

    arg_names, aux_names = symbol.list_arguments(
    ), symbol.list_auxiliary_states()
    arg_shape, _, aux_shape = symbol.infer_shape_partial()
    arg_shape_dict = dict(zip(arg_names, arg_shape))
    aux_shape_dict = dict(zip(aux_names, aux_shape))

    # check shapes
    for k in symbol.list_arguments():
        if k in data_shape_dict or 'label' in k:
            continue
        assert k in arg_params, k + ' not initialized'
        assert arg_params[k].shape == arg_shape_dict[k], \
            'shape inconsistent for ' + k + ' inferred ' + str(arg_shape_dict[k]) + ' provided ' + str(arg_params[k].shape)
    for k in symbol.list_auxiliary_states():
        assert k in aux_params, k + ' not initialized'
        assert aux_params[k].shape == aux_shape_dict[k], \
            'shape inconsistent for ' + k + ' inferred ' + str(aux_shape_dict[k]) + ' provided ' + str(aux_params[k].shape)

    predictor = Predictor(gen_sym_infer,
                          DATA_NAMES,
                          LABEL_NAMES,
                          context=ctx,
                          max_data_shapes=data_shape_dict,
                          provide_data=DATA_SHAPES,
                          provide_label=LABEL_SHAPES,
                          arg_params=arg_params,
                          aux_params=aux_params)
    return predictor
Exemple #8
0
def test_rpn(network, dataset, image_set, root_path, dataset_path, ctx, prefix,
             epoch, vis, shuffle, thresh):
    # rpn generate proposal config
    config.TEST.HAS_RPN = True

    # print config
    pprint(config)

    # load symbol
    sym = eval('get_' + network + '_rpn_test')(num_anchors=config.NUM_ANCHORS)

    # load dataset and prepare imdb for training
    imdb = eval(dataset)(image_set, root_path, dataset_path)
    roidb = imdb.gt_roidb()

    # (possibly) group the roidb by aspect
    horizontal_inds, vertical_inds = [], []
    for ind, roirec in enumerate(roidb):
        if roirec['width'] > roirec['height']:
            horizontal_inds.append(ind)
        else:
            vertical_inds.append(ind)

    aspect_group = True if len(horizontal_inds) > 0 and len(
        vertical_inds) > 0 else False
    print("aspect_group={}".format(aspect_group))

    if aspect_group:
        horizontal_roidb = [roidb[ind] for ind in horizontal_inds]
        vertical_roidb = [roidb[ind] for ind in vertical_inds]
        l1 = TestLoader(horizontal_roidb,
                        batch_size=len(ctx),
                        shuffle=shuffle,
                        has_rpn=True)
        l2 = TestLoader(vertical_roidb,
                        batch_size=len(ctx),
                        shuffle=shuffle,
                        has_rpn=True)
        test_data = SequentialLoader(iters=[l1, l2])
    else:
        test_data = TestLoader(roidb,
                               batch_size=len(ctx),
                               shuffle=shuffle,
                               has_rpn=True)

    # sanity check
    _, out_shape, _ = sym.get_internals().infer_shape(
        **dict(test_data.provide_data))
    out_names = sym.get_internals().list_outputs()
    pprint_with_newlines(zip(out_names, out_shape), "output shape: ")

    # load model
    arg_params, aux_params = load_param(prefix, epoch, convert=True, ctx=None)

    # infer shape
    data_shape_dict = dict(test_data.provide_data)
    arg_shape, _, aux_shape = sym.infer_shape(**data_shape_dict)
    arg_shape_dict = dict(zip(sym.list_arguments(), arg_shape))
    aux_shape_dict = dict(zip(sym.list_auxiliary_states(), aux_shape))

    # check parameters
    for k in sym.list_arguments():
        if k in data_shape_dict or 'label' in k:
            continue
        assert k in arg_params, k + ' not initialized'
        assert arg_params[k].shape == arg_shape_dict[k], \
            'shape inconsistent for ' + k + ' inferred ' + str(arg_shape_dict[k]) + ' provided ' + str(
                arg_params[k].shape)
    for k in sym.list_auxiliary_states():
        assert k in aux_params, k + ' not initialized'
        assert aux_params[k].shape == aux_shape_dict[k], \
            'shape inconsistent for ' + k + ' inferred ' + str(aux_shape_dict[k]) + ' provided ' + str(
                aux_params[k].shape)

    # decide maximum shape
    data_names = [k[0] for k in test_data.provide_data]
    label_names = None if test_data.provide_label is None else [
        k[0] for k in test_data.provide_label
    ]
    max_data_shape = [('data', (len(ctx), 3, max([v[0]
                                                  for v in config.SCALES]),
                                max([v[1] for v in config.SCALES])))]

    # create predictor
    predictor = Predictor(sym,
                          data_names,
                          label_names,
                          context=ctx,
                          max_data_shapes=max_data_shape,
                          provide_data=test_data.provide_data,
                          provide_label=test_data.provide_label,
                          arg_params=arg_params,
                          aux_params=aux_params)

    # start testing
    imdb_boxes, original_boxes = generate_proposals(predictor,
                                                    test_data,
                                                    imdb,
                                                    vis=vis,
                                                    thresh=thresh)

    if aspect_group:
        # imdb_boxes = [imdb_boxes[ind] for ind in (horizontal_inds + vertical_inds)]
        # original_boxes = [original_boxes[ind] for ind in (horizontal_inds + vertical_inds)]
        reordered_imdb_boxes, reordered_original_boxes = [
            None
        ] * len(imdb_boxes), [None] * len(imdb_boxes)
        for i, orig_ind in enumerate(horizontal_inds + vertical_inds):
            reordered_imdb_boxes[orig_ind] = imdb_boxes[i]
            reordered_original_boxes[orig_ind] = original_boxes[i]
        imdb_boxes, original_boxes = reordered_imdb_boxes, reordered_original_boxes

    # save results
    rpn_folder = os.path.join(imdb.root_path, 'rpn_data')
    if not os.path.exists(rpn_folder):
        os.mkdir(rpn_folder)

    rpn_file = os.path.join(rpn_folder, imdb.name + '_rpn.pkl')
    with open(rpn_file, 'wb') as f:
        cPickle.dump(imdb_boxes, f, cPickle.HIGHEST_PROTOCOL)

    if thresh > 0:
        full_rpn_file = os.path.join(rpn_folder, imdb.name + '_full_rpn.pkl')
        with open(full_rpn_file, 'wb') as f:
            cPickle.dump(original_boxes, f, cPickle.HIGHEST_PROTOCOL)

    print 'wrote rpn proposals to {}'.format(rpn_file)

    imdb.evaluate_recall(roidb, candidate_boxes=imdb_boxes)