Ejemplo n.º 1
0
def parse_args():
    parser = config_parser()
    parser.add_argument('--metadatadir', type=str,
                        default='data/nerf_synthetic/metacubes/',
                        help="path of directory containing all scene " +
                        "directories, each containing posed RGB images ")
    parser.add_argument('--pretrained', help='evaluate a pre-trained model',
                        action='store_true', default=False)
    parser.add_argument('--seed', help='random seed', default=0, type=int)
    # parser.add_argument(
    #     '--checkpoint', help='checkpoint directory', default='model_checkpoint')
    parser.add_argument(
        '--shots', help='number of examples per class', default=5, type=int)
    parser.add_argument(
        '--train-shots', help='shots in a training batch', default=0, type=int)
    parser.add_argument(
        '--inner-batch', help='inner batch size', default=5, type=int)
    parser.add_argument(
        '--inner-iters', help='inner iterations', default=50, type=int)
    parser.add_argument(
        '--replacement', help='sample with replacement', action='store_true')
    parser.add_argument('--learning-rate',
                        help='Inner loop step size', default=1e-3, type=float)
    parser.add_argument(
        '--meta-step', help='meta-training step size', default=0.1, type=float)
    parser.add_argument('--meta-step-final', help='meta-training step size by the end',
                        default=0.1, type=float)
    parser.add_argument(
        '--meta-batch', help='meta-training batch size', default=8, type=int)
    parser.add_argument(
        '--meta-iters', help='meta-training iterations', default=400000, type=int)
    parser.add_argument('--eval-interval',
                        help='train steps per eval', default=10, type=int)
    parser.add_argument(
        '--weight-decay', help='weight decay rate', default=1, type=float)
    parser.add_argument(
        '--transductive', help='evaluate all samples at once', action='store_true')
    parser.add_argument(
        '--foml', help='use FOML instead of Reptile', action='store_true')
    parser.add_argument('--foml-tail', help='number of shots for the final mini-batch in FOML',
                        default=None, type=int)
    parser.add_argument(
        '--sgd', help='use vanilla SGD instead of Adam', action='store_true')
    return parser.parse_args()
Ejemplo n.º 2
0
def get_data():
    basedir = './logs'
    expname = 'fern_example'

    config = os.path.join(basedir, expname, 'config.txt')
    print('Args:')
    print(open(config, 'r').read())
    parser = run_nerf.config_parser()

    weights_name = 'model_200000.npy'
    args = parser.parse_args('--config {} --ft_path {}'.format(config, os.path.join(basedir, expname, weights_name)))
    print('loaded args')

    images, poses, bds, render_poses, i_test = load_llff_data(args.datadir, args.factor, 
                                                            recenter=True, bd_factor=.75, 
                                                            spherify=args.spherify)
    H, W, focal = poses[0,:3,-1].astype(np.float32)
    poses = poses[:, :3, :4]
    H = int(H)
    W = int(W)
    hwf = [H, W, focal]

    images = images.astype(np.float32)
    poses = poses.astype(np.float32)

    near = 0.
    far = 1.

    if not isinstance(i_test, list):
        i_test = [i_test]

    if args.llffhold > 0:
        print('Auto LLFF holdout,', args.llffhold)
        i_test = np.arange(images.shape[0])[::args.llffhold]

    _, render_kwargs, start, grad_vars, models = run_nerf.create_nerf(args)

    bds_dict = {
        'near' : tf.cast(near, tf.float32),
        'far' : tf.cast(far, tf.float32),
    }
    render_kwargs.update(bds_dict)

    print('Render kwargs:')
    pprint.pprint(render_kwargs)

    results = {}
    results['pc'] = {}
    results['no_pc'] = {}

    # NOTE: Where to output results!
    result_directory = "./fern_pc_results"
    img_dir = os.path.join(result_directory, "imgs")

    down = 1

    plt.imsave(os.path.join(img_dir, f"GT{i_test[0]}.png"), images[i_test[0]])
    plt.imsave(os.path.join(img_dir, f"GT{i_test[1]}.png"), images[i_test[1]])

    for num_samps in [4,8,16,32,64]:
        print(f'Running {num_samps} sample test')
        for pc in [True, False]:
            print(f'{"not " if not pc else ""}using pc')
            results['pc' if pc else 'no_pc'][num_samps] = {}
            render_kwargs['N_samples'] = num_samps
            render_kwargs['N_importance'] = 2*num_samps

            total_time = 0
            total_mse = 0
            total_psnr = 0
            for i in [i_test[0], i_test[1]]:
                gt = images[i]
                start_time = time.time()
                ret_vals = run_nerf.render(H//down, W//down, focal/down, c2w=poses[i], pc=pc, cloudsize=16, **render_kwargs)
                end_time = time.time()
                
                # add to cum time
                total_time += (end_time - start_time)
                
                # add to accuracy
                img = np.clip(ret_vals[0],0,1)
                # TODO: make sure this is commented out for real results (just used to test that it runs)
                # mse = run_nerf.img2mse(np.zeros((H//down, W//down,3), dtype=np.float32), img)
                mse = run_nerf.img2mse(gt, img)
                psnr = run_nerf.mse2psnr(mse)
                total_mse += float(mse)
                total_psnr += float(psnr)

                plt.imsave(os.path.join(img_dir, f'IMG{i}_{"pc" if pc else "no_pc"}_{num_samps}samples.png'), img)

            total_time /= 2.
            total_mse /= 2.
            total_psnr /= 2.
            results['pc' if pc else 'no_pc'][num_samps]['time'] = total_time
            results['pc' if pc else 'no_pc'][num_samps]['mse'] = total_mse
            results['pc' if pc else 'no_pc'][num_samps]['psnr'] = total_psnr

    with open(os.path.join(result_directory, 'results.txt'), 'w') as outfile:
        json.dump(results,outfile)
Ejemplo n.º 3
0
def cloud_size_vs_performance():
    basedir = './logs'
    expname = 'fern_example'

    config = os.path.join(basedir, expname, 'config.txt')
    print('Args:')
    print(open(config, 'r').read())
    parser = run_nerf.config_parser()

    weights_name = 'model_200000.npy'
    args = parser.parse_args('--config {} --ft_path {}'.format(config, os.path.join(basedir, expname, weights_name)))
    print('loaded args')

    images, poses, bds, render_poses, i_test = load_llff_data(args.datadir, args.factor, 
                                                            recenter=True, bd_factor=.75, 
                                                            spherify=args.spherify)
    H, W, focal = poses[0,:3,-1].astype(np.float32)
    poses = poses[:, :3, :4]
    H = int(H)
    W = int(W)
    hwf = [H, W, focal]

    images = images.astype(np.float32)
    poses = poses.astype(np.float32)

    near = 0.
    far = 1.

    if not isinstance(i_test, list):
        i_test = [i_test]

    if args.llffhold > 0:
        print('Auto LLFF holdout,', args.llffhold)
        i_test = np.arange(images.shape[0])[::args.llffhold]

    _, render_kwargs, start, grad_vars, models = run_nerf.create_nerf(args)
    to_use = i_test[0]

    bds_dict = {
        'near' : tf.cast(near, tf.float32),
        'far' : tf.cast(far, tf.float32),
    }
    render_kwargs.update(bds_dict)

    print('Render kwargs:')
    pprint.pprint(render_kwargs)

    res_dir = "./cloud_size_test"

    res = {}
    res['cloud_size'] = []
    res['mse'] = []
    res['psnr'] = []
    res['time'] = []

    for i in [1,2,4,8,16,32]:
        print(f'Running with cloud downsampled {i}x')
        start_time = time.time()
        ret_vals = run_nerf.render(H, W, focal, c2w=poses[to_use], pc=True, cloudsize=i, **render_kwargs)
        end_time = time.time()
        img = np.clip(ret_vals[0],0,1)
        mse = run_nerf.img2mse(images[to_use], img)
        psnr = run_nerf.mse2psnr(mse)
        res['cloud_size'].append((17 * H * W) // (i * i))
        res['mse'].append(float(mse))
        res['psnr'].append(float(psnr))
        res['time'].append(end_time - start_time)

    # a = [1,2,4,8,16,32]
    # b = [1/x for x in a]

    # make plots
    # cs vs psnr
    fig, ax = plt.subplots(1,1)
    fig.suptitle('PSNR vs Point Cloud Size')
    ax.set_xlabel('Cloud Size')
    ax.set_ylabel('PSNR')
    plt.xscale('log')
    ax.plot(res['cloud_size'],res['psnr'])
    plt.savefig(os.path.join(res_dir, 'cs_psnr.png'))

    fig, ax = plt.subplots(1,1)
    fig.suptitle('PSNR vs Running Time')
    ax.set_xlabel('Time')
    ax.set_ylabel('PSNR')
    plt.xscale('log')
    ax.plot(res['time'],res['psnr'])
    plt.savefig(os.path.join(res_dir, 'time_psnr.png'))

    fig, ax = plt.subplots(1,1)
    fig.suptitle('Running Time vs Cloud Size')
    ax.set_xlabel('Cloud Size')
    ax.set_ylabel('Running Time')
    plt.xscale('log')
    plt.yscale('log')
    ax.plot(res['cloud_size'],res['time'])
    plt.savefig(os.path.join(res_dir, 'cs_time.png'))
    
    with open(os.path.join(res_dir, 'results.txt'), 'w') as outfile:
        json.dump(res,outfile)
Ejemplo n.º 4
0
from load_llff import load_llff_data
from load_deepvoxels import load_dv_data
from load_blender import load_blender_data


# In[2]:


basedir = './logs'
# expname = 'fern_example'
expname = 'fern_test'

config = os.path.join(basedir, expname, 'config.txt')
print('Args:')
print(open(config, 'r').read())
parser = run_nerf.config_parser()

# VERY IMPORTANT:
print("Keep in mind the model number of iterations has changed from 200000 to 40000.")
n_iterations = 40000
n_iterations = str(n_iterations).zfill(6)
args = parser.parse_args('--config {} --ft_path {}'.format(config, os.path.join(basedir, expname, f'model_{n_iterations}.npy')))
print('loaded args')

images, poses, bds, render_poses, i_test = load_llff_data(args.datadir, args.factor, 
                                                          recenter=True, bd_factor=.75, 
                                                          spherify=args.spherify)
H, W, focal = poses[0,:3,-1].astype(np.float32)

H = int(H)
W = int(W)