def evaluate_one_epoch(): stat_dict = {} ap_calculator = APCalculator(ap_iou_thresh=FLAGS.ap_iou_thresh, class2type_map=DATASET_CONFIG.class2type) ap_calculator_l = APCalculator(ap_iou_thresh=FLAGS.ap_iou_thresh * 2, class2type_map=DATASET_CONFIG.class2type) net.eval() # set model to eval mode (for bn and dp) for batch_idx, batch_data_label in enumerate(TEST_DATALOADER): end_points = {} if batch_idx % 10 == 0: print('Eval batch: %d' % (batch_idx)) for key in batch_data_label: batch_data_label[key] = batch_data_label[key].to(device) # Forward pass inputs = {'point_clouds': batch_data_label['point_clouds']} with torch.no_grad(): end_points = net(inputs, end_points) # Compute loss for key in batch_data_label: end_points[key] = batch_data_label[key] loss, end_points = criterion(inputs, end_points, DATASET_CONFIG) # Accumulate statistics and print out for key in end_points: if 'loss' in key or 'acc' in key or 'ratio' in key: if key not in stat_dict: stat_dict[key] = 0 stat_dict[key] += end_points[key].item() batch_pred_map_cls = parse_predictions( end_points, CONFIG_DICT, opt_ang=(FLAGS.dataset == 'sunrgbd')) batch_gt_map_cls = parse_groundtruths(end_points, CONFIG_DICT) ap_calculator.step(batch_pred_map_cls, batch_gt_map_cls) batch_pred_map_cls = parse_predictions( end_points, CONFIG_DICT_L, opt_ang=(FLAGS.dataset == 'sunrgbd')) batch_gt_map_cls = parse_groundtruths(end_points, CONFIG_DICT_L) ap_calculator_l.step(batch_pred_map_cls, batch_gt_map_cls) if FLAGS.dump_results: dump_results(end_points, DUMP_DIR + '/result/', DATASET_CONFIG, TEST_DATASET) # Log statistics for key in sorted(stat_dict.keys()): log_string('eval mean %s: %f' % (key, stat_dict[key] / (float(batch_idx + 1)))) metrics_dict = ap_calculator.compute_metrics() for key in metrics_dict: log_string('iou = 0.25, eval %s: %f' % (key, metrics_dict[key])) metrics_dict = ap_calculator_l.compute_metrics() for key in metrics_dict: log_string('iou = 0.5, eval %s: %f' % (key, metrics_dict[key])) mean_loss = stat_dict['loss'] / float(batch_idx + 1) return mean_loss
if __name__ == '__main__': sys.path.append(os.path.join(ROOT_DIR, 'sunrgbd')) from sunrgbd_detection_dataset import SunrgbdDetectionVotesDataset, DC # Define dataset TRAIN_DATASET = SunrgbdDetectionVotesDataset('train', num_points=20000, use_v1=True) # Define model model = BoxNet(10, 12, 10, np.random.random((10, 3))).cuda() # Model forward pass sample = TRAIN_DATASET[5] inputs = { 'point_clouds': torch.from_numpy(sample['point_clouds']).unsqueeze(0).cuda() } end_points = model(inputs) for key in end_points: print(key, end_points[key]) # Compute loss for key in sample: end_points[key] = torch.from_numpy(sample[key]).unsqueeze(0).cuda() loss, end_points = get_loss(end_points, DC) print('loss', loss) end_points['point_clouds'] = inputs['point_clouds'] end_points['pred_mask'] = np.ones((1, 128)) dump_results(end_points, 'tmp', DC)
def evaluate_one_epoch(): stat_dict = {} # collect statistics ap_calculator = APCalculator(ap_iou_thresh=FLAGS.ap_iou_thresh, class2type_map=DATASET_CONFIG.class2type) ap_calculator_l = APCalculator(ap_iou_thresh=FLAGS.ap_iou_thresh*2, class2type_map=DATASET_CONFIG.class2type) net.eval() # set model to eval mode (for bn and dp) time_file = 'time_file_%s.txt' % FLAGS.dataset time_file = os.path.join(DUMP_DIR, time_file) f = open(time_file, 'w') all_time = 0 for batch_idx, batch_data_label in enumerate(TEST_DATALOADER): if batch_idx % 10 == 0: print('Eval batch: %d'%(batch_idx)) end_points = {} for key in batch_data_label: batch_data_label[key] = batch_data_label[key].to(device) inputs = {'point_clouds': batch_data_label['point_clouds']} tic = time.time() with torch.no_grad(): end_points = net(inputs, end_points) toc = time.time() t = toc - tic all_time += t f.write('batch_idx:%d, infer time:%f\n' % (batch_idx, t)) print('Inference time: %f'%(t)) # Compute loss for key in batch_data_label: end_points[key] = batch_data_label[key] loss, end_points = criterion(inputs, end_points, DATASET_CONFIG) # Accumulate statistics and print out for key in end_points: if 'loss' in key or 'acc' in key or 'ratio' in key: if key not in stat_dict: stat_dict[key] = 0 stat_dict[key] += end_points[key].item() batch_pred_map_cls = parse_predictions(end_points, CONFIG_DICT, opt_ang=(FLAGS.dataset == 'sunrgbd')) batch_gt_map_cls = parse_groundtruths(end_points, CONFIG_DICT) ap_calculator.step(batch_pred_map_cls, batch_gt_map_cls) batch_pred_map_cls = parse_predictions(end_points, CONFIG_DICT_L, opt_ang=(FLAGS.dataset == 'sunrgbd')) batch_gt_map_cls = parse_groundtruths(end_points, CONFIG_DICT_L) ap_calculator_l.step(batch_pred_map_cls, batch_gt_map_cls) if FLAGS.dump_results: dump_results(end_points, DUMP_DIR+'/result/', DATASET_CONFIG, TEST_DATASET) mean_time = all_time/float(batch_idx+1) f.write('Batch number:%d\n' % (batch_idx+1)) f.write('mean infer time: %f\n' % (mean_time)) f.close() print('Mean inference time: %f'%(mean_time)) # Log statistics TEST_VISUALIZER.log_scalars({key:stat_dict[key]/float(batch_idx+1) for key in stat_dict}, (EPOCH_CNT+1)*len(TRAIN_DATALOADER)*BATCH_SIZE) for key in sorted(stat_dict.keys()): log_string('eval mean %s: %f'%(key, stat_dict[key]/(float(batch_idx+1)))) metrics_dict = ap_calculator.compute_metrics() for key in metrics_dict: log_string('eval %s: %f'%(key, metrics_dict[key])) metrics_dict = ap_calculator_l.compute_metrics() for key in metrics_dict: log_string('eval %s: %f'%(key, metrics_dict[key])) mean_loss = stat_dict['loss']/float(batch_idx+1) return mean_loss
print("type of point cloud", type(sample['point_clouds'])) print("sample shape", sample['point_clouds'].shape) inputs = { 'point_clouds': torch.from_numpy(sample['point_clouds']).unsqueeze(0).cuda() } except: print('Dataset has not been prepared. Use a random sample.') inputs = {'point_clouds': torch.rand((20000, 3)).unsqueeze(0).cuda()} end_points = model(inputs) # for key in end_points: # print(key, end_points[key]) try: # Compute loss # for key in sample: # end_points[key] = torch.from_numpy(sample[key]).unsqueeze(0).cuda() # loss, end_points = get_loss(end_points, DC) # print('loss', loss) end_points['point_clouds'] = inputs['point_clouds'] end_points['pred_mask'] = np.ones((1, 128)) pred_map_cls = parse_predictions(end_points, eval_config_dict) end_points['batch_pred_map_cls'] = pred_map_cls print('Finished detection. %d object detected.' % (len(pred_map_cls[0]))) dump_results(end_points, 'tmp', DC, True) except Exception: print('Dataset has not been prepared. Skip loss and dump.')
# Load checkpoint optimizer = optim.Adam(net.parameters(), lr=0.001) checkpoint = torch.load(checkpoint_path) net.load_state_dict(checkpoint['model_state_dict']) optimizer.load_state_dict(checkpoint['optimizer_state_dict']) epoch = checkpoint['epoch'] print("Loaded checkpoint %s (epoch: %d)"%(checkpoint_path, epoch)) # Load and preprocess input point cloud net.eval() # set model to eval mode (for bn and dp) point_cloud = read_ply(pc_path) pc = preprocess_point_cloud(point_cloud) print('Loaded point cloud data: %s'%(pc_path)) # Model inference inputs = {'point_clouds': torch.from_numpy(pc).to(device)} tic = time.time() with torch.no_grad(): end_points = net(inputs) toc = time.time() print('Inference time: %f'%(toc-tic)) end_points['point_clouds'] = inputs['point_clouds'] pred_map_cls = parse_predictions(end_points, eval_config_dict) end_points['batch_pred_map_cls'] = pred_map_cls print('Finished detection. %d object detected.'%(len(pred_map_cls[0]))) dump_dir = os.path.join(demo_dir, '%s_results'%(FLAGS.dataset)) if not os.path.exists(dump_dir): os.mkdir(dump_dir) dump_results(end_points, dump_dir, DC, True) print('Dumped detection results to folder %s'%(dump_dir))