Skip to content

Commit 6587bf5

Browse files
authored
Adds the ability to support "any" resolution.
This incorporates the changes from @rkfg and adds the ability to support "any" resolution instead of a few predefined buckets. This uses the buckets defined in the original implementation for "640" resolution as the source-of-truth for calculating aspect ratios for other resolution buckets.
1 parent 311aa05 commit 6587bf5

1 file changed

Lines changed: 24 additions & 40 deletions

File tree

diffusers_helper/bucket_tools.py

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,36 @@
11
bucket_options = {
2-
240: [
3-
(224, 336),
4-
(256, 304),
5-
(288, 272),
6-
(320, 240),
7-
(352, 208),
8-
(384, 176),
9-
],
10-
320: [
11-
(256, 576),
12-
(288, 544),
13-
(320, 512),
14-
(352, 480),
15-
(384, 448),
16-
(416, 416),
17-
(448, 384),
18-
(480, 352),
19-
(512, 320),
20-
(544, 288),
21-
(576, 256),
22-
],
23-
640: [
24-
(416, 960),
25-
(448, 864),
26-
(480, 832),
27-
(512, 768),
28-
(544, 704),
29-
(576, 672),
30-
(608, 640),
31-
(640, 608),
32-
(672, 576),
33-
(704, 544),
34-
(768, 512),
35-
(832, 480),
36-
(864, 448),
37-
(960, 416),
38-
],
2+
(416, 960),
3+
(448, 864),
4+
(480, 832),
5+
(512, 768),
6+
(544, 704),
7+
(576, 672),
8+
(608, 640),
9+
(640, 608),
10+
(672, 576),
11+
(704, 544),
12+
(768, 512),
13+
(832, 480),
14+
(864, 448),
15+
(960, 416),
3916
}
4017

4118

4219
def find_nearest_bucket(h, w, resolution=640):
43-
min_metric = float("inf")
20+
min_metric = float('inf')
4421
best_bucket = None
45-
for bucket_h, bucket_w in bucket_options[resolution]:
22+
for (bucket_h, bucket_w) in bucket_options:
4623
metric = abs(h * bucket_w - w * bucket_h)
4724
if metric <= min_metric:
4825
min_metric = metric
4926
best_bucket = (bucket_h, bucket_w)
50-
print(f"Best bucket: {best_bucket}")
27+
28+
if resolution != 640:
29+
scale_factor = resolution / 640.0
30+
scaled_height = round(best_bucket[0] * scale_factor / 16) * 16
31+
scaled_width = round(best_bucket[1] * scale_factor / 16) * 16
32+
best_bucket = (scaled_height, scaled_width)
33+
print(f'Resolution: {best_bucket[1]} x {best_bucket[0]}')
34+
5135
return best_bucket
5236

0 commit comments

Comments
 (0)