-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageToRender.py
More file actions
287 lines (235 loc) · 9.82 KB
/
Copy pathImageToRender.py
File metadata and controls
287 lines (235 loc) · 9.82 KB
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
import json
import os
from pathlib import Path
from datetime import datetime
import numpy as np
import cv2
import torch
from PIL import Image, ImageOps, ImageDraw
from diffusers import (
ControlNetModel,
StableDiffusionControlNetPipeline,
DPMSolverMultistepScheduler,
AutoencoderKL,
)
from controlnet_aux.lineart import LineartDetector
from transformers import DPTForDepthEstimation, AutoImageProcessor
# --- GPU Kontrol ---
if torch.cuda.is_available():
print(f"--- GPU AKTİF ---")
print(f"Cihaz Adı : {torch.cuda.get_device_name(0)}")
print(f"Toplam VRAM : {torch.cuda.get_device_properties(0).total_memory / 1024**3:.2f} GB")
else:
print("--- DİKKAT: GPU BULUNAMADI, CPU ÇALIŞIYOR! ---")
# =============================================================
# CONDITION HAZIRLIK FONKSİYONLARI
# =============================================================
def prepare_depth(image_path: str, size: int = 512) -> Image.Image:
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
feature_extractor = AutoImageProcessor.from_pretrained("Intel/dpt-hybrid-midas")
image = Image.open(image_path).convert("RGB").resize((size, size))
inputs = feature_extractor(images=image, return_tensors="pt")
with torch.no_grad():
outputs = model(**inputs)
predicted_depth = outputs.predicted_depth
prediction = torch.nn.functional.interpolate(
predicted_depth.unsqueeze(1),
size=image.size[::-1],
mode="bicubic",
align_corners=False,
)
output = prediction.squeeze().cpu().numpy()
formatted = (output * 255 / np.max(output)).astype("uint8")
return Image.fromarray(formatted).convert("RGB")
def prepare_scribble(path: str, size: int = 512, threshold: int = 200) -> Image.Image:
img = Image.open(path).convert("L")
img = ImageOps.autocontrast(img)
img = img.resize((size, size))
arr = np.array(img)
arr = np.where(arr > threshold, 255, 0).astype(np.uint8)
return Image.fromarray(arr, mode="L").convert("RGB")
def prepare_canny(path: str, size: int = 512, low: int = 100, high: int = 200) -> Image.Image:
img = Image.open(path).convert("RGB").resize((size, size))
arr = np.array(img)
edges = cv2.Canny(arr, low, high)
edges = np.stack([edges] * 3, axis=-1)
return Image.fromarray(edges.astype(np.uint8), mode="RGB")
def prepare_lineart(path: str, size: int = 512, coarse: bool = False) -> Image.Image:
img = Image.open(path).convert("RGB").resize((size, size))
detector = LineartDetector.from_pretrained("lllyasviel/Annotators")
line = detector(img, coarse=coarse)
if line.mode != "RGB":
line = line.convert("RGB")
return line
# =============================================================
# YARDIMCI FONKSİYONLAR
# =============================================================
def compute_condition_stats(img: Image.Image) -> dict:
arr = np.array(img.convert("L"))
edge_pixels = np.count_nonzero(arr < 128)
return {
"mean": round(float(arr.mean()), 2),
"std": round(float(arr.std()), 2),
"edge_density": round(float(edge_pixels / arr.size), 4),
}
def save_contact_sheet(images: list, labels: list, out_path: Path) -> None:
w, h = images[0].size
sheet = Image.new("RGB", (w * len(images), h + 40), color=(255, 255, 255))
draw = ImageDraw.Draw(sheet)
for i, (img, label) in enumerate(zip(images, labels)):
sheet.paste(img, (i * w, 0))
draw.text((i * w + 10, h + 10), label, fill=(0, 0, 0))
sheet.save(out_path)
# =============================================================
# MAIN
# =============================================================
def main():
out_dir = Path("outputs")
out_dir.mkdir(exist_ok=True)
run_id = datetime.now().strftime("run_%Y%m%d_%H%M%S")
run_dir = out_dir / run_id
run_dir.mkdir(parents=True, exist_ok=False)
# --- Model Ayarları ---
base_model = "SG161222/Realistic_Vision_V6.0_B1_noVAE"
controlnet_id = "lllyasviel/control_v11f1p_sd15_depth"
# controlnet_id = "lllyasviel/sd-controlnet-scribble"
# controlnet_id = "lllyasviel/sd-controlnet-canny"
# controlnet_id = "lllyasviel/control_v11p_sd15_lineart"
device = "cuda" if torch.cuda.is_available() else "cpu"
dtype = torch.float16 if device == "cuda" else torch.float32
# --- LoRA Ayarları ---
use_lora = False
lora_path = "loras/RealisticVision-LoRA-libr-0.2.safetensors"
lora_scale = 0.7
# --- Pipeline Yükle ---
vae = AutoencoderKL.from_pretrained("stabilityai/sd-vae-ft-mse", torch_dtype=dtype)
controlnet = ControlNetModel.from_pretrained(controlnet_id, torch_dtype=dtype)
pipe = StableDiffusionControlNetPipeline.from_pretrained(
base_model,
controlnet=controlnet,
vae=vae,
torch_dtype=dtype,
safety_checker=None,
use_safetensors=False,
)
pipe.scheduler = DPMSolverMultistepScheduler.from_config(
pipe.scheduler.config,
use_karras_sigmas=True,
)
pipe = pipe.to(device)
try:
pipe.enable_xformers_memory_efficient_attention()
print("[GPU] xformers aktif.")
except Exception as e:
print(f"[Uyarı] xformers yüklenemedi, attention slicing devrede: {e}")
pipe.enable_attention_slicing()
# --- LoRA Yükle ---
if use_lora and os.path.exists(lora_path):
print(f"[LoRA] Loading: {lora_path}")
pipe.load_lora_weights(
os.path.dirname(lora_path),
weight_name=os.path.basename(lora_path),
adapter_name="mimari_stil",
)
pipe.set_adapters("mimari_stil", adapter_weights=[lora_scale])
print(f"[LoRA] Loaded — scale: {lora_scale}")
else:
print("[LoRA] Disabled")
# --- Girdi & Condition ---
input_image_path = "image.jpg"
depth = prepare_depth(input_image_path, size=512)
# scribble = prepare_scribble("sketch.png", size=512, threshold=200)
# canny = prepare_canny("sketch.png", size=512, low=100, high=200)
# lineart = prepare_lineart("sketch.png", size=512, coarse=False)
depth.save(run_dir / "condition_depth.png")
condition_stats = compute_condition_stats(depth)
# --- Prompt ---
prompt = (
"Photorealistic architectural render of a historical stone building converted into "
"a modern luxury nursing home, serene garden with walking paths and ergonomic benches, "
"large minimalist glass extensions, warm sunlight, elderly-friendly landscape design, "
"high-quality textures, 8k resolution, cinematic lighting, sharp details, exterior,dark red brick facade, gothic pointed arches,"
"preserved original stone masonry, keeping original materiality,blue sky, daylight, bright natural lighting."
)
negative = (
"low quality, dark, scary, messy, blurry, industrial, futuristic, distorted architecture,beige, cream, limestone, white walls, changed facade material,red sky, dark sky, dramatic sky, overcast, night, dark shadows."
)
# --- Üretim Ayarları ---
steps = 30
guidance_scale = 6.5
control_strength = 0.75
seeds = [42, 1001, 2024, 31415]
# --- Run Metadata ---
run_meta = {
"run_id": run_id,
"image_path": input_image_path,
"prompt": prompt,
"negative_prompt": negative,
"steps": steps,
"guidance_scale": guidance_scale,
"control_strength": control_strength,
"model": base_model,
"controlnet": controlnet_id,
"device": device,
"dtype": str(dtype),
"seeds": seeds,
"condition_stats": condition_stats,
"lora": {
"enabled": use_lora,
"path": lora_path if use_lora else None,
"scale": lora_scale if use_lora else None,
},
}
(run_dir / "run_meta.json").write_text(
json.dumps(run_meta, ensure_ascii=False, indent=2), encoding="utf-8"
)
# --- Üretim Döngüsü ---
generated_images = []
labels = []
for i, seed in enumerate(seeds, start=1):
gen = torch.Generator(device=device).manual_seed(seed)
img = pipe(
prompt=prompt,
negative_prompt=negative,
image=depth,
num_inference_steps=steps,
guidance_scale=guidance_scale,
controlnet_conditioning_scale=control_strength,
generator=gen,
).images[0]
img_path = run_dir / f"a{i:02d}_seed{seed}.png"
meta_path = run_dir / f"a{i:02d}_seed{seed}.json"
img.save(img_path)
meta = {
"run_id": run_id,
"index": i,
"seed": seed,
"image_file": img_path.name,
"image_path": input_image_path,
"prompt": prompt,
"negative_prompt": negative,
"steps": steps,
"guidance_scale": guidance_scale,
"control_strength": control_strength,
"model": base_model,
"controlnet": controlnet_id,
"experiment_type": "controlnet_depth",
}
meta_path.write_text(
json.dumps(meta, ensure_ascii=False, indent=2), encoding="utf-8"
)
generated_images.append(img)
labels.append(f"Alt {i} | seed {seed}")
print("Saved:", img_path)
if not generated_images:
raise ValueError("No images were generated; cannot create contact sheet.")
contact_path = run_dir / "comparison_sheet.png"
save_contact_sheet(generated_images, labels, contact_path)
print("Comparison sheet saved:", contact_path)
if __name__ == "__main__":
main()
# ---------- NOTLAR ----------
# pip freeze > requirements.txt → ortamı dondur
# pip install -r requirements.txt → ortamı geri yükle
# pip install -U diffusers → YAPMA, pipeline bozuluyor
# setx HF_HOME E:\hf_cache → HF cache konumunu sabitle