def net_visualization(network=None, num_classes=None, data_shape=None, train=None, output_dir=None, print_net=False, net=None): # if you specify your net, this means that you are calling this function from somewhere else.. if net is None: if not train: net = symbol_factory.get_symbol(network, data_shape, num_classes=num_classes) else: net = symbol_factory.get_symbol_train(network, data_shape, num_classes=num_classes) if not train: a = mx.viz.plot_network(net, shape={"data": (1, 3, data_shape, data_shape)}, \ node_attrs={"shape": 'rect', "fixedsize": 'false'}) filename = "ssd_" + network + '_' + str(data_shape) + '_' + 'test' else: a = mx.viz.plot_network(net, shape=None, \ node_attrs={"shape": 'rect', "fixedsize": 'false'}) filename = "ssd_" + network + '_' + 'train' a.render(os.path.join(output_dir, filename)) if print_net: print(net.tojson())
def get_detector(net, prefix, epoch, data_shape, mean_pixels, ctx, num_class, nms_thresh=0.5, force_nms=True, nms_topk=400): """ wrapper for initialize a detector Parameters: ---------- net : str test network name prefix : str load model prefix epoch : int load model epoch data_shape : int resize image shape mean_pixels : tuple (float, float, float) mean pixel values (R, G, B) ctx : mx.ctx running context, mx.cpu() or mx.gpu(?) num_class : int number of classes nms_thresh : float non-maximum suppression threshold force_nms : bool force suppress different categories """ if net is not None: net = get_symbol(net, data_shape, num_classes=num_class, nms_thresh=nms_thresh, force_nms=force_nms, nms_topk=nms_topk) detector = Detector(net, prefix, epoch, data_shape, mean_pixels, ctx=ctx) return detector
def run_camera(args,ctx): assert args.batch_size == 1, "only batch size of 1 is supported" logging.info("Detection threshold is {}".format(args.thresh)) iter = CameraIterator() class_names = parse_class_names(args.class_names) mean_pixels = (args.mean_r, args.mean_g, args.mean_b) data_shape = int(args.data_shape) batch_size = int(args.batch_size) detector = Detector( get_symbol(args.network, data_shape, num_classes=len(class_names)), network_path(args.prefix, args.network, data_shape), args.epoch, data_shape, mean_pixels, batch_size, ctx ) for frame in iter: logging.info("Frame info: shape %s type %s", frame.shape, frame.dtype) logging.info("Generating batch") data_batch = detector.create_batch(frame) logging.info("Detecting objects") detections_batch = detector.detect_batch(data_batch) #detections = [mx.nd.array((1,1,0.2,0.2,0.4,0.4))] detections = detections_batch[0] logging.info("%d detections", len(detections)) for det in detections: obj = det.asnumpy() (klass, score, x0, y0, x1, y1) = obj if score > args.thresh: draw_detection(frame, obj, class_names) cv2.imshow('frame', frame)
def get_detector(net, prefix, epoch, data_shape, mean_pixels, ctx, num_class, num_tpls, num_inprots, nms_thresh=0.5, force_nms=True, nms_topk=400): """ wrapper for initialize a detector Parameters: ---------- net : str test network name prefix : str load model prefix epoch : int load model epoch data_shape : int resize image shape mean_pixels : tuple (float, float, float) mean pixel values (R, G, B) ctx : mx.ctx running context, mx.cpu() or mx.gpu(?) num_class : int number of classes nms_thresh : float non-maximum suppression threshold force_nms : bool force suppress different categories """ if net is not None: net = get_symbol(net, data_shape, num_classes=num_class, num_tpls = num_tpls, num_inprots = num_inprots, nms_thresh=nms_thresh, force_nms=force_nms, nms_topk=nms_topk) detector = Detector(net, prefix, epoch, data_shape, mean_pixels, ctx=ctx) return detector
def run_camera(args, ctx): assert args.batch_size == 1, "only batch size of 1 is supported" logging.info("Detection threshold is {}".format(args.thresh)) iter = CameraIterator(frame_resize=parse_frame_resize(args.frame_resize)) class_names = parse_class_names(args.class_names) mean_pixels = (args.mean_r, args.mean_g, args.mean_b) data_shape = int(args.data_shape) batch_size = int(args.batch_size) detector = Detector( get_symbol(args.network, data_shape, num_classes=len(class_names)), network_path(args.prefix, args.network, data_shape), args.epoch, data_shape, mean_pixels, batch_size, ctx) for frame in iter: logging.info("Frame info: shape %s type %s", frame.shape, frame.dtype) logging.info("Generating batch") data_batch = detector.create_batch(frame) logging.info("Detecting objects") detections_batch = detector.detect_batch(data_batch) #detections = [mx.nd.array((1,1,0.2,0.2,0.4,0.4))] detections = detections_batch[0] logging.info("%d detections", len(detections)) for det in detections: obj = det.asnumpy() (klass, score, x0, y0, x1, y1) = obj if score > args.thresh: draw_detection(frame, obj, class_names) cv2.imshow('frame', frame)
def end2end_benchmark(body_network, target, batch_size): image_shape = (3, 512, 512) data_shape = (batch_size,) + image_shape data_array = np.random.uniform(0, 255, size=data_shape).astype("float32") _, arg_params, aux_params = mx.model.load_checkpoint('model/ssd_resnet50_512', 0) sym = get_symbol(body_network, 512, num_classes=20) mod = mx.mod.Module(symbol=sym, context=mx.cpu(), label_names=None) mod.bind(data_shapes=[('data', data_shape)]) mod.set_params(arg_params, aux_params) # mod.save_checkpoint('model_updated/ssd_resnet50', 0) mx_data = mx.nd.array(data_array) times = [] for i in range(20): s = time.time() mod.forward(Batch(data=[mx_data]), is_train=False) for output in mod.get_outputs(): output.wait_to_read() mkl_time = time.time() - s times.append(mkl_time) print("MKL SSD inference time for batch size of %d: %f" % (batch_size, np.mean(times) * 1000)) net, params = nnvm.frontend.from_mxnet(sym, mod.get_params()[0], mod.get_params()[1]) ctx = tvm.cpu() opt_level = 3 with nnvm.compiler.build_config(opt_level=opt_level): graph, lib, params = nnvm.compiler.build(net, target, shape={"data": data_shape}, params=params) with open('graph.json', 'w') as fn: fn.writelines(graph.json()) module = graph_runtime.create(graph, lib, ctx) module.set_input(**params) input_data = tvm.nd.array(data_array, ctx=ctx) module.set_input('data', input_data) # warm up for i in range(100): module.run() times = [] s = time.time() for i in range(num_pass): module.run() tvm_time = time.time() - s # times.append(tvm_time) print("TVM %s inference time for batch size of %d: %f" % (body_network, batch_size, tvm_time * 1000.0 / num_pass)) for i in range(len(mod.get_outputs())): print('Check %dth output ...' % i) mxnet_out = mod.get_outputs()[i] out_shape = mxnet_out.shape tvm_out = module.get_output(i, out=tvm.nd.empty(out_shape)) np.testing.assert_array_almost_equal(tvm_out.asnumpy(), mxnet_out.asnumpy(), decimal=2)
def loadObjectDetector(model_path, epoch): # load SSD model net = get_symbol('vgg16_reduced', data_size, num_classes=1, nms_thresh=0.5, force_nms=True, nms_topk=400) sym, arg_params, aux_params = mx.model.load_checkpoint(model_path, epoch) # use cpu instead of gpu model = mx.mod.Module(net, label_names=None) model.bind(data_shapes=[('data', (batchSize, 3, data_size, data_size))]) model.set_params(arg_params, aux_params, allow_missing=True) return model
def get_detection_mod(): #net = get_symbol('vgg16_reduced', data_shape, num_classes=1, nms_thresh=0.5,force_nms=True, nms_topk=400) #sym, args, auxs = mx.model.load_checkpoint('./model/ssd_vgg16_reduced_300', 126) net = get_symbol('mobilenet_little', data_shape, num_classes=1, nms_thresh=0.5,force_nms=True, nms_topk=400) #sym, args, auxs = mx.model.load_checkpoint('./model/ssd_mobilenet_300', 150) sym, args, auxs = mx.model.load_checkpoint('./model/ssd_dg_300', 60) mod = mx.mod.Module(net, label_names=None, context=ctx) mod.bind(for_training=False, data_shapes=[('data', (1, 3, data_shape, data_shape))]) mod.set_params(args, auxs, allow_extra=True) return mod
def get_detector(net, prefix, epoch, data_shape, mean_pixels, ctx, class_names, thresh, plot_confidence, nms_thresh=0.5, force_nms=True, nms_topk=400): if net is not None: net = get_symbol(net, data_shape, num_classes=len(class_names), nms_thresh=nms_thresh, force_nms=force_nms, nms_topk=nms_topk) detector = ImageDetector(net, prefix, epoch, data_shape, mean_pixels, class_names, thresh,\ plot_confidence, ctx=ctx) return detector
help='force non-maximum suppression on different class') parser.add_argument( '--topk', dest='nms_topk', type=int, default=400, help='apply nms only to top k detections based on scores.') args = parser.parse_args() return args if __name__ == '__main__': args = parse_args() net = get_symbol(args.network, args.data_shape, num_classes=args.num_classes, nms_thresh=args.nms_thresh, force_suppress=args.force_nms, nms_topk=args.nms_topk) if args.prefix.endswith('_'): prefix = args.prefix + args.network + '_' + str(args.data_shape) else: prefix = args.prefix _, arg_params, aux_params = mx.model.load_checkpoint(prefix, args.epoch) # new name tmp = prefix.rsplit('/', 1) save_prefix = '/deploy_'.join(tmp) mx.model.save_checkpoint(save_prefix, args.epoch, net, arg_params, aux_params) print("Saved model: {}-{:04d}.params".format(save_prefix, args.epoch)) print("Saved symbol: {}-symbol.json".format(save_prefix))
def evaluate_net(net, path_imgrec, num_classes, mean_pixels, data_shape, model_prefix, epoch, ctx=mx.cpu(), batch_size=1, path_imglist="", nms_thresh=0.45, force_nms=False, ovp_thresh=0.5, use_difficult=False, class_names=None, voc07_metric=False, frequent=20): """ evalute network given validation record file Parameters: ---------- net : str or None Network name or use None to load from json without modifying path_imgrec : str path to the record validation file path_imglist : str path to the list file to replace labels in record file, optional num_classes : int number of classes, not including background mean_pixels : tuple (mean_r, mean_g, mean_b) data_shape : tuple or int (3, height, width) or height/width model_prefix : str model prefix of saved checkpoint epoch : int load model epoch ctx : mx.ctx mx.gpu() or mx.cpu() batch_size : int validation batch size nms_thresh : float non-maximum suppression threshold force_nms : boolean whether suppress different class objects ovp_thresh : float AP overlap threshold for true/false postives use_difficult : boolean whether to use difficult objects in evaluation if applicable class_names : comma separated str class names in string, must correspond to num_classes if set voc07_metric : boolean whether to use 11-point evluation as in VOC07 competition frequent : int frequency to print out validation status """ # set up logger logging.basicConfig() logger = logging.getLogger() logger.setLevel(logging.INFO) # args if isinstance(data_shape, int): data_shape = (3, data_shape, data_shape) assert len(data_shape) == 3 and data_shape[0] == 3 #model_prefix += '_' + str(data_shape[1]) # iterator eval_iter = DetRecordIter(path_imgrec, batch_size, data_shape, mean_pixels=mean_pixels, label_pad_width=350, path_imglist=path_imglist, **cfg.valid) # model params load_net, args, auxs = mx.model.load_checkpoint(model_prefix, epoch) # network if net is None: net = load_net else: net = get_symbol(net, data_shape[1], num_classes=num_classes, nms_thresh=nms_thresh, force_suppress=force_nms) if not 'label' in net.list_arguments(): label = mx.sym.Variable(name='label') net = mx.sym.Group([net, label]) # init module mod = mx.mod.Module(net, label_names=('label', ), logger=logger, context=ctx, fixed_param_names=net.list_arguments()) mod.bind(data_shapes=eval_iter.provide_data, label_shapes=eval_iter.provide_label) mod.set_params(args, auxs, allow_missing=False, force_init=True) # run evaluation if voc07_metric: metric = VOC07MApMetric(ovp_thresh, use_difficult, class_names, roc_output_path=os.path.join( os.path.dirname(model_prefix), 'roc')) else: metric = MApMetric(ovp_thresh, use_difficult, class_names, roc_output_path=os.path.join( os.path.dirname(model_prefix), 'roc')) posemetric = PoseMetric( LINEMOD_path='/data/ZHANGXIN/DATASETS/SIXD_CHALLENGE/LINEMOD/', classes=class_names) # visualize bb8 results # for nbatch, eval_batch in tqdm(enumerate(eval_iter)): # mod.forward(eval_batch) # preds = mod.get_outputs(merge_multi_context=True) # # labels = eval_batch.label[0].asnumpy() # # get generated multi label from network # cls_prob = preds[0] # loc_pred = preds[4] # bb8_pred = preds[5] # anchors = preds[6] # # bb8dets = BB8MultiBoxDetection(cls_prob, loc_pred, bb8_pred, anchors, nms_threshold=0.5, force_suppress=False, # variances=(0.1, 0.1, 0.2, 0.2), nms_topk=400) # bb8dets = bb8dets.asnumpy() # # for nsample, sampleDet in enumerate(bb8dets): # image = eval_batch.data[0][nsample].asnumpy() # image += np.array(mean_pixels).reshape((3, 1, 1)) # image = np.transpose(image, axes=(1, 2, 0)) # draw_dets = [] # draw_cids = [] # # for instanceDet in sampleDet: # if instanceDet[0] == -1: # continue # else: # cid = instanceDet[0].astype(np.int16) # indices = np.where(sampleDet[:, 0] == cid)[0] # # if indices.size > 0: # draw_dets.append(sampleDet[indices[0], 6:]) # draw_cids.append(cid) # sampleDet = np.delete(sampleDet, indices, axis=0) # show_BB8(image / 255., np.transpose(draw_dets[-1].reshape((-1, 8, 2)), axes=(0,2,1)), [cid], # plot_path='./output/bb8results/{:04d}_{}'.format(nbatch * batch_size + nsample, class_names[cid])) # # # draw_dets = np.array(draw_dets) # # draw_cids = np.array(draw_cids) # # # show_BB8(image / 255., np.transpose(draw_dets.reshape((-1, 8, 2)), axes=(0,2,1)), draw_cids, # # plot_path='./output/bb8results/{:04d}'.format(nbatch * batch_size + nsample)) # quantitive results results = mod.score(eval_iter, [metric, posemetric], num_batch=None, batch_end_callback=mx.callback.Speedometer( batch_size, frequent=frequent, auto_reset=False)) results_save_path = os.path.join(os.path.dirname(model_prefix), 'evaluate_results') with open(results_save_path, 'w') as f: for k, v in results: print("{}: {}".format(k, v)) f.write("{}: {}\n".format(k, v)) f.close() reproj_save_path = os.path.join(os.path.dirname(model_prefix), 'reprojection_error') with open(reproj_save_path, 'wb') as f: # for k, v in metric.Reproj.items(): # f.write("{}: {}\n".format(k, v)) pickle.dump(posemetric.Reproj, f, protocol=2) f.close() count_save_path = os.path.join(os.path.dirname(model_prefix), 'gt_count') with open(count_save_path, 'wb') as f: # for k, v in metric.counts.items(): # f.write("{}: {}\n".format(k, v)) pickle.dump(posemetric.counts, f, protocol=2) f.close()
def evaluate_net(net, path_imgrec, num_classes, mean_pixels, data_shape, model_prefix, epoch, ctx=mx.cpu(), batch_size=1, path_imglist="", nms_thresh=0.45, force_nms=False, ovp_thresh=0.5, use_difficult=False, class_names=None, voc07_metric=False, frequent=20): """ evalute network given validation record file Parameters: ---------- net : str or None Network name or use None to load from json without modifying path_imgrec : str path to the record validation file path_imglist : str path to the list file to replace labels in record file, optional num_classes : int number of classes, not including background mean_pixels : tuple (mean_r, mean_g, mean_b) data_shape : tuple or int (3, height, width) or height/width model_prefix : str model prefix of saved checkpoint epoch : int load model epoch ctx : mx.ctx mx.gpu() or mx.cpu() batch_size : int validation batch size nms_thresh : float non-maximum suppression threshold force_nms : boolean whether suppress different class objects ovp_thresh : float AP overlap threshold for true/false postives use_difficult : boolean whether to use difficult objects in evaluation if applicable class_names : comma separated str class names in string, must correspond to num_classes if set voc07_metric : boolean whether to use 11-point evluation as in VOC07 competition frequent : int frequency to print out validation status """ # set up logger logging.basicConfig() logger = logging.getLogger() logger.setLevel(logging.INFO) # args if isinstance(data_shape, int): data_shape = (3, data_shape, data_shape) assert len(data_shape) == 3 and data_shape[0] == 3 #model_prefix += '_' + str(data_shape[1]) # iterator eval_iter = DetRecordIter(path_imgrec, batch_size, data_shape, path_imglist=path_imglist, **cfg.valid) # model params load_net, args, auxs = mx.model.load_checkpoint(model_prefix, epoch) # network if net is None: net = load_net else: net = get_symbol(net, data_shape[1], num_classes=num_classes, nms_thresh=nms_thresh, force_suppress=force_nms) if not 'label' in net.list_arguments(): label = mx.sym.Variable(name='label') net = mx.sym.Group([net, label]) # init module mod = mx.mod.Module(net, label_names=('label',), logger=logger, context=ctx, fixed_param_names=net.list_arguments()) mod.bind(data_shapes=eval_iter.provide_data, label_shapes=eval_iter.provide_label) mod.set_params(args, auxs, allow_missing=False, force_init=True) # run evaluation if voc07_metric: metric = VOC07MApMetric(ovp_thresh, use_difficult, class_names, roc_output_path=os.path.join(os.path.dirname(model_prefix), 'roc')) else: metric = MApMetric(ovp_thresh, use_difficult, class_names, roc_output_path=os.path.join(os.path.dirname(model_prefix), 'roc')) results = mod.score(eval_iter, metric, num_batch=None, batch_end_callback=mx.callback.Speedometer(batch_size, frequent=frequent, auto_reset=False)) for k, v in results: print("{}: {}".format(k, v))
default=os.path.join(os.getcwd(), 'model', 'ssd_'), type=str) parser.add_argument('--data-shape', dest='data_shape', type=int, default=300, help='data shape') parser.add_argument('--num-class', dest='num_classes', help='number of classes', default=20, type=int) parser.add_argument('--nms', dest='nms_thresh', type=float, default=0.5, help='non-maximum suppression threshold, default 0.5') parser.add_argument('--force', dest='force_nms', type=bool, default=True, help='force non-maximum suppression on different class') parser.add_argument('--topk', dest='nms_topk', type=int, default=400, help='apply nms only to top k detections based on scores.') args = parser.parse_args() return args if __name__ == '__main__': args = parse_args() net = get_symbol(args.network, args.data_shape, num_classes=args.num_classes, nms_thresh=args.nms_thresh, force_suppress=args.force_nms, nms_topk=args.nms_topk) if args.prefix.endswith('_'): prefix = args.prefix + args.network + '_' + str(args.data_shape) else: prefix = args.prefix _, arg_params, aux_params = mx.model.load_checkpoint(prefix, args.epoch) # new name tmp = prefix.rsplit('/', 1) save_prefix = '/deploy_'.join(tmp) mx.model.save_checkpoint(save_prefix, args.epoch, net, arg_params, aux_params) print("Saved model: {}-{:04d}.param".format(save_prefix, args.epoch)) print("Saved symbol: {}-symbol.json".format(save_prefix))
def evaluate_net(net, imdb, mean_pixels, data_shape, model_prefix, epoch, ctx=mx.cpu(), batch_size=1, nms_thresh=0.45, force_nms=False, ovp_thresh=0.5, use_difficult=False, voc07_metric=False): """ evalute network given validation record file Parameters: ---------- net : str or None Network name or use None to load from json without modifying path_imgrec : str path to the record validation file path_imglist : str path to the list file to replace labels in record file, optional num_classes : int number of classes, not including background mean_pixels : tuple (mean_r, mean_g, mean_b) data_shape : tuple or int (3, height, width) or height/width model_prefix : str model prefix of saved checkpoint epoch : int load model epoch ctx : mx.ctx mx.gpu() or mx.cpu() batch_size : int validation batch size nms_thresh : float non-maximum suppression threshold force_nms : boolean whether suppress different class objects ovp_thresh : float AP overlap threshold for true/false postives use_difficult : boolean whether to use difficult objects in evaluation if applicable class_names : comma separated str class names in string, must correspond to num_classes if set voc07_metric : boolean whether to use 11-point evluation as in VOC07 competition """ # set up logger logging.basicConfig() logger = logging.getLogger() logger.setLevel(logging.INFO) num_classes = imdb.num_classes class_names = imdb.classes # args if isinstance(data_shape, int): data_shape = (3, data_shape, data_shape) assert len(data_shape) == 3 and data_shape[0] == 3 model_prefix += '_' + str(data_shape[1]) # iterator eval_iter = FaceTestIter(imdb, mean_pixels, img_stride=128, fix_hw=True) # model params load_net, args, auxs = mx.model.load_checkpoint(model_prefix, epoch) # network if net is None: net = load_net else: net = get_symbol(net, data_shape[1], num_classes=num_classes, nms_thresh=nms_thresh, force_suppress=force_nms) if not 'label' in net.list_arguments(): label = mx.sym.Variable(name='label') net = mx.sym.Group([net, label]) # init module mod = mx.mod.Module(net, label_names=('label',), logger=logger, context=ctx, fixed_param_names=net.list_arguments()) mod.bind(data_shapes=eval_iter.provide_data, label_shapes=eval_iter.provide_label) mod.set_params(args, auxs, allow_missing=False, force_init=True) # run evaluation if voc07_metric: metric = VOC07MApMetric(ovp_thresh, use_difficult, class_names) else: metric = MApMetric(ovp_thresh, use_difficult, class_names) results = [] for i, (datum, im_info) in enumerate(eval_iter): mod.reshape(data_shapes=datum.provide_data, label_shapes=datum.provide_label) mod.forward(datum) preds = mod.get_outputs() det0 = preds[0][0].asnumpy() # (n_anchor, 6) det0 = do_nms(det0, 1, nms_thresh) preds[0][0] = mx.nd.array(det0, ctx=preds[0].context) sy, sx, _ = im_info['im_shape'] scaler = mx.nd.array((1.0, sx, sy, sx, sy, 1.0)) scaler = mx.nd.reshape(scaler, (1, 1, -1)) datum.label[0] *= scaler metric.update(datum.label, preds) if i % 10 == 0: print('processed {} images.'.format(i)) # if i == 10: # break results = metric.get_name_value() for k, v in results: print("{}: {}".format(k, v))
def evaluate_net(net, path_imgrec, num_classes, num_batch, mean_pixels, data_shape, model_prefix, epoch, ctx=mx.cpu(), batch_size=32, path_imglist="", nms_thresh=0.45, force_nms=False, ovp_thresh=0.5, use_difficult=False, class_names=None, voc07_metric=False, lite=False): """ evalute network given validation record file Parameters: ---------- net : str or None Network name or use None to load from json without modifying path_imgrec : str path to the record validation file path_imglist : str path to the list file to replace labels in record file, optional num_classes : int number of classes, not including background mean_pixels : tuple (mean_r, mean_g, mean_b) data_shape : tuple or int (3, height, width) or height/width model_prefix : str model prefix of saved checkpoint epoch : int load model epoch ctx : mx.ctx mx.gpu() or mx.cpu() batch_size : int validation batch size nms_thresh : float non-maximum suppression threshold force_nms : boolean whether suppress different class objects ovp_thresh : float AP overlap threshold for true/false postives use_difficult : boolean whether to use difficult objects in evaluation if applicable class_names : comma separated str class names in string, must correspond to num_classes if set voc07_metric : boolean whether to use 11-point evluation as in VOC07 competition """ # set up logger logging.basicConfig() logger = logging.getLogger() logger.setLevel(logging.INFO) # args if isinstance(data_shape, int): data_shape = (3, data_shape, data_shape) assert len(data_shape) == 3 and data_shape[0] == 3 model_prefix += '_' + str(data_shape[1]) + '_' + str(data_shape[2]) # iterator eval_iter = DetRecordIter(path_imgrec, batch_size, data_shape, mean_pixels=mean_pixels, path_imglist=path_imglist, **cfg.valid) # model params load_net, args, auxs = mx.model.load_checkpoint(model_prefix, epoch) # network if net is None: net = load_net else: net = get_symbol(net, data_shape, num_classes=num_classes, nms_thresh=nms_thresh, force_suppress=force_nms, lite=lite) if not 'label' in net.list_arguments(): label = mx.sym.Variable(name='label') net = mx.sym.Group([net, label]) # init module mod = mx.mod.Module(net, label_names=('label', ), logger=logger, context=ctx, fixed_param_names=net.list_arguments()) mod.bind(data_shapes=eval_iter.provide_data, label_shapes=eval_iter.provide_label) mod.set_params(args, auxs, allow_missing=False, force_init=True) # run evaluation if voc07_metric: metric = VOC07MApMetric(ovp_thresh, use_difficult, class_names) else: metric = MApMetric(ovp_thresh, use_difficult, class_names) num = num_batch * batch_size data = [ mx.random.uniform(-1.0, 1.0, shape=shape, ctx=ctx) for _, shape in mod.data_shapes ] batch = mx.io.DataBatch(data, []) # empty label dry_run = 5 # use 5 iterations to warm up for i in range(dry_run): mod.forward(batch, is_train=False) for output in mod.get_outputs(): output.wait_to_read() tic = time.time() results = mod.score(eval_iter, metric, num_batch=None, batch_end_callback=mx.callback.Speedometer( batch_size, frequent=10, auto_reset=False)) speed = num / (time.time() - tic) if logger is not None: logger.info('Finished inference with %d images' % num) logger.info('Finished with %f images per second', speed) for k, v in results: print("{}: {}".format(k, v))
import os import numpy as np import cv2 from timeit import default_timer as timer import mxnet as mx from symbol.symbol_factory import get_symbol from collections import namedtuple Batch = namedtuple('Batch', ['data']) data_shape = 300 net = get_symbol('vgg16_reduced', data_shape, num_classes=20, nms_thresh=0.5, force_nms=True, nms_topk=400) #detector = Detector(net, '/home/dingkou/dev/ylb_det/model/ssd_vgg16_reduced_300', 108, 300, (123,117,104), ctx=mx.gpu()) #exit() sym, args, auxs = mx.model.load_checkpoint('./model/ssd_vgg16_reduced_300', 108) mod = mx.mod.Module(net, label_names=None, context=mx.gpu()) mod.bind(data_shapes=[('data', (1, 3, data_shape, data_shape))]) mod.set_params(args, auxs, allow_extra=True) testdir = '/mnt/6B133E147DED759E/tmp/ylb/zp' imgfiles = [i for i in os.listdir(testdir) if i.endswith('.jpg')]
'conv11_depthwise', 'conv11_pointwise', 'conv12_depthwise', 'conv12_pointwise', 'conv13_depthwise', 'conv13_pointwise', 'conv14_depthwise', 'conv14_pointwise' ] bn_prefixes = [ 'batchnorm0', 'batchnorm1', 'batchnorm2', 'batchnorm3', 'batchnorm4', 'batchnorm5', 'batchnorm6', 'batchnorm7', 'batchnorm8', 'batchnorm9', 'batchnorm10', 'batchnorm11', 'batchnorm12', 'batchnorm13', 'batchnorm14', 'batchnorm15', 'batchnorm16', 'batchnorm17', 'batchnorm18', 'batchnorm19', 'batchnorm20', 'batchnorm21', 'batchnorm22', 'batchnorm23', 'batchnorm24', 'batchnorm25', 'batchnorm26' ] for k, v in args.items(): print k # construct no bn symbol nobn_sym = get_symbol(network, data_shape, num_classes=num_class, nms_thresh=nms_thresh, force_nms=force_nms, nms_topk=nms_topk) for i in xrange(len(conv_names)): conv_name = conv_names[i] bn_prefix = bn_prefixes[i] merge_bn(args, auxs, conv_name, bn_prefix) mx.model.save_checkpoint('mergebn', 0, nobn_sym, args, auxs)
parser.add_argument( '--topk', dest='nms_topk', type=int, default=400, help='apply nms only to top k detections based on scores.') args = parser.parse_args() return args if __name__ == '__main__': args = parse_args() net = get_symbol(args.network, args.data_shape, num_classes=args.num_classes, nms_thresh=args.nms_thresh, threshold=args.threshold, background_id=args.background_id, force_suppress=args.force_nms, nms_topk=args.nms_topk) if args.prefix.endswith('_'): prefix = args.prefix + args.network + '_' + str(args.data_shape) else: prefix = args.prefix _, arg_params, aux_params = mx.model.load_checkpoint(prefix, args.epoch) if args.save_path is None: tmp = prefix.rsplit('/', 1) save_prefix = '/deploy_'.join(tmp) mx.model.save_checkpoint(save_prefix, args.epoch, net, arg_params, aux_params) print("Saved model: {}-{:04d}.params".format(save_prefix, args.epoch)) print("Saved symbol: {}-symbol.json".format(save_prefix))
supported_image_shapes = [300, 512] supported_networks = ['vgg16_reduced', 'inceptionv3', 'resnet50'] if network not in supported_networks: raise Exception(network + " is not supported") if image_shape not in supported_image_shapes: raise Exception("Image shape should be either 300*300 or 512*512!") if b == 0: batch_sizes = [1, 2, 4, 8, 16, 32] else: batch_sizes = [b] data_shape = (3, image_shape, image_shape) net = get_symbol(network, data_shape[1], num_classes=num_classes, nms_thresh=0.4, force_suppress=True) num_batches = 100 dry_run = 5 # use 5 iterations to warm up for bs in batch_sizes: batch = get_data(bs) mod = mx.mod.Module(net, label_names=None, context=mx.cpu()) mod.bind(for_training = False, inputs_need_grad = False, data_shapes = get_data_shapes(bs)) mod.init_params(initializer=mx.init.Xavier(magnitude=2.)) # get data data = [mx.random.uniform(-1.0, 1.0, shape=shape, ctx=mx.cpu()) for _, shape in mod.data_shapes] batch = mx.io.DataBatch(data, [])
help='set image shape') parser.add_argument('--num-class', dest='num_classes', help='number of classes', default=20, type=int) parser.add_argument('--nms', dest='nms_thresh', type=float, default=0.5, help='non-maximum suppression threshold, default 0.5') parser.add_argument('--no-force', dest='force_nms', action='store_false', help='dont force non-maximum suppression on different class') parser.add_argument('--topk', dest='nms_topk', type=int, default=400, help='apply nms only to top k detections based on scores.') parser.add_argument('--lite', dest='lite', action='store_true', help='use SSDLite') args = parser.parse_args() return args if __name__ == '__main__': args = parse_args() net = get_symbol(args.network, (3, args.data_shape_height, args.data_shape_width), num_classes=args.num_classes, nms_thresh=args.nms_thresh, force_suppress=args.force_nms, nms_topk=args.nms_topk, lite=args.lite) if args.prefix.endswith('_'): prefix = args.prefix + args.network + '_' + str(args.data_shape_height) + '_' + str(args.data_shape_width) else: prefix = args.prefix _, arg_params, aux_params = mx.model.load_checkpoint(prefix, args.epoch) # new name tmp = prefix.rsplit('/', 1) save_prefix = '/deploy_'.join(tmp) mx.model.save_checkpoint(save_prefix, args.epoch, net, arg_params, aux_params) print("Saved model: {}-{:04d}.params".format(save_prefix, args.epoch)) print("Saved symbol: {}-symbol.json".format(save_prefix))
def merge_conv_bn(old_checkpoint, old_epoch, new_checkpoint): sym, args, auxs = mx.model.load_checkpoint(old_checkpoint, int(old_epoch)) #_, sym_outputs, _ = sym.infer_shape(data=(1,3,224,224)) data_shape = 300 sym_deploy = get_symbol('mobilenet_deploy', data_shape, num_classes=1, nms_thresh=0.5, force_nms=True, nms_topk=400) #sym_deploy = get_symbol(sym_outputs[0][1]) args_deploy = {} auxs_deploy = {} graph = json.loads(sym.tojson()) merge_dict = {} for i, n in enumerate(graph['nodes']): if n['op'] == 'BatchNorm': pre_layer = graph['nodes'][n['inputs'][0][0]] if pre_layer['op'] == 'Convolution': merge_dict[pre_layer['name']] = i #n['name'] for i, n in enumerate(graph['nodes']): if n['op'] == 'Convolution': if merge_dict.has_key(n['name']): bn = graph['nodes'][merge_dict[n['name']]] gamma = args[bn['name'] + '_gamma'] beta = args[bn['name'] + '_beta'] moving_mean = auxs[bn['name'] + '_moving_mean'] moving_var = auxs[bn['name'] + '_moving_var'] eps = float(bn['attr']['eps']) weight = args[n['name'] + '_weight'] if not n['attr'].has_key( 'no_bias') or n['attr']['no_bias'] == 'False': bias = args[n['name'] + '_bias'] else: bias = mx.nd.zeros((weight.shape[0], )) a = gamma / mx.nd.sqrt(moving_var + eps) b = beta - a * moving_mean a = mx.nd.reshape(a, (-1, 1, 1, 1)) weight = weight * a bias = bias + b args_deploy[n['name'] + '_weight'] = weight args_deploy[n['name'] + '_bias'] = bias else: args_deploy[n['name'] + '_weight'] = args[n['name'] + '_weight'] if not n['attr'].has_key( 'no_bias') or n['attr']['no_bias'] == 'False': args_deploy[n['name'] + '_bias'] = args[n['name'] + '_bias'] elif n['op'] == 'FullyConnected': args_deploy[n['name'] + '_weight'] = args[n['name'] + '_weight'] if not n['attr'].has_key( 'no_bias') or n['attr']['no_bias'] == 'False': args_deploy[n['name'] + '_bias'] = args[n['name'] + '_bias'] model_deploy = mx.mod.Module(symbol=sym_deploy, data_names=['data'], label_names=None) model_deploy.bind(data_shapes=[('data', (1, 3, data_shape, data_shape))], for_training=False) model_deploy.set_params(arg_params=args_deploy, aux_params=auxs_deploy, allow_missing=True) model_deploy.save_checkpoint(new_checkpoint, 0)
parser.add_argument('--network', type=str, default='vgg16_reduced', help='the cnn to use') parser.add_argument('--num-classes', type=int, default=20, help='the number of classes') parser.add_argument('--data-shape', type=int, default=300, help='set image\'s shape') parser.add_argument('--train', action='store_true', default=False, help='show train net') args = parser.parse_args() if not args.train: net = symbol_factory.get_symbol(args.network, args.data_shape, num_classes=args.num_classes) a = mx.viz.plot_network(net, shape={"data":(1,3,args.data_shape,args.data_shape)}, \ node_attrs={"shape":'rect', "fixedsize":'false'}) a.render("ssd_" + args.network + '_' + str(args.data_shape)) else: net = symbol_factory.get_symbol_train(args.network, args.data_shape, num_classes=args.num_classes) print(net.tojson())
def evaluate_net(net, path_imgrec, num_classes, mean_pixels, data_shape, model_prefix, epoch, ctx=mx.cpu(), batch_size=1, path_imglist="", nms_thresh=0.45, force_nms=False, ovp_thresh=0.5, use_difficult=False, class_names=None, voc07_metric=False): """ evalute network given validation record file Parameters: ---------- net : str or None Network name or use None to load from json without modifying path_imgrec : str path to the record validation file path_imglist : str path to the list file to replace labels in record file, optional num_classes : int number of classes, not including background mean_pixels : tuple (mean_r, mean_g, mean_b) data_shape : tuple or int (3, height, width) or height/width model_prefix : str model prefix of saved checkpoint epoch : int load model epoch ctx : mx.ctx mx.gpu() or mx.cpu() batch_size : int validation batch size nms_thresh : float non-maximum suppression threshold force_nms : boolean whether suppress different class objects ovp_thresh : float AP overlap threshold for true/false postives use_difficult : boolean whether to use difficult objects in evaluation if applicable class_names : comma separated str class names in string, must correspond to num_classes if set voc07_metric : boolean whether to use 11-point evluation as in VOC07 competition """ # set up logger logging.basicConfig() logger = logging.getLogger() logger.setLevel(logging.INFO) # args if isinstance(data_shape, int): data_shape = (3, data_shape, data_shape) assert len(data_shape) == 3 and data_shape[0] == 3 model_prefix += '_' + str(data_shape[1]) # iterator eval_iter = DetRecordIter(path_imgrec, batch_size, data_shape, mean_pixels=mean_pixels, path_imglist=path_imglist, **cfg.valid) # model params load_net, args, auxs = mx.model.load_checkpoint(model_prefix, epoch) # network if net is None: net = load_net else: net = get_symbol(net, data_shape[1], num_classes=num_classes, nms_thresh=nms_thresh, force_suppress=force_nms) if not 'label' in net.list_arguments(): label = mx.sym.Variable(name='label') net = mx.sym.Group([net, label]) # init module mod = mx.mod.Module(net, label_names=('label', ), logger=logger, context=ctx, fixed_param_names=net.list_arguments()) mod.bind(data_shapes=eval_iter.provide_data, label_shapes=eval_iter.provide_label) mod.set_params(args, auxs, allow_missing=False, force_init=True) # run evaluation if voc07_metric: metric = VOC07MApMetric(ovp_thresh, use_difficult, class_names) else: metric = MApMetric(ovp_thresh, use_difficult, class_names) results = mod.score(eval_iter, metric, num_batch=None) for k, v in results: print("{}: {}".format(k, v))
def evaluate_net(net, path_imgrec, num_classes, mean_img, data_shape, model_prefix, epoch, path_img, ctx=mx.cpu(), batch_size=1, path_imglist="", nms_thresh=0.45, force_nms=False, ovp_thresh=0.5, use_difficult=False, class_names=None, voc07_metric=False): """ evalute network given validation record file Parameters: ---------- net : str or None Network name or use None to load from json without modifying path_imgrec : str path to the record validation file path_imglist : str path to the list file to replace labels in record file, optional num_classes : int number of classes, not including background mean_pixels : tuple (mean_r, mean_g, mean_b) data_shape : tuple or int (3, height, width) or height/width model_prefix : str model prefix of saved checkpoint epoch : int load model epoch ctx : mx.ctx mx.gpu() or mx.cpu() batch_size : int validation batch size nms_thresh : float non-maximum suppression threshold force_nms : boolean whether suppress different class objects ovp_thresh : float AP overlap threshold for true/false postives use_difficult : boolean whether to use difficult objects in evaluation if applicable class_names : comma separated str class names in string, must correspond to num_classes if set voc07_metric : boolean whether to use 11-point evluation as in VOC07 competition """ # set up logger logging.basicConfig() logger = logging.getLogger() logger.setLevel(logging.INFO) # args if isinstance(data_shape, int): data_shape = (3, data_shape, data_shape) assert len(data_shape) == 3 and data_shape[0] == 3 model_prefix += '_' + str(data_shape[1]) netname = net # iterator eval_iter = DetRecordIter(path_imgrec, batch_size, data_shape, mean_img=mean_img, path_imglist=path_imglist, **cfg.valid) # model params load_net, args, auxs = mx.model.load_checkpoint(model_prefix, epoch) # network if net is None: net = load_net else: net = get_symbol(net, data_shape[1], num_classes=num_classes, nms_thresh=nms_thresh, force_suppress=force_nms) if not 'label' in net.list_arguments(): label = mx.sym.Variable(name='label') net = mx.sym.Group([net, label]) # init module mod = mx.mod.Module(net, label_names=('label',), logger=logger, context=ctx, fixed_param_names=net.list_arguments()) mod.bind(data_shapes=eval_iter.provide_data, label_shapes=eval_iter.provide_label) mod.set_params(args, auxs, allow_missing=False, force_init=True) # # run evaluation # if voc07_metric: # metric = VOC07MApMetric(ovp_thresh, use_difficult, class_names) # else: # metric = MApMetric(ovp_thresh, use_difficult, class_names) # results = mod.score(eval_iter, metric, num_batch=None) # for k, v in results: # print("{}: {}".format(k, v)) predict_results = mod.predict(eval_iter, merge_batches = True) preds = predict_results[0] labels = predict_results[1] (flags, ious) = find_wrong_detection.find_wrong_detection(labels, preds, path_imglist, path_img, ovp_thresh = ovp_thresh) flags_dict = {0:'correct', 1:'lower iou', 2:'wrong class'} flag_count = Counter(flags) for flag in set(flags): print ("%s image number is : %d"%(flags_dict[flag], flag_count[flag])) print ("recall is %f"%((len(flags)-flag_count[1])/float(len(flags)))) if not os.path.exists('./model/iou_distribution'): os.mkdir('./model/iou_distribution') xmin = min(ious) - 0.1 if min(ious) > 0.1 else 0 xmax = max(ious) + 0.1 if max(ious) < 0.9 else 1 draw_hist(ious, "iou distribution", "iou", "image number", xmin, xmax, 0, len(ious)/20, netname)
'w') fhead = open('/opt/incubator-mxnet/example/ctc/car_head_train.txt', 'w') # iterator list_path = args.list_path img_path = args.img_path network = args.network num_classes = args.num_class # model params load_net, model_args, model_auxs = mx.model.load_checkpoint( model_prefix, args.epoch) net = get_symbol(network, data_shape[1], num_classes=num_classes, nms_thresh=0.45, force_suppress=True) if not 'label' in net.list_arguments(): label = mx.sym.Variable(name='label') net = mx.sym.Group([net, label]) mod = mx.mod.Module(net, label_names=('label', ), context=ctx, fixed_param_names=net.list_arguments()) mod.bind(for_training=False, data_shapes=[('data', (1, 3, data_shape[1], data_shape[1]))], label_shapes=[('label', (1, 1, 6))]) mod.set_params(model_args, model_auxs,
def evaluate_net(net, path_imgrec, num_classes, num_batch, mean_pixels, data_shape, model_prefix, epoch, ctx=mx.cpu(), batch_size=32, path_imglist="", nms_thresh=0.45, force_nms=False, ovp_thresh=0.5, use_difficult=False, class_names=None, voc07_metric=False): """ evalute network given validation record file Parameters: ---------- net : str or None Network name or use None to load from json without modifying path_imgrec : str path to the record validation file path_imglist : str path to the list file to replace labels in record file, optional num_classes : int number of classes, not including background mean_pixels : tuple (mean_r, mean_g, mean_b) data_shape : tuple or int (3, height, width) or height/width model_prefix : str model prefix of saved checkpoint epoch : int load model epoch ctx : mx.ctx mx.gpu() or mx.cpu() batch_size : int validation batch size nms_thresh : float non-maximum suppression threshold force_nms : boolean whether suppress different class objects ovp_thresh : float AP overlap threshold for true/false postives use_difficult : boolean whether to use difficult objects in evaluation if applicable class_names : comma separated str class names in string, must correspond to num_classes if set voc07_metric : boolean whether to use 11-point evluation as in VOC07 competition """ # set up logger logging.basicConfig() logger = logging.getLogger() logger.setLevel(logging.INFO) # args if isinstance(data_shape, int): data_shape = (3, data_shape, data_shape) assert len(data_shape) == 3 and data_shape[0] == 3 model_prefix += '_' + str(data_shape[1]) # iterator eval_iter = DetRecordIter(path_imgrec, batch_size, data_shape, mean_pixels=mean_pixels, path_imglist=path_imglist, **cfg.valid) # model params load_net, args, auxs = mx.model.load_checkpoint(model_prefix, epoch) # network if net is None: net = load_net else: net = get_symbol(net, data_shape[1], num_classes=num_classes, nms_thresh=nms_thresh, force_suppress=force_nms) if not 'label' in net.list_arguments(): label = mx.sym.Variable(name='label') net = mx.sym.Group([net, label]) # init module mod = mx.mod.Module(net, label_names=('label',), logger=logger, context=ctx, fixed_param_names=net.list_arguments()) mod.bind(data_shapes=eval_iter.provide_data, label_shapes=eval_iter.provide_label) mod.set_params(args, auxs, allow_missing=False, force_init=True) # run evaluation if voc07_metric: metric = VOC07MApMetric(ovp_thresh, use_difficult, class_names) else: metric = MApMetric(ovp_thresh, use_difficult, class_names) num = num_batch * batch_size data = [mx.random.uniform(-1.0, 1.0, shape=shape, ctx=ctx) for _, shape in mod.data_shapes] batch = mx.io.DataBatch(data, []) # empty label dry_run = 5 # use 5 iterations to warm up for i in range(dry_run): mod.forward(batch, is_train=False) for output in mod.get_outputs(): output.wait_to_read() tic = time.time() results = mod.score(eval_iter, metric, num_batch=num_batch) speed = num / (time.time() - tic) if logger is not None: logger.info('Finished inference with %d images' % num) logger.info('Finished with %f images per second', speed) for k, v in results: print("{}: {}".format(k, v))