Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
loss.backward()
optimizer.step()
if iter_idx % args.log_interval == 0 or \
iter_idx == args.num_iterations - 1:
print('Train iteration: {}/{}\tLoss: {:.6}'.format(
iter_idx, args.num_iterations, loss.item()))
if iter_idx % args.log_interval_vis == 0:
# merge warped and target image for visualization
img_i_to_ref = warper(inv_depth_ref(), img_i)
img_both_vis = 0.5 * (img_i_to_ref + img_ref)
img_both_vis = clip_and_convert_tensor(img_both_vis)
img_i_to_ref_vis = clip_and_convert_tensor(img_i_to_ref)
inv_depth_ref_vis = tgm.utils.tensor_to_image(
inv_depth_ref() / (inv_depth_ref().max() + 1e-6)).squeeze()
inv_depth_ref_vis = np.clip(255. * inv_depth_ref_vis, 0, 255)
inv_depth_ref_vis = inv_depth_ref_vis.astype('uint8')
# save warped image and depth to disk
def file_name(x):
return os.path.join(args.output_dir,
"{0}_{1}.png".format(x, iter_idx))
cv2.imwrite(file_name("warped"), img_i_to_ref_vis)
cv2.imwrite(file_name("warped_both"), img_both_vis)
cv2.imwrite(file_name("inv_depth_ref"), inv_depth_ref_vis)
# compute the inverse depth and warp the source image
inv_depth_ref = 1. / depth_ref
img_i_to_ref = warper(inv_depth_ref, img_i)
# generate occlusion mask
mask = ((img_ref - img_i_to_ref).mean(1) < 1e-1).float()
img_vis_warped = 0.5 * img_i_to_ref + img_ref
img_vis_warped_masked = mask * (0.5 * img_i_to_ref + img_ref)
# save warped image to disk
file_name = os.path.join(
args.output_dir,
'warped_{0}_to_{1}.png'.format(
args.frame_i_id, args.frame_ref_id))
cv2.imwrite(file_name, dgm.utils.tensor_to_image(255. * img_vis_warped))
cv2.imwrite(file_name + 'mask.png', dgm.utils.tensor_to_image(255. * mask))
cv2.imwrite(file_name + 'warpedmask.png',
dgm.utils.tensor_to_image(255. * img_vis_warped_masked))
def clip_and_convert_tensor(tensor):
"""convert the input torch.Tensor to OpenCV image,clip it to be between
[0, 255] and convert it to unit
"""
img = tgm.utils.tensor_to_image(255. * tensor) # convert tensor to numpy
img_cliped = np.clip(img, 0, 255) # clip and reorder the channels
img = img_cliped.astype('uint8') # convert to uint
return img
inv_depth_ref = 1. / depth_ref
img_i_to_ref = warper(inv_depth_ref, img_i)
# generate occlusion mask
mask = ((img_ref - img_i_to_ref).mean(1) < 1e-1).float()
img_vis_warped = 0.5 * img_i_to_ref + img_ref
img_vis_warped_masked = mask * (0.5 * img_i_to_ref + img_ref)
# save warped image to disk
file_name = os.path.join(
args.output_dir,
'warped_{0}_to_{1}.png'.format(
args.frame_i_id, args.frame_ref_id))
cv2.imwrite(file_name, dgm.utils.tensor_to_image(255. * img_vis_warped))
cv2.imwrite(file_name + 'mask.png', dgm.utils.tensor_to_image(255. * mask))
cv2.imwrite(file_name + 'warpedmask.png',
dgm.utils.tensor_to_image(255. * img_vis_warped_masked))
# generate occlusion mask
mask = ((img_ref - img_i_to_ref).mean(1) < 1e-1).float()
img_vis_warped = 0.5 * img_i_to_ref + img_ref
img_vis_warped_masked = mask * (0.5 * img_i_to_ref + img_ref)
# save warped image to disk
file_name = os.path.join(
args.output_dir,
'warped_{0}_to_{1}.png'.format(
args.frame_i_id, args.frame_ref_id))
cv2.imwrite(file_name, dgm.utils.tensor_to_image(255. * img_vis_warped))
cv2.imwrite(file_name + 'mask.png', dgm.utils.tensor_to_image(255. * mask))
cv2.imwrite(file_name + 'warpedmask.png',
dgm.utils.tensor_to_image(255. * img_vis_warped_masked))