-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathinference.py
More file actions
343 lines (296 loc) · 11.4 KB
/
inference.py
File metadata and controls
343 lines (296 loc) · 11.4 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
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
341
342
343
#!/usr/bin/env python
# coding=utf-8
# Copyright 2025 Seochan99. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
SD3.5 LoRA Inference Script
Generate images using trained LoRA adapters with Stable Diffusion 3.5.
Supports single/batch generation, various sampling parameters, and output formats.
Author: Seochan99
"""
import argparse
import os
import torch
from datetime import datetime
from pathlib import Path
from PIL import Image
from diffusers import StableDiffusion3Pipeline
import gc
def parse_args():
"""Parse command line arguments for SD3.5 LoRA inference."""
parser = argparse.ArgumentParser(
description="Generate images using SD3.5 with trained LoRA adapters"
)
# ═══════════════════════════════════════════════════════════
# Model and LoRA Configuration
# ═══════════════════════════════════════════════════════════
parser.add_argument(
"--model_path",
type=str,
default="stabilityai/stable-diffusion-3.5-medium",
help="Path to base SD3.5 model or HuggingFace model ID",
)
parser.add_argument(
"--lora_path",
type=str,
required=True,
help="Path to trained LoRA weights directory",
)
parser.add_argument(
"--lora_scale",
type=float,
default=1.0,
help="LoRA scaling factor (0.0 = no effect, 1.0 = full effect)",
)
# ═══════════════════════════════════════════════════════════
# Generation Parameters
# ═══════════════════════════════════════════════════════════
parser.add_argument(
"--prompt",
type=str,
required=True,
help="Text prompt for image generation",
)
parser.add_argument(
"--negative_prompt",
type=str,
default="blurry, low quality, distorted, bad anatomy",
help="Negative prompt to avoid unwanted elements",
)
parser.add_argument(
"--num_images",
type=int,
default=1,
help="Number of images to generate",
)
parser.add_argument(
"--height",
type=int,
default=1024,
help="Image height in pixels",
)
parser.add_argument(
"--width",
type=int,
default=1024,
help="Image width in pixels",
)
parser.add_argument(
"--num_inference_steps",
type=int,
default=28,
help="Number of denoising steps (more steps = better quality, slower)",
)
parser.add_argument(
"--guidance_scale",
type=float,
default=7.0,
help="Classifier-free guidance scale (higher = more prompt adherence)",
)
parser.add_argument(
"--seed",
type=int,
default=None,
help="Random seed for reproducible generation",
)
# ═══════════════════════════════════════════════════════════
# Output Configuration
# ═══════════════════════════════════════════════════════════
parser.add_argument(
"--output_dir",
type=str,
default="./generated_images",
help="Directory to save generated images",
)
parser.add_argument(
"--output_format",
type=str,
default="png",
choices=["png", "jpg", "webp"],
help="Output image format",
)
parser.add_argument(
"--save_prompt",
action="store_true",
help="Save prompt information in filename",
)
# ═══════════════════════════════════════════════════════════
# System Configuration
# ═══════════════════════════════════════════════════════════
parser.add_argument(
"--device",
type=str,
default="auto",
help="Device to use (auto, cuda, cpu, mps)",
)
parser.add_argument(
"--dtype",
type=str,
default="auto",
choices=["auto", "float16", "bfloat16", "float32"],
help="Model precision (auto recommended)",
)
parser.add_argument(
"--enable_memory_efficient_attention",
action="store_true",
help="Enable memory efficient attention for lower VRAM usage",
)
parser.add_argument(
"--enable_cpu_offload",
action="store_true",
help="Offload models to CPU when not in use (saves VRAM)",
)
return parser.parse_args()
def setup_device_and_dtype(args):
"""Determine optimal device and dtype configuration."""
# Device selection
if args.device == "auto":
if torch.cuda.is_available():
device = "cuda"
elif torch.backends.mps.is_available():
device = "mps"
else:
device = "cpu"
else:
device = args.device
# Dtype selection
if args.dtype == "auto":
if device == "cuda":
# Use bfloat16 for modern GPUs, float16 for older ones
if torch.cuda.get_device_capability()[0] >= 8: # Ampere and newer
dtype = torch.bfloat16
else:
dtype = torch.float16
elif device == "mps":
dtype = torch.float16 # MPS doesn't support bfloat16
else:
dtype = torch.float32 # CPU
else:
dtype = getattr(torch, args.dtype)
print(f"🔧 Using device: {device}, dtype: {dtype}")
return device, dtype
def load_pipeline(args, device, dtype):
"""Load and configure the SD3.5 pipeline with LoRA weights."""
print("📦 Loading Stable Diffusion 3.5 pipeline...")
# Load base pipeline
pipeline = StableDiffusion3Pipeline.from_pretrained(
args.model_path,
torch_dtype=dtype,
device_map=None if not args.enable_cpu_offload else "auto",
)
# Apply memory optimizations
if args.enable_memory_efficient_attention:
pipeline.enable_attention_slicing()
if hasattr(pipeline.vae, "enable_tiling"):
pipeline.vae.enable_tiling()
print("✅ Memory efficient attention enabled")
if args.enable_cpu_offload:
pipeline.enable_model_cpu_offload()
print("✅ CPU offloading enabled")
else:
pipeline = pipeline.to(device)
# Load LoRA weights
print(f"🎯 Loading LoRA weights from: {args.lora_path}")
try:
pipeline.load_lora_weights(args.lora_path)
print(f"✅ LoRA weights loaded successfully (scale: {args.lora_scale})")
except Exception as e:
print(f"❌ Failed to load LoRA weights: {e}")
print("💡 Make sure the LoRA path contains pytorch_lora_weights.safetensors")
raise
return pipeline
def generate_images(pipeline, args):
"""Generate images using the loaded pipeline and LoRA weights."""
print(f"🎨 Generating {args.num_images} image(s)...")
print(f"📝 Prompt: {args.prompt}")
# Setup generator for reproducibility
generator = None
if args.seed is not None:
generator = torch.Generator(device=pipeline.device).manual_seed(args.seed)
print(f"🎲 Using seed: {args.seed}")
# Create output directory
os.makedirs(args.output_dir, exist_ok=True)
generated_images = []
for i in range(args.num_images):
print(f"🖼️ Generating image {i+1}/{args.num_images}...")
try:
# Generate image
with torch.no_grad():
output = pipeline(
prompt=args.prompt,
negative_prompt=args.negative_prompt,
height=args.height,
width=args.width,
num_inference_steps=args.num_inference_steps,
guidance_scale=args.guidance_scale,
generator=generator,
cross_attention_kwargs={"scale": args.lora_scale},
)
image = output.images[0]
generated_images.append(image)
# Save image
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
if args.save_prompt:
# Create safe filename from prompt
safe_prompt = "".join(
c
for c in args.prompt[:50]
if c.isalnum() or c in (" ", "-", "_")
).rstrip()
safe_prompt = safe_prompt.replace(" ", "_")
filename = (
f"{timestamp}_{i+1:03d}_{safe_prompt}.{args.output_format}"
)
else:
filename = f"{timestamp}_{i+1:03d}.{args.output_format}"
filepath = os.path.join(args.output_dir, filename)
# Save with appropriate quality settings
if args.output_format == "jpg":
image.save(filepath, "JPEG", quality=95, optimize=True)
elif args.output_format == "webp":
image.save(filepath, "WEBP", quality=90, method=6)
else: # png
image.save(filepath, "PNG", optimize=True)
print(f"✅ Saved: {filepath}")
except Exception as e:
print(f"❌ Failed to generate image {i+1}: {e}")
continue
# Clear cache between generations to prevent OOM
if torch.cuda.is_available():
torch.cuda.empty_cache()
gc.collect()
return generated_images
def main():
"""Main inference function."""
args = parse_args()
print("🚀 SD3.5 LoRA Inference")
print("=" * 50)
# Setup device and precision
device, dtype = setup_device_and_dtype(args)
# Load pipeline with LoRA
pipeline = load_pipeline(args, device, dtype)
# Generate images
images = generate_images(pipeline, args)
print("\n🎉 Generation complete!")
print(f"📂 Generated {len(images)} image(s) in: {args.output_dir}")
# Print generation summary
print("\n📋 Generation Summary:")
print(f" Model: {args.model_path}")
print(f" LoRA: {args.lora_path} (scale: {args.lora_scale})")
print(f" Resolution: {args.width}x{args.height}")
print(f" Steps: {args.num_inference_steps}")
print(f" Guidance: {args.guidance_scale}")
print(f" Seed: {args.seed}")
if __name__ == "__main__":
main()