imagejenerator 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- imagejenerator/__init__.py +0 -0
- imagejenerator/config.py +26 -0
- imagejenerator/core/__init__.py +0 -0
- imagejenerator/core/image_generation_record.py +135 -0
- imagejenerator/core/image_generator.py +247 -0
- imagejenerator/examples/generate_image.py +6 -0
- imagejenerator/models/__init__.py +5 -0
- imagejenerator/models/registry.py +55 -0
- imagejenerator/models/sd_1_5.py +94 -0
- imagejenerator/models/sd_schedulers.py +9 -0
- imagejenerator/tools/__init__.py +0 -0
- imagejenerator-1.0.0.dist-info/METADATA +114 -0
- imagejenerator-1.0.0.dist-info/RECORD +15 -0
- imagejenerator-1.0.0.dist-info/WHEEL +4 -0
- imagejenerator-1.0.0.dist-info/licenses/LICENSE +21 -0
|
File without changes
|
imagejenerator/config.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
config = {
|
|
2
|
+
"model": "stable-diffusion-v1-5",
|
|
3
|
+
"model_path": "runwayml/stable-diffusion-v1-5",
|
|
4
|
+
|
|
5
|
+
"device": "cuda",
|
|
6
|
+
"enable_attention_slicing": True,
|
|
7
|
+
"scheduler": "EulerDiscreteScheduler",
|
|
8
|
+
|
|
9
|
+
"height": 512,
|
|
10
|
+
"width": 512,
|
|
11
|
+
"num_inference_steps": 30,
|
|
12
|
+
"guidance_scale": 10,
|
|
13
|
+
"images_to_generate": 1,
|
|
14
|
+
"seeds": [], # leave empty for random
|
|
15
|
+
"dtype": "bfloat16",
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
"image_save_folder": "./images/",
|
|
19
|
+
|
|
20
|
+
"save_image_gen_stats": True,
|
|
21
|
+
"image_gen_data_file_path": "./stats/image_gen_stats.csv",
|
|
22
|
+
|
|
23
|
+
"prompts": [
|
|
24
|
+
"A rockstar playing a guitar solo on stage"
|
|
25
|
+
]
|
|
26
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
from dataclasses import dataclass, fields, field
|
|
2
|
+
import csv
|
|
3
|
+
import os
|
|
4
|
+
from typing import List
|
|
5
|
+
|
|
6
|
+
@dataclass
|
|
7
|
+
class ImageGenerationRecord():
|
|
8
|
+
"""
|
|
9
|
+
A data container for tracking and saving image generation metadata.
|
|
10
|
+
|
|
11
|
+
This class serves as a structured record for a single image generation event.
|
|
12
|
+
It includes functionality to serialize its own data to a CSV file, handling
|
|
13
|
+
formatting (e.g., float precision) and file management (creation vs. appending).
|
|
14
|
+
|
|
15
|
+
Attributes:
|
|
16
|
+
image_gen_data_file_path (str): Path to the CSV file where records are saved.
|
|
17
|
+
filename (str): Name of the generated image file.
|
|
18
|
+
timestamp (str): Timestamp of generation.
|
|
19
|
+
model (str): Name of the model used.
|
|
20
|
+
device (str): Compute device used (e.g., 'cuda', 'cpu').
|
|
21
|
+
dtype (str): Data type used (e.g., 'float16').
|
|
22
|
+
prompt (str): The text prompt used for generation.
|
|
23
|
+
seed (int): Random seed used.
|
|
24
|
+
height (int): Image height.
|
|
25
|
+
width (int): Image width.
|
|
26
|
+
inf_steps (int): Number of inference steps.
|
|
27
|
+
guidance_scale (float): Classifier-free guidance scale.
|
|
28
|
+
total_generation_time (float): Total time taken for the batch/image.
|
|
29
|
+
generation_time_per_image (float): Normalized time per single image.
|
|
30
|
+
image_rating (float): Placeholder for user rating (defaults to -1).
|
|
31
|
+
fields_to_not_save (list[str]): Internal list of field names to exclude from the CSV.
|
|
32
|
+
"""
|
|
33
|
+
image_gen_data_file_path: str = ""
|
|
34
|
+
filename: str = ""
|
|
35
|
+
timestamp: str = ""
|
|
36
|
+
model: str = ""
|
|
37
|
+
device: str = ""
|
|
38
|
+
dtype: str = ""
|
|
39
|
+
prompt: str = ""
|
|
40
|
+
seed: int = 0
|
|
41
|
+
height: int = 0
|
|
42
|
+
width: int = 0
|
|
43
|
+
inf_steps: int = 0
|
|
44
|
+
guidance_scale: float = 0
|
|
45
|
+
total_generation_time: float = 0
|
|
46
|
+
generation_time_per_image: float = 0
|
|
47
|
+
image_rating: float = -1
|
|
48
|
+
fields_to_not_save: list[str] = field(default_factory=list)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def __post_init__(self):
|
|
52
|
+
"""
|
|
53
|
+
Post-initialization hook to define excluded fields.
|
|
54
|
+
|
|
55
|
+
Populates `fields_to_not_save` with technical fields that should not
|
|
56
|
+
appear in the final CSV report (e.g., file paths or the exclusion list itself).
|
|
57
|
+
"""
|
|
58
|
+
self.fields_to_not_save += ["image_gen_data_file_path", "fields_to_not_save", "path_to_csv"]
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def create_data_row(self):
|
|
62
|
+
"""
|
|
63
|
+
Constructs a list of values for the CSV row based on current attributes.
|
|
64
|
+
|
|
65
|
+
Iterates through the dataclass fields, filtering out those in `fields_to_not_save`.
|
|
66
|
+
Floats are formatted to 2 decimal places.
|
|
67
|
+
|
|
68
|
+
Returns:
|
|
69
|
+
list: A list of values corresponding to the CSV columns.
|
|
70
|
+
"""
|
|
71
|
+
row = []
|
|
72
|
+
for field in fields(self):
|
|
73
|
+
if field.name in self.fields_to_not_save:
|
|
74
|
+
continue
|
|
75
|
+
field_value = getattr(self, field.name)
|
|
76
|
+
|
|
77
|
+
if isinstance(field_value, float):
|
|
78
|
+
row.append(f"{field_value:.2f}")
|
|
79
|
+
else:
|
|
80
|
+
row.append(field_value)
|
|
81
|
+
return row
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def append_data_to_csv(self):
|
|
85
|
+
"""
|
|
86
|
+
Appends the current record to the CSV file.
|
|
87
|
+
|
|
88
|
+
Opens the file at `image_gen_data_file_path` in append mode and writes
|
|
89
|
+
the data row generated by `create_data_row()`.
|
|
90
|
+
"""
|
|
91
|
+
with open(self.image_gen_data_file_path, "a", newline="") as f:
|
|
92
|
+
writer = csv.writer(f)
|
|
93
|
+
writer.writerow(self.create_data_row())
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def create_header_row(self):
|
|
97
|
+
"""
|
|
98
|
+
Generates the CSV header row based on field names.
|
|
99
|
+
|
|
100
|
+
Returns:
|
|
101
|
+
list[str]: A list of column names, excluding those in `fields_to_not_save`.
|
|
102
|
+
"""
|
|
103
|
+
headers = []
|
|
104
|
+
for header in fields(self):
|
|
105
|
+
if header.name in self.fields_to_not_save:
|
|
106
|
+
continue
|
|
107
|
+
headers.append(header.name)
|
|
108
|
+
return headers
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def create_new_stats_file(self):
|
|
112
|
+
"""
|
|
113
|
+
Creates a new CSV file and writes the header row.
|
|
114
|
+
|
|
115
|
+
Uses 'x' mode to open the file, which ensures a new file is created
|
|
116
|
+
and raises an error if the file already exists (though `save_data()` guards against this).
|
|
117
|
+
"""
|
|
118
|
+
with open(self.image_gen_data_file_path, "x", newline="") as f:
|
|
119
|
+
writer = csv.writer(f)
|
|
120
|
+
writer.writerow(self.create_header_row())
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def save_data(self):
|
|
124
|
+
"""
|
|
125
|
+
Saves the record of the image generation stats to disk.
|
|
126
|
+
|
|
127
|
+
Checks if the target CSV file exists:
|
|
128
|
+
- If no: Creates the file and writes headers.
|
|
129
|
+
- If yes: Appends the current data row.
|
|
130
|
+
"""
|
|
131
|
+
if not os.path.exists(self.image_gen_data_file_path):
|
|
132
|
+
self.create_new_stats_file()
|
|
133
|
+
|
|
134
|
+
self.append_data_to_csv()
|
|
135
|
+
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
import datetime
|
|
3
|
+
import time
|
|
4
|
+
import os
|
|
5
|
+
import random
|
|
6
|
+
import torch
|
|
7
|
+
|
|
8
|
+
from imagejenerator.core.image_generation_record import ImageGenerationRecord
|
|
9
|
+
from imagejenerator.config import config
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class ImageGenerator(ABC):
|
|
13
|
+
"""
|
|
14
|
+
Abstract base class for image generation pipelines using PyTorch.
|
|
15
|
+
|
|
16
|
+
This class handles the boilerplate for device detection, data type management,
|
|
17
|
+
random seed generation, image saving, and performance logging. Subclasses
|
|
18
|
+
must implement the actual pipeline creation and execution logic.
|
|
19
|
+
|
|
20
|
+
Attributes:
|
|
21
|
+
config (dict): Configuration dictionary containing model parameters, paths, and settings.
|
|
22
|
+
device (str): The device being used ('cuda' or 'cpu').
|
|
23
|
+
dtype (torch.dtype): The tensor data type (e.g., torch.float16, torch.bfloat16).
|
|
24
|
+
seeds (list[int]): List of random seeds used for generation.
|
|
25
|
+
generators (list[torch.Generator]): List of PyTorch generators initialized with seeds.
|
|
26
|
+
images (list): List of generated image objects (usually PIL.Image).
|
|
27
|
+
image_generation_record (ImageGenerationRecord): Object for tracking generation statistics.
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
def __init__(self, config = config):
|
|
31
|
+
"""
|
|
32
|
+
Initializes the ImageGenerator with a configuration.
|
|
33
|
+
|
|
34
|
+
Args:
|
|
35
|
+
config (dict): A dictionary containing configuration parameters.
|
|
36
|
+
Expected keys include:
|
|
37
|
+
- 'device': 'detect', 'cuda', or 'cpu'.
|
|
38
|
+
- 'dtype': 'detect', 'bfloat16', 'float16', or 'float32'.
|
|
39
|
+
- 'seeds': List of integers or None.
|
|
40
|
+
- 'prompts': List of prompt strings.
|
|
41
|
+
- 'images_to_generate': Int, number of images per prompt.
|
|
42
|
+
- 'image_save_folder': Path to save output images.
|
|
43
|
+
"""
|
|
44
|
+
self.DTYPES_MAP = {
|
|
45
|
+
"bfloat16": torch.bfloat16,
|
|
46
|
+
"float16": torch.float16,
|
|
47
|
+
"float32": torch.float32,
|
|
48
|
+
}
|
|
49
|
+
self.config = config
|
|
50
|
+
self.pipe = None
|
|
51
|
+
self.images = None
|
|
52
|
+
self.image_generation_record = ImageGenerationRecord()
|
|
53
|
+
self.save_timestamp = None
|
|
54
|
+
self.prompts = []
|
|
55
|
+
self.dtype = None
|
|
56
|
+
self.device = None
|
|
57
|
+
self.seeds = config["seeds"]
|
|
58
|
+
self.generators = []
|
|
59
|
+
self.detect_device_and_dtype()
|
|
60
|
+
self.create_generators()
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def detect_device_and_dtype(self):
|
|
64
|
+
"""
|
|
65
|
+
If 'device' or 'dtype' in config are set to "detect", this method attempts
|
|
66
|
+
to choose the optimal settings based on hardware availability (e.g., CUDA).
|
|
67
|
+
"""
|
|
68
|
+
if self.config["device"] == "detect":
|
|
69
|
+
self.set_device()
|
|
70
|
+
else:
|
|
71
|
+
self.device = self.config["device"]
|
|
72
|
+
|
|
73
|
+
self.set_dtype()
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def set_device(self):
|
|
77
|
+
"""
|
|
78
|
+
Sets the computation device based on CUDA availability.
|
|
79
|
+
|
|
80
|
+
Sets `self.device` to 'cuda' if available, otherwise defaults to 'cpu'.
|
|
81
|
+
"""
|
|
82
|
+
if torch.cuda.is_available():
|
|
83
|
+
self.device = "cuda"
|
|
84
|
+
else:
|
|
85
|
+
self.device = "cpu"
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def set_dtype(self):
|
|
89
|
+
"""
|
|
90
|
+
Sets the torch data type based on the device and configuration.
|
|
91
|
+
|
|
92
|
+
If config['dtype'] is "detect":
|
|
93
|
+
- Sets to torch.bfloat16 if device is 'cuda'.
|
|
94
|
+
- Sets to torch.float32 otherwise.
|
|
95
|
+
Otherwise, maps the string config to the actual torch.dtype object in self.DTYPES_MAP.
|
|
96
|
+
"""
|
|
97
|
+
if self.config["dtype"] == "detect":
|
|
98
|
+
if self.device == "cuda":
|
|
99
|
+
self.dtype = torch.bfloat16
|
|
100
|
+
self.config["dtype"] = "bfloat16"
|
|
101
|
+
else:
|
|
102
|
+
self.dtype = torch.float32
|
|
103
|
+
self.config["dtype"] = "float32"
|
|
104
|
+
return
|
|
105
|
+
|
|
106
|
+
self.dtype = self.DTYPES_MAP[self.config["dtype"]]
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def create_generators(self):
|
|
110
|
+
"""
|
|
111
|
+
Initializes random seeds and PyTorch Generators.
|
|
112
|
+
|
|
113
|
+
If seeds are not provided in the config, random seeds are generated
|
|
114
|
+
for the total batch size (number of prompts * images per prompt).
|
|
115
|
+
Populates `self.generators` with `torch.Generator` objects.
|
|
116
|
+
"""
|
|
117
|
+
if not self.seeds:
|
|
118
|
+
batch_size = len(self.config["prompts"]) * self.config["images_to_generate"]
|
|
119
|
+
self.seeds = [self.create_random_seed() for i in range(batch_size)]
|
|
120
|
+
|
|
121
|
+
self.generators = [
|
|
122
|
+
torch.Generator(device=self.device).manual_seed(seed)
|
|
123
|
+
for seed in self.seeds
|
|
124
|
+
]
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
@staticmethod
|
|
128
|
+
def create_random_seed(size: int = 32) -> int:
|
|
129
|
+
"""
|
|
130
|
+
Generates a random integer to serve as a seed.
|
|
131
|
+
|
|
132
|
+
Args:
|
|
133
|
+
size (int, optional): The bit-size for the random range. Defaults to 32.
|
|
134
|
+
|
|
135
|
+
Returns:
|
|
136
|
+
int: A random integer in the range [0, 2**size - 1].
|
|
137
|
+
"""
|
|
138
|
+
return random.randint(0, (2**size) - 1)
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
@abstractmethod
|
|
142
|
+
def create_pipeline(self):
|
|
143
|
+
"""
|
|
144
|
+
Abstract method to initialize the model pipeline.
|
|
145
|
+
|
|
146
|
+
Subclasses must implement this to load the specific model (e.g., Stable Diffusion)
|
|
147
|
+
and assign it to `self.pipe`.
|
|
148
|
+
"""
|
|
149
|
+
pass
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def run_pipeline(self):
|
|
153
|
+
"""
|
|
154
|
+
Executes the pipeline implementation and tracks performance metrics.
|
|
155
|
+
|
|
156
|
+
Calculates total generation time and average time per image, updating
|
|
157
|
+
the `image_generation_record`.
|
|
158
|
+
"""
|
|
159
|
+
start_time = time.time()
|
|
160
|
+
self.run_pipeline_impl()
|
|
161
|
+
end_time = time.time()
|
|
162
|
+
self.image_generation_record.total_generation_time = end_time - start_time
|
|
163
|
+
self.image_generation_record.generation_time_per_image = (
|
|
164
|
+
self.image_generation_record.total_generation_time / (self.config["images_to_generate"] * len(self.prompts))
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
@abstractmethod
|
|
169
|
+
def run_pipeline_impl(self):
|
|
170
|
+
"""
|
|
171
|
+
Abstract method containing the core generation logic.
|
|
172
|
+
|
|
173
|
+
Subclasses must implement this to call the model pipeline and populate `self.images`.
|
|
174
|
+
"""
|
|
175
|
+
pass
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def generate_image(self):
|
|
179
|
+
"""
|
|
180
|
+
Main workflow method to generate and save images.
|
|
181
|
+
|
|
182
|
+
Steps:
|
|
183
|
+
1. Creates the pipeline.
|
|
184
|
+
2. Runs the pipeline implementation.
|
|
185
|
+
3. Saves the resulting images to disk.
|
|
186
|
+
4. Optionally saves generation statistics.
|
|
187
|
+
"""
|
|
188
|
+
self.create_pipeline()
|
|
189
|
+
self.run_pipeline()
|
|
190
|
+
self.save_image()
|
|
191
|
+
if self.config["save_image_gen_stats"]:
|
|
192
|
+
self.save_image_gen_stats()
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def save_image(self):
|
|
196
|
+
"""
|
|
197
|
+
Saves generated images to the configured directory.
|
|
198
|
+
|
|
199
|
+
Images are saved with a timestamped filename. Updates `self.save_timestamp`.
|
|
200
|
+
"""
|
|
201
|
+
self.save_timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
|
|
202
|
+
for i, image in enumerate(self.images):
|
|
203
|
+
file_name = f"{self.save_timestamp}_no{i}.png"
|
|
204
|
+
save_path = os.path.join(self.config["image_save_folder"], file_name)
|
|
205
|
+
image.save(save_path)
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def complete_image_generation_record(self, prompt, i):
|
|
209
|
+
"""
|
|
210
|
+
Populates the image generation record with metadata for a specific image.
|
|
211
|
+
|
|
212
|
+
Args:
|
|
213
|
+
prompt (str): The prompt used to generate the image.
|
|
214
|
+
i (int): The index of the image in the current batch.
|
|
215
|
+
"""
|
|
216
|
+
self.image_generation_record.image_gen_data_file_path = self.config["image_gen_data_file_path"]
|
|
217
|
+
self.image_generation_record.filename = f"{self.save_timestamp}_no{i}.png"
|
|
218
|
+
self.image_generation_record.timestamp = self.save_timestamp
|
|
219
|
+
self.image_generation_record.model = self.config["model"]
|
|
220
|
+
self.image_generation_record.device = self.device
|
|
221
|
+
self.image_generation_record.dtype = self.config["dtype"]
|
|
222
|
+
self.image_generation_record.prompt = prompt
|
|
223
|
+
self.image_generation_record.seed = self.seeds[i]
|
|
224
|
+
self.image_generation_record.height = self.config["height"]
|
|
225
|
+
self.image_generation_record.width = self.config["width"]
|
|
226
|
+
self.image_generation_record.inf_steps = self.config["num_inference_steps"]
|
|
227
|
+
self.image_generation_record.guidance_scale = self.config["guidance_scale"]
|
|
228
|
+
self.complete_image_generation_record_impl()
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
@abstractmethod
|
|
232
|
+
def complete_image_generation_record_impl(self):
|
|
233
|
+
"""
|
|
234
|
+
Abstract hook for subclasses to add model-specific statistics to the record.
|
|
235
|
+
"""
|
|
236
|
+
pass
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def save_image_gen_stats(self):
|
|
240
|
+
"""
|
|
241
|
+
Iterates through generated images and saves metadata to the record file.
|
|
242
|
+
"""
|
|
243
|
+
all_prompts_used = self.config["prompts"] * self.config["images_to_generate"]
|
|
244
|
+
for i, prompt in enumerate(all_prompts_used):
|
|
245
|
+
self.complete_image_generation_record(prompt, i)
|
|
246
|
+
self.image_generation_record.save_data()
|
|
247
|
+
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Implements a simple decorator-based model registry for dynamically managing
|
|
3
|
+
different image generation classes.
|
|
4
|
+
|
|
5
|
+
This system allows concrete ImageGenerator subclasses to register themselves
|
|
6
|
+
using a specific string key (e.g., 'stable-diffusion-v1-5'), enabling the
|
|
7
|
+
application to instantiate the correct class based on a configuration setting
|
|
8
|
+
without explicit imports.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
MODEL_REGISTRY = {}
|
|
12
|
+
|
|
13
|
+
def register_model(name):
|
|
14
|
+
"""
|
|
15
|
+
A decorator factory used to register an ImageGenerator subclass.
|
|
16
|
+
|
|
17
|
+
The decorated class is stored in the global MODEL_REGISTRY dictionary
|
|
18
|
+
under the provided `name`.
|
|
19
|
+
|
|
20
|
+
Args:
|
|
21
|
+
name (str): The string key used to reference the model class
|
|
22
|
+
in the configuration (e.g., 'stable-diffusion-v1-5').
|
|
23
|
+
|
|
24
|
+
Returns:
|
|
25
|
+
Callable: A decorator function that takes a class and registers it.
|
|
26
|
+
"""
|
|
27
|
+
def decorator(cls):
|
|
28
|
+
MODEL_REGISTRY[name] = cls
|
|
29
|
+
return cls
|
|
30
|
+
return decorator
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def get_model_class(config):
|
|
34
|
+
"""
|
|
35
|
+
Retrieves and instantiates the correct ImageGenerator class based on the
|
|
36
|
+
configuration dictionary.
|
|
37
|
+
|
|
38
|
+
It looks up the class in MODEL_REGISTRY using the key found in
|
|
39
|
+
`config['model']`.
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
config (dict): The configuration dictionary, which must contain a
|
|
43
|
+
'model' key corresponding to a registered model name.
|
|
44
|
+
|
|
45
|
+
Returns:
|
|
46
|
+
ImageGenerator: An instantiated object of the registered generator class.
|
|
47
|
+
|
|
48
|
+
Raises:
|
|
49
|
+
KeyError: If the value of `config['model']` is not found in the
|
|
50
|
+
MODEL_REGISTRY.
|
|
51
|
+
"""
|
|
52
|
+
ModelClass = MODEL_REGISTRY[config["model"]]
|
|
53
|
+
image_generator = ModelClass(config)
|
|
54
|
+
|
|
55
|
+
return image_generator
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
from diffusers import StableDiffusionPipeline
|
|
2
|
+
import torch
|
|
3
|
+
from torch import autocast
|
|
4
|
+
import time
|
|
5
|
+
import datetime
|
|
6
|
+
from imagejenerator.models.registry import register_model
|
|
7
|
+
|
|
8
|
+
from imagejenerator.core.image_generator import ImageGenerator
|
|
9
|
+
from imagejenerator.models.sd_schedulers import schedulers
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@register_model("stable-diffusion-v1-5")
|
|
13
|
+
class StableDiffusion_1_5(ImageGenerator):
|
|
14
|
+
"""
|
|
15
|
+
Concrete implementation of ImageGenerator for Stable Diffusion v1.5.
|
|
16
|
+
|
|
17
|
+
This class handles the loading and inference of the Stable Diffusion v1.5 model
|
|
18
|
+
using the Hugging Face Diffusers library. It supports custom schedulers,
|
|
19
|
+
attention slicing for memory optimization, and mixed-precision inference.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
def __init__(self, config):
|
|
23
|
+
"""
|
|
24
|
+
Initializes the Stable Diffusion 1.5 generator.
|
|
25
|
+
|
|
26
|
+
This method expands the 'prompts' list in the config to match the total
|
|
27
|
+
number of images to generate (prompts * images_per_prompt) to facilitate
|
|
28
|
+
batch processing.
|
|
29
|
+
|
|
30
|
+
Args:
|
|
31
|
+
config (dict): Configuration dictionary. Must include standard ImageGenerator
|
|
32
|
+
keys plus model-specific keys.
|
|
33
|
+
"""
|
|
34
|
+
super().__init__(config)
|
|
35
|
+
self.pipe = None
|
|
36
|
+
self.images = None
|
|
37
|
+
self.prompts = config["prompts"] * config["images_to_generate"]
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def create_pipeline(self):
|
|
41
|
+
"""
|
|
42
|
+
Loads the Stable Diffusion pipeline and applies configurations.
|
|
43
|
+
|
|
44
|
+
Steps taken:
|
|
45
|
+
1. Loads the pipeline using `StableDiffusionPipeline.from_pretrained`.
|
|
46
|
+
2. Moves the pipeline to the specific device (CPU/CUDA).
|
|
47
|
+
3. Enables attention slicing if `config['enable_attention_slicing']` is True.
|
|
48
|
+
4. Swaps the default scheduler if `config['scheduler']` is specified.
|
|
49
|
+
|
|
50
|
+
Raises:
|
|
51
|
+
KeyError: If specific config keys (like 'model_path') are missing.
|
|
52
|
+
"""
|
|
53
|
+
self.pipe = StableDiffusionPipeline.from_pretrained(
|
|
54
|
+
self.config["model_path"],
|
|
55
|
+
torch_dtype=self.dtype,
|
|
56
|
+
safety_checker=None
|
|
57
|
+
).to(self.device)
|
|
58
|
+
|
|
59
|
+
if self.config["enable_attention_slicing"]:
|
|
60
|
+
self.pipe.enable_attention_slicing()
|
|
61
|
+
|
|
62
|
+
if self.config["scheduler"]:
|
|
63
|
+
scheduler = schedulers[self.config["scheduler"]]
|
|
64
|
+
self.pipe.scheduler = scheduler.from_config(self.pipe.scheduler.config)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def run_pipeline_impl(self):
|
|
68
|
+
"""
|
|
69
|
+
Executes the Stable Diffusion inference.
|
|
70
|
+
|
|
71
|
+
Runs the pipeline within a `torch.autocast` context to ensure the correct
|
|
72
|
+
precision (e.g., bfloat16) is used on the target device.
|
|
73
|
+
|
|
74
|
+
The resulting images are stored in `self.images`.
|
|
75
|
+
"""
|
|
76
|
+
with autocast(self.device):
|
|
77
|
+
self.images = self.pipe(
|
|
78
|
+
self.prompts,
|
|
79
|
+
height = self.config["height"],
|
|
80
|
+
width = self.config["width"],
|
|
81
|
+
num_inference_steps = self.config["num_inference_steps"],
|
|
82
|
+
guidance_scale = self.config["guidance_scale"],
|
|
83
|
+
generator=self.generators,
|
|
84
|
+
).images
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def complete_image_generation_record_impl(self):
|
|
88
|
+
"""
|
|
89
|
+
Implementation hook for recording extra stats.
|
|
90
|
+
|
|
91
|
+
Currently a no-op for SD 1.5 as the base class records all necessary metadata.
|
|
92
|
+
"""
|
|
93
|
+
pass
|
|
94
|
+
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
from diffusers import EulerDiscreteScheduler
|
|
2
|
+
from diffusers import DPMSolverMultistepScheduler
|
|
3
|
+
from diffusers import EulerAncestralDiscreteScheduler
|
|
4
|
+
|
|
5
|
+
schedulers = {
|
|
6
|
+
"EulerDiscreteScheduler": EulerDiscreteScheduler,
|
|
7
|
+
"DPMSolverMultistepScheduler": DPMSolverMultistepScheduler,
|
|
8
|
+
"EulerAncestralDiscreteScheduler": EulerAncestralDiscreteScheduler
|
|
9
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: imagejenerator
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Image generation suite.
|
|
5
|
+
License: MIT
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Author: Warren Davies
|
|
8
|
+
Requires-Python: >=3.10,<4.0
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
17
|
+
Classifier: Topic :: Multimedia :: Graphics :: Graphics Conversion
|
|
18
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
19
|
+
Requires-Dist: Pillow (>=10.0)
|
|
20
|
+
Requires-Dist: accelerate (>=0.30)
|
|
21
|
+
Requires-Dist: diffusers (>=0.30)
|
|
22
|
+
Requires-Dist: pyyaml (>=6.0)
|
|
23
|
+
Requires-Dist: torch (>=2.0)
|
|
24
|
+
Requires-Dist: transformers (>=4.40)
|
|
25
|
+
Project-URL: Homepage, https://github.com/WarrenDavies/imagejenerator
|
|
26
|
+
Project-URL: Repository, https://github.com/WarrenDavies/imagejenerator
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# Image Jenerator
|
|
30
|
+
|
|
31
|
+
**A Flexible and Configurable Image Generation Suite**
|
|
32
|
+
|
|
33
|
+
`Image Jenerator` is an extensible Python framework designed to run various image generation models (like Stable Diffusion) through a unified, standardized pipeline.
|
|
34
|
+
|
|
35
|
+
It takes care of the setup such as device management (CUDA/CPU), mixed-precision handling, and random seed generation. It also automatically generates performance logs including generation time, which you can use for benchmarking, and saves these to CSV.
|
|
36
|
+
|
|
37
|
+
## Requirements
|
|
38
|
+
|
|
39
|
+
* Python 3.10+
|
|
40
|
+
* GPU and CUDA drivers (or equivalent) are recommended. Image Jenerator supports CPU, but don't blame me if your system crashes or your images take half an hour to generate.
|
|
41
|
+
|
|
42
|
+
## Installation
|
|
43
|
+
|
|
44
|
+
Not on PyPI yet, so...
|
|
45
|
+
|
|
46
|
+
```sh
|
|
47
|
+
git clone https://github.com/WarrenDavies/image-jenerator.git
|
|
48
|
+
cd imagejenerator
|
|
49
|
+
python -m venv venv
|
|
50
|
+
(Linux/macOS) source venv/bin/activate
|
|
51
|
+
(Windows) .\venv\Scripts\activate
|
|
52
|
+
pip install -e .
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Config
|
|
56
|
+
|
|
57
|
+
Create a config dictionary with the following keys (default parameters are located at: `src/imagejenerator/config.py`):
|
|
58
|
+
|
|
59
|
+
| **Setting** | **Type** | **Description** |
|
|
60
|
+
| ----------- | -------- | --------------- |
|
|
61
|
+
| `model` | `str` | The registered model name to use (e.g., `"stable-diffusion-v1-5"`). |
|
|
62
|
+
| `model_path` | `str` | Local path or Hugging Face repository ID for the model weights. |
|
|
63
|
+
| `device` | `str` | Compute device (`"detect"`, `"cuda"`, or `"cpu"`). |
|
|
64
|
+
| `dtype` | `str` | Tensor precision (`"detect"`, `"bfloat16"`, `"float16"`, or `"float32"`). |
|
|
65
|
+
| `scheduler` | `str` | The name of the scheduler/sampler to use (must be a key in `sd_schedulers.py`). |
|
|
66
|
+
| `enable_attention_slicing` | `bool` | If True, enables attention slicing to reduce VRAM usage. |
|
|
67
|
+
| `height` | `int` | The height of the generated image in pixels. |
|
|
68
|
+
| `width` | `int` | The width of the generated image in pixels. |
|
|
69
|
+
| `num_inference_steps` | `int` | The number of diffusion steps to run. |
|
|
70
|
+
| `guidance_scale` | `float` | Classifier-free guidance scale (CFG). Higher values increase adherence to the prompt. |
|
|
71
|
+
| `prompts` | `list[str]` | The list of prompts to generate - more than one prompt will be batched so watch your VRAM! |
|
|
72
|
+
| `images_to_generate` | `int` | The number of images to generate per prompt. The batch size will be number of prompts * images_to_generate |
|
|
73
|
+
| `seeds` | `list[int]` | List of random seeds. Leave empty (`[]`) for automatic random generation. |
|
|
74
|
+
| `image_save_folder` | `str` | Output directory for image files. |
|
|
75
|
+
| `save_image_gen_stats` | `bool` | If True, saves detailed metadata to the statistics CSV file. |
|
|
76
|
+
| `image_gen_data_file_path` | `str` | Path to the output statistics CSV file. |
|
|
77
|
+
|
|
78
|
+
## Generating images
|
|
79
|
+
|
|
80
|
+
You can run the example script:
|
|
81
|
+
|
|
82
|
+
```sh
|
|
83
|
+
python src/imagejenerator/examples/generate_image.py
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Which simply contains:
|
|
87
|
+
|
|
88
|
+
```py
|
|
89
|
+
from imagejenerator.models import registry
|
|
90
|
+
from imagejenerator.config import config
|
|
91
|
+
|
|
92
|
+
image_generator = registry.get_model_class(config)
|
|
93
|
+
image_generator.generate_image()
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Your images and a statistics CSV file will be saved to the configured output folders.
|
|
97
|
+
|
|
98
|
+
## Architecture and Extensibility
|
|
99
|
+
|
|
100
|
+
The core of `Image Jenerator` is built around a base ImageGenerator class and a model registry, making it easy to add new models (like SDXL, Stable Cascade, or custom pipelines) without modifying the core logic.
|
|
101
|
+
|
|
102
|
+
### 1. Abstract Base Class (`ImageGenerator`)
|
|
103
|
+
|
|
104
|
+
All model pipelines inherit from `ImageGenerator`, which defines the standard workflow methods and parameters common to all models.
|
|
105
|
+
|
|
106
|
+
### 2. Model Registration
|
|
107
|
+
|
|
108
|
+
Each model (or group of models that require the same diffusers class) use their own model class, with the `@register_model` decorator applied to it. This automatically adds model classes to the registry, so that they can be specified in the config.
|
|
109
|
+
|
|
110
|
+
### 3. Data Logging (`ImageGenerationRecord`)
|
|
111
|
+
|
|
112
|
+
The `ImageGenerationRecord` class uses Python dataclass features to structure and save the generation parameters (prompt, seed, model, timings, etc.) to a specified CSV file. So you can track and analyse your experiments with different inference steps, prompt lengths, and so on.
|
|
113
|
+
|
|
114
|
+
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
imagejenerator/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
imagejenerator/config.py,sha256=lSEWYO7KkDt4td4KPDAHRjvK-TQrPK16XzUNirts3-g,611
|
|
3
|
+
imagejenerator/core/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
imagejenerator/core/image_generation_record.py,sha256=kK4deAyL1kVDU9vocBXkyIQC3ApBBiDLAlunHlI0wfY,4711
|
|
5
|
+
imagejenerator/core/image_generator.py,sha256=pD-Gv7dvK3P5OrN_Q3-29mo8DAEAwGHQ3ap0E8QNEsc,8864
|
|
6
|
+
imagejenerator/examples/generate_image.py,sha256=jB1080XSMAV9WjJth5Y-fRG1m-ZDWK9Vzw5loRdT7PA,170
|
|
7
|
+
imagejenerator/models/__init__.py,sha256=ElV0OJlTZwEsz-8czTuQPNEf8nPBp2sIPQtVBQMjoqs,136
|
|
8
|
+
imagejenerator/models/registry.py,sha256=ZOyrKo_SwYpqdvjcyx45FCgQz0E3dJ4oqcXfFQBuUDY,1697
|
|
9
|
+
imagejenerator/models/sd_1_5.py,sha256=rx07JkxsuEqDmpw_nlBDb5eyVvabXEfVTFWCQwYcnwk,3322
|
|
10
|
+
imagejenerator/models/sd_schedulers.py,sha256=j5mRSzmRt5N_vDKxkYDVgp04Ss8W26nPO9_9sXpnMTk,356
|
|
11
|
+
imagejenerator/tools/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
12
|
+
imagejenerator-1.0.0.dist-info/METADATA,sha256=nV4ShAfQxZHddgzdqYoe-yy4ULgLL1KSpiXoR_u4tPc,5206
|
|
13
|
+
imagejenerator-1.0.0.dist-info/WHEEL,sha256=zp0Cn7JsFoX2ATtOhtaFYIiE2rmFAD4OcMhtUki8W3U,88
|
|
14
|
+
imagejenerator-1.0.0.dist-info/licenses/LICENSE,sha256=w7RwZZIGGJ2N4R9zMujxLEdeVQklNLxEu2AmN-Uwaqc,1070
|
|
15
|
+
imagejenerator-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Warren Davies
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|