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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CLIP score #1311

Closed
chinoll opened this issue Nov 3, 2022 · 5 comments 路 Fixed by #1314
Closed

Add CLIP score #1311

chinoll opened this issue Nov 3, 2022 · 5 comments 路 Fixed by #1314
Assignees
Labels
Milestone

Comments

@chinoll
Copy link

chinoll commented Nov 3, 2022

馃殌 Feature

Calculate the correlation between image and text

Motivation

Evaluating the performance of the text2image model

Pitch

pytorch-like Pseudocode

def clip_score(img_inputs, txt_inputs):
    img_features = clip.get_image_features(img_inputs)
    txt_features = clip.get_text_features(txt_inputs)
    img_features, txt_features = [
        x / torch.linalg.norm(x, axis=-1, keepdims=True)
        for x in [img_features, txt_features]
    ]
    return (img_features * txt_features).sum(axis=-1)

Alternatives

Additional context

clip score

@chinoll chinoll added the enhancement New feature or request label Nov 3, 2022
@github-actions
Copy link

github-actions bot commented Nov 3, 2022

Hi! thanks for your contribution!, great first issue!

@stancld
Copy link
Contributor

stancld commented Nov 3, 2022

Hi @chinoll, this sounds like a nice addition to add the first multi-model metric. Would you have please any reference implementation?

@SkafteNicki
Copy link
Member

Here is at least one reference implementation:
https://github.com/mehdidc/DALLE_clip_score

@chinoll
Copy link
Author

chinoll commented Nov 4, 2022

Hi @chinoll, this sounds like a nice addition to add the first multi-model metric. Would you have please any reference implementation?

simple pytorch implementation,Reference CLIP-score-vs-FID-pareto-curves

from transformers import CLIPModel,CLIPTokenizer,CLIPFeatureExtractor
import torch
import PIL
version = "openai/clip-vit-large-patch14"
tokenizer = CLIPTokenizer.from_pretrained(version)
model = CLIPModel.from_pretrained(version)
feature_extractor = CLIPFeatureExtractor.from_pretrained(version)

def clip_score(text:str, image:PIL.Image):
    txt_features = model.get_text_features(tokenizer(text,return_tensors="pt")["input_ids"])
    img_features = model.get_image_features(torch.tensor(feature_extractor(image)['pixel_values'][0][None]))
    img_features, txt_features = [
        x / torch.linalg.norm(x, axis=-1, keepdims=True)
        for x in [img_features, txt_features]
    ]
    return (img_features * txt_features).sum(axis=-1)

@SkafteNicki SkafteNicki mentioned this issue Nov 5, 2022
4 tasks
@SkafteNicki
Copy link
Member

I started the work of adding the metric in #1314

@SkafteNicki SkafteNicki self-assigned this Nov 5, 2022
@stancld stancld added this to the v0.11 milestone Nov 6, 2022
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 a pull request may close this issue.

4 participants