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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WWB]: Add ImageText-to-Image pipeline validation #1373

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
ad5865c
Added ImageText2Image class. Restructured code.
AlexKoff88 Dec 12, 2024
2cf506b
Added missed fuile
AlexKoff88 Dec 12, 2024
74a6fa2
Fixed issues
AlexKoff88 Dec 12, 2024
a382f8e
Tests
AlexKoff88 Dec 12, 2024
3d12a51
Merge branch 'master' into ak/wwb_inpainting
AlexKoff88 Dec 12, 2024
9f6913e
Stle
AlexKoff88 Dec 12, 2024
a2351da
Merge branch 'ak/wwb_inpainting' of https://github.com/AlexKoff88/ope…
AlexKoff88 Dec 12, 2024
c115dac
Aligned namings. Fixed tests
AlexKoff88 Dec 13, 2024
d76e93b
Merge branch 'master' into ak/wwb_inpainting
ilya-lavrenov Dec 13, 2024
50d3372
Merge branch 'master' into ak/wwb_inpainting
AlexKoff88 Dec 16, 2024
fca5b2a
Merge branch 'master' into ak/wwb_inpainting
AlexKoff88 Dec 16, 2024
e13dda6
Merge branch 'master' into ak/wwb_inpainting
AlexKoff88 Dec 16, 2024
12c5fa5
Merge remote-tracking branch 'origin/master' into ak/wwb_inpainting
AlexKoff88 Dec 17, 2024
3ad5e03
Replaced generator with GenAI version
AlexKoff88 Dec 17, 2024
6f478a2
Removed default resolution
AlexKoff88 Dec 17, 2024
a00b11d
Merge branch 'master' into ak/wwb_inpainting
AlexKoff88 Dec 17, 2024
150a0fa
Merge remote-tracking branch 'origin/master' into ak/wwb_inpainting
AlexKoff88 Dec 17, 2024
b925511
Merge branch 'ak/wwb_inpainting' of https://github.com/AlexKoff88/ope…
AlexKoff88 Dec 17, 2024
459101f
Removed resolution from Im2im pipeline
AlexKoff88 Dec 17, 2024
8e69378
Fixed discrepancy between im2im pipelines
AlexKoff88 Dec 18, 2024
2fa9530
Merge remote-tracking branch 'origin/master' into ak/wwb_inpainting
AlexKoff88 Dec 18, 2024
f213f47
Reverted tokenizer version
AlexKoff88 Dec 18, 2024
8fc8fb1
Merge branch 'master' into ak/wwb_inpainting
AlexKoff88 Dec 20, 2024
61a1447
Merge branch 'master' into ak/wwb_inpainting
AlexKoff88 Dec 23, 2024
bfc7419
Merge branch 'master' into ak/wwb_inpainting
AlexKoff88 Dec 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion tools/who_what_benchmark/tests/test_cli_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def run_wwb(args):
@pytest.mark.parametrize(
("model_id", "model_type", "backend"),
[
("hf-internal-testing/tiny-stable-diffusion-torch", "image-to-image", "hf"),
("hf-internal-testing/tiny-stable-diffusion-xl-pipe", "image-to-image", "hf"),
("hf-internal-testing/tiny-stable-diffusion-torch", "text-to-image", "hf"),
("hf-internal-testing/tiny-stable-diffusion-torch", "text-to-image", "openvino"),
("hf-internal-testing/tiny-stable-diffusion-xl-pipe", "text-to-image", "hf"),
Expand Down Expand Up @@ -65,6 +67,7 @@ def test_image_model_types(model_id, model_type, backend):
@pytest.mark.parametrize(
("model_id", "model_type"),
[
("echarlaix/tiny-random-stable-diffusion-xl", "image-to-image"),
("echarlaix/tiny-random-stable-diffusion-xl", "text-to-image"),
],
)
Expand All @@ -81,7 +84,7 @@ def test_image_model_genai(model_id, model_type):

wwb_args = [
"--base-model",
MODEL_PATH,
model_id,
"--num-samples",
"1",
"--gt-data",
Expand All @@ -90,6 +93,7 @@ def test_image_model_genai(model_id, model_type):
"CPU",
"--model-type",
model_type,
"--hf",
]
result = run_wwb(wwb_args)
assert result.returncode == 0
Expand Down Expand Up @@ -131,6 +135,7 @@ def test_image_model_genai(model_id, model_type):
model_type,
"--output",
output_dir,
"--genai",
]
result = run_wwb(wwb_args)
assert result.returncode == 0
Expand Down
2 changes: 2 additions & 0 deletions tools/who_what_benchmark/whowhatbench/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .text_evaluator import TextEvaluator as Evaluator
from .text2image_evaluator import Text2ImageEvaluator
from .visualtext_evaluator import VisualTextEvaluator
from .image2image import Image2ImageEvaluator


__all__ = [
Expand All @@ -11,5 +12,6 @@
"TextEvaluator",
"Text2ImageEvaluator",
"VisualTextEvaluator",
"Image2ImageEvaluator",
"EVALUATOR_REGISTRY",
]
129 changes: 129 additions & 0 deletions tools/who_what_benchmark/whowhatbench/image2image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import os
from typing import Any, Union

import datasets
import pandas as pd
from tqdm import tqdm
from transformers import set_seed
import torch
import openvino_genai

from .registry import register_evaluator
from .text2image_evaluator import Text2ImageEvaluator

from .whowhat_metrics import ImageSimilarity


def preprocess_fn(example):
return {
"prompts": example["Instruction_VLM-LLM"],
"images": example["source_img"],
}


def prepare_default_data(num_samples=None):
DATASET_NAME = "paint-by-inpaint/PIPE"
NUM_SAMPLES = 10 if num_samples is None else num_samples
set_seed(42)
default_dataset = datasets.load_dataset(
DATASET_NAME, split="test", streaming=True
).filter(lambda example: example["Instruction_VLM-LLM"] != "").take(NUM_SAMPLES)
return default_dataset.map(
lambda x: preprocess_fn(x), remove_columns=default_dataset.column_names
)


@register_evaluator("image-to-image")
class Image2ImageEvaluator(Text2ImageEvaluator):
def __init__(
self,
base_model: Any = None,
gt_data: str = None,
test_data: Union[str, list] = None,
metrics="similarity",
similarity_model_id: str = "openai/clip-vit-large-patch14",
num_inference_steps=4,
eaidova marked this conversation as resolved.
Show resolved Hide resolved
crop_prompts=True,
num_samples=None,
gen_image_fn=None,
seed=42,
is_genai=False,
) -> None:
assert (
base_model is not None or gt_data is not None
), "Text generation pipeline for evaluation or ground trush data must be defined"

self.test_data = test_data
self.metrics = metrics
self.crop_prompt = crop_prompts
self.num_samples = num_samples
self.num_inference_steps = num_inference_steps
self.seed = seed
self.similarity = None
self.similarity = ImageSimilarity(similarity_model_id)
self.last_cmp = None
self.gt_dir = os.path.dirname(gt_data)
self.generation_fn = gen_image_fn
self.is_genai = is_genai
self.resolution = None

if base_model:
self.gt_data = self._generate_data(
base_model, gen_image_fn, os.path.join(self.gt_dir, "reference")
)
else:
self.gt_data = pd.read_csv(gt_data, keep_default_na=False)

def _generate_data(self, model, gen_image_fn=None, image_dir="reference"):
def default_gen_image_fn(model, prompt, image, num_inference_steps, generator=None):
with torch.no_grad():
output = model(
prompt,
image=image,
num_inference_steps=num_inference_steps,
output_type="pil",
strength=0.8,
generator=generator,
)
return output.images[0]

generation_fn = gen_image_fn or default_gen_image_fn

if self.test_data:
if isinstance(self.test_data, str):
data = pd.read_csv(self.test_data)
else:
if isinstance(self.test_data, dict):
assert "prompts" in self.test_data
assert "images" in self.test_data
data = dict(self.test_data)
data = pd.DataFrame.from_dict(data)
else:
data = pd.DataFrame.from_dict(prepare_default_data(self.num_samples))

prompts = data["prompts"]
images = data["images"]
output_images = []
rng = torch.Generator(device="cpu")

if not os.path.exists(image_dir):
os.makedirs(image_dir)

for i, (prompt, image) in tqdm(enumerate(zip(prompts, images)), desc="Evaluate pipeline"):
set_seed(self.seed)
rng = rng.manual_seed(self.seed)
output = generation_fn(
model,
prompt,
image=image,
num_inference_steps=self.num_inference_steps,
generator=openvino_genai.TorchGenerator(self.seed) if self.is_genai else rng
)
image_path = os.path.join(image_dir, f"{i}.png")
output.save(image_path)
output_images.append(image_path)

res_data = {"prompts": list(prompts), "images": output_images}
df = pd.DataFrame(res_data)

return df
Loading
Loading