Convenient function to call lpips library to calculate the LPIPS metric. Not differentiable. Args: rgb: (h, w, 3), in the range of [0, 1] gts: (h, w, 3), in the range of [0, 1] Returns: LPIPS value
(
rgb: torch.Tensor,
gts: torch.Tensor,
lpips_model: torch.nn.Module = None,
)
| 39 | return lpips_model |
| 40 | |
| 41 | def lpips( |
| 42 | rgb: torch.Tensor, |
| 43 | gts: torch.Tensor, |
| 44 | lpips_model: torch.nn.Module = None, |
| 45 | ) -> float: |
| 46 | """ |
| 47 | Convenient function to call lpips library to calculate the LPIPS metric. |
| 48 | Not differentiable. |
| 49 | |
| 50 | Args: |
| 51 | rgb: (h, w, 3), in the range of [0, 1] |
| 52 | gts: (h, w, 3), in the range of [0, 1] |
| 53 | |
| 54 | Returns: |
| 55 | LPIPS value |
| 56 | """ |
| 57 | assert (rgb.shape[-1] == 3) |
| 58 | assert (gts.shape[-1] == 3) |
| 59 | |
| 60 | if lpips_model is None: |
| 61 | lpips_model = LPIPS(net='vgg').to(device=rgb.device) |
| 62 | |
| 63 | return lpips_model( |
| 64 | (2.0 * rgb[..., :3] - 1.0).permute(2, 0, 1), |
| 65 | (2.0 * gts[..., :3] - 1.0).permute(2, 0, 1), |
| 66 | ).mean().item() |
| 67 | |
| 68 | |
| 69 | def ssim( |