Exemplo n.º 1
0
def test_weight_async_reorder():
    data = mx.sym.Variable("data")
    w1 = mx.sym.Variable("1_weight")
    w2 = mx.sym.Variable("2_weight")
    conv1 = mx.sym.Convolution(data=data,
                               weight=w1 + w1,
                               num_filter=32,
                               no_bias=True,
                               kernel=(3, 3))
    conv2 = mx.sym.Convolution(data=conv1,
                               weight=w2 + w2,
                               num_filter=32,
                               no_bias=True,
                               kernel=(1, 1))
    mod = Module(symbol=conv2, label_names=None, context=mx.current_context())
    mod.bind(for_training=False, data_shapes=[('data', (10, 16, 50, 50))])
    mod.init_params(initializer=mx.init.Xavier(magnitude=2.))
    data = [
        mx.random.uniform(-1.0,
                          1.0,
                          shape=(10, 16, 50, 50),
                          ctx=mx.current_context())
    ]
    batch = mx.io.DataBatch(data, [])
    for i in range(2):
        mod.forward(batch, is_train=False)
        for output in mod.get_outputs():
            output.wait_to_read()
Exemplo n.º 2
0
def check_quantize(sym, data_shape, out_type, name='conv',
                   check_calibration=True, gluon_forward=False):
  sg_pass_name = config[name][SG_PASS_NAME]
  post_sg_pass_name = config[name][POST_SG_PASS_NAME]

  fc = mx.sym.FullyConnected(data=sym, num_hidden=10, flatten=True, name='fc_softmax')
  if gluon_forward == True:
    sym = fc
    sym_sg = sym.get_backend_symbol(sg_pass_name)
    mod = Module(symbol=sym, label_names=[])
    mod.bind(for_training=False,
            data_shapes=[('data', data_shape)])
  else:
    sym = mx.sym.SoftmaxOutput(data=fc, name='softmax')
    sym_sg = sym.get_backend_symbol(sg_pass_name)
    label_shape = (data_shape[0], 10)
    mod = Module(symbol=sym)
    mod.bind(for_training=False,
            data_shapes=[('data', data_shape)],
            label_shapes=[('softmax_label', label_shape)])
  mod.init_params(mx.init.Normal(0.5))
  arg_params, aux_params = mod.get_params()

  data = [mx.random.uniform(-1, 1, shape=shape, ctx=mx.current_context()) for _, shape in mod.data_shapes]
  batch = mx.io.DataBatch(data, [])

  mod.forward(batch, is_train=False)
  for output in mod.get_outputs():
      output.wait_to_read()
  ref_out = mod.get_outputs()

  excluded_sym_names = []
  if mx.current_context() == mx.cpu() and gluon_forward == True:
    excluded_sym_names += ['sg_mkldnn_fully_connected_0']
    excluded_sym_names += ['fc_softmax']

  calib_data = mx.nd.random.uniform(shape=data_shape)
  calib_data = NDArrayIter(data=calib_data)
  calib_data = DummyIter(calib_data)
  calib_layer = lambda name: name.endswith('_output')
  qsym, qarg_params, qaux_params = mx.contrib.quant.quantize_model(sym=sym_sg,
                                                                   arg_params=arg_params,
                                                                   aux_params=aux_params,
                                                                   ctx=mx.current_context(),
                                                                   excluded_sym_names=excluded_sym_names,
                                                                   quantized_dtype=out_type,
                                                                   calib_mode='naive',
                                                                   calib_data=calib_data,
                                                                   calib_layer=calib_layer,
                                                                   num_calib_examples=5)
  qsym = qsym.get_backend_symbol(post_sg_pass_name)
  if check_calibration:
    check_qsym_calibrated(qsym, out_type, name=name)
  if gluon_forward == True:
    check_qsym_gluon_forward(qsym, qarg_params, qaux_params, data_shape)
  else:
    check_qsym_dummy_forward(qsym, batch, data_shape, label_shape)
    quantized_out = check_qsym_forward(qsym, qarg_params, qaux_params, batch, data_shape, label_shape)
    for i in range(len(ref_out)):
      assert_almost_equal(ref_out[i].asnumpy(), quantized_out[i].asnumpy(), atol = 1)
Exemplo n.º 3
0
def demo_net(sym, class_names, args):
    # print config
    print('called with args\n{}'.format(pprint.pformat(vars(args))))

    # setup context
    if args.gpu:
        ctx = mx.gpu(int(args.gpu))
    else:
        ctx = mx.cpu(0)

    # load single test
    im_tensor, im_info, im_orig = load_test(args.image, short=args.img_short_side, max_size=args.img_long_side,
                                            mean=args.img_pixel_means, std=args.img_pixel_stds)

    # generate data batch
    data_batch = generate_batch(im_tensor, im_info)

    # load params
    arg_params, aux_params = load_param(args.params, ctx=ctx)

    # produce shape max possible
    data_names = ['data', 'im_info']
    label_names = None
    data_shapes = [('data', (1, 3, args.img_long_side, args.img_long_side)), ('im_info', (1, 3))]
    label_shapes = None

    # check shapes
    check_shape(sym, data_shapes, arg_params, aux_params)

    # create and bind module
    mod = Module(sym, data_names, label_names, context=ctx)
    mod.bind(data_shapes, label_shapes, for_training=False)
    mod.init_params(arg_params=arg_params, aux_params=aux_params)

    # forward
    forward_starts = time.time()
    mod.forward(data_batch)
    rois, scores, bbox_deltas = mod.get_outputs()
    rois.wait_to_read()
    rois = rois[:, 1:]
    scores = scores[0]
    bbox_deltas = bbox_deltas[0]
    forward_costs = time.time() - forward_starts
    print("forward costs %.4f" % (forward_costs))

    im_info = im_info[0]
    # decode detection
    det = im_detect(rois, scores, bbox_deltas, im_info,
                    bbox_stds=args.rcnn_bbox_stds, nms_thresh=args.rcnn_nms_thresh,
                    conf_thresh=args.rcnn_conf_thresh)

    # print out
    for [cls, conf, x1, y1, x2, y2] in det:
        if cls > 0 and conf > args.vis_thresh:
            print(class_names[int(cls)], conf, [x1, y1, x2, y2])

    # if vis
    if args.vis:
        vis_detection(im_orig, det, class_names, thresh=args.vis_thresh, prefix=args.image)
Exemplo n.º 4
0
def check_qsym_forward(qsym, qarg_params, qaux_params, batch, data_shape):
    mod = Module(symbol=qsym, label_names=None, context=mx.current_context())
    mod.bind(for_training=False, data_shapes=[('data', data_shape)])
    mod.set_params(qarg_params, qaux_params)
    mod.forward(batch, is_train=False)
    for output in mod.get_outputs():
        output.wait_to_read()
    return mod.get_outputs()
Exemplo n.º 5
0
def check_qsym_dummy_forward(qsym, batch, data_shape):
    mod = Module(symbol=qsym, label_names=None, context=mx.current_context())
    mod.bind(for_training=False, data_shapes=[('data', data_shape)])
    mod.init_params(initializer=mx.init.Xavier(magnitude=2.))
    mod.forward(batch, is_train=False)
    for output in mod.get_outputs():
        output.wait_to_read()
    return mod.get_outputs()
Exemplo n.º 6
0
def check_quantize(sym, data_shape, check_conv=True):
    fc = mx.sym.FullyConnected(data=sym,
                               num_hidden=10,
                               flatten=True,
                               name='fc')
    sym = mx.sym.SoftmaxOutput(data=fc, name='softmax')
    sym_sg = sym.get_backend_symbol("MKLDNN")
    label_shape = (data_shape[0], 10)
    mod = Module(symbol=sym)
    mod.bind(for_training=False,
             data_shapes=[('data', data_shape)],
             label_shapes=[('softmax_label', label_shape)])
    mod.init_params(mx.init.Normal(0.5))
    arg_params, aux_params = mod.get_params()

    data = [
        mx.random.uniform(-1, 1, shape=shape, ctx=mx.current_context())
        for _, shape in mod.data_shapes
    ]
    batch = mx.io.DataBatch(data, [])

    mod.forward(batch, is_train=False)
    for output in mod.get_outputs():
        output.wait_to_read()
    ref_out = mod.get_outputs()

    excluded_sym_names = []
    if mx.current_context() == mx.cpu():
        excluded_sym_names += ['fc']

    calib_data = mx.nd.random.uniform(shape=data_shape)
    calib_data = NDArrayIter(data=calib_data)
    calib_data = DummyIter(calib_data)
    calib_layer = lambda name: name.endswith('_output')
    qsym, qarg_params, qaux_params = mx.contrib.quant.quantize_model(
        sym=sym_sg,
        arg_params=arg_params,
        aux_params=aux_params,
        ctx=mx.current_context(),
        excluded_sym_names=excluded_sym_names,
        quantized_dtype='uint8',
        calib_mode='naive',
        calib_data=calib_data,
        calib_layer=calib_layer,
        calib_quantize_op=True,
        num_calib_examples=5)
    qsym = qsym.get_backend_symbol("MKLDNN_POST_QUANTIZE")
    if check_conv:
        check_qsym_calibrated(qsym)
    quantized_out = check_qsym_forward(qsym, qarg_params, qaux_params, batch,
                                       data_shape, label_shape)
    for i in range(len(ref_out)):
        assert_almost_equal(ref_out[i].asnumpy(),
                            quantized_out[i].asnumpy(),
                            atol=1)
    check_qsym_dummy_forward(qsym, batch, data_shape, label_shape)
Exemplo n.º 7
0
def check_quantize(sym, data_shape, out_type, name='conv',
                   check_calibration=True, gluon_forward=False, check_scale_align=False):
  if name in config:
    name = config[name][OP_NAME]
  sym_sg = sym.get_backend_symbol(QUANTIZE_SG_PASS_NAME)
  mod = Module(symbol=sym, label_names=None)
  mod.bind(for_training=False,
            data_shapes=[('data', data_shape)])
  mod.init_params(mx.init.Normal(0.5))
  arg_params, aux_params = mod.get_params()

  if out_type == 'uint8':
    data = [mx.random.uniform(0.0, 1.0, shape=shape, ctx=mx.current_context()) for _, shape in mod.data_shapes]
  else:
    data = [mx.random.uniform(-1.0, 1.0, shape=shape, ctx=mx.current_context()) for _, shape in mod.data_shapes]
  batch = mx.io.DataBatch(data, [])

  mod.forward(batch, is_train=False)
  for output in mod.get_outputs():
      output.wait_to_read()
  ref_out = mod.get_outputs()

  excluded_sym_names = []
  excluded_op_names = []
  if mx.current_context() == mx.cpu() and gluon_forward == True:
    excluded_op_names += ['_sg_mkldnn_fully_connected']

  calib_data = CalibIter(batch, data_shape, 1)

  qsym, qarg_params, qaux_params = mx.contrib.quant.quantize_model(sym=sym_sg,
                                                                   arg_params=arg_params,
                                                                   aux_params=aux_params,
                                                                   ctx=mx.current_context(),
                                                                   excluded_sym_names=excluded_sym_names,
                                                                   excluded_op_names=excluded_op_names,
                                                                   quantized_dtype=out_type,
                                                                   calib_mode='naive',
                                                                   calib_data=calib_data,
                                                                   calib_layer=None,
                                                                   label_names=None,
                                                                   num_calib_examples=1)
  qsym = qsym.get_backend_symbol(QUANTIZE_SG_PASS_NAME)
  if check_calibration:
    check_qsym_calibrated(qsym, out_type, name=name)
  if check_scale_align:
    check_qsym_scale_align(qsym)
  if gluon_forward == True:
    check_qsym_gluon_forward(qsym, qarg_params, qaux_params, data_shape)
  else:
    quantized_out = check_qsym_forward(qsym, qarg_params, qaux_params, batch, data_shape)
    for i in range(len(ref_out)):
      min_range = mx.nd.min(ref_out[i]).asscalar()
      max_range = mx.nd.max(ref_out[i]).asscalar()
      atol = 0.1 * max(abs(min_range), abs(max_range))
      assert_almost_equal_with_err(quantized_out[i].asnumpy(), ref_out[i].asnumpy(), rtol=0.1, atol=atol, etol=0.2)
    check_qsym_dummy_forward(qsym, batch, data_shape)
Exemplo n.º 8
0
 def check_qsym_forward(qsym, qarg_params, qaux_params, data_shape):
   mod = Module(symbol=qsym, label_names=None, context=mx.current_context())
   mod.bind(for_training=False,
            data_shapes=[('data', data_shape)])
   mod.set_params(qarg_params, qaux_params)
   data = [mx.random.uniform(-1.0, 1.0, shape=shape) for _, shape in mod.data_shapes]
   batch = mx.io.DataBatch(data, [])
   mod.forward(batch, is_train=False)
   for output in mod.get_outputs():
       output.wait_to_read()
Exemplo n.º 9
0
def demo_net(sym, class_names, args):
    # print config
    print('called with args\n{}'.format(pprint.pformat(vars(args))))

    # setup context
    if args.gpu:
        ctx = mx.gpu(int(args.gpu))
    else:
        ctx = mx.cpu(0)

    # load single test
    im_tensor, im_info, im_orig = load_test(args.image, short=args.img_short_side, max_size=args.img_long_side,
                                            mean=args.img_pixel_means, std=args.img_pixel_stds)

    # generate data batch
    data_batch = generate_batch(im_tensor, im_info)

    # load params
    arg_params, aux_params = load_param(args.params, ctx=ctx)

    # produce shape max possible
    data_names = ['data', 'im_info']
    label_names = None
    data_shapes = [('data', (1, 3, args.img_long_side, args.img_long_side)), ('im_info', (1, 3))]
    label_shapes = None

    # check shapes
    check_shape(sym, data_shapes, arg_params, aux_params)

    # create and bind module
    mod = Module(sym, data_names, label_names, context=ctx)
    mod.bind(data_shapes, label_shapes, for_training=False)
    mod.init_params(arg_params=arg_params, aux_params=aux_params)

    # forward
    mod.forward(data_batch)
    rois, scores, bbox_deltas = mod.get_outputs()
    rois = rois[:, 1:]
    scores = scores[0]
    bbox_deltas = bbox_deltas[0]
    im_info = im_info[0]

    # decode detection
    det = im_detect(rois, scores, bbox_deltas, im_info,
                    bbox_stds=args.rcnn_bbox_stds, nms_thresh=args.rcnn_nms_thresh,
                    conf_thresh=args.rcnn_conf_thresh)

    # print out
    for [cls, conf, x1, y1, x2, y2] in det:
        if cls > 0 and conf > args.vis_thresh:
            print(class_names[int(cls)], conf, [x1, y1, x2, y2])

    # if vis
    if args.vis:
        vis_detection(im_orig, det, class_names, thresh=args.vis_thresh)
Exemplo n.º 10
0
def test_net(sym, imdb, args):
    # print config
    logger.info('called with args\n{}'.format(pprint.pformat(vars(args))))

    # setup context
    ctx = mx.gpu(args.gpu)

    # load testing data
    test_data = TestLoader(imdb.roidb, batch_size=1, short=args.img_short_side, max_size=args.img_long_side,
                           mean=args.img_pixel_means, std=args.img_pixel_stds)

    # load params
    arg_params, aux_params = load_param(args.params, ctx=ctx)

    # produce shape max possible
    data_names = ['data', 'im_info']
    label_names = None
    data_shapes = [('data', (1, 3, args.img_long_side, args.img_long_side)), ('im_info', (1, 3))]
    label_shapes = None

    # check shapes
    check_shape(sym, data_shapes, arg_params, aux_params)

    # create and bind module
    mod = Module(sym, data_names, label_names, context=ctx)
    mod.bind(data_shapes, label_shapes, for_training=False)
    mod.init_params(arg_params=arg_params, aux_params=aux_params)

    # all detections are collected into:
    #    all_boxes[cls][image] = N x 5 array of detections in
    #    (x1, y1, x2, y2, score)
    all_boxes = [[[] for _ in range(imdb.num_images)]
                 for _ in range(imdb.num_classes)]

    # start detection
    with tqdm(total=imdb.num_images) as pbar:
        for i, data_batch in enumerate(test_data):
            # forward
            im_info = data_batch.data[1][0]
            mod.forward(data_batch)
            rois, scores, bbox_deltas = mod.get_outputs()
            rois = rois[:, 1:]
            scores = scores[0]
            bbox_deltas = bbox_deltas[0]

            det = im_detect(rois, scores, bbox_deltas, im_info,
                            bbox_stds=args.rcnn_bbox_stds, nms_thresh=args.rcnn_nms_thresh,
                            conf_thresh=args.rcnn_conf_thresh)
            for j in range(1, imdb.num_classes):
                indexes = np.where(det[:, 0] == j)[0]
                all_boxes[j][i] = np.concatenate((det[:, -4:], det[:, [1]]), axis=-1)[indexes, :]
            pbar.update(data_batch.data[0].shape[0])

    # evaluate model
    imdb.evaluate_detections(all_boxes)
Exemplo n.º 11
0
def check_quantize(sym, data_shape, check_conv=True):
  fc = mx.sym.FullyConnected(data=sym, num_hidden=10, flatten=True, name='fc')
  sym = mx.sym.SoftmaxOutput(data=fc, name='softmax')
  sym_sg = sym.get_backend_symbol("MKLDNN")
  label_shape = (data_shape[0], 10)
  mod = Module(symbol=sym)
  mod.bind(for_training=False,
           data_shapes=[('data', data_shape)],
           label_shapes=[('softmax_label', label_shape)])
  mod.init_params(mx.init.Normal(0.5))
  arg_params, aux_params = mod.get_params()

  data = [mx.random.uniform(-1, 1, shape=shape, ctx=mx.current_context()) for _, shape in mod.data_shapes]
  batch = mx.io.DataBatch(data, [])

  mod.forward(batch, is_train=False)
  for output in mod.get_outputs():
      output.wait_to_read()
  ref_out = mod.get_outputs()

  excluded_sym_names = []
  if mx.current_context() == mx.cpu():
    excluded_sym_names += ['fc']

  calib_data = mx.nd.random.uniform(shape=data_shape)
  calib_data = NDArrayIter(data=calib_data)
  calib_data = DummyIter(calib_data)
  calib_layer = lambda name: name.endswith('_output')
  qsym, qarg_params, qaux_params = mx.contrib.quant.quantize_model(sym=sym_sg,
                                                                   arg_params=arg_params,
                                                                   aux_params=aux_params,
                                                                   ctx=mx.current_context(),
                                                                   excluded_sym_names=excluded_sym_names,
                                                                   quantized_dtype='uint8',
                                                                   calib_mode='naive',
                                                                   calib_data=calib_data,
                                                                   calib_layer=calib_layer,
                                                                   calib_quantize_op=True,
                                                                   num_calib_examples=5)
  qsym = qsym.get_backend_symbol("MKLDNN_POST_QUANTIZE")
  if check_conv:
    check_qsym_calibrated(qsym)
  quantized_out = check_qsym_forward(qsym, qarg_params, qaux_params, batch, data_shape, label_shape)
  for i in range(len(ref_out)):
    assert_almost_equal(ref_out[i].asnumpy(), quantized_out[i].asnumpy(), atol = 1)
  check_qsym_dummy_forward(qsym, batch, data_shape, label_shape)
Exemplo n.º 12
0
class Predictor(object):
    def __init__(self,
                 symbol,
                 data_names,
                 label_names,
                 context=mx.cpu(),
                 max_data_shapes=None,
                 provide_data=None,
                 provide_label=None,
                 arg_params=None,
                 aux_params=None):
        #self._mod = MutableModule(symbol, data_names, label_names,
        #                          context=context, max_data_shapes=max_data_shapes)
        self._mod = Module(symbol, data_names, label_names, context=context)
        self._mod.bind(provide_data, provide_label, for_training=False)
        self._mod.init_params(arg_params=arg_params, aux_params=aux_params)

    def predict(self, data_batch):
        self._mod.forward(data_batch)
        return dict(zip(self._mod.output_names,
                        self._mod.get_outputs()))  #TODO
Exemplo n.º 13
0
def test_net(sym, imdb, args):
    # print config
    logger.info('called with args\n{}'.format(pprint.pformat(vars(args))))

    # setup context
    ctx = mx.gpu(args.gpu)

    # load testing data
    test_data = TestLoader(imdb.roidb,
                           batch_size=1,
                           short=args.img_short_side,
                           max_size=args.img_long_side,
                           mean=args.img_pixel_means,
                           std=args.img_pixel_stds)

    # load params
    arg_params, aux_params = load_param(args.params, ctx=ctx)

    # produce shape max possible
    data_names = ['data', 'im_info']
    label_names = None
    data_shapes = [('data', (1, 3, args.img_long_side, args.img_long_side)),
                   ('im_info', (1, 3))]
    label_shapes = None

    # check shapes
    check_shape(sym, data_shapes, arg_params, aux_params)

    # create and bind module
    mod = Module(sym, data_names, label_names, context=ctx)
    mod.bind(data_shapes, label_shapes, for_training=False)
    mod.init_params(arg_params=arg_params, aux_params=aux_params)

    # all detections are collected into:
    #    all_boxes[cls][image] = N x 5 array of detections in
    #    (x1, y1, x2, y2, score)
    results_list = []
    all_boxes = [[[] for _ in range(imdb.num_images)]
                 for _ in range(imdb.num_classes)]
    all_masks = [[[] for _ in range(imdb.num_images)]
                 for _ in range(imdb.num_classes)]
    all_rois = [[[] for _ in range(imdb.num_images)]
                for _ in range(imdb.num_classes)]

    # start detection
    with tqdm(total=imdb.num_images) as pbar:
        for i, data_batch in enumerate(test_data):
            # forward
            im_info = data_batch.data[1][0]
            mod.forward(data_batch)
            rois, scores, bbox_deltas, mask_prob = mod.get_outputs()
            rois = rois[:, 1:]
            scores = scores[0]
            bbox_deltas = bbox_deltas[0]

            det, masks, rois_out = im_detect(rois,
                                             scores,
                                             bbox_deltas,
                                             mask_prob,
                                             im_info,
                                             bbox_stds=args.rcnn_bbox_stds,
                                             nms_thresh=args.rcnn_nms_thresh,
                                             conf_thresh=args.rcnn_conf_thresh)
            # print(det.shape, masks.shape)
            for j in range(1, imdb.num_classes):
                indexes = np.where(det[:, 0] == j)[0]
                all_boxes[j][i] = np.concatenate((det[:, -4:], det[:, [1]]),
                                                 axis=-1)[indexes, :]
                # print(type(masks), type(rois_out))
                all_masks[j][i] = masks[indexes]
                all_rois[j][i] = rois_out[indexes]

            boxes_this_image = [[]] + [
                all_boxes[cls_ind][i]
                for cls_ind in range(1, imdb.num_classes)
            ]
            masks_this_image = [[]] + [
                all_masks[cls_ind][i]
                for cls_ind in range(1, imdb.num_classes)
            ]
            rois_this_image = [[]] + [
                all_rois[cls_ind][i] for cls_ind in range(1, imdb.num_classes)
            ]
            results_list.append({
                'image': '{}.png'.format(i),
                'im_info': im_info.asnumpy(),
                'boxes': boxes_this_image,
                'masks': masks_this_image,
                'rois': rois_this_image
            })

            pbar.update(data_batch.data[0].shape[0])

    # evaluate model
    results_pack = {
        'all_boxes': all_boxes,
        'all_masks': all_masks,
        'results_list': results_list
    }
    imdb.evaluate_mask(results_pack)
Exemplo n.º 14
0
def test_net(sym, imdb, args, config):
    logger.addHandler(
        logging.FileHandler("{0}/{1}".format(args.prefix, 'test.log')))
    # print config
    logger.info('called with args\n{}'.format(pprint.pformat(vars(args))))

    # setup context
    ctx = mx.gpu(args.gpu)

    # load testing data
    test_data = TestLoader(imdb.roidb,
                           batch_size=1,
                           short=args.img_short_side,
                           max_size=args.img_long_side,
                           mean=config.transform['img_pixel_means'],
                           std=config.transform['img_pixel_stds'])

    # load params
    arg_params, aux_params = load_param(args.params, ctx=ctx)

    # produce shape max possible
    data_names = ['data', 'im_info']
    label_names = None
    data_shapes = [('data', (1, 3, args.img_long_side, args.img_long_side)),
                   ('im_info', (1, 3))]
    label_shapes = None

    # check shapes
    check_shape(sym, data_shapes, arg_params, aux_params)

    # create and bind module
    mod = Module(sym, data_names, label_names, context=ctx)
    mod.bind(data_shapes, label_shapes, for_training=False)
    mod.init_params(arg_params=arg_params, aux_params=aux_params)

    # all detections are collected into:
    #    all_boxes[cls][image] = N x 5 array of detections in
    #    (x1, y1, x2, y2, score)
    all_boxes = [[[] for _ in range(imdb.num_images)]
                 for _ in range(imdb.num_classes)]

    # start detection
    with tqdm(total=imdb.num_images) as pbar:
        for i, data_batch in enumerate(test_data):
            # forward
            im_info = data_batch.data[1][0]
            mod.forward(data_batch)
            rois, scores, bbox_deltas = mod.get_outputs()
            rois = rois[:, 1:]
            scores = scores[0]
            bbox_deltas = bbox_deltas[0]

            det = im_detect(rois,
                            scores,
                            bbox_deltas,
                            im_info,
                            bbox_stds=args.rcnn_bbox_stds,
                            nms_thresh=args.rcnn_nms_thresh,
                            conf_thresh=args.rcnn_conf_thresh)
            for j in range(1, imdb.num_classes):
                indexes = np.where(det[:, 0] == j)[0]
                all_boxes[j][i] = np.concatenate((det[:, -4:], det[:, [1]]),
                                                 axis=-1)[indexes, :]
            pbar.update(data_batch.data[0].shape[0])

    # evaluate model
    imdb.evaluate_detections(all_boxes)
Exemplo n.º 15
0
class FaceD(object):
    def __init__(self, config):
        # size = config.SCALE.lower()
        # if size == "small":
        #     scale = [576, 1024]
        # elif size == "middle":
        #     scale = [864, 1536]
        # elif size == "big":
        #     scale = [1152, 2048]    
        sym = mx.sym.load(config.SYMBOL_PATH)
        
        self.nms = py_nms_wrapper(0.3)
        self.scale = config.SCALE
        self.mod = Module(sym, ['data', 'im_info'], [], context=[mx.gpu(config.GPU_ID)])
        self.thresh = config.THRESH
        self.rebind = not config.FIXSIZE
        self.model_path = config.MODEL_PATH
        self.font = config.FONT_PATH
        self.preprocess = False

    def bbox_detect(self, im, im_scale, force_rebind=False):
    
        im_tensor = transform(im, [103.06, 115.9, 123.15])
        im_info = np.array([[im_tensor.shape[2], im_tensor.shape[3], im_scale]], dtype=np.float32)

        data = [mx.nd.array(im_tensor), mx.nd.array(im_info)]
        data_batch = mx.io.DataBatch(data=data, label=[], pad=0, index=0,
                                    provide_data=[[(k, v.shape) for k, v in zip(self.mod.data_names, data)]],
                                    provide_label=[None])

        if not self.mod.binded:
            arg_params, aux_params = load_param(self.model_path, 0, process=True)
            self.mod.bind([('data', (1L, 3L, im_tensor.shape[2], im_tensor.shape[3])), ('im_info', (1L, 3L))], None, 
                        for_training=False, inputs_need_grad=False, force_rebind=True,
                        shared_module=None)
            self.mod.init_params(arg_params=arg_params, aux_params=aux_params)
        
        if self.rebind or force_rebind:
            self.mod.bind([('data', (1L, 3L, im_tensor.shape[2], im_tensor.shape[3])), ('im_info', (1L, 3L))], None, 
                          for_training=False, inputs_need_grad=False, force_rebind=True,
                          shared_module=None)

        scale = data_batch.data[1].asnumpy()[0, 2]
        self.mod.forward(data_batch)
        output=dict(zip(self.mod.output_names, tuple(self.mod.get_outputs(merge_multi_context=False))))

        rois = output['rois_output'][0].asnumpy()[:, 1:]
        im_shape = data[0].shape

        scores = output['cls_prob_reshape_output'][0].asnumpy()[0]
        bbox_deltas = output['bbox_pred_reshape_output'][0].asnumpy()[0]

        pred_boxes = bbox_pred(rois, bbox_deltas)
        pred_boxes = clip_boxes(pred_boxes, im_shape[-2:])

        pred_boxes = pred_boxes / scale

        pred_boxes = pred_boxes.astype('f')
        scores = scores.astype('f')
        
        indexes = np.where(scores[:, 1] > self.thresh)[0]
        cls_scores = scores[indexes, 1, np.newaxis]
        cls_boxes = pred_boxes[indexes, 4:8]
        cls_dets = np.hstack((cls_boxes, cls_scores))
        keep = self.nms(cls_dets)
        return cls_dets[keep, :]

    def Detect(self, img):
        im, im_scale = resize(img, self.scale)
        dets = self.bbox_detect(im, im_scale)
        return dets
    
    def Detect_raw(self, img):
        im, im_scale = resize(img, [200, 400])
        dets = self.bbox_detect(im, im_scale, True)
        return dets

    def reset(self):
        self.mod.binded = False

    def vis_detections(self, img, dets, save='./tmp.jpg'):
        for bbox in dets:
            cv2.rectangle(img,(bbox[0], bbox[1]),(bbox[2],bbox[3]),(127, 255, 0), 4)
        cv2.imwrite(save, img)

    def vis_dets(self, img, dets, names, scores=None):
        img = img.copy()
        num = len(dets)
        for idx, bbox in enumerate(dets):
            cv2.rectangle(img,(int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (127, 255, 0), 4)
            cv2.rectangle(img,(int(bbox[0]-2), int(bbox[1]-25)),(int(bbox[0]+100), int(bbox[1])),(255, 0, 0), -1)
            if scores is not None:
                cv2.putText(img, '%.3f' % scores[idx], (int(bbox[0]-2), int(bbox[1]+20)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), thickness=1, lineType=8)
        font = ImageFont.truetype(self.font, 22)
        img_pil = Image.fromarray(img)
        draw = ImageDraw.Draw(img_pil)
        for idx, bbox in enumerate(dets):
            draw.text((int(bbox[0]), int(bbox[1]-22)),  names[idx].decode('utf8'), font = font, fill = (255 ,255 ,255 ,0))
        img = np.array(img_pil)
        #cv2.putText(img, 'person %d' % num, (100,200), cv2.FONT_HERSHEY_SIMPLEX, 7, (0, 0 ,255), thickness = 5, lineType = 8)
        return img
Exemplo n.º 16
0
class Solver(object):
    def __init__(self,
                 symbol,
                 data_names,
                 label_names,
                 data_shapes,
                 label_shapes,
                 logger=logging,
                 context=mx.cpu(),
                 work_load_list=None,
                 fixed_param_names=None):
        self.symbol = symbol
        self.data_names = data_names
        self.label_names = label_names
        self.data_shapes = data_shapes
        self.label_shapes = label_shapes
        self.context = context
        self.work_load_list = work_load_list
        self.fixed_param_names = fixed_param_names

        if logger is None:
            logger = logging.getLogger()
            logger.setLevel(logging.INFO)
        self.logger = logger
        self.module = Module(symbol=self.symbol,
                             data_names=self.data_names,
                             label_names=self.label_names,
                             logger=self.logger,
                             context=self.context,
                             work_load_list=self.work_load_list,
                             fixed_param_names=self.fixed_param_names)

    def fit(self,
            train_data,
            eval_data=None,
            eval_metric='acc',
            validate_metric=None,
            work_load_list=None,
            epoch_end_callback=None,
            batch_end_callback=None,
            fixed_param_prefix=None,
            initializer=None,
            arg_params=None,
            aux_params=None,
            allow_missing=False,
            optimizer=None,
            optimizer_params=None,
            begin_epoch=0,
            num_epoch=None,
            kvstore='device',
            teacher_modules=None):
        if type(teacher_modules) is not list:
            teacher_modules = [teacher_modules]
        self.module.bind(data_shapes=self.data_shapes,
                         label_shapes=self.label_shapes,
                         for_training=True)
        self.module.init_params(initializer=initializer,
                                arg_params=arg_params,
                                aux_params=aux_params,
                                allow_missing=allow_missing)
        self.module.init_optimizer(kvstore=kvstore,
                                   optimizer=optimizer,
                                   optimizer_params=optimizer_params)

        if validate_metric is None:
            validate_metric = eval_metric
        if not isinstance(eval_metric, metric.EvalMetric):
            eval_metric = metric.create(eval_metric)

        # training loop
        for epoch in range(begin_epoch, num_epoch):
            tic = time.time()
            eval_metric.reset()
            nbatch = 0
            data_iter = iter(train_data)
            end_of_batch = False
            next_data_batch = next(data_iter)
            while not end_of_batch:
                data_batch = next_data_batch

                if teacher_modules[0] is not None:
                    for teacher_module in teacher_modules:
                        teacher_module.forward(data_batch=data_batch,
                                               is_train=True)
                        transfer_label = teacher_module.get_outputs()
                        data_batch.label = data_batch.label + transfer_label
                self.module.forward(data_batch, is_train=True)
                self.module.backward()
                self.module.update()

                try:
                    next_data_batch = next(data_iter)
                except StopIteration:
                    end_of_batch = True

                self.module.update_metric(eval_metric, data_batch.label)

                if batch_end_callback is not None:
                    batch_end_params = BatchEndParam(epoch=epoch,
                                                     nbatch=nbatch,
                                                     eval_metric=eval_metric,
                                                     locals=locals())
                    for callback in _as_list(batch_end_callback):
                        callback(batch_end_params)
                nbatch += 1

            for name, val in eval_metric.get_name_value():
                self.logger.info('Epoch[%d] Train-%s=%f', epoch, name, val)
            toc = time.time()
            self.logger.info('Epoch[%d] Time cost=%.3f', epoch, (toc - tic))

            arg_params, aux_params = self.module.get_params()
            self.module.set_params(arg_params, aux_params)

            if epoch_end_callback is not None:
                for callback in _as_list(epoch_end_callback):
                    callback(epoch, self.symbol, arg_params, aux_params)
            if eval_data:
                res = self.module.score(eval_data,
                                        validate_metric,
                                        score_end_callback=None,
                                        batch_end_callback=None,
                                        reset=True,
                                        epoch=epoch)
                for name, val in res:
                    self.logger.info('Epoch[%d] Validation-%s=%f', epoch, name,
                                     val)

            train_data.reset()
Exemplo n.º 17
0
def demo_net(sym, class_names, args, result_path):
    # print config
    print('called with args\n{}'.format(pprint.pformat(vars(args))))

    # setup context
    if args.gpu:
        ctx = mx.gpu(int(args.gpu))
    else:
        ctx = mx.cpu(0)

    # load single test
    im_tensor, im_info, im_orig = load_test(args.image,
                                            short=args.img_short_side,
                                            max_size=args.img_long_side,
                                            mean=args.img_pixel_means,
                                            std=args.img_pixel_stds)

    # generate data batch
    data_batch = generate_batch(im_tensor, im_info)

    # load params
    arg_params, aux_params = load_param(args.params, ctx=ctx)

    # produce shape max possible
    data_names = ['data', 'im_info']
    label_names = None
    data_shapes = [('data', (1, 3, args.img_long_side, args.img_long_side)),
                   ('im_info', (1, 3))]
    label_shapes = None

    # check shapes
    check_shape(sym, data_shapes, arg_params, aux_params)

    # create and bind module
    mod = Module(sym, data_names, label_names, context=ctx)
    mod.bind(data_shapes, label_shapes, for_training=False)
    mod.init_params(arg_params=arg_params, aux_params=aux_params)

    # forward
    forward_starts = time.time()
    mod.forward(data_batch)
    rois, scores, bbox_deltas = mod.get_outputs()
    rois.wait_to_read()
    rois = rois[:, 1:]
    scores = scores[0]
    bbox_deltas = bbox_deltas[0]
    forward_costs = time.time() - forward_starts
    print("forward costs %.4f" % (forward_costs))

    im_info = im_info[0]
    # decode detection
    det = im_detect(rois,
                    scores,
                    bbox_deltas,
                    im_info,
                    bbox_stds=args.rcnn_bbox_stds,
                    nms_thresh=args.rcnn_nms_thresh,
                    conf_thresh=args.rcnn_conf_thresh)

    fieldnames = ['name', 'coordinate']
    if result_path.exists():
        csvfile = result_path.open("a")
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    else:
        csvfile = result_path.open("w+")
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
        writer.writeheader()

    img_name = Path(args.image).name
    bbox_str = ''
    for [cls, conf, x1, y1, x2, y2] in det:
        if cls > 0 and conf > args.vis_thresh:
            print(class_names[int(cls)], conf, [x1, y1, x2, y2])
            bbox_str += "%d_%d_%d_%d;" % (int(x1), int(y1), int(x2 - x1),
                                          int(y2 - y1))
    writer.writerow({'name': img_name, 'coordinate': bbox_str[:-1]})
    csvfile.close()
    print("detect image %s" % img_name)

    # if vis
    if args.vis:
        vis_detection(im_orig,
                      det,
                      class_names,
                      thresh=args.vis_thresh,
                      prefix=args.image)
Exemplo n.º 18
0
class Solver(object):
    def __init__(
            self,
            symbol,
            data_names,
            label_names,
            data_shapes,
            label_shapes,
            logger=logging,
            context=mx.cpu(),
            work_load_list=None,
            fixed_param_names=None,
            allow_missing=False,
            # for evaluate fold bn to create eval symbol
            config=None):
        self.symbol = symbol
        self.data_names = data_names
        self.label_names = label_names
        self.data_shapes = data_shapes
        self.label_shapes = label_shapes
        self.context = context
        self.work_load_list = work_load_list
        self.fixed_param_names = fixed_param_names

        if logger is None:
            logger = logging.getLogger()
            logger.setLevel(logging.INFO)
        self.logger = logger
        self.module = Module(symbol=self.symbol,
                             data_names=self.data_names,
                             label_names=self.label_names,
                             logger=self.logger,
                             context=self.context,
                             work_load_list=self.work_load_list,
                             fixed_param_names=self.fixed_param_names)
        # for fold bn
        self.config = config

    def fit(self,
            train_data,
            eval_data=None,
            eval_metric='acc',
            validate_metric=None,
            work_load_list=None,
            epoch_end_callback=None,
            batch_end_callback=None,
            fixed_param_prefix=None,
            initializer=None,
            arg_params=None,
            aux_params=None,
            allow_missing=False,
            optimizer=None,
            optimizer_params=None,
            begin_epoch=0,
            num_epoch=None,
            kvstore='device'):

        self.module.bind(data_shapes=self.data_shapes,
                         label_shapes=self.label_shapes,
                         for_training=True)
        self.module.init_params(initializer=initializer,
                                arg_params=arg_params,
                                aux_params=aux_params,
                                allow_missing=allow_missing)
        self.module.init_optimizer(kvstore=kvstore,
                                   optimizer=optimizer,
                                   optimizer_params=optimizer_params)

        if validate_metric is None:
            validate_metric = eval_metric
        if not isinstance(eval_metric, metric.EvalMetric):
            eval_metric = metric.create(eval_metric)

        temp_count = 0

        # # test model size by saving params of model
        # arg_params, aux_params = self.module.get_params()
        # for callback in _as_list(epoch_end_callback):
        #     callback(0, self.symbol, arg_params, aux_params)
        # raise NotImplementedError

        # training loop
        for epoch in range(begin_epoch, num_epoch):

            train_time = AverageMeter()
            kvstore_sync_time = AverageMeter()
            get_data_time = AverageMeter()
            iter_total_time = AverageMeter()

            tic = time.time()
            eval_metric.reset()
            nbatch = 0
            data_iter = iter(train_data)
            end_of_batch = False
            next_data_batch = next(data_iter)
            while not end_of_batch:
                start_time = time.time()
                data_batch = next_data_batch

                self.module.forward(data_batch, is_train=True)
                self.module.backward()

                # ndarray.waitall()
                train_time.update(time.time() - start_time)

                self.module.update()

                # ndarray.waitall()
                kvstore_sync_time.update(time.time() - start_time)

                try:
                    next_data_batch = next(data_iter)
                except StopIteration:
                    end_of_batch = True

                # ndarray.waitall()
                get_data_time.update(time.time() - start_time)

                if isinstance(data_batch, list):
                    self.module.update_metric(eval_metric,
                                              [db.label for db in data_batch],
                                              pre_sliced=True)
                else:
                    self.module.update_metric(eval_metric, data_batch.label)

                # ndarray.waitall()
                iter_total_time.update(time.time() - start_time)

                if batch_end_callback is not None:
                    # batch_end_params = BatchEndParam(epoch=epoch, nbatch=nbatch,
                    #                                  eval_metric=eval_metric,
                    #                                  locals=locals())

                    batch_end_params = BatchEndParam(
                        epoch=epoch,
                        nbatch=nbatch,
                        eval_metric=eval_metric,
                        locals=locals(),
                        rank=kvstore.rank,
                        total_iter=temp_count,
                        cur_data_time=get_data_time.val,
                        avg_data_time=get_data_time.avg,
                        cur_batch_time=train_time.val,
                        avg_batch_time=train_time.avg,
                        cur_kvstore_sync_time=kvstore_sync_time.val,
                        avg_kvstore_sync_time=kvstore_sync_time.avg,
                        cur_iter_total_time=iter_total_time.val,
                        avg_iter_total_time=iter_total_time.avg)
                    for callback in _as_list(batch_end_callback):
                        callback(batch_end_params)
                nbatch += 1
                temp_count += 1

            for name, val in eval_metric.get_name_value():
                self.logger.info('Epoch[%d] Train-%s=%f', epoch, name, val)
            toc = time.time()
            self.logger.info('Epoch[%d] Time cost=%.3f', epoch, (toc - tic))

            arg_params, aux_params = self.module.get_params()
            self.module.set_params(arg_params, aux_params)

            if epoch_end_callback is not None and kvstore.rank == 0:
                for callback in _as_list(epoch_end_callback):
                    callback(epoch, self.symbol, arg_params, aux_params)
            if eval_data:
                if self.config.network == 'mobilenet_int8_foldbn':
                    # for fold bn to create inference symbol
                    total_params_path = "./model/%s-%04d.params" % (
                        self.config.model_prefix, epoch + 1)
                    # total_params_path = "./model/mobilenet_flodbn_0904/mobilenet_int8_flodbn_imagenet_retrain_80_pertensor-fold-0100.params"
                    # _, arg_params, aux_params = mx.model.load_checkpoint('./model/mobilenet_flodbn_0904/mobilenet_int8_flodbn_imagenet_retrain_80_pertensor-fold', 100)
                    import os
                    assert os.path.exists(
                        total_params_path
                    ), "please provide the correct total_params_path for foldbn eval"
                    eval_sym = eval(self.config.network)(
                        num_classes=self.config.num_classes,
                        quant_mod=self.config.quant_mod,
                        delay_quant=self.config.delay_quant,
                        is_weight_perchannel=self.config.is_weight_perchannel,
                        total_params_path=total_params_path,
                        quantize_flag=self.config.quantize_flag)
                    eval_module = Module(
                        symbol=eval_sym,
                        data_names=self.data_names,
                        label_names=self.label_names,
                        logger=self.logger,
                        context=self.context,
                        work_load_list=self.work_load_list,
                        fixed_param_names=self.fixed_param_names)
                    eval_module.bind(data_shapes=self.data_shapes,
                                     label_shapes=self.label_shapes,
                                     for_training=False)
                    eval_module.init_params(initializer=initializer,
                                            arg_params=arg_params,
                                            aux_params=aux_params)
                    res = eval_module.score(eval_data,
                                            validate_metric,
                                            score_end_callback=None,
                                            batch_end_callback=None,
                                            reset=True,
                                            epoch=epoch)
                else:
                    res = self.module.score(eval_data,
                                            validate_metric,
                                            score_end_callback=None,
                                            batch_end_callback=None,
                                            reset=True,
                                            epoch=epoch)
                for name, val in res:
                    self.logger.info('Epoch[%d] Validation-%s=%f', epoch, name,
                                     val)

            train_data.reset()
Exemplo n.º 19
0
def demo_net(sym, class_names, args):
    # print config
    # setup context
    if args.gpu:
        ctx = mx.gpu(int(args.gpu))
    else:
        ctx = mx.cpu(0)

    print('called with args\n{}'.format(pprint.pformat(vars(args))))
    # load params
    arg_params, aux_params = load_param(args.params, ctx=ctx)

    # produce shape max possible
    data_names = ['data', 'im_info']
    label_names = None
    data_shapes = [('data', (1, 3, args.img_long_side, args.img_long_side)),
                   ('im_info', (1, 3))]
    label_shapes = None

    # check shapes
    check_shape(sym, data_shapes, arg_params, aux_params)

    # create and bind module
    mod = Module(sym, data_names, label_names, context=ctx)
    mod.bind(data_shapes, label_shapes, for_training=False)
    mod.init_params(arg_params=arg_params, aux_params=aux_params)

    f = open(
        "/home/skutukov/datasets/VOCdevkit/VOC2007/ImageSets/Main/test.txt",
        "r")
    for file in tqdm.tqdm(f.readlines()):
        path = os.path.join(args.image, str(file).strip() + '.jpg')
        path = '/home/skutukov/Pictures/demo.jpg'
        # load single test
        im_tensor, im_info, im_orig = load_test(path,
                                                short=args.img_short_side,
                                                max_size=args.img_long_side,
                                                mean=args.img_pixel_means,
                                                std=args.img_pixel_stds,
                                                ctx=ctx)

        # generate data batch
        data_batch = generate_batch(im_tensor, im_info)
        # forward
        mod.forward(data_batch)
        rois, scores, bbox_deltas = mod.get_outputs()
        rois = rois[:, 1:]
        scores = scores[0]
        bbox_deltas = bbox_deltas[0]
        im_info = im_info[0]

        # decode detection
        det = im_detect(rois,
                        scores,
                        bbox_deltas,
                        im_info,
                        bbox_stds=args.rcnn_bbox_stds,
                        nms_thresh=args.rcnn_nms_thresh,
                        conf_thresh=args.rcnn_conf_thresh)

        # print out
        for [cls, conf, x1, y1, x2, y2] in det:
            if cls > 0 and conf > args.vis_thresh:
                print(class_names[int(cls)], conf, [x1, y1, x2, y2])

        # if vis
        if args.vis:
            vis_detection(im_orig,
                          det,
                          class_names,
                          thresh=args.vis_thresh,
                          file=file)

        break
Exemplo n.º 20
0
                              rand_mirror=False,
                              shuffle=shuffle_dataset,
                              shuffle_chunk_seed=shuffle_seed,
                              seed=shuffle_seed,
                              **mean_args)
 mod = Module(symbol=convnet_code_sym, label_names=None, context=ctx)
 mod.bind(for_training=False, data_shapes=data.provide_data)
 mod.set_params(arg_params, aux_params)
 num_images = 0
 convnet_codes = None  # N * 1000
 resized_images = None  # NCHW
 labels = None
 for batch in data:
     if num_images >= args.max_num_images:
         break
     mod.forward(data_batch=batch, is_train=False)
     fc_output = mod.get_outputs()[0].flatten().copyto(mx.cpu(0))
     num_images += batch_size
     fc_output.wait_to_read()
     if convnet_codes is None:
         convnet_codes = fc_output
     else:
         convnet_codes = mx.nd.concat(*[convnet_codes, fc_output], dim=0)
     if labels is None:
         labels = batch.label[0].copyto(mx.cpu(0))
     else:
         labels = mx.nd.concat(*[labels, batch.label[0]], dim=0)
     images = batch.data[0].copyto(mx.cpu(0))  # batch images in NCHW
     images = images.transpose((0, 2, 3, 1))  # batch images in NHWC
     images.wait_to_read()
     for i in range(images.shape[0]):
Exemplo n.º 21
0
def demo_net(sym, class_names, args):
    # print config
    print('called with args\n{}'.format(pprint.pformat(vars(args))))

    # setup context
    if args.gpu:
        ctx = mx.gpu(int(args.gpu))
    else:
        ctx = mx.cpu(0)

    # load single test
    im_tensor, im_info, im_orig = load_test(args.image, short=args.img_short_side, max_size=args.img_long_side,
                                            mean=args.img_pixel_means, std=args.img_pixel_stds)


    # generate data batch
    data_batch = generate_batch(im_tensor, im_info)

    # load params
    arg_params, aux_params = load_param(args.params, ctx=ctx)

    # produce shape max possible
    data_names = ['data', 'im_info']
    label_names = None
    data_shapes = [('data', (1, 3, args.img_long_side, args.img_long_side)), ('im_info', (1, 3))]
    label_shapes = None

    # check shapes
    check_shape(sym, data_shapes, arg_params, aux_params)

    # create and bind module
    mod = Module(sym, data_names, label_names, context=ctx)
    mod.bind(data_shapes, label_shapes, for_training=False)
    mod.init_params(arg_params=arg_params, aux_params=aux_params)

    # forward
    start=time.time()
    mod.forward(data_batch)
    rois, scores, bbox_deltas = mod.get_outputs()
    print("time=", time.time() - start)

    #rois = rois.asnumpy()


    rois = rois[:, 1:]
    #print('rois=',rois)
    scores = scores[0]
    bbox_deltas = bbox_deltas[0]
    #print("BBox_deltas.shape=",bbox_deltas.shape)
    #print("BBOX_deltas=",bbox_deltas)
    im_info = im_info[0]

    # decode detection

    det = im_detect(rois, scores, bbox_deltas, im_info,
                    bbox_stds=args.rcnn_bbox_stds, nms_thresh=args.rcnn_nms_thresh,
                    conf_thresh=args.rcnn_conf_thresh)

    # print out
    for [cls, conf, x_c,y_c,w,h,theta] in det:
        if cls > 0 and conf > args.vis_thresh:
            print('class_name=',class_names[int(cls)], 'conf=',conf, [x_c

                , y_c, w,h,theta])

    if True:

        draw_rotate_box_cv(det,class_names,0.95)
Exemplo n.º 22
0
def demo_net(sym, class_names, args):
    # print config
    print('called with args\n{}'.format(pprint.pformat(vars(args))))

    # setup context
    if args.gpu:
        ctx = mx.gpu(int(args.gpu))
    else:
        ctx = mx.cpu(0)

    # load single test
    im_tensor, im_info, im_orig = load_test(args.image,
                                            short=args.img_short_side,
                                            max_size=args.img_long_side,
                                            mean=args.img_pixel_means,
                                            std=args.img_pixel_stds)

    # generate data batch
    data_batch = generate_batch(im_tensor, im_info)

    # load params
    arg_params, aux_params = load_param(args.params, ctx=ctx)

    # produce shape max possible
    data_names = ['data', 'im_info']
    label_names = None
    data_shapes = [('data', (1, 3, args.img_long_side, args.img_long_side)),
                   ('im_info', (1, 3))]
    label_shapes = None

    # check shapes
    check_shape(sym, data_shapes, arg_params, aux_params)

    # create and bind module
    mod = Module(sym, data_names, label_names, context=ctx)
    mod.bind(data_shapes, label_shapes, for_training=False)
    mod.init_params(arg_params=arg_params, aux_params=aux_params)

    # forward
    mod.forward(data_batch)
    rois, scores, bbox_deltas, mask_prob = mod.get_outputs()
    rois = rois[:, 1:]
    scores = scores[0]
    bbox_deltas = bbox_deltas[0]
    im_info = im_info[0]

    # decode detection
    det, masks = im_detect(rois,
                           scores,
                           bbox_deltas,
                           mask_prob,
                           im_info,
                           bbox_stds=args.rcnn_bbox_stds,
                           nms_thresh=args.rcnn_nms_thresh,
                           conf_thresh=args.rcnn_conf_thresh)

    im = cv2.imread(args.image)
    print(im.shape)
    print(im_info)
    # print out
    for index, [cls, conf, x1, y1, x2, y2] in enumerate(det):
        print(masks[index].max())
        if cls > 0 and conf > args.vis_thresh:
            print(class_names[int(cls)], conf, [x1, y1, x2, y2])
            print((int(x1), int(y1)), (int(x2), int(y2)))
            cv2.rectangle(im, (int(x1), int(y1)), (int(x2), int(y2)),
                          (255, 0, 0), 10)
            cv2.imwrite("mask{}.png".format(index),
                        np.uint8(masks[index] * 255))

    cv2.imwrite('demo.png', im)

    # if vis
    if args.vis:
        vis_detection(im_orig, det, class_names, thresh=args.vis_thresh)