def initialize_model_from_cfg(): def create_input_blobs(net_def): for op in net_def.op: for blob_in in op.input: if not workspace.HasBlob(blob_in): workspace.CreateBlob(blob_in) model = model_builder.create( cfg.MODEL.TYPE, train=False, init_params=cfg.TEST.INIT_RANDOM_VARS_BEFORE_LOADING) model_builder.add_inputs(model) if cfg.TEST.INIT_RANDOM_VARS_BEFORE_LOADING: workspace.RunNetOnce(model.param_init_net) net_utils.initialize_from_weights_file(model, cfg.TEST.WEIGHTS, broadcast=False) create_input_blobs(model.net.Proto()) workspace.CreateNet(model.net) workspace.CreateNet(model.conv_body_net) if cfg.MODEL.MASK_ON: create_input_blobs(model.mask_net.Proto()) workspace.CreateNet(model.mask_net) if cfg.MODEL.KEYPOINTS_ON: create_input_blobs(model.keypoint_net.Proto()) workspace.CreateNet(model.keypoint_net) return model
def test_retinanet(ind_range=None): """ Test RetinaNet model either on the entire dataset or the subset of dataset specified by the index range """ assert cfg.RETINANET.RETINANET_ON, \ 'RETINANET_ON must be set for testing RetinaNet model' output_dir = get_output_dir(training=False) dataset = JsonDataset(cfg.TEST.DATASET) roidb = dataset.get_roidb() if ind_range is not None: start, end = ind_range roidb = roidb[start:end] # Create and load the model model = model_builder.create(cfg.MODEL.TYPE, train=False) if cfg.TEST.WEIGHTS: nu.initialize_from_weights_file( model, cfg.TEST.WEIGHTS, broadcast=False ) model_builder.add_inference_inputs(model) workspace.CreateNet(model.net) # Compute the detections all_boxes = im_list_detections(model, roidb) # Save the detections cfg_yaml = yaml.dump(cfg) if ind_range is not None: det_name = 'detection_range_%s_%s.pkl' % tuple(ind_range) else: det_name = 'detections.pkl' det_file = os.path.join(output_dir, det_name) save_object( dict(all_boxes=all_boxes, cfg=cfg_yaml), det_file) logger.info('Wrote detections to: {}'.format(os.path.abspath(det_file))) return all_boxes
def initialize_model_from_cfg(): def create_input_blobs(net_def): for op in net_def.op: for blob_in in op.input: if not workspace.HasBlob(blob_in): workspace.CreateBlob(blob_in) model = model_builder.create( cfg.MODEL.TYPE, train=False, init_params=cfg.TEST.INIT_RANDOM_VARS_BEFORE_LOADING) model_builder.add_inputs(model) if cfg.TEST.INIT_RANDOM_VARS_BEFORE_LOADING: workspace.RunNetOnce(model.param_init_net) net_utils.initialize_from_weights_file( model, cfg.TEST.WEIGHTS, broadcast=False) create_input_blobs(model.net.Proto()) workspace.CreateNet(model.net) workspace.CreateNet(model.conv_body_net) if cfg.MODEL.MASK_ON: create_input_blobs(model.mask_net.Proto()) workspace.CreateNet(model.mask_net) if cfg.MODEL.KEYPOINTS_ON: create_input_blobs(model.keypoint_net.Proto()) workspace.CreateNet(model.keypoint_net) return model
def generate_rpn_on_range(ind_range=None): assert cfg.TEST.WEIGHTS != '', \ 'TEST.WEIGHTS must be set to the model file to test' assert cfg.TEST.DATASET != '', \ 'TEST.DATASET must be set to the dataset name to test' assert cfg.MODEL.RPN_ONLY or cfg.MODEL.FASTER_RCNN im_list, start_ind, end_ind, total_num_images = get_image_list(ind_range) output_dir = get_output_dir(training=False) logger.info( 'Output will be saved to: {:s}'.format(os.path.abspath(output_dir))) model = model_builder.create(cfg.MODEL.TYPE, train=False) model_builder.add_inputs(model) nu.initialize_from_weights_file(model, cfg.TEST.WEIGHTS) workspace.CreateNet(model.net) boxes, scores, ids = im_list_proposals( model, im_list, start_ind=start_ind, end_ind=end_ind, total_num_images=total_num_images) cfg_yaml = yaml.dump(cfg) if ind_range is not None: rpn_name = 'rpn_proposals_range_%s_%s.pkl' % tuple(ind_range) else: rpn_name = 'rpn_proposals.pkl' rpn_file = os.path.join(output_dir, rpn_name) robust_pickle_dump( dict(boxes=boxes, scores=scores, ids=ids, cfg=cfg_yaml), rpn_file) logger.info('Wrote RPN proposals to {}'.format(os.path.abspath(rpn_file))) return boxes, scores, ids, rpn_file
def initialize_model_from_cfg(): """Initialize a model from the global cfg. Loads test-time weights and creates the networks in the Caffe2 workspace. """ model = model_builder.create(cfg.MODEL.TYPE, train=False) net_utils.initialize_from_weights_file( model, cfg.TEST.WEIGHTS, broadcast=False ) model_builder.add_inference_inputs(model) workspace.CreateNet(model.net) workspace.CreateNet(model.conv_body_net) if cfg.MODEL.MASK_ON: workspace.CreateNet(model.mask_net) if cfg.MODEL.KEYPOINTS_ON: workspace.CreateNet(model.keypoint_net) return model
def initialize_model_from_cfg(): """Initialize a model from the global cfg. Loads test-time weights and creates the networks in the Caffe2 workspace. """ model = model_builder.create(cfg.MODEL.TYPE, train=False) net_utils.initialize_from_weights_file( model, cfg.TEST.WEIGHTS, broadcast=False ) model_builder.add_inference_inputs(model) workspace.CreateNet(model.net) workspace.CreateNet(model.conv_body_net) if cfg.MODEL.MASK_ON: workspace.CreateNet(model.mask_net) if cfg.MODEL.KEYPOINTS_ON: workspace.CreateNet(model.keypoint_net) return model
def test_retinanet(ind_range=None): """ Test RetinaNet model either on the entire dataset or the subset of dataset specified by the index range """ assert cfg.RETINANET.RETINANET_ON, \ 'RETINANET_ON must be set for testing RetinaNet model' output_dir = get_output_dir(training=False) dataset = JsonDataset(cfg.TEST.DATASET) im_list = dataset.get_roidb() if ind_range is not None: start, end = ind_range im_list = im_list[start:end] logger.info('Testing on roidb range: {}-{}'.format(start, end)) else: # if testing over the whole dataset, use the NUM_TEST_IMAGES setting # the NUM_TEST_IMAGES could be over a small set of images for quick # debugging purposes im_list = im_list[0:cfg.TEST.NUM_TEST_IMAGES] model = model_builder.create(cfg.MODEL.TYPE, train=False) if cfg.TEST.WEIGHTS: nu.initialize_from_weights_file(model, cfg.TEST.WEIGHTS, broadcast=False) model_builder.add_inference_inputs(model) workspace.CreateNet(model.net) boxes, scores, classes, image_ids = im_list_detections( model, im_list[0:cfg.TEST.NUM_TEST_IMAGES]) cfg_yaml = yaml.dump(cfg) if ind_range is not None: det_name = 'retinanet_detections_range_%s_%s.pkl' % tuple(ind_range) else: det_name = 'retinanet_detections.pkl' det_file = os.path.join(output_dir, det_name) save_object( dict(boxes=boxes, scores=scores, classes=classes, ids=image_ids, cfg=cfg_yaml), det_file) logger.info('Wrote detections to: {}'.format(os.path.abspath(det_file))) return boxes, scores, classes, image_ids
def test_retinanet(ind_range=None): """ Test RetinaNet model either on the entire dataset or the subset of dataset specified by the index range """ assert cfg.RETINANET.RETINANET_ON, \ 'RETINANET_ON must be set for testing RetinaNet model' output_dir = get_output_dir(training=False) dataset = JsonDataset(cfg.TEST.DATASET) im_list = dataset.get_roidb() if ind_range is not None: start, end = ind_range im_list = im_list[start:end] logger.info('Testing on roidb range: {}-{}'.format(start, end)) else: # if testing over the whole dataset, use the NUM_TEST_IMAGES setting # the NUM_TEST_IMAGES could be over a small set of images for quick # debugging purposes im_list = im_list[0:cfg.TEST.NUM_TEST_IMAGES] model = model_builder.create(cfg.MODEL.TYPE, train=False) if cfg.TEST.WEIGHTS: nu.initialize_from_weights_file( model, cfg.TEST.WEIGHTS, broadcast=False ) model_builder.add_inference_inputs(model) workspace.CreateNet(model.net) boxes, scores, classes, image_ids = im_list_detections( model, im_list[0:cfg.TEST.NUM_TEST_IMAGES]) cfg_yaml = yaml.dump(cfg) if ind_range is not None: det_name = 'retinanet_detections_range_%s_%s.pkl' % tuple(ind_range) else: det_name = 'retinanet_detections.pkl' det_file = os.path.join(output_dir, det_name) save_object( dict(boxes=boxes, scores=scores, classes=classes, ids=image_ids, cfg=cfg_yaml), det_file) logger.info('Wrote detections to: {}'.format(os.path.abspath(det_file))) return boxes, scores, classes, image_ids
def generate_rpn_on_range(ind_range=None): """Run inference on all images in a dataset or over an index range of images in a dataset using a single GPU. """ assert cfg.TEST.WEIGHTS != '', \ 'TEST.WEIGHTS must be set to the model file to test' assert cfg.TEST.DATASET != '', \ 'TEST.DATASET must be set to the dataset name to test' assert cfg.MODEL.RPN_ONLY or cfg.MODEL.FASTER_RCNN roidb, start_ind, end_ind, total_num_images = get_roidb(ind_range) output_dir = get_output_dir(training=False) logger.info( 'Output will be saved to: {:s}'.format(os.path.abspath(output_dir)) ) model = model_builder.create(cfg.MODEL.TYPE, train=False) nu.initialize_from_weights_file(model, cfg.TEST.WEIGHTS) model_builder.add_inference_inputs(model) workspace.CreateNet(model.net) boxes, scores, ids = generate_proposals_on_roidb( model, roidb, start_ind=start_ind, end_ind=end_ind, total_num_images=total_num_images ) cfg_yaml = yaml.dump(cfg) if ind_range is not None: rpn_name = 'rpn_proposals_range_%s_%s.pkl' % tuple(ind_range) else: rpn_name = 'rpn_proposals.pkl' rpn_file = os.path.join(output_dir, rpn_name) save_object( dict(boxes=boxes, scores=scores, ids=ids, cfg=cfg_yaml), rpn_file ) logger.info('Wrote RPN proposals to {}'.format(os.path.abspath(rpn_file))) return boxes, scores, ids, rpn_file