def do_evaluate_unlabeled(pred_config, output_file, reuse=True): """Evaluate unlabled data.""" for i, dataset in enumerate(cfg.DATA.VAL): output = output_file + '-' + dataset if not os.path.isfile(output) or not reuse: if i == 0: num_tower = max(cfg.TRAIN.NUM_GPUS, 1) graph_funcs = MultiTowerOfflinePredictor( pred_config, list(range(num_tower))).get_predictors() logger.info('Evaluating {} ...'.format(dataset)) dataflows = [ get_eval_dataflow(dataset, shard=k, num_shards=num_tower) for k in range(num_tower) ] all_results = multithread_predict_dataflow(dataflows, graph_funcs) eval_metrics = DatasetRegistry.get( dataset).eval_inference_results2( all_results, output, threshold=cfg.TRAIN.CONFIDENCE) else: all_results = json.load(open(output, 'r')) eval_metrics = DatasetRegistry.get( dataset).eval_inference_results2( all_results, output, threshold=cfg.TRAIN.CONFIDENCE, metric_only=True) with open(output + '_cocometric.json', 'w') as f: json.dump(eval_metrics, f)
def do_evaluate(pred_config, output_file): num_tower = max(cfg.TRAIN.NUM_GPUS, 1) graph_funcs = MultiTowerOfflinePredictor( pred_config, list(range(num_tower))).get_predictors() for dataset in cfg.DATA.VAL: logger.info("Evaluating {} ...".format(dataset)) dataflows = [ get_eval_dataflow(dataset, shard=k, num_shards=num_tower) for k in range(num_tower)] all_results = multithread_predict_dataflow(dataflows, graph_funcs) output = output_file + '-' + dataset DatasetRegistry.get(dataset).eval_inference_results(all_results, output)
def do_evaluate(pred_config, output_file, batch_size): ''' Multi-gpu evaluation, if available ''' num_tower = max(cfg.TRAIN.NUM_GPUS, 1) graph_funcs = MultiTowerOfflinePredictor(pred_config, list( range(num_tower))).get_predictors() dataflows = [ get_eval_dataflow(batch_size, shard=k, num_shards=num_tower) for k in range(num_tower) ] all_results = multithread_pred_dataflow(dataflows, graph_funcs) # df = get_eval_dataflow() # all_results = pred_dataflow(df, lambda img: detect_batch(img, pred_func)) logger.info('Dumping evaluation results') np.savez(output_file, **all_results) return print_evaluation_scores(output_file)
def predict_unlabeled(model, model_path, nr_visualize=100, output_dir='output_patch_samples'): """Predict the pseudo label information of unlabeled data.""" assert cfg.EVAL.PSEUDO_INFERENCE, 'set cfg.EVAL.PSEUDO_INFERENCE=True' df, dataset_size = get_eval_unlabeled_dataflow(cfg.DATA.TRAIN, return_size=True) df.reset_state() predcfg = PredictConfig( model=model, session_init=SmartInit(model_path), input_names=['image'], # ['image', 'gt_boxes', 'gt_labels'], output_names=[ 'generate_{}_proposals/boxes'.format( 'fpn' if cfg.MODE_FPN else 'rpn'), 'generate_{}_proposals/scores'.format( 'fpn' if cfg.MODE_FPN else 'rpn'), 'fastrcnn_all_scores', 'output/boxes', 'output/scores', # score of the labels 'output/labels', ]) pred = OfflinePredictor(predcfg) if os.path.isdir(output_dir): if os.path.isfile(os.path.join(output_dir, 'pseudo_data.npy')): os.remove(os.path.join(output_dir, 'pseudo_data.npy')) if not os.path.isdir(os.path.join(output_dir, 'vis')): os.makedirs(os.path.join(output_dir, 'vis')) else: shutil.rmtree(os.path.join(output_dir, 'vis')) fs.mkdir_p(output_dir + '/vis') else: fs.mkdir_p(output_dir) fs.mkdir_p(output_dir + '/vis') logger.warning('-' * 100) logger.warning('Write to {}'.format(output_dir)) logger.warning('-' * 100) with tqdm.tqdm(total=nr_visualize) as pbar: for idx, dp in itertools.islice(enumerate(df), nr_visualize): img, img_id = dp # dp['image'], dp['img_id'] rpn_boxes, rpn_scores, all_scores, \ final_boxes, final_scores, final_labels = pred(img) outs = { 'proposals_boxes': rpn_boxes, # (?,4) 'proposals_scores': rpn_scores, # (?,) 'boxes': final_boxes, 'scores': final_scores, 'labels': final_labels } ratios = [10, 10] # [top 20% as background, bottom 20% as background] bg_ind, fg_ind = custom.find_bg_and_fg_proposals(all_scores, ratios=ratios) bg_viz = draw_predictions(img, rpn_boxes[bg_ind], all_scores[bg_ind]) fg_viz = draw_predictions(img, rpn_boxes[fg_ind], all_scores[fg_ind]) results = [ DetectionResult(*args) for args in zip(final_boxes, final_scores, final_labels, [None] * len(final_labels)) ] final_viz = draw_final_outputs(img, results) viz = tpviz.stack_patches([bg_viz, fg_viz, final_viz], 2, 2) if os.environ.get('DISPLAY', None): tpviz.interactive_imshow(viz) assert cv2.imwrite('{}/vis/{:03d}.png'.format(output_dir, idx), viz) pbar.update() logger.info('Write {} samples to {}'.format(nr_visualize, output_dir)) ## Parallel inference the whole unlabled data pseudo_preds = collections.defaultdict(list) num_tower = max(cfg.TRAIN.NUM_GPUS, 1) graph_funcs = MultiTowerOfflinePredictor(predcfg, list( range(num_tower))).get_predictors() dataflows = [ get_eval_unlabeled_dataflow(cfg.DATA.TRAIN, shard=k, num_shards=num_tower) for k in range(num_tower) ] all_results = multithread_predict_dataflow(dataflows, graph_funcs) for id, result in tqdm.tqdm(enumerate(all_results)): img_id = result['image_id'] outs = { 'proposals_boxes': result['proposal_box'].astype(np.float16), # (?,4) 'proposals_scores': result['proposal_score'].astype(np.float16), # (?,) # 'frcnn_all_scores': result['frcnn_score'].astype(np.float16), 'boxes': result['bbox'].astype(np.float16), # (?,4) 'scores': result['score'].astype(np.float16), # (?,) 'labels': result['category_id'].astype(np.float16) # (?,) } pseudo_preds[img_id] = outs logger.warn('Writing to {}'.format( os.path.join(output_dir, 'pseudo_data.npy'))) try: dd.io.save(os.path.join(output_dir, 'pseudo_data.npy'), pseudo_preds) except RuntimeError: logger.error('Save failed. Check reasons manually...')