Text-to-Image
Diffusers
Safetensors
recoilme commited on
Commit
ef9dc2d
·
1 Parent(s): fd9a095
Files changed (1) hide show
  1. README.md +77 -0
README.md CHANGED
@@ -104,6 +104,83 @@ Development Note: We have not provided direct comparisons with other popular ups
104
  ### sdxs / swinir2
105
  <img src="media/0000.png" height="512"/>
106
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  ### Prompt refine
108
  ```
109
  refined = pipe.refine_prompts("girl")
 
104
  ### sdxs / swinir2
105
  <img src="media/0000.png" height="512"/>
106
 
107
+ ### upscale code example
108
+
109
+ ```
110
+ import torch
111
+ import numpy as np
112
+
113
+ from diffusers import AsymmetricAutoencoderKL
114
+ from typing import List, Union
115
+ from PIL import Image
116
+
117
+ vae = AsymmetricAutoencoderKL.from_pretrained("AiArtLab/sdxs-1b",subfolder="vae").cuda().half()
118
+ device = "cuda"
119
+
120
+ @torch.no_grad()
121
+ def image_upscale(
122
+ image: Union[str, Image.Image, List[Union[str, Image.Image]]],
123
+ batch_size: int = 1
124
+ ) -> List[Image.Image]:
125
+ """
126
+ Upscales images using asymmetric VAE (x2).
127
+ Uses smart batching: processes in parallel if sizes match, else falls back to sequential.
128
+ """
129
+ images = [image] if isinstance(image, (str, Image.Image)) else image
130
+
131
+ # 1. Preprocess: Load, Handle Alpha, Pad to %8, Normalize
132
+ batch_data = []
133
+ for img in images:
134
+ if isinstance(img, str): img = Image.open(img)
135
+ if img.mode == "RGBA":
136
+ img = Image.alpha_composite(Image.new("RGBA", img.size, (255, 255, 255)), img)
137
+ img = img.convert("RGB")
138
+
139
+ w, h = img.size
140
+ pw, ph = (8 - w % 8) % 8, (8 - h % 8) % 8
141
+ if pw or ph:
142
+ padded = Image.new("RGB", (w + pw, h + ph), (255, 255, 255))
143
+ padded.paste(img)
144
+ img = padded
145
+
146
+ t = torch.from_numpy(np.array(img).astype(np.float32) / 127.5 - 1.0).permute(2, 0, 1)
147
+ batch_data.append((t.to(device, torch.float16), w, h))
148
+
149
+ # 2. Determine Execution Strategy
150
+ # If all shapes are identical, use batch_size. Else fallback to 1.
151
+ unique_shapes = {t.shape for t, _, _ in batch_data}
152
+ step = batch_size if len(unique_shapes) == 1 else 1
153
+
154
+ output_images = []
155
+
156
+ # 3. Process Batches
157
+ for i in range(0, len(batch_data), step):
158
+ chunk = batch_data[i : i + step]
159
+
160
+ # Stack tensors [B, C, H, W]
161
+ tensors = torch.stack([c[0] for c in chunk])
162
+
163
+ # Encode -> Decode (using mean for deterministic upscale)
164
+ latents = vae.encode(tensors).latent_dist.mean
165
+ decoded = vae.decode(latents.to(vae.dtype))[0]
166
+
167
+ # 4. Post-process: Denormalize and Crop
168
+ decoded = (decoded.clamp(-1, 1) + 1) / 2
169
+ for j, tensor in enumerate(decoded):
170
+ w, h = chunk[j][1], chunk[j][2] # Original sizes
171
+
172
+ # Crop to exact 2x
173
+ arr = tensor.cpu().permute(1, 2, 0).float().numpy()
174
+ arr = arr[:h * 2, :w * 2]
175
+
176
+ output_images.append(Image.fromarray((arr * 255).astype("uint8")))
177
+
178
+ return output_images
179
+
180
+ up = image_upscale("1.jpg")
181
+ up[0].show()
182
+ ```
183
+
184
  ### Prompt refine
185
  ```
186
  refined = pipe.refine_prompts("girl")