Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LIQE and LIQE-mix return different results for the same image when using a batch #178

Open
giacomov opened this issue Aug 7, 2024 · 1 comment

Comments

@giacomov
Copy link

giacomov commented Aug 7, 2024

I found this weird behavior in LIQE and LIQE-mix (did not test other metrics, so it could be present elsewhere as well):

import torch
import pyiqa
import matplotlib.cbook as cbook
import matplotlib.pyplot as plt
from torchvision import transforms
from PIL import Image

with cbook.get_sample_data('grace_hopper.jpg') as image_file:
    image_path = image_file.name

# Open and display the image using PIL
image = Image.open(image_path)

one_image = transforms.ToTensor()(image).unsqueeze(dim=0)
two_images = torch.stack([transforms.ToTensor()(x) for x in [image, image]], dim=0)
five_images = torch.stack([transforms.ToTensor()(x) for x in [image] * 5], dim=0)

iqa_metric = pyiqa.create_metric("liqe_mix")

print(f"Result on file path: {iqa_metric(image_path)}")
print(f"Result on one tensor: {iqa_metric(one_image)}")
print(f"Result on two identical tensors: {iqa_metric(two_images)}")
print(f"Result on five identical tensors: {iqa_metric(five_images)}")

I would expect all results to be identical, instead I get:

Result on file path: tensor([4.9667], device='cuda:0')
Result on one tensor: tensor([4.9667], device='cuda:0')
Result on two identical tensors: tensor([4.9827, 4.9502], device='cuda:0')
Result on five identical tensors: tensor([4.9842, 4.9845, 4.9794, 4.9810, 4.9327], device='cuda:0')

so the same image repeated in the same batch gives different results, and none of them is identical to the result you get when you use only one image (either as a path, or as a batch of one). The problem gets worse the more images you have in one batch.

@chaofengc
Copy link
Owner

Thanks to your information. There is a bug in the following shape permutation code:

x = x.unfold(2, 224, self.step).unfold(3, 224, self.step).permute(2, 3, 0, 1, 4, 5).reshape(bs, -1, 3,

After unfold the shape should be (b, c, ph, pw, h, w) and permutation should be (0, 2, 3, 1, 4, 5) which makes (b, ph, pw, c, h, w).
Since the scores are calculated patch-wise, this bug only makes difference when inferring with batch size >1.

Similar bug is unlikely to happen in other metrics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants