-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_enhancement.py
340 lines (287 loc) · 12.6 KB
/
image_enhancement.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# image_enhancement.py
import logging
from typing import Tuple, List, Dict
import numpy as np
from PIL import Image
import torch
import cv2
import json
import re
from diffusers import DiffusionPipeline, StableDiffusionLatentUpscalePipeline, StableDiffusionControlNetPipeline, ControlNetModel
from torchvision import transforms
from config import (
LOG_FORMAT, LOG_DATE_FORMAT, UPSCALER_MODEL, SD_BASE_MODEL,
CONTROLNET_MODEL, CONTROLNET_CONDITIONING_SCALE,
CONTROL_GUIDANCE_START, CONTROL_GUIDANCE_END,
FREESTYLE_PROMPT_JSON, FREESTYLE_N, FREESTYLE_B, FREESTYLE_S,
MODEL_HIGH_RES, FREESTYLE_MODEL
)
logging.basicConfig(level=logging.INFO, format=LOG_FORMAT, datefmt=LOG_DATE_FORMAT)
logger = logging.getLogger(__name__)
def get_device() -> torch.device:
"""Determine and return the appropriate device for computation."""
if torch.cuda.is_available():
device = torch.device("cuda")
device_name = torch.cuda.get_device_name(0)
logger.info(f"Using GPU: {device_name}")
if 'AMD' in device_name or 'MI' in device_name:
logger.info(f"AMD GPU detected. ROCm version: {torch.version.hip}")
torch.backends.cudnn.benchmark = True
else:
device = torch.device("cpu")
logger.info("GPU not available. Using CPU")
return device
device = get_device()
# Use mixed precision for GPU, full precision for CPU
dtype = torch.float16 if device.type == "cuda" else torch.float32
def load_style_prompts() -> List[Dict[str, str]]:
"""Load and return the style prompts from the JSON file."""
try:
with open(FREESTYLE_PROMPT_JSON, 'r') as f:
return json.load(f)
except Exception as e:
logger.error(f"Error loading style prompts: {str(e)}")
return []
def select_style_prompt(style_prompts: List[Dict[str, str]]) -> Dict[str, str]:
"""Prompt the user to select a style from the available options."""
print("Available styles:")
for i, style in enumerate(style_prompts):
print(f"{i + 1}. {style['name']}")
while True:
try:
choice = int(input("Select a style by number: ")) - 1
if 0 <= choice < len(style_prompts):
return style_prompts[choice]
else:
print("Invalid selection. Please try again.")
except ValueError:
print("Please enter a valid number.")
def combine_prompts(user_prompt: str, style: Dict[str, str], max_tokens: int = 77) -> Tuple[str, str]:
"""
Combine user prompt with style prompt, ensuring it doesn't exceed the token limit.
Truncates the negative prompt first, then the positive prompt if necessary.
Args:
user_prompt: The user's input prompt.
style: The selected style dictionary.
max_tokens: Maximum number of tokens allowed (default is 77 for SDXL).
Returns:
Tuple[str, str]: Combined positive prompt and negative prompt.
"""
def count_tokens(text: str) -> int:
# This is a simple approximation. For more accurate counting, consider using a tokenizer.
return len(re.findall(r'\w+', text))
user_tokens = count_tokens(user_prompt)
style_tokens = count_tokens(style['prompt'])
neg_tokens = count_tokens(style.get('negative_prompt', ''))
# Combine user prompt and style prompt
full_prompt = f"{user_prompt}, {style['prompt']}"
full_prompt_tokens = user_tokens + style_tokens
# Calculate remaining tokens for negative prompt
remaining_tokens = max_tokens - full_prompt_tokens
# Truncate negative prompt if necessary
if neg_tokens > remaining_tokens:
words = style['negative_prompt'].split()
neg_prompt = ' '.join(words[:remaining_tokens])
logger.warning(f"Negative prompt truncated to fit within token limit. Original: {style['negative_prompt']}, Truncated: {neg_prompt}")
else:
neg_prompt = style.get('negative_prompt', '')
# If negative prompt is completely removed and we still exceed the token limit, truncate the positive prompt
if not neg_prompt and full_prompt_tokens > max_tokens:
words = full_prompt.split()
full_prompt = ' '.join(words[:max_tokens])
logger.warning(f"Positive prompt truncated to fit within token limit. Truncated: {full_prompt}")
# Log warnings
if not neg_prompt:
logger.warning("Negative prompt completely removed due to token limit.")
if full_prompt_tokens > max_tokens:
logger.warning("Positive prompt truncated due to token limit.")
return full_prompt, neg_prompt
def apply_freestyle(image: np.ndarray, prompt: str) -> np.ndarray:
"""
Apply Freestyle to the given image using the SDXL model from Hugging Face.
Args:
image: Input image as a numpy array.
prompt: Text prompt for Freestyle.
Returns:
np.ndarray: Processed image as a numpy array.
"""
try:
pipe = DiffusionPipeline.from_pretrained(FREESTYLE_MODEL, torch_dtype=dtype)
pipe.to(device)
style_prompts = load_style_prompts()
if not style_prompts:
raise ValueError("No style prompts available.")
selected_style = select_style_prompt(style_prompts)
# Combine prompts and handle potential token limit
full_prompt, neg_prompt = combine_prompts(prompt, selected_style)
input_image = Image.fromarray(image).resize((1024, 1024))
with torch.no_grad():
result = generate_image_with_pipeline(pipe, full_prompt, input_image, neg_prompt)
logger.info(f"Freestyle processing completed successfully using {selected_style['name']} style")
return np.array(result)
except Exception as e:
logger.error(f"Error during Freestyle processing: {str(e)}")
logger.warning("Falling back to original image due to Freestyle processing error.")
return image
def generate_image_with_pipeline(pipe, prompt: str, image: Image.Image, negative_prompt: str) -> Image.Image:
"""Generate image using the provided pipeline and parameters."""
if device.type == "cuda":
with torch.autocast(device_type="cuda", dtype=dtype):
result = pipe(
prompt=prompt,
image=image,
num_inference_steps=10,
guidance_scale=5,
num_images_per_prompt=1,
negative_prompt=negative_prompt,
).images[0]
else:
result = pipe(
prompt=prompt,
image=image,
num_inference_steps=10,
guidance_scale=5,
num_images_per_prompt=1,
negative_prompt=negative_prompt,
).images[0]
return result
def latent_upscale_image(image: Image.Image, prompt: str, output_size: Tuple[int, int] = (1024, 1024)) -> np.ndarray:
"""
Upscale the given image using Stable Diffusion x2 latent upscaler.
Args:
image: PIL Image to upscale.
prompt: Text prompt for guided upscaling.
output_size: Desired output size as a tuple (width, height).
Returns:
np.ndarray: Upscaled image as a numpy array.
"""
try:
sd_pipeline = StableDiffusionLatentUpscalePipeline.from_pretrained(UPSCALER_MODEL, torch_dtype=dtype)
sd_pipeline.to(device)
with torch.no_grad():
upscaled_image = generate_image_with_pipeline(sd_pipeline, prompt, image.resize((512, 512)), "")
upscaled_image = upscaled_image.resize(output_size, Image.LANCZOS)
logger.info(f"Image upscaled successfully to {output_size[0]}x{output_size[1]}")
return np.array(upscaled_image)
except Exception as e:
logger.error(f"Error during latent image upscaling: {str(e)}")
logger.info("Falling back to simple resize")
return np.array(image.resize(output_size, Image.LANCZOS))
def apply_controlnet(image: np.ndarray, prompt: str) -> np.ndarray:
"""
Apply ControlNet to the given image and output at 1024x1024 resolution.
Args:
image: Input image as a numpy array.
prompt: Text prompt for ControlNet.
Returns:
np.ndarray: Processed image as a numpy array at 1024x1024 resolution.
"""
try:
controlnet = ControlNetModel.from_pretrained(CONTROLNET_MODEL, torch_dtype=dtype)
pipe = StableDiffusionControlNetPipeline.from_pretrained(
SD_BASE_MODEL,
controlnet=controlnet,
torch_dtype=dtype
)
pipe.to(device)
control_image = get_canny_image(image).resize((1024, 1024), Image.LANCZOS)
with torch.no_grad():
output = generate_controlnet_image(pipe, prompt, control_image)
logger.info("ControlNet processing completed successfully at 1024x1024 resolution")
return np.array(output)
except Exception as e:
logger.error(f"Error during ControlNet processing: {str(e)}")
return cv2.resize(image, (1024, 1024)) # Fallback to simple resize
def generate_controlnet_image(pipe, prompt: str, control_image: Image.Image) -> Image.Image:
"""Generate image using ControlNet pipeline."""
if device.type == "cuda":
with torch.autocast(device_type="cuda", dtype=dtype):
output = pipe(
prompt,
image=control_image,
num_inference_steps=10,
controlnet_conditioning_scale=CONTROLNET_CONDITIONING_SCALE,
control_guidance_start=CONTROL_GUIDANCE_START,
control_guidance_end=CONTROL_GUIDANCE_END,
height=1024,
width=1024
).images[0]
else:
output = pipe(
prompt,
image=control_image,
num_inference_steps=10,
controlnet_conditioning_scale=CONTROLNET_CONDITIONING_SCALE,
control_guidance_start=CONTROL_GUIDANCE_START,
control_guidance_end=CONTROL_GUIDANCE_END,
height=1024,
width=1024
).images[0]
return output
def get_canny_image(image: np.ndarray) -> Image.Image:
"""
Apply Canny edge detection to the input image.
Args:
image: Input image as a numpy array.
Returns:
PIL.Image.Image: Canny edge detected image.
"""
image = cv2.Canny(image, 100, 200)
image = image[:, :, None]
image = np.concatenate([image, image, image], axis=2)
return Image.fromarray(image)
def apply_pixart(image: np.ndarray, prompt: str, temperature: float) -> np.ndarray:
"""
Apply Pixart enhancement to the given image.
Args:
image: Input image as a numpy array.
prompt: Text prompt for Pixart.
temperature: Temperature for generation guidance.
Returns:
np.ndarray: Processed image as a numpy array.
"""
try:
pipe = DiffusionPipeline.from_pretrained(MODEL_HIGH_RES, torch_dtype=dtype)
pipe.to(device)
input_image = Image.fromarray(image).resize((1024, 1024))
with torch.no_grad():
result = generate_pixart_image(pipe, prompt, input_image, temperature)
logger.info("Pixart enhancement completed successfully")
return np.array(result)
except Exception as e:
logger.error(f"Error during Pixart enhancement: {str(e)}")
return image
def generate_pixart_image(pipe, prompt: str, image: Image.Image, temperature: float) -> Image.Image:
"""Generate image using Pixart pipeline."""
if device.type == "cuda":
with torch.autocast(device_type="cuda", dtype=dtype):
result = pipe(
prompt=prompt,
image=image,
num_inference_steps=10,
guidance_scale=temperature,
num_images_per_prompt=1,
).images[0]
else:
result = pipe(
prompt=prompt,
image=image,
num_inference_steps=10,
guidance_scale=temperature,
num_images_per_prompt=1,
).images[0]
return result
def apply_enhancement(image: np.ndarray, prompt: str, enhancement_option: str, temperature: float = 5.0) -> np.ndarray:
"""Apply the selected enhancement to the image."""
enhancement_functions = {
"Freestyle": apply_freestyle,
"Upscaler": lambda img, p: latent_upscale_image(Image.fromarray(img), p),
"ControlNet": apply_controlnet,
"Pixart": lambda img, p: apply_pixart(img, p, temperature),
"None": lambda img, _: img
}
return enhancement_functions.get(enhancement_option, lambda img, _: img)(image, prompt)
# Print PyTorch and CUDA versions for debugging
logger.info(f"PyTorch version: {torch.__version__}")
if torch.cuda.is_available():
logger.info(f"CUDA version: {torch.version.cuda}")