-
Notifications
You must be signed in to change notification settings - Fork 26
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
Feature: Add new CLIP-IQA metric #157
Draft
StefanPetersTM
wants to merge
1
commit into
LCAV:main
Choose a base branch
from
StefanPetersTM:feature/metric_CLIP_IQA
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -112,10 +112,19 @@ | |
from skimage.metrics import mean_squared_error, peak_signal_noise_ratio, structural_similarity | ||
import lpips as lpips_lib | ||
import torch | ||
import torch.nn.functional as F | ||
from torchmetrics.multimodal import CLIPImageQualityAssessment | ||
from scipy.ndimage import rotate | ||
from lensless.utils.image import resize | ||
|
||
|
||
# Initialize CLIP-IQA model | ||
clip_iqa_model = CLIPImageQualityAssessment( | ||
model_name_or_path=("clip_iqa"), | ||
prompts=("noisiness", ), # TODO change if different metric is required | ||
).to(torch.device('cuda' if torch.cuda.is_available() else 'cpu')) | ||
|
||
|
||
def mse(true, est, normalize=True): | ||
""" | ||
Compute the mean-squared error between two images. The closer to 0, the | ||
|
@@ -260,7 +269,6 @@ def lpips(true, est, normalize=True): | |
) | ||
return loss_fn.forward(true, est).squeeze().item() | ||
|
||
|
||
def extract( | ||
estimate, original, vertical_crop=None, horizontal_crop=None, rotation=0, verbose=False | ||
): | ||
|
@@ -329,3 +337,36 @@ def extract( | |
print(img_resize.max()) | ||
|
||
return estimate, img_resize | ||
|
||
def clip_iqa(true, est, normalize=True): | ||
""" | ||
Computes the CLIP Image Quality Assessment (CLIP-IQA) score between the true and estimated images. | ||
Args: | ||
true (Tensor): The ground truth image tensor. | ||
est (Tensor): The estimated image tensor. | ||
normalize (bool, optional): If True, normalize the images before computing the CLIP-IQA score. Default is True. | ||
Returns: | ||
float: The CLIP-IQA score. | ||
""" | ||
# if normalize: | ||
# true = np.array(true, dtype=np.float32) | ||
# est = np.array(est, dtype=np.float32) | ||
# true /= true.max() | ||
# est /= est.max() | ||
|
||
# Compute CLIP-IQA | ||
with torch.no_grad(): | ||
# Resize images to 224x224 for CLIP-IQA | ||
outputs_resized = F.interpolate( | ||
est, size=(224, 224), mode="bilinear", align_corners=False | ||
) | ||
|
||
outputs_3d = outputs_resized | ||
|
||
#clip_iqa_scores = self.clip_iqa(outputs_3d) | ||
|
||
|
||
return clip_iqa_model(outputs_3d) | ||
|
||
# Compute CLIP-IQA scores over the batch | ||
clip_iqa = clip_iqa_scores.mean().item() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does need to be there? since it is run after the return statement |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can this (and other commented code) be removed?