コード例 #1
0
def main():
    args = parse_args()
    num_gpus = torch.cuda.device_count()

    cfg = load_cfg_from_file(args.config_file)
    cfg.merge_from_list(args.opts)
    cfg.freeze()
    assert cfg.TEST.BATCH_SIZE == 1

    isCPU = args.cpu

    output_dir = cfg.OUTPUT_DIR
    if output_dir:
        config_path = osp.splitext(args.config_file)[0]
        config_path = config_path.replace("configs", "outputs")
        output_dir = output_dir.replace('@', config_path)
        mkdir(output_dir)

    logger = setup_logger("pointmvsnet", output_dir, prefix="test")
    if isCPU:
        logger.info("Using CPU")
    else:
        logger.info("Using {} GPUs".format(num_gpus))
    logger.info(args)

    logger.info("Loaded configuration file {}".format(args.config_file))
    logger.info("Running with config:\n{}".format(cfg))

    test(cfg, output_dir, isCPU=isCPU)
コード例 #2
0
def file_logger(data_batch, preds, step, output_dir, prefix):
    step_dir = osp.join(output_dir, "{}_step{:05d}".format(prefix, step))
    mkdir(step_dir)
    print("start saving files in ", step_dir)

    img_list = data_batch["img_list"]
    batch_size, num_view, img_channel, img_height, img_width = list(
        img_list.size())

    cam_params_list = data_batch["cam_params_list"]

    for i in range(num_view):
        np.savetxt(osp.join(step_dir, "img{}.txt".format(i)),
                   img_list[0, i, 0].detach().cpu().numpy(),
                   fmt="%.4f")
        np.savetxt(osp.join(step_dir, "cam{}_extrinsic.txt".format(i)),
                   cam_params_list[0, i, 0].detach().cpu().numpy(),
                   fmt="%.4f")
        np.savetxt(osp.join(step_dir, "cam{}_intrinsic.txt".format(i)),
                   cam_params_list[0, i, 1].detach().cpu().numpy(),
                   fmt="%.4f")
    np.savetxt(osp.join(step_dir, "gt_depth_img.txt"),
               data_batch["gt_depth_img"][0, 0].detach().cpu().numpy(),
               fmt="%.4f")
    np.savetxt(osp.join(step_dir, "coarse_depth_img.txt"),
               preds["coarse_depth_map"][0, 0].detach().cpu().numpy(),
               fmt="%.4f")

    cam_extrinsic = cam_params_list[0, 0, 0, :3, :4].clone()  # (3, 4)

    cam_intrinsic = cam_params_list[0, 0, 1, :3, :3].clone()

    world_points = preds["world_points"]
    world_points = world_points[0].cpu().numpy().transpose()
    save_points(osp.join(step_dir, "world_points.xyz"), world_points)

    prob_map = preds["coarse_prob_map"][0][0].cpu().numpy()

    coarse_points = depth2pts(preds["coarse_depth_map"], prob_map,
                              cam_intrinsic, cam_extrinsic,
                              (img_height, img_width))
    save_points(osp.join(step_dir, "coarse_point.xyz"), coarse_points)

    gt_points = depth2pts(data_batch["gt_depth_img"], prob_map, cam_intrinsic,
                          cam_extrinsic, (img_height, img_width))
    save_points(osp.join(step_dir, "gt_points.xyz"), gt_points)

    if "flow1" in preds.keys():
        flow1_points = depth2pts(preds["flow1"], prob_map, cam_intrinsic,
                                 cam_extrinsic, (img_height, img_width))
        save_points(osp.join(step_dir, "flow1_points.xyz"), flow1_points)

    if "flow2" in preds.keys():
        flow2_points = depth2pts(preds["flow2"], prob_map, cam_intrinsic,
                                 cam_extrinsic, (img_height, img_width))
        save_points(osp.join(step_dir, "flow2_points.xyz"), flow2_points)

    print("saving finished.")
コード例 #3
0
def eval_file_logger(data_batch, preds, ref_img_path, folder):
    l = ref_img_path.split("/")
    eval_folder = "/".join(l[:-3])

    scene = l[-2]

    scene_folder = osp.join(eval_folder, folder, scene)

    if not osp.isdir(scene_folder):
        mkdir(scene_folder)
        print("**** {} ****".format(scene))

    out_index = int(l[-1][5:8]) - 1

    cam_params_list = data_batch["cam_params_list"].cpu().numpy()

    ref_cam_paras = cam_params_list[0, 0, :, :, :]

    init_depth_map_path = scene_folder + ('/%08d_init.pfm' % out_index)
    init_prob_map_path = scene_folder + ('/%08d_init_prob.pfm' % out_index)
    out_ref_image_path = scene_folder + ('/%08d.jpg' % out_index)

    init_depth_map = preds["coarse_depth_map"].cpu().numpy()[0, 0]
    init_prob_map = preds["coarse_prob_map"].cpu().numpy()[0, 0]
    ref_image = data_batch["ref_img"][0].cpu().numpy()

    write_pfm(init_depth_map_path, init_depth_map)
    write_pfm(init_prob_map_path, init_prob_map)
    cv2.imwrite(out_ref_image_path, ref_image)

    out_init_cam_path = scene_folder + ('/cam_%08d_init.txt' % out_index)
    init_cam_paras = ref_cam_paras.copy()
    init_cam_paras[1, :2, :3] *= (float(init_depth_map.shape[0]) /
                                  ref_image.shape[0])
    write_cam_dtu(out_init_cam_path, init_cam_paras)

    interval_list = np.array([-2.0, -1.0, 0.0, 1.0, 2.0])
    interval_list = np.reshape(interval_list, [1, 1, -1])

    for i, k in enumerate(preds.keys()):
        if "flow" in k:
            if "prob" in k:
                out_flow_prob_map = preds[k][0].cpu().permute(1, 2, 0).numpy()
                num_interval = out_flow_prob_map.shape[-1]
                assert num_interval == interval_list.size
                pred_interval = np.sum(out_flow_prob_map * interval_list,
                                       axis=-1) + 2.0
                pred_floor = np.floor(pred_interval).astype(np.int)[...,
                                                                    np.newaxis]
                pred_ceil = pred_floor + 1
                pred_ceil = np.clip(pred_ceil, 0, num_interval - 1)
                prob_height, prob_width = pred_floor.shape[:2]
                prob_height_ind = np.tile(
                    np.reshape(np.arange(prob_height), [-1, 1, 1]),
                    [1, prob_width, 1])
                prob_width_ind = np.tile(
                    np.reshape(np.arange(prob_width), [1, -1, 1]),
                    [prob_height, 1, 1])
                floor_prob = np.squeeze(
                    out_flow_prob_map[prob_height_ind, prob_width_ind,
                                      pred_floor], -1)
                ceil_prob = np.squeeze(
                    out_flow_prob_map[prob_height_ind, prob_width_ind,
                                      pred_ceil], -1)
                flow_prob = floor_prob + ceil_prob
                flow_prob_map_path = scene_folder + "/{:08d}_{}.pfm".format(
                    out_index, k)
                write_pfm(flow_prob_map_path, flow_prob)

            else:
                out_flow_depth_map = preds[k][0, 0].cpu().numpy()
                flow_depth_map_path = scene_folder + "/{:08d}_{}.pfm".format(
                    out_index, k)
                write_pfm(flow_depth_map_path, out_flow_depth_map)
                out_flow_cam_path = scene_folder + "/cam_{:08d}_{}.txt".format(
                    out_index, k)
                flow_cam_paras = ref_cam_paras.copy()
                flow_cam_paras[1, :2, :3] *= (
                    float(out_flow_depth_map.shape[0]) /
                    float(ref_image.shape[0]))
                write_cam_dtu(out_flow_cam_path, flow_cam_paras)

                world_pts = depth2pts_np(out_flow_depth_map,
                                         flow_cam_paras[1][:3, :3],
                                         flow_cam_paras[0])
                save_points(
                    osp.join(scene_folder,
                             "{:08d}_{}pts.xyz".format(out_index, k)),
                    world_pts)