def test_rcnn(imageset, year, root_path, devkit_path, prefix, epoch, ctx, vis=False, has_rpn=True, proposal='rpn', end2end=False): # load symbol and testing data if has_rpn: sym = get_vgg_test() config.TEST.HAS_RPN = True config.TEST.RPN_PRE_NMS_TOP_N = 6000 config.TEST.RPN_POST_NMS_TOP_N = 300 voc, roidb = load_gt_roidb(imageset, year, root_path, devkit_path) else: sym = get_vgg_rcnn_test() voc, roidb = eval('load_test_' + proposal + '_roidb')(imageset, year, root_path, devkit_path) # get test data iter test_data = ROIIter(roidb, batch_size=1, shuffle=False, mode='test') # load model args, auxs, _ = load_param(prefix, epoch, convert=True, ctx=ctx) # detect detector = Detector(sym, ctx, args, auxs) pred_eval(detector, test_data, voc, vis=vis)
def main(): args = parse_args() ctx = mx.gpu(args.gpu) symbol = get_vgg_test(num_classes=config.NUM_CLASSES, num_anchors=config.NUM_ANCHORS) predictor = get_net(symbol, args.prefix, args.epoch, ctx) demo_net(predictor, args.image, args.vis)
def main(): args = parse_args() ctx = mx.gpu(args.gpu) symbol = get_vgg_test(num_classes=config.NUM_CLASSES, num_anchors=config.NUM_ANCHORS) predictor = get_net(symbol, args.prefix, args.epoch, ctx) assert os.path.exists(args.image), args.image + ' not found' im = cv2.imread(args.image) demo_net(predictor, im, args.vis)
def main(): color = cv2.imread(args.img) # read image in b,g,r order img, scale = resize(color.copy(), 640, 1024) im_info = np.array([[img.shape[0], img.shape[1], scale]], dtype=np.float32) # (h, w, scale) img = np.swapaxes(img, 0, 2) img = np.swapaxes(img, 1, 2) # change to r,g,b order img = img[np.newaxis, :] # extend to (n, c, h, w) ctx = mx.gpu(args.gpu) _, arg_params, aux_params = mx.model.load_checkpoint( args.prefix, args.epoch) arg_params, aux_params = ch_dev(arg_params, aux_params, ctx) if 'resnet' in args.prefix: sym = resnet_50(num_class=2, bn_mom=0.99, bn_global=True, is_train=False) else: sym = get_vgg_test(num_classes=2) arg_params["data"] = mx.nd.array(img, ctx) arg_params["im_info"] = mx.nd.array(im_info, ctx) exe = sym.bind(ctx, arg_params, args_grad=None, grad_req="null", aux_states=aux_params) exe.forward(is_train=False) output_dict = { name: nd for name, nd in zip(sym.list_outputs(), exe.outputs) } rois = output_dict['rpn_rois_output'].asnumpy( )[:, 1:] # first column is index scores = output_dict['cls_prob_reshape_output'].asnumpy()[0] bbox_deltas = output_dict['bbox_pred_reshape_output'].asnumpy()[0] pred_boxes = bbox_pred(rois, bbox_deltas) pred_boxes = clip_boxes(pred_boxes, (im_info[0][0], im_info[0][1])) cls_boxes = pred_boxes[:, 4:8] cls_scores = scores[:, 1] keep = np.where(cls_scores >= args.thresh)[0] cls_boxes = cls_boxes[keep, :] cls_scores = cls_scores[keep] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets.astype(np.float32), args.nms_thresh) dets = dets[keep, :] keep = nest(dets, thresh=args.nest_thresh) dets = dets[keep, :] for i in range(dets.shape[0]): bbox = dets[i, :4] cv2.rectangle( color, (int(round(bbox[0] / scale)), int(round(bbox[1] / scale))), (int(round(bbox[2] / scale)), int(round(bbox[3] / scale))), (0, 255, 0), 2) cv2.imwrite("result.jpg", color)
def main(): args = parse_args() ctx = mx.gpu(args.gpu) symbol = get_vgg_test(num_classes=config.NUM_CLASSES, num_anchors=config.NUM_ANCHORS) viz = mx.viz.plot_network(symbol, shape={'data': (1, 3, 600, 800)}) viz.view('faster-rcnn') predictor = get_net(symbol, args.prefix, args.epoch, ctx) demo_net(predictor, args.image, args.vis)
def __init__(self, gpu=0, prefix=os.path.abspath( os.path.join(os.path.dirname(__file__), '..', '..', 'data', 'final')), epoch=0): ctx = mx.gpu(gpu) symbol = get_vgg_test(num_classes=config.NUM_CLASSES, num_anchors=config.NUM_ANCHORS) self.predictor = self.get_net(symbol, prefix, epoch, ctx)
def main(): args = parse_args() ctx = mx.gpu(args.gpu) symbol = get_vgg_test(num_classes=config.NUM_CLASSES, num_anchors=config.NUM_ANCHORS) df_load = pd.read_csv(args.rec) classes = ['blouse', 'outwear', 'skirt', 'dress', 'trousers'] results = [] for cls in classes: print('loading the model') prefix = args.prefix + cls df_pred = df_load[df_load['image_category'] == cls].copy() df_pred.reset_index(inplace=True) del df_pred['index'] predictor = get_net(symbol, prefix, args.epoch, ctx) record = [] for idx, row in df_pred.iterrows(): image_id = row['image_id'] cat = image_id.split('/')[1] if cat != cls: continue image = os.path.join(data_root, image_id) #cat = row['image_category'] res = demo_net(predictor, image, cls, args.vis) print(res) datum = [ image_id, cls, int(res[0]), int(res[1]), int(res[2]), int(res[3]), res[4] ] record.append(datum) #if idx >10: # break if res[4] < 0.1: print(image_id) cloumns = [ 'image_id', 'image_category', 'xmin', 'ymin', 'xmax', 'ymax', 'score' ] df_res = pd.DataFrame(record, columns=cloumns) results.append(df_res) df_res.to_csv(cls + '_res.csv', index=False) data = pd.concat(results, axis=0) data.to_csv('./rcnn_res.csv', index=None)
def main(): args = parse_args() ctx = mx.gpu(args.gpu) symbol = get_vgg_test(num_classes=config.NUM_CLASSES, num_anchors=config.NUM_ANCHORS) predictor = get_net(symbol, args.prefix, args.epoch, ctx) txt_name = args.image_path[:-1] result_txt = open(txt_name + '.txt', 'wb') vis = False for directory in os.listdir(args.image_path): print directory[-4:] if ((directory[-4:] != '.txt') and (directory[-4:] != 'json')) and (directory != 'cache'): img_dir = args.image_path + directory for img in os.listdir(img_dir): demo_net(predictor, img_dir + '/' + img, result_txt, vis) result_txt.close()
def main(): color = cv2.imread(args.img) # read image in b,g,r order img, scale = resize(color.copy(), 640, 1024) im_info = np.array([[img.shape[0], img.shape[1], scale]], dtype=np.float32) # (h, w, scale) img = np.swapaxes(img, 0, 2) img = np.swapaxes(img, 1, 2) # change to r,g,b order img = img[np.newaxis, :] # extend to (n, c, h, w) ctx = mx.gpu(args.gpu) _, arg_params, aux_params = mx.model.load_checkpoint(args.prefix, args.epoch) arg_params, aux_params = ch_dev(arg_params, aux_params, ctx) if 'resnet' in args.prefix: sym = resnet_50(num_class=2, bn_mom=0.99, bn_global=True, is_train=False) else: sym = get_vgg_test(num_classes=2) arg_params["data"] = mx.nd.array(img, ctx) arg_params["im_info"] = mx.nd.array(im_info, ctx) exe = sym.bind(ctx, arg_params, args_grad=None, grad_req="null", aux_states=aux_params) exe.forward(is_train=False) output_dict = {name: nd for name, nd in zip(sym.list_outputs(), exe.outputs)} rois = output_dict['rpn_rois_output'].asnumpy()[:, 1:] # first column is index scores = output_dict['cls_prob_reshape_output'].asnumpy()[0] bbox_deltas = output_dict['bbox_pred_reshape_output'].asnumpy()[0] pred_boxes = bbox_pred(rois, bbox_deltas) pred_boxes = clip_boxes(pred_boxes, (im_info[0][0], im_info[0][1])) cls_boxes = pred_boxes[:, 4:8] cls_scores = scores[:, 1] keep = np.where(cls_scores >= args.thresh)[0] cls_boxes = cls_boxes[keep, :] cls_scores = cls_scores[keep] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets.astype(np.float32), args.nms_thresh) dets = dets[keep, :] keep = nest(dets, thresh=args.nest_thresh) dets = dets[keep, :] for i in range(dets.shape[0]): bbox = dets[i, :4] cv2.rectangle(color, (int(round(bbox[0]/scale)), int(round(bbox[1]/scale))), (int(round(bbox[2]/scale)), int(round(bbox[3]/scale))), (0, 255, 0), 2) cv2.imwrite("result.jpg", color)
def test_net(imageset, year, root_path, devkit_path, prefix, epoch, ctx, vis): # set config config.TEST.HAS_RPN = True # set up logger logger = logging.getLogger() logger.setLevel(logging.INFO) # load testing data voc, roidb = load_gt_roidb(imageset, year, root_path, devkit_path) test_data = ROIIter(roidb, batch_size=1, shuffle=False, mode='test') # load model args, auxs = load_param(prefix, epoch, convert=True, ctx=ctx) # load symbol sym = get_vgg_test() # detect detector = Detector(sym, ctx, args, auxs) pred_eval(detector, test_data, voc, vis=vis)
def test_rcnn(imageset, year, root_path, devkit_path, prefix, epoch, ctx, vis=False, has_rpn=True, proposal='rpn'): # load symbol and testing data if has_rpn: sym = get_vgg_test() config.TEST.HAS_RPN = True config.TEST.RPN_PRE_NMS_TOP_N = 6000 config.TEST.RPN_POST_NMS_TOP_N = 300 voc, roidb = load_gt_roidb(imageset, year, root_path, devkit_path) else: sym = get_vgg_rcnn_test() voc, roidb = eval('load_test_' + proposal + '_roidb')(imageset, year, root_path, devkit_path) # get test data iter test_data = ROIIter(roidb, batch_size=1, shuffle=False, mode='test') # load model args, auxs, _ = load_param(prefix, epoch, convert=True, ctx=ctx) # detect detector = Detector(sym, ctx, args, auxs) pred_eval(detector, test_data, voc, vis=vis)
def main(): args = parse_args() ctx = mx.gpu(args.gpu) symbol = get_vgg_test(num_classes=config.NUM_CLASSES, num_anchors=config.NUM_ANCHORS) predictor = get_net(symbol, args.prefix, args.epoch, ctx) if args.image: # single test image demo_net(predictor, args.image, args.vis) else: # a image dir for test # import pdb img_list = os.listdir(args.dir) num = len(img_list) for line in img_list: img_path = os.path.join(args.dir, line) if os.path.isfile(img_path) and os.path.splitext(img_path)[-1] in [ '.jpg' ]: # pdb.set_trace() logger.info('%s' % num) demo_net(predictor, img_path, args.vis) num = num - 1
def get_net(prefix, epoch, ctx): args, auxs = load_param(prefix, epoch, convert=True, ctx=ctx) sym = get_vgg_test() detector = Detector(sym, ctx, args, auxs) return detector
def main(): args = parse_args() ctx = mx.gpu(args.gpu) symbol = get_vgg_test() predictor = get_net(symbol, args.prefix, args.epoch, ctx) demo_net(predictor, args.image)
def get_net(prefix, epoch, ctx): args, auxs, num_class = load_param(prefix, epoch, convert=True, ctx=ctx) sym = get_vgg_test(num_classes=num_class) detector = Detector(sym, ctx, args, auxs) return detector
def main(): args = parse_args() ctx = mx.gpu(args.gpu) symbol = get_vgg_test(num_classes=21, num_anchors=9) predictor = get_net(symbol, args.prefix, args.epoch, ctx) demo_net(predictor, args.image, args.vis)
help='saved model prefix', default='model/e2e', type=str) parser.add_argument('--epoch', help='epoch of pretrained model', default=50, type=int) parser.add_argument('--gpu', help='GPU device to use', default=0, type=int) parser.add_argument('--vis', help='display result', action='store_true') args = parser.parse_args() return args if __name__ == '__main__': args = parse_args() ctx = mx.gpu(0) if (Global.prefix_value == 'model/e2e'): symbol = get_resnet_test(num_classes=Global.num_class_value, num_anchors=config.NUM_ANCHORS) if (Global.prefix_value == 'model/final'): symbol = get_vgg_test(num_classes=Global.num_class_value, num_anchors=config.NUM_ANCHORS) predictor = get_net(symbol, args.prefix, ctx) from glob import glob res = glob("/root/mx-rcnn/testimage/bird/*.JPG") cnt = 0 for i in res: demo_net(predictor, i, False) # if cnt>10: # break