text
stringlengths
1
93.6k
metric_start_time = time.time()
metric = metric_harness(
postprocess_fn(rendering['rgb']), postprocess_fn(test_batch['rgb']))
logger.info(f'Eval {step}: {eval_time:0.3f}s, {rays_per_sec:0.0f} rays/sec')
logger.info(f'Metrics computed in {(time.time() - metric_start_time):0.3f}s')
for name, val in metric.items():
if not np.isnan(val):
logger.info(f'{name} = {val:.4f}')
summary_writer.add_scalar('train_metrics/' + name, val, step)
if config.vis_decimate > 1:
d = config.vis_decimate
decimate_fn = lambda x, d=d: None if x is None else x[::d, ::d]
else:
decimate_fn = lambda x: x
rendering = tree_map(decimate_fn, rendering)
test_batch = tree_map(decimate_fn, test_batch)
vis_start_time = time.time()
vis_suite = vis.visualize_suite(rendering, test_batch)
with tqdm.external_write_mode():
logger.info(f'Visualized in {(time.time() - vis_start_time):0.3f}s')
if config.rawnerf_mode:
# Unprocess raw output.
vis_suite['color_raw'] = rendering['rgb']
# Autoexposed colors.
vis_suite['color_auto'] = postprocess_fn(rendering['rgb'], None)
summary_writer.add_image('test_true_auto',
tb_process_fn(postprocess_fn(test_batch['rgb'], None)), step)
# Exposure sweep colors.
exposures = test_dataset.metadata['exposure_levels']
for p, x in list(exposures.items()):
vis_suite[f'color_exp{p}'] = postprocess_fn(rendering['rgb'], x)
summary_writer.add_image(f'test_true_color_exp{p}',
tb_process_fn(postprocess_fn(test_batch['rgb'], x)), step)
summary_writer.add_image('test_true_color', tb_process_fn(test_batch['rgb']), step)
if config.compute_normal_metrics:
summary_writer.add_image('test_true_normals',
tb_process_fn(test_batch['normals']) / 2. + 0.5, step)
utils.save_img_u8(test_batch['normals'] / 2. + 0.5, os.path.join(config.test_vis_dir, f'test_output_normals_{step:06d}.png'))
for k, v in vis_suite.items():
summary_writer.add_image('test_output_' + k, tb_process_fn(v), step)
utils.save_img_u8(v, os.path.join(config.test_vis_dir, f'test_output_{k}_{step:06d}.png'))
if accelerator.is_main_process and config.max_steps > init_step:
logger.info('Saving last checkpoint at step {} to {}'.format(step, config.checkpoint_dir))
checkpoints.save_checkpoint(config.checkpoint_dir,
accelerator, step,
config.checkpoints_total_limit)
logger.info('Finish training.')
if __name__ == '__main__':
configs.define_common_flags()
with gin.config_scope('train'):
app.run(main)
# <FILESEP>
import torch
import numpy as np
from tqdm import tqdm
import util
import homography_project
def test(model, predictions, query_img, gallery_img, test_dataset, num_reranked_predictions=5,
recall_values=[1,5,10,20], test_batch_size=8):
"""Compute the test by warping the query-prediction pairs.
Parameters
----------
model : network.Network
predictions : np.array of int, containing the first 20 predictions for each query,
with shape [queries_num, 20].
test_dataset : dataset_geoloc.GeolocDataset, which contains the test-time images (queries and gallery).
num_reranked_predictions : int, how many predictions to re-rank.
recall_values : list of int, recalls to compute (e.g. R@1, R@5...).
test_batch_size : int.
Returns
-------
recalls : np.array of int, containing R@1, R@5, r@10, r@20.
recalls_pretty_str : str, pretty-printed recalls
"""
model = model.eval()
reranked_predictions = predictions.copy()
with torch.no_grad():
for num_q in tqdm(range(test_dataset.queries_num), desc="Testing", ncols=100):
dot_prods_wqp = np.zeros((num_reranked_predictions))
query_path = test_dataset.queries_paths[num_q]
query = query_img[num_q].cuda()
for i1 in range(0, num_reranked_predictions, test_batch_size):
batch_indexes = list(range(num_reranked_predictions))[i1:i1+test_batch_size]
current_batch_size = len(batch_indexes)
query_repeated_twice = torch.repeat_interleave(query.unsqueeze(0), current_batch_size, 0)
preds = gallery_img[predictions[num_q, batch_indexes]].cuda()
similarity = homography_project.compute_score(model, query_repeated_twice, preds)