Skip to content

Commit e3eef01

Browse files
authored
move sd notebook to genai (#2730)
CVS-161646
1 parent a852a4b commit e3eef01

File tree

5 files changed

+1199
-203
lines changed

5 files changed

+1199
-203
lines changed

.ci/ignore_treon_docker.txt

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ notebooks/model-server/model-server.ipynb
44
notebooks/quantizing-model-with-accuracy-control/speech-recognition-quantization-wav2vec2.ipynb
55
notebooks/quantizing-model-with-accuracy-control/yolov11-quantization-with-accuracy-control.ipynb
66
notebooks/big-transfer-quantization/tensorflow-bit-image-classification-nncf-quantization.ipynb
7-
notebooks/stable-diffusion-text-to-image/stable-diffusion-text-to-image.ipynb
87
notebooks/clip-zero-shot-image-classification/clip-zero-shot-classification.ipynb
98
notebooks/instruct-pix2pix-image-editing/instruct-pix2pix-image-editing.ipynb
109
notebooks/blip-visual-language-processing/blip-visual-language-processing.ipynb

.ci/skipped_notebooks.yml

-7
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,6 @@
5050
- os:
5151
- ubuntu-20.04
5252
- ubuntu-22.04
53-
- notebook: notebooks/stable-diffusion-text-to-image/stable-diffusion-text-to-image.ipynb
54-
skips:
55-
- os:
56-
- macos-13
57-
- ubuntu-20.04
58-
- ubuntu-22.04
59-
- windows-2019
6053
- notebook: notebooks/clip-zero-shot-image-classification/clip-zero-shot-classification.ipynb
6154
skips:
6255
- os:

notebooks/stable-diffusion-text-to-image/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ The following image shows an example of the input sequence and corresponding pre
2626

2727
## Notebook Contents
2828

29-
This notebook demonstrates how to convert and run stable diffusion using OpenVINO.
29+
This notebook demonstrates how to convert and run stable diffusion using OpenVINO. For user experience simplification, we will use [Hugging Face Optimum](https://huggingface.co/docs/optimum/installation) library accelerated by OpenVINO integration for model conversion and [OpenVINO GenAI API](https://docs.openvino.ai/2024/learn-openvino/llm_inference_guide/genai-guide.html) for model inference.
3030

3131
Notebook contains the following parts:
3232
1. Download the model from the Hugging Face Hub and converted to OpenVINO IR format with [Optimum Intel](https://huggingface.co/docs/optimum/intel/inference#stable-diffusion).

notebooks/stable-diffusion-text-to-image/gradio_helper.py

+70-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,76 @@
11
import gradio as gr
22
import numpy as np
3+
from tqdm.auto import tqdm
4+
import openvino as ov
5+
import openvino_genai as ov_genai
6+
import sys
7+
from PIL import Image
38

9+
sample_text = (
10+
"cyberpunk cityscape like Tokyo New York with tall buildings at dusk golden hour cinematic lighting, epic composition. "
11+
"A golden daylight, hyper-realistic environment. "
12+
"Hyper and intricate detail, photo-realistic. "
13+
"Cinematic and volumetric light. "
14+
"Epic concept art. "
15+
"Octane render and Unreal Engine, trending on artstation"
16+
)
417

5-
def make_demo(pipeline, preprocess, postprocess, default_image_path):
18+
19+
def image_to_tensor(image) -> ov.Tensor:
20+
pic = image.convert("RGB")
21+
image_data = np.array(pic.getdata()).reshape(1, pic.size[1], pic.size[0], 3).astype(np.uint8)
22+
return ov.Tensor(image_data)
23+
24+
25+
def make_demo(pipeline):
26+
def generate_from_text(text, seed, num_steps, _=gr.Progress(track_tqdm=True)):
27+
random_generator = ov_genai.TorchGenerator(seed)
28+
29+
pbar = tqdm(total=num_steps)
30+
31+
def callback(step, num_steps, latent):
32+
if num_steps != pbar.total:
33+
pbar.reset(num_steps)
34+
pbar.update(1)
35+
sys.stdout.flush()
36+
return False
37+
38+
result = pipeline.generate(text, num_inference_steps=num_steps, generator=random_generator, callback=callback)
39+
40+
pbar.close()
41+
return Image.fromarray(result.data[0])
42+
43+
with gr.Blocks() as demo:
44+
with gr.Tab("Text-to-Image generation"):
45+
with gr.Row():
46+
with gr.Column():
47+
text_input = gr.Textbox(lines=3, label="Text")
48+
seed_input = gr.Slider(0, 10000000, value=42, step=1, label="Seed")
49+
steps_input = gr.Slider(1, 50, value=20, step=1, label="Steps")
50+
out = gr.Image(label="Result", type="pil")
51+
btn = gr.Button()
52+
btn.click(generate_from_text, [text_input, seed_input, steps_input], out)
53+
gr.Examples([[sample_text, 42, 20]], [text_input, seed_input, steps_input])
54+
return demo
55+
56+
57+
def make_demo_i2i(pipeline, default_image_path):
658
def generate_from_image(img, text, seed, num_steps, strength, _=gr.Progress(track_tqdm=True)):
7-
preprocessed_img, meta_data = preprocess(img)
8-
np.random.seed(seed)
9-
result = pipeline(text, preprocessed_img, num_inference_steps=num_steps, strength=strength)
10-
result_img = postprocess(result["images"][0], meta_data["src_width"], meta_data["src_height"])
11-
return result_img
59+
img_tensor = image_to_tensor(img)
60+
61+
pbar = tqdm(total=int(num_steps * strength) + 1)
62+
63+
def callback(step, num_steps, latent):
64+
if num_steps != pbar.total:
65+
pbar.reset(num_steps)
66+
pbar.update(1)
67+
sys.stdout.flush()
68+
return False
69+
70+
random_generator = ov_genai.TorchGenerator(seed)
71+
result = pipeline.generate(text, img_tensor, num_inference_steps=num_steps, strength=strength, generator=random_generator, callaback=callback)
72+
pbar.close()
73+
return Image.fromarray(result.data[0])
1274

1375
with gr.Blocks() as demo:
1476
with gr.Tab("Image-to-Image generation"):
@@ -18,7 +80,7 @@ def generate_from_image(img, text, seed, num_steps, strength, _=gr.Progress(trac
1880
i2i_text_input = gr.Textbox(lines=3, label="Text")
1981
i2i_seed_input = gr.Slider(0, 1024, value=42, step=1, label="Seed")
2082
i2i_steps_input = gr.Slider(1, 50, value=10, step=1, label="Steps")
21-
strength_input = gr.Slider(0, 1, value=0.5, label="Strength")
83+
strength_input = gr.Slider(0, 1, value=0.25, label="Strength")
2284
i2i_out = gr.Image(label="Result")
2385
i2i_btn = gr.Button()
2486
sample_i2i_text = "amazing watercolor painting"
@@ -34,7 +96,7 @@ def generate_from_image(img, text, seed, num_steps, strength, _=gr.Progress(trac
3496
i2i_out,
3597
)
3698
gr.Examples(
37-
[[str(default_image_path), sample_i2i_text, 42, 10, 0.5]],
99+
[[str(default_image_path), sample_i2i_text, 42, 10, 0.25]],
38100
[
39101
i2i_input,
40102
i2i_text_input,

0 commit comments

Comments
 (0)