File size: 5,309 Bytes
9e9fb6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9aff4b8
9e9fb6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13e3eda
 
9e9fb6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c8dc44
9e9fb6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import io
import base64
from typing import List, Tuple

import numpy as np
import gradio as gr
from datasets import load_dataset
from transformers import AutoProcessor, AutoModel
import torch
from PIL import Image

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

# Load example dataset
dataset = load_dataset("xzuyn/dalle-3_vs_sd-v1-5_dpo", num_proc=4)

processor_name = "laion/CLIP-ViT-H-14-laion2B-s32B-b79K"
model_name = "yuvalkirstain/PickScore_v1"
processor = AutoProcessor.from_pretrained(processor_name)
model = AutoModel.from_pretrained(model_name, torch_dtype=dtype).to(device)


def decode_image(image: str) -> Image:
    """
    Decodes base64 string to PIL image.
    Args:
        image: base64 string
    Returns:
        PIL image
    """
    img_byte_arr = base64.b64decode(image)
    img_byte_arr = io.BytesIO(img_byte_arr)
    img_byte_arr = Image.open(img_byte_arr)
    return img_byte_arr


def get_preference(img_1: Image.Image, img_2: Image.Image, caption: str) -> Image.Image:
    """
    Returns the preference of the caption for the two images.
    Args:
        img_1: PIL image
        img_2: PIL image
        caption: string
    Returns:
        preference image: PIL image
    """
    imgs = [img_1, img_2]
    logits = get_logits(caption, imgs)
    preference = logits.argmax().item()

    return imgs[preference]


def sample_example() -> Tuple[Image.Image, Image.Image, Image.Image, str]:
    """
    Samples a random example from the dataset and displays it.

    Returns:
        img_1: PIL image
        img_2: PIL image
        preference: PIL image
        caption: string
    """
    example = dataset["train"][np.random.randint(0, len(dataset["train"]))]
    img_1 = decode_image(example["jpg_0"])
    img_2 = decode_image(example["jpg_1"])
    caption = example["caption"]
    imgs = [img_1, img_2]
    logits = get_logits(caption, imgs)
    preference = logits.argmax().item()
    return (img_1, img_2, imgs[preference], caption)


def get_logits(caption: str, imgs: List[Image.Image]) -> torch.Tensor:
    """
    Returns the logits for the caption and images.

    Args:
        caption: string
        imgs: list of PIL images
    Returns:
        logits: torch.Tensor
    """

    inputs = processor(
        text=caption,
        images=imgs,
        return_tensors="pt",
        padding=True,
        truncation=True,
        max_length=77,
    ).to(device)
    inputs["pixel_values"] = (
        inputs["pixel_values"].half() if device == "cuda" else inputs["pixel_values"]
    )
    with torch.no_grad():
        outputs = model(**inputs)
    logits_per_image = outputs.logits_per_image

    return logits_per_image


### Description
title = r"""
<h1 align="center">Aesthetic Scorer: CLIP fine-tuned for DPO scoring </h1>
"""

description = r"""
<b> This is a demo for the paper <a href="https://arxiv.org/abs/2109.04436">Pick-a-Pic: An Open Dataset of User Preferences for Text-to-Image Generation </a> </b> <br>

How to use this demo: <br>
1. Upload two images generated using the same caption.
2. Enter the caption used to generate the images.
3. Click on the "Get Preference" button to get the image which scores higher on user preferences according to the model. <br>
<b> OR </b> <br>
1. Click on the "Random Example" button to get a random example from a <a href="https://huggingface.co/datasets/xzuyn/dalle-3_vs_sd-v1-5_dpo">Dalle 3 vs SD 1.5 DPO dataset. </a><br>

This demo demonstrates the use of this CLIP variant for DPO scoring. The scores can then be used for DPO fine-tuning with these <a href="https://github.com/huggingface/diffusers/tree/main/examples/research_projects/diffusion_dpo">scripts. </a><br>

Accuracy on the <a href="https://huggingface.co/datasets/xzuyn/dalle-3_vs_sd-v1-5_dpo">Dalle 3 vs SD 1.5 DPO dataset: </a><br>
<a href="https://huggingface.co/yuvalkirstain/PickScore_v1">PickScore_v1</a> - 97.3 <br>
<a href="https://huggingface.co/CIDAS/clipseg-rd64-refined">CLIPSeg</a> - 70.9 <br>
<a href="https://huggingface.co/laion/CLIP-ViT-H-14-laion2B-s32B-b79K">CLIP-ViT-H-14-laion2B-s32B-b79K</a> - 82.3 <br>
"""

citation = r"""
πŸ“ **Citation**
```bibtex
@inproceedings{Kirstain2023PickaPicAO,
  title={Pick-a-Pic: An Open Dataset of User Preferences for Text-to-Image Generation},
  author={Yuval Kirstain and Adam Polyak and Uriel Singer and Shahbuland Matiana and Joe Penna and Omer Levy},
  year={2023}
}
```
"""

with gr.Blocks() as demo:
    gr.Markdown(title)
    gr.Markdown(description)

    with gr.Row():
        first_image = gr.Image(height=400, width=400, label="First Image")
        second_image = gr.Image(height=400, width=400, label="Second Image")

    caption_box = gr.Textbox(lines=1, label="Caption")

    with gr.Row():
        image_button = gr.Button("Get Preference")
        random_example = gr.Button("Random Example")

    image_output = gr.Image(height=400, width=400, label="Preference")

    image_button.click(
        get_preference,
        inputs=[first_image, second_image, caption_box],
        outputs=image_output,
    )

    random_example.click(
        sample_example, outputs=[first_image, second_image, image_output, caption_box]
    )

    gr.Markdown(citation)

if __name__ == "__main__":
    demo.launch()