LPIPS gives negative value #2310
Unanswered
mshooter
asked this question in
CompVision
Replies: 3 comments
|
@mshooter @F-Barto — this is a known issue and there are two common causes: Cause 1: Input range mismatch. This is the most common one.
If your inputs are in [0, 1] but from torchmetrics.image.lpip import LearnedPerceptualImagePatchSimilarity
# If your images are [0, 1]:
lpips = LearnedPerceptualImagePatchSimilarity(net_type="vgg", normalize=True)
# If your images are [-1, 1]:
lpips = LearnedPerceptualImagePatchSimilarity(net_type="vgg", normalize=False)Cause 2: Using LPIPS as a training loss. When backpropagating through the LPIPS network, the learned linear layers can yield negative values. The LPIPS paper states scores should be non-negative for detached evaluation, but during training gradient flow can push intermediate values negative. Workaround if you need LPIPS as a loss: loss = lpips(pred, target)
loss = torch.clamp(loss, min=0.0) # safety clampVerify your setup: import torch
img1 = torch.rand(4, 3, 64, 64) # [0, 1] range
img2 = torch.rand(4, 3, 64, 64)
lpips = LearnedPerceptualImagePatchSimilarity(net_type="vgg", normalize=True)
score = lpips(img1, img2)
print(score) # should be positive, typically 0.0–1.0Docs: LPIPS |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

Uh oh!
There was an error while loading. Please reload this page.
Hi everyone,
my pseudo code is
both x and y_hat have values between [0,1].
But I don't understand why my loss is negative..
All reactions