forked from AIOTSonline/Marine_Biology_GenAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenAI_image_generator.py
More file actions
97 lines (82 loc) · 3.08 KB
/
Copy pathGenAI_image_generator.py
File metadata and controls
97 lines (82 loc) · 3.08 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
import torch
import gc
from diffusers import StableDiffusion3Pipeline, BitsAndBytesConfig, SD3Transformer2DModel
from transformers import T5EncoderModel
# ------------------ MEMORY CLEANUP ------------------
def clean_memory():
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
torch.cuda.ipc_collect()
print("GPU memory cleared.")
else:
print("No GPU available.")
# ------------------ LOAD PIPELINE ------------------
def load_pipeline(model_path="./models/stable-diffusion-3.5-large-turbo"):
"""
Loads Stable Diffusion 3.5 pipeline from a local folder
with NF4 quantization and CPU offloading.
"""
nf4_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16
)
# Load transformer with 4-bit quantization
model_nf4 = SD3Transformer2DModel.from_pretrained(
model_path,
subfolder="transformer",
quantization_config=nf4_config,
torch_dtype=torch.bfloat16
)
# Load T5 text encoder in 4-bit
t5_nf4 = T5EncoderModel.from_pretrained(
"models/t5-nf4", # <-- also local version (downloaded & placed in models folder)
# "diffusers/t5-nf4",
torch_dtype=torch.bfloat16
)
# Final pipeline with offloading
pipeline = StableDiffusion3Pipeline.from_pretrained(
model_path,
transformer=model_nf4,
text_encoder_3=t5_nf4,
torch_dtype=torch.bfloat16
)
pipeline.enable_model_cpu_offload()
return pipeline
# ------------------ TEXT TO IMAGE FUNCTION ------------------
def text_to_image(prompt, output_path="generated.png",
negative_prompt=None, steps=28, guidance=7.0,
model_path="models/stable-diffusion-3.5-large-turbo"):
"""
Generate a 2D image from text prompt using Stable Diffusion 3.5 (local model).
"""
clean_memory()
pipe = load_pipeline(model_path)
image = pipe(
prompt=prompt,
negative_prompt=negative_prompt,
num_inference_steps=steps,
guidance_scale=guidance,
).images[0]
image.save(output_path)
print(f" Image saved at {output_path}")
# ------------------ EXAMPLE USAGE ------------------
if __name__ == "__main__":
positive_prompt = (
"photograph of a Dumbo Octopus (Grimpoteuthis), anatomically correct, "
"8 webbed arms, 2 large ear-like fins on its mantle. Symmetrical, centered, "
"neutral pose. Full body shot, front view. Shot on a plain white background, "
"bright studio lighting, no shadows. 4k, high detail, sharp focus."
)
negative_prompt = (
"blurry, deformed, mutated, disfigured, extra fins, extra arms, missing limbs, "
"tentacles, cartoon, painting, artistic, dark, shadows, text, watermark, "
"underwater scene, ocean background, noisy background."
)
text_to_image(
positive_prompt,
"dumbo_octopus_reference.png",
negative_prompt,
model_path="./models/stable-diffusion-3.5-large-turbo" # local model path
)