text
stringlengths 1
93.6k
|
|---|
"""
|
grad_disp_x = torch.abs(disp[:, :, :, :-1] - disp[:, :, :, 1:])
|
grad_disp_y = torch.abs(disp[:, :, :-1, :] - disp[:, :, 1:, :])
|
grad_img_x = torch.mean(torch.abs(img[:, :, :, :-1] - img[:, :, :, 1:]), 1, keepdim=True)
|
grad_img_y = torch.mean(torch.abs(img[:, :, :-1, :] - img[:, :, 1:, :]), 1, keepdim=True)
|
grad_disp_x *= torch.exp(-grad_img_x)
|
grad_disp_y *= torch.exp(-grad_img_y)
|
return grad_disp_x.mean() + grad_disp_y.mean()
|
class SSIM(nn.Module):
|
"""Layer to compute the SSIM loss between a pair of images"""
|
def __init__(self):
|
super(SSIM, self).__init__()
|
self.mu_x_pool = nn.AvgPool2d(3, 1)
|
self.mu_y_pool = nn.AvgPool2d(3, 1)
|
self.sig_x_pool = nn.AvgPool2d(3, 1)
|
self.sig_y_pool = nn.AvgPool2d(3, 1)
|
self.sig_xy_pool = nn.AvgPool2d(3, 1)
|
self.refl = nn.ReflectionPad2d(1)
|
self.C1 = 0.01 ** 2
|
self.C2 = 0.03 ** 2
|
def forward(self, x, y):
|
x = self.refl(x)
|
y = self.refl(y)
|
mu_x = self.mu_x_pool(x)
|
mu_y = self.mu_y_pool(y)
|
sigma_x = self.sig_x_pool(x ** 2) - mu_x ** 2
|
sigma_y = self.sig_y_pool(y ** 2) - mu_y ** 2
|
sigma_xy = self.sig_xy_pool(x * y) - mu_x * mu_y
|
SSIM_n = (2 * mu_x * mu_y + self.C1) * (2 * sigma_xy + self.C2)
|
SSIM_d = (mu_x ** 2 + mu_y ** 2 + self.C1) * (sigma_x + sigma_y + self.C2)
|
return torch.clamp((1 - SSIM_n / SSIM_d) / 2, 0, 1)
|
def compute_depth_errors(gt, pred):
|
"""Computation of error metrics between predicted and ground truth depths
|
"""
|
thresh = torch.max((gt / pred), (pred / gt))
|
a1 = (thresh < 1.25 ).float().mean()
|
a2 = (thresh < 1.25 ** 2).float().mean()
|
a3 = (thresh < 1.25 ** 3).float().mean()
|
rmse = (gt - pred) ** 2
|
rmse = torch.sqrt(rmse.mean())
|
rmse_log = (torch.log(gt) - torch.log(pred)) ** 2
|
rmse_log = torch.sqrt(rmse_log.mean())
|
abs_rel = torch.mean(torch.abs(gt - pred) / gt)
|
sq_rel = torch.mean((gt - pred) ** 2 / gt)
|
return abs_rel, sq_rel, rmse, rmse_log, a1, a2, a3
|
# <FILESEP>
|
from typing import List, Optional, Tuple, Union
|
from matplotlib import pyplot as plt
|
import numpy as np
|
import seaborn as sns
|
sns.set(style='white')
|
def lineplot(x: np.array, y: Union[List[float], List[List[float]]], filename: str, xlabel: str, ylabel: str, xlim: Optional[Tuple[float, float]]=None, ylim: Optional[Tuple[float, float]]=None, baseline_y: Optional[float]=None):
|
if isinstance(y[0], list):
|
y = np.transpose(np.array(y))
|
palette = sns.color_palette('husl', y.shape[0])
|
for i, y_i in enumerate(y):
|
sns.lineplot(x=x, y=y_i, color=palette[i])
|
else:
|
sns.lineplot(x=x, y=y, color='coral')
|
if baseline_y: sns.lineplot(x=x, y=np.full_like(x, baseline_y), color='gray', linestyle='--')
|
plt.xticks(fontsize=14)
|
plt.yticks(fontsize=14)
|
if xlabel == '':
|
plt.xticks([]) # Remove x-axis labels
|
else:
|
plt.xlabel(xlabel, fontsize=16)
|
plt.ylabel(ylabel, fontsize=16)
|
plt.xlim(*xlim) if xlim is not None else plt.margins(x=0)
|
plt.ylim(*ylim) if ylim is not None else plt.margins(y=0)
|
plt.tight_layout()
|
plt.savefig(f'results/{filename}.png')
|
plt.close()
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.