def main(): args = parser.parse_args() weights_pose = torch.load(args.pretrained_posenet) pose_net = models.PoseResNet().to(device) pose_net.load_state_dict(weights_pose['state_dict'], strict=False) pose_net.eval() image_dir = Path(args.dataset_dir + args.sequence + "/image_2/") output_dir = Path(args.output_dir) output_dir.makedirs_p() test_files = sum( [image_dir.files('*.{}'.format(ext)) for ext in args.img_exts], []) test_files.sort() print('{} files to test'.format(len(test_files))) print(test_files) global_pose = np.eye(4) poses = [global_pose[0:3, :].reshape(1, 12)] n = len(test_files) tensor_img1 = load_tensor_image(test_files[0], args) for iter in tqdm(range(n - 1)): tensor_img2 = load_tensor_image(test_files[iter + 1], args) pose = pose_net(tensor_img1, tensor_img2) pose_mat = pose_vec2mat(pose).squeeze(0).cpu().numpy() pose_mat = np.vstack([pose_mat, np.array([0, 0, 0, 1])]) global_pose = global_pose @ np.linalg.inv(pose_mat) poses.append(global_pose[0:3, :].reshape(1, 12)) # update tensor_img1 = tensor_img2 poses = np.concatenate(poses, axis=0) filename = Path(args.output_dir + args.sequence + ".txt") np.savetxt(filename, poses, delimiter=' ', fmt='%1.8e')
def main(): args = parser.parse_args() weights_pose = torch.load(args.pretrained_posenet) pose_net = models.PoseResNet().to(device) pose_net.load_state_dict(weights_pose['state_dict'], strict=False) pose_net.eval() output_dir = Path(args.output_dir) output_dir.makedirs_p() test_files = [args.tensor_img_1, args.tensor_img_2] print('{} files to test'.format(len(test_files))) print(device) #print(test_files) global_pose = np.eye(4) poses = [global_pose[0:3, :].reshape(1, 12)] n = len(test_files) vo_single_pkg.estimate_pose(pose_net, test_files, args, device)
def main(): args = parser.parse_args() from kitti_eval.pose_evaluation_utils import test_framework_KITTI as test_framework weights = torch.load(args.pretrained_posenet) seq_length = 5 pose_net = models.PoseResNet(18, False).to(device) pose_net.load_state_dict(weights['state_dict'], strict=False) dataset_dir = Path(args.dataset_dir) framework = test_framework(dataset_dir, args.sequences, seq_length) print('{} snippets to test'.format(len(framework))) errors = np.zeros((len(framework), 2), np.float32) if args.output_dir is not None: output_dir = Path(args.output_dir) output_dir.makedirs_p() predictions_array = np.zeros((len(framework), seq_length, 3, 4)) for j, sample in enumerate(tqdm(framework)): imgs = sample['imgs'] h, w, _ = imgs[0].shape if (not args.no_resize) and (h != args.img_height or w != args.img_width): imgs = [ imresize(img, (args.img_height, args.img_width)).astype(np.float32) for img in imgs ] imgs = [np.transpose(img, (2, 0, 1)) for img in imgs] squence_imgs = [] for i, img in enumerate(imgs): img = torch.from_numpy(img).unsqueeze(0) img = ((img / 255 - 0.45) / 0.225).to(device) squence_imgs.append(img) global_pose = np.eye(4) poses = [] poses.append(global_pose[0:3, :]) for iter in range(seq_length - 1): pose = pose_net(squence_imgs[iter], squence_imgs[iter + 1]) pose_mat = pose_vec2mat(pose).squeeze(0).cpu().numpy() pose_mat = np.vstack([pose_mat, np.array([0, 0, 0, 1])]) global_pose = global_pose @ np.linalg.inv(pose_mat) poses.append(global_pose[0:3, :]) final_poses = np.stack(poses, axis=0) if args.output_dir is not None: predictions_array[j] = final_poses ATE, RE = compute_pose_error(sample['poses'], final_poses) errors[j] = ATE, RE mean_errors = errors.mean(0) std_errors = errors.std(0) error_names = ['ATE', 'RE'] print('') print("Results") print("\t {:>10}, {:>10}".format(*error_names)) print("mean \t {:10.4f}, {:10.4f}".format(*mean_errors)) print("std \t {:10.4f}, {:10.4f}".format(*std_errors)) if args.output_dir is not None: np.save(output_dir / 'predictions.npy', predictions_array)
def main(): global best_error, n_iter, device args = parser.parse_args() timestamp = datetime.datetime.now().strftime("%m-%d-%H:%M") save_path = Path(args.name) args.save_path = 'checkpoints' / save_path / timestamp print('=> will save everything to {}'.format(args.save_path)) args.save_path.makedirs_p() torch.manual_seed(args.seed) np.random.seed(args.seed) cudnn.deterministic = True cudnn.benchmark = True training_writer = SummaryWriter(args.save_path) output_writers = [] if args.log_output: for i in range(3): output_writers.append( SummaryWriter(args.save_path / 'valid' / str(i))) # Data loading code normalize = custom_transforms.Normalize(mean=[0.45, 0.45, 0.45], std=[0.225, 0.225, 0.225]) train_transform = custom_transforms.Compose([ custom_transforms.RandomHorizontalFlip(), custom_transforms.RandomScaleCrop(), custom_transforms.ArrayToTensor(), normalize ]) valid_transform = custom_transforms.Compose( [custom_transforms.ArrayToTensor(), normalize]) print("=> fetching scenes in '{}'".format(args.data)) train_set = SequenceFolder(args.data, transform=train_transform, seed=args.seed, train=True, sequence_length=args.sequence_length) # if no Groundtruth is avalaible, Validation set is the same type as training set to measure photometric loss from warping if args.with_gt: from datasets.validation_folders import ValidationSet val_set = ValidationSet(args.data, transform=valid_transform) else: val_set = SequenceFolder(args.data, transform=valid_transform, seed=args.seed, train=False, sequence_length=args.sequence_length) print('{} samples found in {} train scenes'.format(len(train_set), len(train_set.scenes))) print('{} samples found in {} valid scenes'.format(len(val_set), len(val_set.scenes))) train_loader = torch.utils.data.DataLoader(train_set, batch_size=args.batch_size, shuffle=True, num_workers=args.workers, pin_memory=True) val_loader = torch.utils.data.DataLoader(val_set, batch_size=args.batch_size, shuffle=False, num_workers=args.workers, pin_memory=True) if args.epoch_size == 0: args.epoch_size = len(train_loader) # create model print("=> creating model") disp_net = models.DispResNet(args.resnet_layers, args.with_pretrain).to(device) pose_net = models.PoseResNet(18, args.with_pretrain).to(device) # load parameters if args.pretrained_disp: print("=> using pre-trained weights for DispResNet") weights = torch.load(args.pretrained_disp) disp_net.load_state_dict(weights['state_dict'], strict=False) if args.pretrained_pose: print("=> using pre-trained weights for PoseResNet") weights = torch.load(args.pretrained_pose) pose_net.load_state_dict(weights['state_dict'], strict=False) disp_net = torch.nn.DataParallel(disp_net) pose_net = torch.nn.DataParallel(pose_net) print('=> setting adam solver') optim_params = [{ 'params': disp_net.parameters(), 'lr': args.lr }, { 'params': pose_net.parameters(), 'lr': args.lr }] optimizer = torch.optim.Adam(optim_params, betas=(args.momentum, args.beta), weight_decay=args.weight_decay) with open(args.save_path / args.log_summary, 'w') as csvfile: writer = csv.writer(csvfile, delimiter='\t') writer.writerow(['train_loss', 'validation_loss']) with open(args.save_path / args.log_full, 'w') as csvfile: writer = csv.writer(csvfile, delimiter='\t') writer.writerow([ 'train_loss', 'photo_loss', 'smooth_loss', 'geometry_consistency_loss' ]) logger = TermLogger(n_epochs=args.epochs, train_size=min(len(train_loader), args.epoch_size), valid_size=len(val_loader)) logger.epoch_bar.start() for epoch in range(args.epochs): logger.epoch_bar.update(epoch) # train for one epoch logger.reset_train_bar() train_loss = train(args, train_loader, disp_net, pose_net, optimizer, args.epoch_size, logger, training_writer) logger.train_writer.write(' * Avg Loss : {:.3f}'.format(train_loss)) # evaluate on validation set logger.reset_valid_bar() if args.with_gt: errors, error_names = validate_with_gt(args, val_loader, disp_net, epoch, logger, output_writers) else: errors, error_names = validate_without_gt(args, val_loader, disp_net, pose_net, epoch, logger, output_writers) error_string = ', '.join('{} : {:.3f}'.format(name, error) for name, error in zip(error_names, errors)) logger.valid_writer.write(' * Avg {}'.format(error_string)) for error, name in zip(errors, error_names): training_writer.add_scalar(name, error, epoch) # Up to you to chose the most relevant error to measure your model's performance, careful some measures are to maximize (such as a1,a2,a3) decisive_error = errors[1] if best_error < 0: best_error = decisive_error # remember lowest error and save checkpoint is_best = decisive_error < best_error best_error = min(best_error, decisive_error) save_checkpoint(args.save_path, { 'epoch': epoch + 1, 'state_dict': disp_net.module.state_dict() }, { 'epoch': epoch + 1, 'state_dict': pose_net.module.state_dict() }, is_best) with open(args.save_path / args.log_summary, 'a') as csvfile: writer = csv.writer(csvfile, delimiter='\t') writer.writerow([train_loss, decisive_error]) logger.epoch_bar.finish()
def main(): args = parser.parse_args() weights_pose = torch.load(args.pretrained_posenet) pose_net = models.PoseResNet().to(device) pose_net.load_state_dict(weights_pose['state_dict'], strict=False) pose_net.eval() image_dir = Path(args.dataset_dir) output_dir = Path(args.output_dir) output_dir.makedirs_p() test_files = sum( [image_dir.files('*.{}'.format(ext)) for ext in args.img_exts], []) test_files.sort() print('{} files to test'.format(len(test_files))) #print(test_files) global_pose = np.eye(4) if 'advio-04' in args.dataset_dir: global_pose = np.array([[ 0.2337503561460601, 0.71124984004749991, -0.66293618387207998, 0.0037387620000000001 ], [ 0.10487759004941999, 0.65940272844750003, 0.74443851971943986, 0.082704390000000003 ], [ 0.96662371684119996, -0.24353990138999998, 0.079541459813400106, -0.023607989999999999 ], [0.0, 0.0, 0.0, 1.0]]) # advio-04 elif 'advio-23' in args.dataset_dir: global_pose = np.array([[ -0.02024017794777988, -0.7758527702428, 0.630589204478, -0.005759389 ], [0.11342629147720007, 0.62486446287, 0.7724499038534, -0.09049783], [ -0.993340194646, 0.08715994761339996, 0.0753547505262201, -0.001374606 ], [0.0, 0.0, 0.0, 1.0]]) # advio-23 print(str(global_pose)) poses = [global_pose[0:3, :].reshape(1, 12)] n = len(test_files) tensor_img1 = load_tensor_image(test_files[0], args) for iter in tqdm(range(n - 1)): tensor_img2 = load_tensor_image(test_files[iter + 1], args) pose = pose_net(tensor_img1, tensor_img2) pose_mat = pose_vec2mat(pose).squeeze(0).cpu().numpy() pose_mat = np.vstack([pose_mat, np.array([0, 0, 0, 1])]) global_pose = global_pose @ np.linalg.inv(pose_mat) poses.append(global_pose[0:3, :].reshape(1, 12)) # update tensor_img1 = tensor_img2 poses = np.concatenate(poses, axis=0) filename = Path(args.output_dir + args.sequence + ".txt") np.savetxt(filename, poses, delimiter=' ', fmt='%1.8e')