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)
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)
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)
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 predictor = get_net(sym, args.params, ctx, short=args.img_short_side, max_size=args.img_long_side) # 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] output = predictor.predict(data_batch) rois = output['rois_output'][:, 1:] scores = output['cls_prob_reshape_output'][0] bbox_deltas = output['bbox_pred_reshape_output'][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)
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) # assemble executor predictor = get_net(sym, args.params, ctx, short=args.img_short_side, max_size=args.img_long_side) # forward output = predictor.predict(data_batch) rois = output['rois_output'][:, 1:] scores = output['cls_prob_reshape_output'][0] bbox_deltas = output['bbox_pred_reshape_output'][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)
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)