本文整理匯總了Python中utils.tensor2array方法的典型用法代碼示例。如果您正苦於以下問題:Python utils.tensor2array方法的具體用法?Python utils.tensor2array怎麽用?Python utils.tensor2array使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類utils
的用法示例。
在下文中一共展示了utils.tensor2array方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: log_result
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import tensor2array [as 別名]
def log_result(pred_depth, GT, input_batch, selected_index, folder, prefix):
def save(path, to_save):
to_save = (255*to_save.transpose(1,2,0)).astype(np.uint8)
imageio.imsave(path, to_save)
pred_to_save = tensor2array(pred_depth, max_value=100)
gt_to_save = tensor2array(torch.from_numpy(GT), max_value=100)
prefix = folder/prefix
save('{}_depth_pred.jpg'.format(prefix), pred_to_save)
save('{}_depth_gt.jpg'.format(prefix), gt_to_save)
disp_to_save = tensor2array(1/pred_depth, max_value=None, colormap='magma')
gt_disp = np.zeros_like(GT)
valid_depth = GT > 0
gt_disp[valid_depth] = 1/GT[valid_depth]
gt_disp_to_save = tensor2array(torch.from_numpy(gt_disp), max_value=None, colormap='magma')
save('{}_disp_pred.jpg'.format(prefix), disp_to_save)
save('{}_disp_gt.jpg'.format(prefix), gt_disp_to_save)
to_save = tensor2array(input_batch.cpu().data[selected_index,:3])
save('{}_input0.jpg'.format(prefix), to_save)
to_save = tensor2array(input_batch.cpu()[selected_index,3:])
save('{}_input1.jpg'.format(prefix), to_save)
for i, batch_elem in enumerate(input_batch.cpu().data):
to_save = tensor2array(batch_elem[:3])
save('{}_batch_{}_0.jpg'.format(prefix, i), to_save)
to_save = tensor2array(batch_elem[3:])
save('{}_batch_{}_1.jpg'.format(prefix, i), to_save)
示例2: main
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import tensor2array [as 別名]
def main():
args = parser.parse_args()
if not(args.output_disp or args.output_depth):
print('You must at least output one value !')
return
disp_net = DispNetS().to(device)
weights = torch.load(args.pretrained)
disp_net.load_state_dict(weights['state_dict'])
disp_net.eval()
dataset_dir = Path(args.dataset_dir)
output_dir = Path(args.output_dir)
output_dir.makedirs_p()
if args.dataset_list is not None:
with open(args.dataset_list, 'r') as f:
test_files = [dataset_dir/file for file in f.read().splitlines()]
else:
test_files = sum([list(dataset_dir.walkfiles('*.{}'.format(ext))) for ext in args.img_exts], [])
print('{} files to test'.format(len(test_files)))
for file in tqdm(test_files):
img = imread(file)
h,w,_ = img.shape
if (not args.no_resize) and (h != args.img_height or w != args.img_width):
img = np.array(Image.fromarray(img).imresize((args.img_height, args.img_width)))
img = np.transpose(img, (2, 0, 1))
tensor_img = torch.from_numpy(img.astype(np.float32)).unsqueeze(0)
tensor_img = ((tensor_img/255 - 0.5)/0.5).to(device)
output = disp_net(tensor_img)[0]
file_path, file_ext = file.relpath(args.dataset_dir).splitext()
print(file_path)
print(file_path.splitall())
file_name = '-'.join(file_path.splitall()[1:])
print(file_name)
if args.output_disp:
disp = (255*tensor2array(output, max_value=None, colormap='bone')).astype(np.uint8)
imsave(output_dir/'{}_disp{}'.format(file_name, file_ext), np.transpose(disp, (1,2,0)))
if args.output_depth:
depth = 1/output
depth = (255*tensor2array(depth, max_value=10, colormap='rainbow')).astype(np.uint8)
imsave(output_dir/'{}_depth{}'.format(file_name, file_ext), np.transpose(depth, (1,2,0)))
示例3: validate_with_gt
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import tensor2array [as 別名]
def validate_with_gt(args, val_loader, disp_net, epoch, logger, tb_writer, sample_nb_to_log=3):
global device
batch_time = AverageMeter()
error_names = ['abs_diff', 'abs_rel', 'sq_rel', 'a1', 'a2', 'a3']
errors = AverageMeter(i=len(error_names))
log_outputs = sample_nb_to_log > 0
# switch to evaluate mode
disp_net.eval()
end = time.time()
logger.valid_bar.update(0)
for i, (tgt_img, depth) in enumerate(val_loader):
tgt_img = tgt_img.to(device)
depth = depth.to(device)
# compute output
output_disp = disp_net(tgt_img)
output_depth = 1/output_disp[:,0]
if log_outputs and i < sample_nb_to_log:
if epoch == 0:
tb_writer.add_image('val Input/{}'.format(i), tensor2array(tgt_img[0]), 0)
depth_to_show = depth[0]
tb_writer.add_image('val target Depth Normalized/{}'.format(i),
tensor2array(depth_to_show, max_value=None),
epoch)
depth_to_show[depth_to_show == 0] = 1000
disp_to_show = (1/depth_to_show).clamp(0,10)
tb_writer.add_image('val target Disparity Normalized/{}'.format(i),
tensor2array(disp_to_show, max_value=None, colormap='magma'),
epoch)
tb_writer.add_image('val Dispnet Output Normalized/{}'.format(i),
tensor2array(output_disp[0], max_value=None, colormap='magma'),
epoch)
tb_writer.add_image('val Depth Output Normalized/{}'.format(i),
tensor2array(output_depth[0], max_value=None),
epoch)
errors.update(compute_errors(depth, output_depth))
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
logger.valid_bar.update(i+1)
if i % args.print_freq == 0:
logger.valid_writer.write('valid: Time {} Abs Error {:.4f} ({:.4f})'.format(batch_time, errors.val[0], errors.avg[0]))
logger.valid_bar.update(len(val_loader))
return errors.avg, error_names
示例4: main
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import tensor2array [as 別名]
def main():
args = parser.parse_args()
if not(args.output_disp or args.output_depth):
print('You must at least output one value !')
return
disp_net = DispNetS().cuda()
weights = torch.load(args.pretrained)
disp_net.load_state_dict(weights['state_dict'])
disp_net.eval()
dataset_dir = Path(args.dataset_dir)
output_dir = Path(args.output_dir)
output_dir.makedirs_p()
if args.dataset_list is not None:
with open(args.dataset_list, 'r') as f:
test_files = [dataset_dir/file for file in f.read().splitlines()]
else:
test_files = sum([dataset_dir.files('*.{}'.format(ext)) for ext in args.img_exts], [])
print('{} files to test'.format(len(test_files)))
for file in tqdm(test_files):
img = imread(file).astype(np.float32)
h,w,_ = img.shape
if (not args.no_resize) and (h != args.img_height or w != args.img_width):
img = imresize(img, (args.img_height, args.img_width)).astype(np.float32)
img = np.transpose(img, (2, 0, 1))
tensor_img = torch.from_numpy(img).unsqueeze(0)
tensor_img = ((tensor_img/255 - 0.5)/0.2).cuda()
var_img = torch.autograd.Variable(tensor_img, volatile=True)
output = disp_net(var_img).data.cpu()[0]
if args.output_disp:
disp = (255*tensor2array(output, max_value=None, colormap='bone')).astype(np.uint8)
imsave(output_dir/'{}_disp{}'.format(file.namebase,file.ext), disp)
if args.output_depth:
depth = 1/output
depth = (255*tensor2array(depth, max_value=10, colormap='rainbow')).astype(np.uint8)
imsave(output_dir/'{}_depth{}'.format(file.namebase,file.ext), depth)
示例5: validate_depth_with_gt
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import tensor2array [as 別名]
def validate_depth_with_gt(val_loader, disp_net, epoch, logger, output_writers=[]):
global args
batch_time = AverageMeter()
error_names = ['abs_diff', 'abs_rel', 'sq_rel', 'a1', 'a2', 'a3']
errors = AverageMeter(i=len(error_names))
log_outputs = len(output_writers) > 0
# switch to evaluate mode
disp_net.eval()
end = time.time()
for i, (tgt_img, depth) in enumerate(val_loader):
tgt_img_var = Variable(tgt_img.cuda(), volatile=True)
output_disp = disp_net(tgt_img_var)
if args.spatial_normalize:
output_disp = spatial_normalize(output_disp)
output_depth = 1/output_disp
depth = depth.cuda()
# compute output
if log_outputs and i % 100 == 0 and i/100 < len(output_writers):
index = int(i//100)
if epoch == 0:
output_writers[index].add_image('val Input', tensor2array(tgt_img[0]), 0)
depth_to_show = depth[0].cpu()
output_writers[index].add_image('val target Depth', tensor2array(depth_to_show, max_value=10), epoch)
depth_to_show[depth_to_show == 0] = 1000
disp_to_show = (1/depth_to_show).clamp(0,10)
output_writers[index].add_image('val target Disparity Normalized', tensor2array(disp_to_show, max_value=None, colormap='bone'), epoch)
output_writers[index].add_image('val Dispnet Output Normalized', tensor2array(output_disp.data[0].cpu(), max_value=None, colormap='bone'), epoch)
output_writers[index].add_image('val Depth Output', tensor2array(output_depth.data[0].cpu(), max_value=10), epoch)
errors.update(compute_errors(depth, output_depth.data.squeeze(1)))
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if args.log_terminal:
logger.valid_bar.update(i)
if i % args.print_freq == 0:
logger.valid_writer.write('valid: Time {} Abs Error {:.4f} ({:.4f})'.format(batch_time, errors.val[0], errors.avg[0]))
if args.log_terminal:
logger.valid_bar.update(len(val_loader))
return errors.avg, error_names
示例6: validate_with_gt
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import tensor2array [as 別名]
def validate_with_gt(args, val_loader, dpsnet, epoch, output_writers=[]):
batch_time = AverageMeter()
error_names = ['abs_rel', 'abs_diff', 'sq_rel', 'a1', 'a2', 'a3']
errors = AverageMeter(i=len(error_names))
log_outputs = len(output_writers) > 0
# switch to evaluate mode
dpsnet.eval()
end = time.time()
with torch.no_grad():
for i, (tgt_img, ref_imgs, ref_poses, intrinsics, intrinsics_inv, tgt_depth) in enumerate(val_loader):
tgt_img_var = Variable(tgt_img.cuda())
ref_imgs_var = [Variable(img.cuda()) for img in ref_imgs]
ref_poses_var = [Variable(pose.cuda()) for pose in ref_poses]
intrinsics_var = Variable(intrinsics.cuda())
intrinsics_inv_var = Variable(intrinsics_inv.cuda())
tgt_depth_var = Variable(tgt_depth.cuda())
pose = torch.cat(ref_poses_var,1)
output_depth = dpsnet(tgt_img_var, ref_imgs_var, pose, intrinsics_var, intrinsics_inv_var)
output_disp = args.nlabel*args.mindepth/(output_depth)
mask = (tgt_depth <= args.nlabel*args.mindepth) & (tgt_depth >= args.mindepth) & (tgt_depth == tgt_depth)
output = torch.squeeze(output_depth.data.cpu(),1)
if log_outputs and i % 100 == 0 and i/100 < len(output_writers):
index = int(i//100)
if epoch == 0:
output_writers[index].add_image('val Input', tensor2array(tgt_img[0]), 0)
depth_to_show = tgt_depth_var.data[0].cpu()
depth_to_show[depth_to_show > args.nlabel*args.mindepth] = args.nlabel*args.mindepth
disp_to_show = (args.nlabel*args.mindepth/depth_to_show)
disp_to_show[disp_to_show > args.nlabel] = 0
output_writers[index].add_image('val target Disparity Normalized', tensor2array(disp_to_show, max_value=args.nlabel, colormap='bone'), epoch)
output_writers[index].add_image('val target Depth Normalized', tensor2array(depth_to_show, max_value=args.nlabel*args.mindepth*0.3), epoch)
output_writers[index].add_image('val Dispnet Output Normalized', tensor2array(output_disp.data[0].cpu(), max_value=args.nlabel, colormap='bone'), epoch)
output_writers[index].add_image('val Depth Output', tensor2array(output_depth.data[0].cpu(), max_value=args.nlabel*args.mindepth*0.3), epoch)
errors.update(compute_errors_train(tgt_depth, output, mask))
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if i % args.print_freq == 0:
print('valid: Time {} Abs Error {:.4f} ({:.4f})'.format(batch_time, errors.val[0], errors.avg[0]))
return errors.avg, error_names
示例7: main
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import tensor2array [as 別名]
def main():
args = parser.parse_args()
if not(args.output_disp or args.output_depth):
print('You must at least output one value !')
return
disp_net = DispResNet(args.resnet_layers, False).to(device)
weights = torch.load(args.pretrained)
disp_net.load_state_dict(weights['state_dict'])
disp_net.eval()
dataset_dir = Path(args.dataset_dir)
output_dir = Path(args.output_dir)
output_dir.makedirs_p()
if args.dataset_list is not None:
with open(args.dataset_list, 'r') as f:
test_files = [dataset_dir/file for file in f.read().splitlines()]
else:
test_files = sum([dataset_dir.files('*.{}'.format(ext)) for ext in args.img_exts], [])
print('{} files to test'.format(len(test_files)))
for file in tqdm(test_files):
img = imread(file).astype(np.float32)
h, w, _ = img.shape
if (not args.no_resize) and (h != args.img_height or w != args.img_width):
img = imresize(img, (args.img_height, args.img_width)).astype(np.float32)
img = np.transpose(img, (2, 0, 1))
tensor_img = torch.from_numpy(img).unsqueeze(0)
tensor_img = ((tensor_img/255 - 0.45)/0.225).to(device)
output = disp_net(tensor_img)[0]
file_path, file_ext = file.relpath(args.dataset_dir).splitext()
file_name = '-'.join(file_path.splitall())
if args.output_disp:
disp = (255*tensor2array(output, max_value=None, colormap='bone')).astype(np.uint8)
imsave(output_dir/'{}_disp{}'.format(file_name, file_ext), np.transpose(disp, (1, 2, 0)))
if args.output_depth:
depth = 1/output
depth = (255*tensor2array(depth, max_value=10, colormap='rainbow')).astype(np.uint8)
imsave(output_dir/'{}_depth{}'.format(file_name, file_ext), np.transpose(depth, (1, 2, 0)))
示例8: validate_without_gt
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import tensor2array [as 別名]
def validate_without_gt(args, val_loader, disp_net, pose_net, epoch, logger, output_writers=[]):
global device
batch_time = AverageMeter()
losses = AverageMeter(i=4, precision=4)
log_outputs = len(output_writers) > 0
# switch to evaluate mode
disp_net.eval()
pose_net.eval()
end = time.time()
logger.valid_bar.update(0)
for i, (tgt_img, ref_imgs, intrinsics, intrinsics_inv) in enumerate(val_loader):
tgt_img = tgt_img.to(device)
ref_imgs = [img.to(device) for img in ref_imgs]
intrinsics = intrinsics.to(device)
intrinsics_inv = intrinsics_inv.to(device)
# compute output
tgt_depth = [1 / disp_net(tgt_img)]
ref_depths = []
for ref_img in ref_imgs:
ref_depth = [1 / disp_net(ref_img)]
ref_depths.append(ref_depth)
if log_outputs and i < len(output_writers):
if epoch == 0:
output_writers[i].add_image('val Input', tensor2array(tgt_img[0]), 0)
output_writers[i].add_image('val Dispnet Output Normalized',
tensor2array(1/tgt_depth[0][0], max_value=None, colormap='magma'),
epoch)
output_writers[i].add_image('val Depth Output',
tensor2array(tgt_depth[0][0], max_value=10),
epoch)
poses, poses_inv = compute_pose_with_inv(pose_net, tgt_img, ref_imgs)
loss_1, loss_3 = compute_photo_and_geometry_loss(tgt_img, ref_imgs, intrinsics, tgt_depth, ref_depths,
poses, poses_inv, args.num_scales, args.with_ssim,
args.with_mask, False, args.padding_mode)
loss_2 = compute_smooth_loss(tgt_depth, tgt_img, ref_depths, ref_imgs)
loss_1 = loss_1.item()
loss_2 = loss_2.item()
loss_3 = loss_3.item()
loss = loss_1
losses.update([loss, loss_1, loss_2, loss_3])
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
logger.valid_bar.update(i+1)
if i % args.print_freq == 0:
logger.valid_writer.write('valid: Time {} Loss {}'.format(batch_time, losses))
logger.valid_bar.update(len(val_loader))
return losses.avg, ['Total loss', 'Photo loss', 'Smooth loss', 'Consistency loss']
示例9: validate_with_gt
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import tensor2array [as 別名]
def validate_with_gt(args, val_loader, disp_net, epoch, logger, output_writers=[]):
global device
batch_time = AverageMeter()
error_names = ['abs_diff', 'abs_rel', 'sq_rel', 'a1', 'a2', 'a3']
errors = AverageMeter(i=len(error_names))
log_outputs = len(output_writers) > 0
# switch to evaluate mode
disp_net.eval()
end = time.time()
logger.valid_bar.update(0)
for i, (tgt_img, depth) in enumerate(val_loader):
tgt_img = tgt_img.to(device)
depth = depth.to(device)
# check gt
if depth.nelement() == 0:
continue
# compute output
output_disp = disp_net(tgt_img)
output_depth = 1/output_disp[:, 0]
if log_outputs and i < len(output_writers):
if epoch == 0:
output_writers[i].add_image('val Input', tensor2array(tgt_img[0]), 0)
depth_to_show = depth[0]
output_writers[i].add_image('val target Depth',
tensor2array(depth_to_show, max_value=10),
epoch)
depth_to_show[depth_to_show == 0] = 1000
disp_to_show = (1/depth_to_show).clamp(0, 10)
output_writers[i].add_image('val target Disparity Normalized',
tensor2array(disp_to_show, max_value=None, colormap='magma'),
epoch)
output_writers[i].add_image('val Dispnet Output Normalized',
tensor2array(output_disp[0], max_value=None, colormap='magma'),
epoch)
output_writers[i].add_image('val Depth Output',
tensor2array(output_depth[0], max_value=10),
epoch)
if depth.nelement() != output_depth.nelement():
b, h, w = depth.size()
output_depth = torch.nn.functional.interpolate(output_depth.unsqueeze(1), [h, w]).squeeze(1)
errors.update(compute_errors(depth, output_depth, args.dataset))
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
logger.valid_bar.update(i+1)
if i % args.print_freq == 0:
logger.valid_writer.write('valid: Time {} Abs Error {:.4f} ({:.4f})'.format(batch_time, errors.val[0], errors.avg[0]))
logger.valid_bar.update(len(val_loader))
return errors.avg, error_names