-
Notifications
You must be signed in to change notification settings - Fork 3
/
pipeline.py
163 lines (145 loc) · 5.23 KB
/
pipeline.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
import os
import cv2
import numpy as np
import shared
from shared import logging
from onediff import OneFlowStableDiffusionImg2ImgPipeline
from onediff import OneFlowStableDiffusionPipeline
import oneflow as flow
flow.mock_torch.enable()
from PIL import Image
from diffusers import (
DPMSolverMultistepScheduler,
)
logging.basicConfig(
level=shared.logging.INFO,
format="%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s",
filename="output.log",
filemode="w",
)
device_placement = shared.cmd_opts.device
model_id = "runwayml/stable-diffusion-v1-5"
class DiffusionImg2ImgPipelineHandler:
if not shared.cmd_opts.ui_debug_mode:
logging.info("OneFlowStableDiffusionImg2ImgPipeline initialization")
pipe = OneFlowStableDiffusionImg2ImgPipeline.from_pretrained(
model_id,
revision="fp16",
torch_dtype=flow.float16,
)
pipe.scheduler = DPMSolverMultistepScheduler.from_config(
model_id, subfolder="scheduler"
)
pipe = pipe.to(device_placement)
logging.info("OneFlowStableDiffusionImg2ImgPipeline initialization completed")
def __init__(
self,
prompt: str,
init_image: Image.Image,
strength: float = 0.8,
width: int = 768,
height: int = 768,
num_inference_steps: int = 50,
guidance_scale: float = 7.5,
negative_prompt: str = None,
num_images_per_prompt: int = 1,
eta=0.0,
seed: int = -1,
output_type="pil",
device_placement="cuda",
):
self.prompt = prompt
self.image = init_image.resize((width, height))
self.strength = strength
self.num_inference_steps = num_inference_steps
self.guidance_scale = guidance_scale
self.negative_prompt = negative_prompt
self.num_images_per_prompt = num_images_per_prompt
self.eta = eta
self.seed = seed
self.output_type = output_type
self.device_placement = device_placement
def __call__(self):
generator = None
if self.seed != -1:
generator = flow.Generator(device=device_placement)
generator.manual_seed(self.seed)
with flow.autocast("cuda"):
result = DiffusionImg2ImgPipelineHandler.pipe(
prompt=self.prompt,
image=self.image,
strength=self.strength,
num_inference_steps=self.num_inference_steps,
guidance_scale=self.guidance_scale,
negative_prompt=self.negative_prompt,
num_images_per_prompt=self.num_images_per_prompt,
eta=self.eta,
generator=generator,
output_type=self.output_type,
compile_unet=shared.cmd_opts.graph_mode,
).images
return result
class DiffusionPipelineHandler:
if not shared.cmd_opts.ui_debug_mode:
logging.info("OneFlowStableDiffusionPipeline initialization")
pipe = OneFlowStableDiffusionPipeline.from_pretrained(
model_id, revision="fp16", torch_dtype=flow.float16
)
pipe.scheduler = DPMSolverMultistepScheduler.from_config(
model_id, subfolder="scheduler"
)
pipe = pipe.to(device_placement)
logging.info("OneFlowStableDiffusionPipeline initialization completed")
def __init__(
self,
prompt: str,
width: int = 768,
height: int = 768,
num_inference_steps: int = 25,
guidance_scale: float = 7.5,
negative_prompt: str = None,
num_images_per_prompt: int = 1,
eta=0.0,
seed: int = -1,
output_type="pil",
device_placement="cuda",
):
self.prompt = prompt
self.width = width
self.hegiht = height
self.num_inference_steps = num_inference_steps
self.guidance_scale = guidance_scale
self.negative_prompt = negative_prompt
self.num_images_per_prompt = num_images_per_prompt
self.eta = eta
self.seed = seed
self.output_type = output_type
self.device_placement = device_placement
def __call__(self):
generator = None
if self.seed != -1:
generator = flow.Generator(device=device_placement)
generator.manual_seed(self.seed)
with flow.autocast("cuda"):
result = DiffusionPipelineHandler.pipe(
prompt=self.prompt,
height=self.hegiht,
width=self.width,
num_inference_steps=self.num_inference_steps,
guidance_scale=self.guidance_scale,
negative_prompt=self.negative_prompt,
num_images_per_prompt=self.num_images_per_prompt,
eta=self.eta,
generator=generator,
output_type=self.output_type,
compile_unet=shared.cmd_opts.graph_mode,
).images
return result
if __name__ == "__main__":
if not shared.cmd_opts.ui_debug_mode:
init_image = Image.open("test.jpg").convert("RGB")
phandler = DiffusionImg2ImgPipelineHandler(
"a dog with glasses", init_image=init_image
)
imgs = phandler()
imgs[0].save("demo.png")