File size: 3,306 Bytes
e69c065
 
 
 
 
 
 
 
 
 
 
 
 
 
e729560
e69c065
 
 
 
 
8b1b45a
e69c065
 
 
 
 
 
 
 
 
 
 
 
5631221
e69c065
 
 
 
 
 
 
 
 
 
 
70404a2
bd7010d
e69c065
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25dd93a
e69c065
25dd93a
 
 
 
e69c065
 
 
 
a68965c
e69c065
 
ff970cc
a68965c
 
e69c065
6c3b041
6e9f802
 
25dd93a
6c3b041
54a9374
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
---
language:
- en
base_model:
- openai/clip-vit-large-patch14
tags:
- IQA
- computer_vision
- perceptual_tasks
- CLIP
- KonIQ-10k
---
**PerceptCLIP-IQA** is a model designed to predict **image quality assessment (IQA) score**. This is the official model from the paper:  
📄 **["Don't Judge Before You CLIP: A Unified Approach for Perceptual Tasks"](https://arxiv.org/abs/2503.13260)**.
We apply **LoRA adaptation** on the **CLIP visual encoder** and add an **MLP head** for IQA score prediction. Our model achieves **state-of-the-art results** as described in our paper.

## Training Details

- *Dataset*: [KonIQ-10k](https://arxiv.org/pdf/1910.06180) 
- *Architecture*: CLIP Vision Encoder (ViT-L/14) with *LoRA adaptation*
- *Loss Function*: Pearson correlation induced loss  <img src="https://huggingface.co/PerceptCLIP/PerceptCLIP_IQA/resolve/main/loss_formula.png" width="220" style="vertical-align: middle;" />
- *Optimizer*: AdamW
- *Learning Rate*: 5e-05
- *Batch Size*: 32

## Installation & Requirements

You can set up the environment using environment.yml or manually install dependencies:
- python=3.9.15
- cudatoolkit=11.7
- torchvision=0.14.0
- transformers=4.45.2
- peft=0.14.0
- numpy=1.26.4

## Usage

To use the model for inference:

```python
from torchvision import transforms
import torch
from PIL import Image
from huggingface_hub import hf_hub_download
import importlib.util
import numpy as np
import random

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# Load the model class definition dynamically
class_path = hf_hub_download(repo_id="PerceptCLIP/PerceptCLIP_IQA", filename="modeling.py")
spec = importlib.util.spec_from_file_location("modeling", class_path)
modeling = importlib.util.module_from_spec(spec)
spec.loader.exec_module(modeling)

# initialize a model
ModelClass = modeling.clip_lora_model 
model = ModelClass().to(device)

# Load pretrained model
model_path = hf_hub_download(repo_id="PerceptCLIP/PerceptCLIP_IQA", filename="perceptCLIP_IQA.pth")
model.load_state_dict(torch.load(model_path, map_location=device))
model.eval()
# Load an image
image = Image.open("image_path.jpg").convert("RGB")

# Preprocess and predict
def IQA_preprocess():
    random.seed(3407)
    transform = transforms.Compose([
      transforms.Resize((512,384)),
      transforms.RandomCrop(size=(224,224)),  
      transforms.ToTensor(),
      transforms.Normalize(mean=(0.48145466, 0.4578275, 0.40821073), 
                             std=(0.26862954, 0.26130258, 0.27577711))
    ])
    return transform

batch = torch.stack([IQA_preprocess()(image) for _ in range(15)]).to(device)  # Shape: (15, 3, 224, 224)

with torch.no_grad():
    scores = model(batch).cpu().numpy()
 
iqa_score = np.mean(scores)

# maps the predicted score to [0,1] range
min_pred = -6.52
max_pred = 3.11

normalized_score = ((iqa_score - min_pred) / (max_pred - min_pred))
print(f"Predicted quality Score: {normalized_score:.4f}")
```

## Citation

If you use this model in your research, please cite:

```bibtex
@article{zalcher2025don,
  title={Don't Judge Before You CLIP: A Unified Approach for Perceptual Tasks},
  author={Zalcher, Amit and Wasserman, Navve and Beliy, Roman and Heinimann, Oliver and Irani, Michal},
  journal={arXiv preprint arXiv:2503.13260},
  year={2025}
}