Skip to content
This repository was archived by the owner on Dec 13, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pyrightconfig.json
**/__pycache__/

1 change: 1 addition & 0 deletions configs/multiblender.gin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Config.batching = 'single_image'
2 changes: 1 addition & 1 deletion eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def render_eval_fn(variables, _, rays):

vis_suite = vis.visualize_suite(pred_distance, pred_acc)

if jax.host_id() != 0: # Only record via host 0.
if jax.process_index() != 0: # Only record via host 0.
continue
if not FLAGS.eval_once and idx == showcase_index:
showcase_color = pred_color
Expand Down
4 changes: 2 additions & 2 deletions internal/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def __init__(self, split, data_dir, config):
raise ValueError(
'the split argument should be either \'train\' or \'test\', set'
'to {} here.'.format(split))
self.batch_size = config.batch_size // jax.host_count()
self.batch_size = config.batch_size // jax.process_count()
self.batching = config.batching
self.render_path = config.render_path
self.start()
Expand Down Expand Up @@ -149,7 +149,7 @@ def _next_train(self):
batch_rays = utils.namedtuple_map(lambda r: r[ray_indices], self.rays)
elif self.batching == 'single_image':
image_index = np.random.randint(0, self.n_examples, ())
ray_indices = np.random.randint(0, self.rays[0][0].shape[0],
ray_indices = np.random.randint(0, self.rays[0][image_index].shape[0],
(self.batch_size,))
batch_pixels = self.images[image_index][ray_indices]
batch_rays = utils.namedtuple_map(lambda r: r[image_index][ray_indices],
Expand Down
4 changes: 2 additions & 2 deletions internal/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def render_image(render_fn, rays, rng, chunk=8192):
num_rays = height * width
rays = utils.namedtuple_map(lambda r: r.reshape((num_rays, -1)), rays)

host_id = jax.host_id()
host_id = jax.process_index()
results = []
for i in range(0, num_rays, chunk):
# pylint: disable=cell-var-from-loop
Expand All @@ -253,7 +253,7 @@ def render_image(render_fn, rays, rng, chunk=8192):
padding = 0
# After padding the number of chunk_rays is always divisible by
# host_count.
rays_per_host = chunk_rays[0].shape[0] // jax.host_count()
rays_per_host = chunk_rays[0].shape[0] // jax.process_count()
start, stop = host_id * rays_per_host, (host_id + 1) * rays_per_host
chunk_rays = utils.namedtuple_map(lambda r: utils.shard(r[start:stop]),
chunk_rays)
Expand Down
3 changes: 3 additions & 0 deletions scripts/train_blender.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ EXPERIMENT=debug
TRAIN_DIR=/Users/barron/tmp/nerf_results/$EXPERIMENT/$SCENE
DATA_DIR=/Users/barron/data/nerf_synthetic/$SCENE

# TRAIN_DIR=/home/akashsharma/tmp/nerf_results/$EXPERIMENT/$SCENE
# DATA_DIR=/home/akashsharma/Documents/datasets/nerf/$SCENE

rm $TRAIN_DIR/*
python -m train \
--data_dir=$DATA_DIR \
Expand Down
5 changes: 4 additions & 1 deletion scripts/train_multiblender.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
SCENE=lego
EXPERIMENT=debug
TRAIN_DIR=/Users/barron/tmp/nerf_results/$EXPERIMENT/$SCENE
# TRAIN_DIR=/home/akashsharma/tmp/nerf_results/$EXPERIMENT/$SCENE
DATA_DIR=/Users/barron/data/down4/$SCENE
# DATA_DIR=/home/akashsharma/Documents/datasets/multiscale/$SCENE

rm $TRAIN_DIR/*
python -m train \
--data_dir=$DATA_DIR \
--train_dir=$TRAIN_DIR \
--gin_file=configs/multiblender.gin \
--logtostderr
--logtostderr \
--gin_param="Config.batch_size = 1024"
16 changes: 8 additions & 8 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,18 @@ def main(unused_argv):
rng = random.PRNGKey(20200823)
# Shift the numpy random seed by host_id() to shuffle data loaded by different
# hosts.
np.random.seed(20201473 + jax.host_id())
np.random.seed(20201473 + jax.process_index())

config = utils.load_config()

if config.batch_size % jax.device_count() != 0:
if config.batch_size % jax.process_count() != 0:
raise ValueError('Batch size must be divisible by the number of devices.')

dataset = datasets.get_dataset('train', FLAGS.data_dir, config)
test_dataset = datasets.get_dataset('test', FLAGS.data_dir, config)

rng, key = random.split(rng)
model, variables = models.construct_mipnerf(key, dataset.peek())
print("Loaded model")
num_params = jax.tree_util.tree_reduce(
lambda x, y: x + jnp.prod(jnp.array(y.shape)), variables, initializer=0)
print(f'Number of parameters being optimized: {num_params}')
Expand Down Expand Up @@ -208,12 +208,12 @@ def render_eval_fn(variables, _, rays):
init_step = state.optimizer.state.step + 1
state = flax.jax_utils.replicate(state)

if jax.host_id() == 0:
if jax.process_index() == 0:
summary_writer = tensorboard.SummaryWriter(FLAGS.train_dir)

# Prefetch_buffer_size = 3 x batch_size
pdataset = flax.jax_utils.prefetch_to_device(dataset, 3)
rng = rng + jax.host_id() # Make random seed separate across hosts.
rng = rng + jax.process_index() # Make random seed separate across hosts.
keys = random.split(rng, jax.local_device_count()) # For pmapping RNG keys.
gc.disable() # Disable automatic garbage collection for efficiency.
stats_trace = []
Expand All @@ -224,15 +224,15 @@ def render_eval_fn(variables, _, rays):
reset_timer = False
lr = learning_rate_fn(step)
state, stats, keys = train_pstep(keys, state, batch, lr)
if jax.host_id() == 0:
if jax.process_index() == 0:
stats_trace.append(stats)
if step % config.gc_every == 0:
gc.collect()

# Log training summaries. This is put behind a host_id check because in
# multi-host evaluation, all hosts need to run inference even though we
# only use host 0 to record results.
if jax.host_id() == 0:
if jax.process_index() == 0:
if step % config.print_every == 0:
summary_writer.scalar('num_params', num_params, step)
summary_writer.scalar('train_loss', stats.loss[0], step)
Expand Down Expand Up @@ -295,7 +295,7 @@ def render_eval_fn(variables, _, rays):
vis_suite = vis.visualize_suite(pred_distance, pred_acc)

# Log eval summaries on host 0.
if jax.host_id() == 0:
if jax.process_index() == 0:
psnr = math.mse_to_psnr(((pred_color - test_case['pixels'])**2).mean())
ssim = ssim_fn(pred_color, test_case['pixels'])
eval_time = time.time() - t_eval_start
Expand Down