Skip to content

feat: add sharpness metric#261

Merged
davidberenstein1957 merged 4 commits intomainfrom
feat/sharpness-metric
Jul 24, 2025
Merged

feat: add sharpness metric#261
davidberenstein1957 merged 4 commits intomainfrom
feat/sharpness-metric

Conversation

@begumcig
Copy link
Copy Markdown
Member

Description

  • Adds SharpnessMetric, which computes focus via the variance of the Laplacian (higher = sharper).

  • CPU‑side numpy/OpenCV implementation.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Additional Notes

You can use the SharpnessMetric directly or via the EvaluationAgent:

from pruna.evaluation.metrics.metric_sharpness import SharpnessMetric
from pruna.evaluation.task import Task
from pruna.evaluation.evaluation_agent import EvaluationAgent
from pruna.data.pruna_datamodule import PrunaDataModule

# 1) Direct use 
metric = SharpnessMetric(kernel_size=5)

inputs, gts = next(iter(my_dataloader))          # your B×C×H×W tensors
outputs = my_model(inputs)

metric.update(inputs, gts, outputs)
print("Sharpness:", metric.compute().result)     # higher = sharper

# 2) Via Task / EvaluationAgent
dm = PrunaDataModule.from_string("LAION256")
dm.limit_datasets(10)

task = Task(
    request=["sharpness"],        # or [SharpnessMetric(kernel_size=5)]
    datamodule=dm,
)
agent = EvaluationAgent(task=task)

results = agent.evaluate(my_model)
print(results[0])                 # MetricResult(name='sharpness', value=…)

Copy link
Copy Markdown
Member

@sharpenb sharpenb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool new metric! This is close to merge. Beyond my comments, a last thing would be to have/extend a tutorial with these introduced metric (it can be in another PR). This tutorial could take an image compute the metrics on it, and then perturb it it (e.g. by adding blur or other things mentioned in this paper). The goal would be to justify the relevance of these metrics :)

* Introduced `SharpnessMetric` for evaluating image sharpness using Laplacian variance.
* Added tests for `SharpnessMetric` to validate its functionality across different models and configurations.
* Included `opencv-python` as a new dependency in `pyproject.toml` to support sharpness calculations.
…cessing

- Added detailed descriptions of the sharpness metric calculation and its parameters.
- Clarified that sharpness is computed on the grayscale version of images.
- Improved comments in the code for better understanding of image processing steps.
@davidberenstein1957 davidberenstein1957 merged commit faeaf7c into main Jul 24, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants