-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c218a8
commit dbadb17
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import torch | ||
from typing import Optional | ||
from pixr.properties import LOSS_MSE | ||
|
||
|
||
def compute_loss( | ||
predic: torch.Tensor, | ||
target: torch.Tensor, | ||
mode: Optional[str] = LOSS_MSE | ||
) -> torch.Tensor: | ||
""" | ||
Compute loss based on the predicted and true values. | ||
Args: | ||
predic (torch.Tensor): [N, C, H, W] predicted values | ||
target (torch.Tensor): [N, C, H, W] target values. | ||
mode (Optional[str], optional): mode of loss computation. | ||
Returns: | ||
torch.Tensor: The computed loss. | ||
""" | ||
assert mode in [LOSS_MSE], f"Mode {mode} not supported" | ||
if mode == LOSS_MSE: | ||
loss = torch.nn.functional.mse_loss(predic, target) | ||
return loss |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import torch | ||
from pixr.properties import METRIC_PSNR, REDUCTION_AVERAGE, REDUCTION_SKIP | ||
|
||
|
||
def compute_psnr( | ||
predic: torch.Tensor, | ||
target: torch.Tensor, | ||
clamp_mse=1e-10, | ||
reduction=REDUCTION_AVERAGE | ||
) -> torch.Tensor: | ||
""" | ||
Compute the average PSNR metric for a batch of predicted and true values. | ||
Args: | ||
predic (torch.Tensor): [N, C, H, W] predicted values. | ||
target (torch.Tensor): [N, C, H, W] target values. | ||
Returns: | ||
torch.Tensor: The average PSNR value for the batch. | ||
""" | ||
mse_per_image = torch.mean((predic - target) ** 2, dim=(-3, -2, -1)) | ||
mse_per_image = torch.clamp(mse_per_image, min=clamp_mse) | ||
psnr_per_image = 10 * torch.log10(1 / mse_per_image) | ||
if reduction == REDUCTION_AVERAGE: | ||
average_psnr = torch.mean(psnr_per_image) | ||
elif reduction == REDUCTION_SKIP: | ||
average_psnr = psnr_per_image | ||
else: | ||
raise ValueError(f"Unknown reduction {reduction}") | ||
return average_psnr | ||
|
||
|
||
def compute_metrics(predic: torch.Tensor, target: torch.Tensor, reduction=REDUCTION_AVERAGE) -> dict: | ||
""" | ||
Compute the metrics for a batch of predicted and true values. | ||
Args: | ||
predic (torch.Tensor): [N, C, H, W] predicted values. | ||
target (torch.Tensor): [N, C, H, W] target values. | ||
Returns: | ||
dict: computed metrics. | ||
""" | ||
average_psnr = compute_psnr(predic, target, reduction=reduction) | ||
metrics = {METRIC_PSNR: average_psnr.item() if reduction != REDUCTION_SKIP else average_psnr} | ||
return metrics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters