def main(): args = parser.parse_args() with open(args.config) as f: config = yaml.load(f) for k, v in config['common'].items(): setattr(args, k, v) test_transforms = transforms.Compose([d_utils.PointcloudToTensor()]) test_dataset = ModelNet40Cls(num_points=args.num_points, root=args.data_root, transforms=test_transforms, train=False) test_dataloader = DataLoader(test_dataset, batch_size=args.batch_size, shuffle=False, num_workers=int(args.workers), pin_memory=True) model = DensePoint(num_classes=args.num_classes, input_channels=args.input_channels, use_xyz=True) model.cuda() if args.checkpoint is not '': model.load_state_dict(torch.load(args.checkpoint)) print('Load model successfully: %s' % (args.checkpoint)) # evaluate PointcloudScale = d_utils.PointcloudScale() # initialize random scaling model.eval() global_acc = 0 for i in range(NUM_REPEAT): preds = [] labels = [] s = time.time() for j, data in enumerate(test_dataloader, 0): points, target = data points, target = points.cuda(), target.cuda() points, target = Variable(points, volatile=True), Variable(target, volatile=True) # points [batch_size, num_points, dimensions], e.g., [256, 2048, 3] # furthest point sampling # fps_idx = pointnet2_utils.furthest_point_sample(points, 1200) # (B, npoint) # random sampling fps_idx = np.random.randint(0, points.shape[1] - 1, size=[points.shape[0], 1200]) fps_idx = torch.from_numpy(fps_idx).type(torch.IntTensor).cuda() pred = 0 for v in range(NUM_VOTE): new_fps_idx = fps_idx[:, np.random.choice(1200, args. num_points, False)] new_points = pointnet2_utils.gather_operation( points.transpose(1, 2).contiguous(), new_fps_idx).transpose(1, 2).contiguous() if v > 0: new_points.data = PointcloudScale(new_points.data) pred += F.softmax(model(new_points), dim=1) pred /= NUM_VOTE target = target.view(-1) _, pred_choice = torch.max(pred.data, -1) preds.append(pred_choice) labels.append(target.data) e = time.time() preds = torch.cat(preds, 0) labels = torch.cat(labels, 0) acc = (preds == labels).sum() / labels.numel() if acc > global_acc: global_acc = acc print('Repeat %3d \t Acc: %0.6f' % (i + 1, acc)) print('time (secs) for 1 epoch: ', (e - s)) print('\nBest voting acc: %0.6f' % (global_acc))
parser.add_argument('--visdom-port', type=int, default=8097) return parser.parse_args() lr_clip = 1e-5 bnm_clip = 1e-2 if __name__ == "__main__": args = parse_args() BASE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data') transforms = transforms.Compose([ d_utils.PointcloudToTensor(), d_utils.PointcloudScale(), d_utils.PointcloudRotate(), d_utils.PointcloudRotatePerturbation(), d_utils.PointcloudTranslate(), d_utils.PointcloudJitter(), d_utils.PointcloudRandomInputDropout() ]) test_set = ModelNet40Cls(args.num_points, BASE_DIR, transforms=transforms, train=False) test_loader = DataLoader(test_set, batch_size=args.batch_size, shuffle=True, num_workers=2,
def main(): args = parser.parse_args() with open(args.config) as f: config = yaml.load(f) for k, v in config['common'].items(): setattr(args, k, v) test_transforms = transforms.Compose([ d_utils.PointcloudToTensor() ]) test_dataset = ShapeNetPart(root = args.data_root, num_points = args.num_points, split = 'test', normalize = True, transforms = test_transforms) test_dataloader = DataLoader( test_dataset, batch_size=args.batch_size, shuffle=False, num_workers=int(args.workers), pin_memory=True ) model = RSCNN_MSN(num_classes = args.num_classes, input_channels = args.input_channels, relation_prior = args.relation_prior, use_xyz = True) model.cuda() if args.checkpoint is not '': model.load_state_dict(torch.load(args.checkpoint)) print('Load model successfully: %s' % (args.checkpoint)) # evaluate PointcloudScale = d_utils.PointcloudScale(scale_low=0.87, scale_high=1.15) # initialize random scaling model.eval() global_Class_mIoU, global_Inst_mIoU = 0, 0 seg_classes = test_dataset.seg_classes seg_label_to_cat = {} # {0:Airplane, 1:Airplane, ...49:Table} for cat in seg_classes.keys(): for label in seg_classes[cat]: seg_label_to_cat[label] = cat for i in range(NUM_REPEAT): shape_ious = {cat:[] for cat in seg_classes.keys()} for _, data in enumerate(test_dataloader, 0): points, target, cls = data points, target = Variable(points, volatile=True), Variable(target, volatile=True) points, target = points.cuda(), target.cuda() batch_one_hot_cls = np.zeros((len(cls), 16)) # 16 object classes for b in range(len(cls)): batch_one_hot_cls[b, int(cls[b])] = 1 batch_one_hot_cls = torch.from_numpy(batch_one_hot_cls) batch_one_hot_cls = Variable(batch_one_hot_cls.float().cuda()) pred = 0 new_points = Variable(torch.zeros(points.size()[0], points.size()[1], points.size()[2]).cuda(), volatile=True) for v in range(NUM_VOTE): if v > 0: new_points.data = PointcloudScale(points.data) pred += F.softmax(model(new_points, batch_one_hot_cls), dim = 2) pred /= NUM_VOTE pred = pred.data.cpu() target = target.data.cpu() pred_val = torch.zeros(len(cls), args.num_points).type(torch.LongTensor) # pred to the groundtruth classes (selected by seg_classes[cat]) for b in range(len(cls)): cat = seg_label_to_cat[target[b, 0]] logits = pred[b, :, :] # (num_points, num_classes) pred_val[b, :] = logits[:, seg_classes[cat]].max(1)[1] + seg_classes[cat][0] for b in range(len(cls)): segp = pred_val[b, :] segl = target[b, :] cat = seg_label_to_cat[segl[0]] part_ious = [0.0 for _ in range(len(seg_classes[cat]))] for l in seg_classes[cat]: if torch.sum((segl == l) | (segp == l)) == 0: # part is not present in this shape part_ious[l - seg_classes[cat][0]] = 1.0 else: part_ious[l - seg_classes[cat][0]] = torch.sum((segl == l) & (segp == l)) / float(torch.sum((segl == l) | (segp == l))) shape_ious[cat].append(np.mean(part_ious)) instance_ious = [] for cat in shape_ious.keys(): for iou in shape_ious[cat]: instance_ious.append(iou) shape_ious[cat] = np.mean(shape_ious[cat]) mean_class_ious = np.mean(list(shape_ious.values())) print('\n------ Repeat %3d ------' % (i + 1)) for cat in sorted(shape_ious.keys()): print('%s: %0.6f'%(cat, shape_ious[cat])) print('Class_mIoU: %0.6f' % (mean_class_ious)) print('Instance_mIoU: %0.6f' % (np.mean(instance_ious))) if mean_class_ious > global_Class_mIoU: global_Class_mIoU = mean_class_ious global_Inst_mIoU = np.mean(instance_ious) print('\nBest voting Class_mIoU = %0.6f, Instance_mIoU = %0.6f' % (global_Class_mIoU, global_Inst_mIoU))
def main(): args = parser.parse_args() with open(args.config) as f: config = yaml.load(f) for k, v in config['common'].items(): setattr(args, k, v) test_transforms = transforms.Compose([d_utils.PointcloudToTensor()]) test_dataset = ModelNet40Cls(num_points=args.num_points, root=args.data_root, transforms=test_transforms, train=False) test_dataloader = DataLoader(test_dataset, batch_size=args.batch_size, shuffle=False, num_workers=int(args.workers), pin_memory=False) model = RSCNN_SSN(num_classes=args.num_classes, input_channels=args.input_channels, relation_prior=args.relation_prior, use_xyz=True) # for multi GPU device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") if torch.cuda.is_available() and torch.cuda.device_count() >= 2: model = nn.DataParallel(model, device_ids=[0, 1]) model.to(device) elif torch.cuda.is_available() and torch.cuda.device_count() == 1: model.cuda() if args.checkpoint is not '': model.load_state_dict(torch.load(args.checkpoint)) print('Load model successfully: %s' % (args.checkpoint)) # evaluate PointcloudScale = d_utils.PointcloudScale() # initialize random scaling model.eval() global_acc = 0 for i in range(NUM_REPEAT): preds = [] labels = [] for j, data in enumerate(test_dataloader, 0): points, target = data points, target = points.cuda(), target.cuda() points, target = Variable(points, volatile=True), Variable(target, volatile=True) # fastest point sampling fps_idx = pointnet2_utils.furthest_point_sample( points, 1200) # (B, npoint) pred = 0 for v in range(NUM_VOTE): new_fps_idx = fps_idx[:, np.random.choice(1200, args. num_points, False)] new_points = pointnet2_utils.gather_operation( points.transpose(1, 2).contiguous(), new_fps_idx).transpose(1, 2).contiguous() if v > 0: new_points.data = PointcloudScale(new_points.data) pred += F.softmax(model(new_points), dim=1) pred /= NUM_VOTE target = target.view(-1) _, pred_choice = torch.max(pred.data, -1) preds.append(pred_choice) labels.append(target.data) preds = torch.cat(preds, 0) labels = torch.cat(labels, 0) acc = (preds == labels).sum() / labels.numel() if acc > global_acc: global_acc = acc print('Repeat %3d \t Acc: %0.6f' % (i + 1, acc)) print('\nBest voting acc: %0.6f' % (global_acc))