Skip to main content

A small example package

Project description

Imscore

Work by RE-N-Y and friends @ krea.ai

teaser

Imscore is a minimal library curating a set of fully differentiable aesthetic and preference scorers for images. We provide a set of popular scorers such as PickScore, MPS, HPSv2, and LAION aesthetic scorer as well as our own models trained on open source preference datasets.

imscore allows ...

  1. Benchmarking your generative models on aesthetic and preference scorers.
  2. Post training your generative models to align with human preference.
  3. Prevent headaches of porting over models from different repositories.

Installation

pip install imscore

Usage

from imscore.aesthetic.model import ShadowAesthetic, LAIONAestheticScorer
from imscore.hps.model import HPSv2
from imscore.mps.model import MPS
from imscore.preference.model import SiglipPreferenceScorer, CLIPScore
from imscore.pickscore.model import PickScorer
from imscore.imreward.model import ImageReward
from imscore.vqascore.model import VQAScore
from imscore.cyclereward.model import CycleReward
from imscore.evalmuse.model import EvalMuse
from imscore.hpsv3.model import HPSv3

import torch
import numpy as np
from PIL import Image
from einops import rearrange

# popular aesthetic/preference scorers
model = ShadowAesthetic.from_pretrained("RE-N-Y/aesthetic-shadow-v2") # ShadowAesthetic aesthetic scorer (my favorite)
model = CLIPScore.from_pretrained("RE-N-Y/clipscore-vit-large-patch14") # CLIPScore
model = PickScorer.from_pretrained("RE-N-Y/pickscore") # PickScore preference scorer
model = MPS.from_pretrained("RE-N-Y/mpsv1") # MPS (ovreall) preference scorer
model = HPSv2.from_pretrained("RE-N-Y/hpsv21") # HPSv2.1 preference scorer
model = ImageReward.from_pretrained("RE-N-Y/ImageReward") # ImageReward aesthetic scorer
model = LAIONAestheticScorer.from_pretrained("RE-N-Y/laion-aesthetic") # LAION aesthetic scorer
model = CycleReward.from_pretrained('NagaSaiAbhinay/CycleReward-Combo') # CycleReward preference scorer.
model = VQAScore.from_pretrained("RE-N-Y/clip-t5-xxl")
model = EvalMuse.from_pretrained("RE-N-Y/evalmuse")
mode = HPSv3.from_pretrained("RE-N-Y/hpsv3")

# multimodal (pixels + text) preference scorers trained on PickaPicv2 dataset 
model = SiglipPreferenceScorer.from_pretrained("RE-N-Y/pickscore-siglip")

prompt = "a photo of a cat"
pixels = Image.open("cat.jpg")
pixels = np.array(pixels)
pixels = rearrange(torch.tensor(pixels), "h w c -> 1 c h w") / 255.0

# prompts and pixels should have the same batch dimension
# pixels should be in the range [0, 1]
# score == logits
score = model.score(pixels, [prompt]) # full differentiable reward

Post Training for Generative Models

import torch
from imscore.preference.model import SiglipPreferenceScorer

G = model() # your generative model
dataloader = ... # your dataloader with conditioning (ex.prompts)

rm = SiglipPreferenceScorer.from_pretrained("RE-N-Y/pickscore-siglip") # pretrained preference model
optim = torch.optim.AdamW(G.parameters(), lr=3e-4)

# post training
for prompts in dataloader:
    optim.zero_grad()
    
    images = G(prompts)
    scores = rm.score(images, prompts) # ensure images are in the range [0, 1]
    loss = -scores.mean() # maximise reward

    loss.backward()
    optim.step()

List of available models

Aesthetic Scorers

from imscore.aesthetic.model import ShadowAesthetic, CLIPAestheticScorer, SiglipAestheticScorer, Dinov2AestheticScorer, LAIONAestheticScorer

# pixel only scorers trained on imscore dataset's aesthetic rating
SiglipAestheticScorer.from_pretrained("RE-N-Y/imreward-fidelity_rating-siglip")
CLIPAestheticScorer.from_pretrained("RE-N-Y/imreward-fidelity_rating-clip")
Dinov2AestheticScorer.from_pretrained("RE-N-Y/imreward-fidelity_rating-dinov2")

# pixel only scorers trained on imreward dataset's overall rating
SiglipAestheticScorer.from_pretrained("RE-N-Y/imreward-overall_rating-siglip")
CLIPAestheticScorer.from_pretrained("RE-N-Y/imreward-overall_rating-clip")
Dinov2AestheticScorer.from_pretrained("RE-N-Y/imreward-overall_rating-dinov2")

# pixel only scorers trained on AVA dataset
CLIPAestheticScorer.from_pretrained("RE-N-Y/ava-rating-clip-sampled-True")
CLIPAestheticScorer.from_pretrained("RE-N-Y/ava-rating-clip-sampled-False")
SiglipAestheticScorer.from_pretrained("RE-N-Y/ava-rating-siglip-sampled-True")
SiglipAestheticScorer.from_pretrained("RE-N-Y/ava-rating-siglip-sampled-False")
Dinov2AestheticScorer.from_pretrained("RE-N-Y/ava-rating-dinov2-sampled-True")
Dinov2AestheticScorer.from_pretrained("RE-N-Y/ava-rating-dinov2-sampled-False")

# Common aesthetic scorers
LAIONAestheticScorer.from_pretrained("RE-N-Y/laion-aesthetic") # LAION aesthetic scorer
ShadowAesthetic.from_pretrained("RE-N-Y/aesthetic-shadow-v2") # ShadowAesthetic aesthetic scorer for anime images

Preference Scorers

from imscore.hps.model import HPSv2
from imscore.mps.model import MPS
from imscore.imreward.model import ImageReward
from imscore.preference.model import SiglipPreferenceScorer, CLIPPreferenceScorer
from imscore.pickscore.model import PickScorer
from imscore.cyclereward.model import CycleReward

HPSv2.from_pretrained("RE-N-Y/hpsv21") # HPSv2.1 preference scorer
MPS.from_pretrained("RE-N-Y/mpsv1") # MPS (ovreall) preference scorer
PickScorer("yuvalkirstain/PickScore_v1") # PickScore preference scorer
ImageReward.from_pretrained("RE-N-Y/ImageReward") # ImageReward preference scorer
CycleReward.from_pretrained('NagaSaiAbhinay/CycleReward-Combo') # CycleReward preference scorer trained on combined CyclePrefDB
CycleReward.from_pretrained('NagaSaiAbhinay/CycleReward-T2I')# CycleReward preference scorer trained on CyclePrefDB-T2I only
CycleReward.from_pretrained('NagaSaiAbhinay/CycleReward-I2T')
# CycleReward preference scorer trained on CyclePrefDB-I2T only

# multimodal scorers trained on PickAPicv2 dataset
SiglipPreferenceScorer.from_pretrained("RE-N-Y/pickscore-siglip")
CLIPPreferenceScorer.from_pretrained("RE-N-Y/pickscore-clip")

Benchmark on Preference datasets

Model HPD v2 Accuracy ImageReward Accuracy Pickapicv2 Accuracy Average
pickscore 0.792157 0.667448 0.801887 0.753831
hpsv21 0.833464 0.674793 0.693396 0.733884
mpsv1 0.838562 0.677762 0.650943 0.722423
imreward 0.740131 0.657915 0.608491 0.668846
laion-aesthetic 0.736013 0.566807 0.551887 0.618236
clipscore 0.626078 0.571652 0.606132 0.601287

For full benchmark results and methodology, please refer to bench.md.

Differenes between original and ported versions

Model Mean Error Mean Error %
pickscore 0.0364375 0.1697%
mps 0.1221515 1.3487%
hps 0.0010474 0.3733%
laion 0.0202606 0.3461%
imreward 0.0135808 0.7608%
clip-t5-xxl 0.0111 2.0091%
evalmuse xxx xxx
hpsv3 xxx xxx

imscore library ports popular scorers such as PickScore, MPS, HPSv2, etc. In order to ensure that .score function is (1) fully differentiable and (2) takes pixels of range [0, 1], the image processing pipeline had to be modified. The above table reports the mean and standard error between the original and ported versions.

Most ported models have a mean absolute error less than < 1% w.r.t original output. These statistics were computed on PickAPicv2 test unique set images.

Why did I make this?

  1. To save myself headaches.
  2. To provide a common interface for dataset filtering, posttraining, and image model benchmarking.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

imscore-0.0.11.tar.gz (25.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

imscore-0.0.11-py3-none-any.whl (33.2 kB view details)

Uploaded Python 3

File details

Details for the file imscore-0.0.11.tar.gz.

File metadata

  • Download URL: imscore-0.0.11.tar.gz
  • Upload date:
  • Size: 25.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for imscore-0.0.11.tar.gz
Algorithm Hash digest
SHA256 070f5bad3efafc61d8ca6ffafc6fe8067b619c4c822968e3e4ec8a39c37b226e
MD5 90c7b3e33092c228b54244de89f26095
BLAKE2b-256 6d242309a514bf637887aec356e70513ef125ed3a21e418eb314870f434b0738

See more details on using hashes here.

Provenance

The following attestation bundles were made for imscore-0.0.11.tar.gz:

Publisher: python-publish.yml on RE-N-Y/imscore

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file imscore-0.0.11-py3-none-any.whl.

File metadata

  • Download URL: imscore-0.0.11-py3-none-any.whl
  • Upload date:
  • Size: 33.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for imscore-0.0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 2ce9577732f53a2e2380ad35fed4f4f5bea26efa750b6cd86f1d3902090870dc
MD5 5e6659083217c28b0c1362a120e22b32
BLAKE2b-256 46ad4d16b176e9c014f56bf70c85c57ebe2450892b7fcfc29f9154792dd8abbd

See more details on using hashes here.

Provenance

The following attestation bundles were made for imscore-0.0.11-py3-none-any.whl:

Publisher: python-publish.yml on RE-N-Y/imscore

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page