-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamdiff.py
169 lines (126 loc) · 4.64 KB
/
streamdiff.py
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
168
169
# -*- coding: utf-8 -*-
"""StreamDiffusionImg2Img_colab.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/114zR1eeJfXH4aotosubtiWlBSz9lWgja
"""
# Commented out IPython magic to ensure Python compatibility.
# %load_ext autoreload
# %autoreload 2
# Commented out IPython magic to ensure Python compatibility.
# %cd /content
import torch
from diffusers import AutoPipelineForInpainting, AutoencoderTiny, StableDiffusionPipeline, StableDiffusionInpaintPipeline
from diffusers.utils import load_image, make_image_grid
print(torch.cuda.is_available())
init_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/inpaint.png")
mask_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/inpaint_mask.png")
print(mask_image)
import cv2
from PIL import Image
import numpy as np
import os
import sys
# sys.path.append("/content/StreamDiffusion")
sys.path.append("./StreamDiffusion")
sys.path.append("./StreamDiffusion/src")
# sys.path.append("./RealStream")
# sys.path.append("./RealStream/src")
from streamdiffusion.pipeline import StreamDiffusion
from streamdiffusion.image_utils import postprocess_image, process_image
from utils.wrapper import StreamDiffusionWrapper
# Wrap the pipeline in StreamDiffusion
# Requires more long steps (len(t_index_list)) in text2image
# You should use cfg_type="none" when text2image
# stream = StreamDiffusionWrapper(
# model_id_or_path="stabilityai/sd-turbo",
# lora_dict=None,
# t_index_list=[5,22,32,45],
# frame_buffer_size=1,
# width=512,
# height=512,
# warmup=1,
# acceleration="tensorrt",
# mode="img2img",
# use_denoising_batch=True,
# cfg_type="self",
# seed=2,
# )
stream = StreamDiffusionWrapper(
model_id_or_path="stabilityai/sd-turbo",
lora_dict=None,
t_index_list=[22, 32, 45],
frame_buffer_size=1,
width=512,
height=512,
warmup=2,
acceleration="xformers",
mode="img2img",
use_denoising_batch=True,
cfg_type="self",
seed=2,
)
prompt = "realistic, batman, mask"
# prompt = "realistic, mount rushmore"
# prompt = "realistic, volcano, magma, lava, fire, red"
negative_prompt = "cartoon, smoke, grey"
# Prepare the stream
stream.prepare(
prompt=prompt,
negative_prompt=negative_prompt,
num_inference_steps=50,
guidance_scale=1.2,
delta=0.5,
)
# Prepare image
# init_image = load_image("/content/StreamDiffusion/assets/img2img_example.png").resize((512, 512))
# print(image_tensor.size())
def setprompt(prompt, negative_prompt):
stream.prepare(
prompt=prompt,
negative_prompt=negative_prompt,
num_inference_steps=50,
guidance_scale=1.2,
delta=0.5,
)
def streamdiffusion(image, mask=None):
image = Image.fromarray(image)
maskArr = np.array(mask)
# print(maskArr.shape, maskArr[256][256])
# print(maskArr.shape)
# make 3 channels (512, 512) -> (512, 512, 3) with numpy
# print(maskArr.shape)
maskArr = np.stack((maskArr,)*3, axis=-1)
# print(maskArr.shape)
# print(maskArr.shape)
blurred_mask_image = cv2.blur(maskArr, (35,35))
# print(blurred_mask_image.shape)
# make 3 channels (512, 512) -> (512, 512, 3)
# blurred_mask_image = cv2.cvtColor(blurred_mask_image, cv2.COLOR_GRAY2RGB)
# blurred_mask_image = Image.fromarray(blurred_mask_image)
blurred_mask_image = np.array(blurred_mask_image)/255
# print(blurred_mask_image.shape, blurred_mask_image[256][256][0])
process_start = cv2.getTickCount()
image_tensor = stream.preprocess_image(image)
process_end = cv2.getTickCount()
process_fps = cv2.getTickFrequency() / (process_end - process_start)
# print("PROCESS FPS:", process_fps)
stream_start = cv2.getTickCount()
output_image = stream(image_tensor, mask=blurred_mask_image)
# output_image = stream(image_tensor)
stream_end = cv2.getTickCount()
stream_fps = cv2.getTickFrequency() / (stream_end - stream_start)
# print("STREAM FPS:", stream_fps)
mask = (np.array(blurred_mask_image)).astype(np.float16)
output_arr = np.array(output_image)
# print(mask.shape)
# print(mask[256][256][0])
# print(output_arr.shape)
fixed = output_arr * mask + image * (1-mask)
fixed = fixed.astype(np.uint8)
# output_image
return Image.fromarray(fixed)
# if main
if __name__ == "__main__":
while True:
streamdiffusion(np.array(init_image), mask_image)