This repository has been archived by the owner on Jul 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
225 lines (193 loc) · 7.69 KB
/
model.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# pylint: skip-file
import os
TORCH_GPU_DEVICE_ID = 0
os.environ["CUDA_VISIBLE_DEVICES"] = f"{TORCH_GPU_DEVICE_ID}"
import time
import random
from PIL import Image
import numpy as np
import torch
import cv2
import diffusers
from instill.helpers.const import DataType, ImageToImageInput
from instill.helpers.ray_io import StandardTaskIO
from instill.helpers.ray_config import instill_deployment, InstillDeployable
from instill.helpers import (
construct_infer_response,
construct_metadata_response,
Metadata,
)
@instill_deployment
class ControlNet:
def __init__(self):
print(f"torch version: {torch.__version__}")
print(f"torch.cuda.is_available() : {torch.cuda.is_available()}")
print(f"torch.cuda.device_count() : {torch.cuda.device_count()}")
print(f"torch.cuda.current_device() : {torch.cuda.current_device()}")
print(f"torch.cuda.device(0) : {torch.cuda.device(0)}")
print(f"torch.cuda.get_device_name(0) : {torch.cuda.get_device_name(0)}")
controlnet = diffusers.ControlNetModel.from_pretrained(
"sd-controlnet-canny", torch_dtype=torch.float16, use_safetensors=True
)
self.pipe = diffusers.StableDiffusionControlNetPipeline.from_pretrained(
"stable-diffusion-v1-5",
controlnet=controlnet,
safety_checker=None,
torch_dtype=torch.float16,
use_safetensors=True,
)
self.pipe.scheduler = diffusers.UniPCMultistepScheduler.from_config(
self.pipe.scheduler.config
)
self.pipe.enable_model_cpu_offload()
def ModelMetadata(self, req):
resp = construct_metadata_response(
req=req,
inputs=[
Metadata(
name="prompt",
datatype=str(DataType.TYPE_STRING.name),
shape=[1],
),
# TODO: Check Wether `negative_prompt` is needed?
# model-bakcend supports negative_prompt but not Python-Sdk
Metadata(
name="negative_prompt",
datatype=str(DataType.TYPE_STRING.name),
shape=[1],
),
Metadata(
name="prompt_image",
datatype=str(DataType.TYPE_STRING.name),
shape=[1],
),
Metadata(
name="samples",
datatype=str(DataType.TYPE_INT32.name),
shape=[1],
),
Metadata(
name="scheduler",
datatype=str(DataType.TYPE_STRING.name),
shape=[1],
),
Metadata(
name="steps",
datatype=str(DataType.TYPE_INT32.name),
shape=[1],
),
Metadata(
name="guidance_scale",
datatype=str(DataType.TYPE_FP32.name),
shape=[1],
),
Metadata(
name="seed",
datatype=str(DataType.TYPE_INT64.name),
shape=[1],
),
Metadata(
name="extra_params",
datatype=str(DataType.TYPE_STRING.name),
shape=[1],
),
],
outputs=[
Metadata(
name="images",
datatype=str(DataType.TYPE_FP32.name),
shape=[-1, -1, -1, -1],
),
],
)
return resp
async def __call__(self, req):
task_image_to_image_input: ImageToImageInput = (
StandardTaskIO.parse_task_image_to_image_input(request=req)
)
print("----------------________")
print(task_image_to_image_input)
print("----------------________")
print("print(task_image_to_image_input.prompt_image)")
print(task_image_to_image_input.prompt_image)
print(task_image_to_image_input.prompt_image.shape)
print("-------\n")
print("print(task_image_to_image_input.prompt)")
print(task_image_to_image_input.prompt)
print("-------\n")
# print("print(task_image_to_image_input.negative_prompt)")
# print(task_image_to_image_input.negative_prompt)
# print("-------\n")
print("print(task_image_to_image_input.steps)")
print(task_image_to_image_input.steps)
print("-------\n")
print("print(task_image_to_image_input.guidance_scale)")
print(task_image_to_image_input.guidance_scale)
print("-------\n")
print("print(task_image_to_image_input.seed)")
print(task_image_to_image_input.seed)
print("-------\n")
print("print(task_image_to_image_input.samples)")
print(task_image_to_image_input.samples)
print("-------\n")
print("print(task_image_to_image_input.extra_params)")
print(task_image_to_image_input.extra_params)
print("-------\n")
if task_image_to_image_input.seed > 0:
random.seed(task_image_to_image_input.seed)
np.random.seed(task_image_to_image_input.seed)
# torch.manual_seed(task_image_to_image_input.seed)
# if torch.cuda.is_available():
# torch.cuda.manual_seed_all(task_image_to_image_input.seed)
low_threshold = 100
if "low_threshold" in task_image_to_image_input.extra_params:
low_threshold = task_image_to_image_input.extra_params["low_threshold"]
high_threshold = 200
if "high_threshold" in task_image_to_image_input.extra_params:
high_threshold = task_image_to_image_input.extra_params["high_threshold"]
num_inference_steps = task_image_to_image_input.steps
if "num_inference_steps" in task_image_to_image_input.extra_params:
num_inference_steps = task_image_to_image_input.extra_params[
"um_inference_steps"
]
t0 = time.time()
processed_image = cv2.Canny(
task_image_to_image_input.prompt_image, low_threshold, high_threshold
)
processed_image = processed_image[:, :, None]
processed_image = np.concatenate(
[processed_image, processed_image, processed_image], axis=2
)
canny_image = Image.fromarray(processed_image)
print("canny_image")
print(canny_image)
# https://github.com/huggingface/diffusers/blob/ea9dc3fa90c70c7cd825ca2346a31153e08b5367/src/diffusers/pipelines/controlnet/pipeline_controlnet.py#L900
# `(batch_size, height, width, num_channels)`
output_arr = self.pipe(
task_image_to_image_input.prompt,
image=canny_image,
# negative_prompt
height=canny_image.height,
width=canny_image.width,
num_inference_steps=num_inference_steps,
generator=torch.manual_seed(2),
output_type="np",
).images
print("Output:")
output_arr_tansponse = output_arr
response_shape = list(output_arr_tansponse.shape)
task_output = output_arr_tansponse.tobytes()
print("output_arr.shape:", response_shape)
return construct_infer_response(
req=req,
outputs=[
Metadata(
name="images",
datatype=str(DataType.TYPE_FP32.name),
# shape=[-1, -1, -1, -1],
shape=response_shape,
)
],
raw_outputs=[task_output],
)
entrypoint = InstillDeployable(ControlNet).get_deployment_handle()