mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-19 22:25:44 +08:00
Add ssim and update typechecks (#66)
* Add ssim and update typechecks * update requirements * Fix type error
This commit is contained in:
1 parent
6340f975db
commit
29ba6d8abc
4 files changed
+72
-5
No files matched your search
Vendored
+6
@@ -21,4 +21,10 @@
|
||||
],
|
||||
"python.testing.unittestEnabled": false,
|
||||
"python.testing.pytestEnabled": true,
|
||||
"python.linting.ignorePatterns": [
|
||||
"**/site-packages/**/*.py",
|
||||
".vscode/*.py",
|
||||
"external/**/*.py",
|
||||
],
|
||||
"python.analysis.typeCheckingMode": "basic",
|
||||
}
|
||||
@@ -10,6 +10,7 @@ mediapy
|
||||
plotly==5.7.0
|
||||
pylint==2.13.4
|
||||
pytest
|
||||
pytorch-msssim>=0.2.1
|
||||
tensorboard==2.8.0
|
||||
torch==1.11.0
|
||||
torchtyping
|
||||
|
||||
@@ -9,12 +9,12 @@ import torch
|
||||
from torch import nn
|
||||
from torch.nn import Parameter
|
||||
|
||||
from mattport.nerf import metrics
|
||||
from mattport.nerf.field_modules.encoding import NeRFEncoding
|
||||
from mattport.nerf.field_modules.field_heads import DensityFieldHead, FieldHeadNames, RGBFieldHead
|
||||
from mattport.nerf.field_modules.mlp import MLP
|
||||
from mattport.nerf.graph.base import Graph
|
||||
from mattport.nerf.loss import MSELoss
|
||||
from mattport.nerf.metrics import get_psnr
|
||||
from mattport.nerf.renderers import AccumulationRenderer, DepthRenderer, RGBRenderer
|
||||
from mattport.nerf.sampler import PDFSampler, UniformSampler
|
||||
from mattport.structures import colors
|
||||
@@ -221,15 +221,25 @@ class NeRFGraph(Graph):
|
||||
combined_image = torch.cat([depth_coarse, depth_fine], dim=1)
|
||||
writer.write_event({"name": f"image_idx_{image_idx}", "x": combined_image, "step": step, "group": "val_depth"})
|
||||
|
||||
coarse_psnr = get_psnr(image, rgb_coarse)
|
||||
coarse_psnr = metrics.get_psnr(image, rgb_coarse)
|
||||
writer.write_event(
|
||||
{"name": f"image_idx_{image_idx}", "scalar": float(coarse_psnr), "step": step, "group": "val"}
|
||||
)
|
||||
|
||||
fine_psnr = get_psnr(image, rgb_fine)
|
||||
coarse_ssim = metrics.get_ssim(image, rgb_coarse)
|
||||
writer.write_event(
|
||||
{"name": f"image_idx_{image_idx}", "scalar": float(coarse_ssim), "step": step, "group": "ssim"}
|
||||
)
|
||||
|
||||
fine_psnr = metrics.get_psnr(image, rgb_fine)
|
||||
stats_tracker.update_stats(
|
||||
{"name": stats_tracker.Stats.CURR_TEST_PSNR, "value": float(fine_psnr), "step": step}
|
||||
)
|
||||
writer.write_event(
|
||||
{"name": f"image_idx_{image_idx}-fine_psnr", "scalar": float(fine_psnr), "step": step, "group": "val"}
|
||||
)
|
||||
|
||||
fine_ssim = metrics.get_ssim(image, rgb_fine)
|
||||
writer.write_event(
|
||||
{"name": f"image_idx_{image_idx}-fine_ssim", "scalar": float(fine_ssim), "step": step, "group": "ssim"}
|
||||
)
|
||||
@@ -3,11 +3,61 @@ Collection of metrics.
|
||||
"""
|
||||
|
||||
import torch
|
||||
from pytorch_msssim import ssim, ms_ssim
|
||||
from torchtyping import TensorType
|
||||
|
||||
|
||||
def get_psnr(image_gt, image_pred):
|
||||
def get_psnr(image_gt: TensorType, image_pred: TensorType) -> TensorType:
|
||||
"""
|
||||
Returns the psnr for an image. TODO: add masking
|
||||
Returns the PSNR (peak signal-to-noise ratio) for an image. TODO: add masking
|
||||
|
||||
Args:
|
||||
image_gt (TensorType): Ground truth image
|
||||
image_pred (TensorType): Predicted image
|
||||
Returns:
|
||||
float: PSNR
|
||||
"""
|
||||
mean = torch.mean((image_gt - image_pred) ** 2)
|
||||
return -10 * torch.log10(mean)
|
||||
|
||||
|
||||
def get_ssim(image_gt: TensorType, image_pred: TensorType) -> TensorType:
|
||||
"""
|
||||
Returns the SSIM (structural similarty) for an image.
|
||||
|
||||
Args:
|
||||
image_gt (TensorType): Ground truth image
|
||||
image_pred (TensorType): Predicted image
|
||||
Returns:
|
||||
float: SSIM
|
||||
"""
|
||||
assert image_gt.shape[-1] in [1, 3]
|
||||
assert image_pred.shape[-1] in [1, 3]
|
||||
assert image_gt.shape == image_pred.shape
|
||||
if image_gt.dim() == 3:
|
||||
image_gt = image_gt[None, ...]
|
||||
if image_pred.dim() == 3:
|
||||
image_pred = image_pred[None, ...]
|
||||
|
||||
return ssim(image_gt, image_pred, data_range=1.0)
|
||||
|
||||
|
||||
def get_ms_ssim(image_gt: TensorType, image_pred: TensorType) -> TensorType:
|
||||
"""
|
||||
Returns the MS_SSIM (multi-scale structural similarty) for an image.
|
||||
|
||||
Args:
|
||||
image_gt (TensorType): Ground truth image
|
||||
image_pred (TensorType): Predicted image
|
||||
Returns:
|
||||
float: MS-SSIM
|
||||
"""
|
||||
assert image_gt.shape[-1] in [1, 3]
|
||||
assert image_pred.shape[-1] in [1, 3]
|
||||
assert image_gt.shape == image_pred.shape
|
||||
if image_gt.dim() == 3:
|
||||
image_gt = image_gt[None, ...]
|
||||
if image_pred.dim() == 3:
|
||||
image_pred = image_pred[None, ...]
|
||||
|
||||
return ms_ssim(image_gt, image_pred, data_range=1.0)
|
||||
Reference in new issue
Block a user