forked from openai/guided-diffusion
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupsample_windows.py
More file actions
237 lines (192 loc) Β· 8.11 KB
/
Copy pathupsample_windows.py
File metadata and controls
237 lines (192 loc) Β· 8.11 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
#!/usr/bin/env python3
"""
Windows-compatible super-resolution upsampling script
Upsamples 128x128 images to 512x512 using diffusion-based super-resolution
Usage:
Step 1: Generate 128x128 base images
python generate_images_windows.py --resolution 128 --num_samples 4 --use_classifier --classes "1,207,323,92"
Step 2: Upsample to 512x512
python upsample_windows.py --base_samples outputs/generated_128x128/samples_4x128x128x3.npz
"""
import argparse
import os
import sys
import glob
# Get the directory where this script is located
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
os.chdir(SCRIPT_DIR)
sys.path.insert(0, SCRIPT_DIR)
import numpy as np
import torch as th
from PIL import Image
from guided_diffusion.script_util import (
sr_model_and_diffusion_defaults,
sr_create_model_and_diffusion,
)
def main():
parser = argparse.ArgumentParser(description="Upsample 128x128 images to 512x512")
parser.add_argument("--base_samples", type=str, required=True,
help="Path to the 128x128 samples NPZ file")
parser.add_argument("--output_dir", type=str, default="outputs/upsampled_512",
help="Output directory for upsampled images")
parser.add_argument("--batch_size", type=int, default=1,
help="Batch size (use 1 for RTX 3050 4GB)")
parser.add_argument("--steps", type=int, default=250,
help="Number of diffusion steps")
parser.add_argument("--use_fp16", action="store_true",
help="Use half precision (faster, less VRAM)")
parser.add_argument("--seed", type=int, default=42,
help="Random seed for reproducibility")
args = parser.parse_args()
print("=" * 60)
print("π SUPER-RESOLUTION: 128Γ128 β 512Γ512")
print("=" * 60)
print()
# Check base samples exist
if not os.path.exists(args.base_samples):
print(f"β Error: Base samples not found at {args.base_samples}")
print()
print("Please generate 128x128 base samples first:")
print(' python generate_images_windows.py --resolution 128 --num_samples 4 --use_classifier --classes "1,207,323,92"')
sys.exit(1)
# Check upsampler model exists
upsampler_path = os.path.join(SCRIPT_DIR, "models", "128_512_upsampler.pt")
if not os.path.exists(upsampler_path):
print(f"β Error: Upsampler model not found at {upsampler_path}")
print()
print("Please download it:")
print(" https://openaipublic.blob.core.windows.net/diffusion/jul-2021/128_512_upsampler.pt")
sys.exit(1)
# Set device
device = th.device('cuda' if th.cuda.is_available() else 'cpu')
print(f"π₯οΈ Using device: {device}")
if device.type == 'cpu':
print("β οΈ Warning: Running on CPU will be very slow!")
# Set random seed
if args.seed is not None:
th.manual_seed(args.seed)
np.random.seed(args.seed)
print(f"π² Random seed: {args.seed}")
# Load base samples
print(f"\nπ Loading base samples from: {args.base_samples}")
data = np.load(args.base_samples)
base_images = data['arr_0']
base_labels = data['arr_1'] if 'arr_1' in data.files else None
num_samples = len(base_images)
print(f" Found {num_samples} images at {base_images.shape[1]}x{base_images.shape[2]}")
if base_labels is not None:
print(f" Labels: {base_labels.tolist()}")
# Create model
print(f"\nπ¦ Loading 128β512 upsampler model...")
model, diffusion = sr_create_model_and_diffusion(
large_size=512,
small_size=128,
class_cond=True,
learn_sigma=True,
num_channels=192,
num_res_blocks=2,
num_heads=-1,
num_head_channels=64,
num_heads_upsample=-1,
attention_resolutions="32,16",
dropout=0.0,
diffusion_steps=1000,
noise_schedule="linear",
timestep_respacing=str(args.steps),
use_kl=False,
predict_xstart=False,
rescale_timesteps=False,
rescale_learned_sigmas=False,
use_checkpoint=False,
use_scale_shift_norm=True,
resblock_updown=True,
use_fp16=args.use_fp16,
)
# Load weights
print(f" Loading weights from: {upsampler_path}")
state_dict = th.load(upsampler_path, map_location="cpu")
model.load_state_dict(state_dict)
model.to(device)
if args.use_fp16:
model.convert_to_fp16()
model.eval()
print("β Upsampler model loaded!")
# Create output directory
os.makedirs(args.output_dir, exist_ok=True)
print(f"\nπ¨ Upsampling {num_samples} images from 128Γ128 to 512Γ512...")
print(f" Steps: {args.steps}")
print(f" Output: {args.output_dir}")
print()
all_upsampled = []
for i in range(0, num_samples, args.batch_size):
batch_end = min(i + args.batch_size, num_samples)
current_batch_size = batch_end - i
print(f"π Processing image {i+1}-{batch_end}/{num_samples}...")
# Prepare low-res batch
low_res_batch = base_images[i:batch_end]
low_res_batch = th.from_numpy(low_res_batch).float()
low_res_batch = low_res_batch / 127.5 - 1.0 # Normalize to [-1, 1]
low_res_batch = low_res_batch.permute(0, 3, 1, 2) # BHWC -> BCHW
low_res_batch = low_res_batch.to(device)
model_kwargs = {"low_res": low_res_batch}
# Add class labels if available
if base_labels is not None:
labels = th.from_numpy(base_labels[i:batch_end]).to(device)
model_kwargs["y"] = labels
# Upsample
with th.no_grad():
upsampled = diffusion.p_sample_loop(
model,
(current_batch_size, 3, 512, 512),
clip_denoised=True,
model_kwargs=model_kwargs,
progress=True,
)
# Convert to uint8
upsampled = ((upsampled + 1) * 127.5).clamp(0, 255).to(th.uint8)
upsampled = upsampled.permute(0, 2, 3, 1).contiguous().cpu().numpy()
all_upsampled.append(upsampled)
print(f" β Batch complete")
# Concatenate all results
arr = np.concatenate(all_upsampled, axis=0)
# Save as NPZ
shape_str = "x".join([str(x) for x in arr.shape])
npz_path = os.path.join(args.output_dir, f"upsampled_{shape_str}.npz")
if base_labels is not None:
np.savez(npz_path, arr, base_labels)
else:
np.savez(npz_path, arr)
print(f"\nπΎ Saved NPZ: {npz_path}")
# Save as individual PNG images
print("\nπΈ Saving PNG images...")
# Class name mapping for your classes
CLASS_NAMES = {
1: "goldfish",
207: "golden_retriever",
323: "monarch_butterfly",
92: "bee_eater",
}
for i, img in enumerate(arr):
if base_labels is not None:
class_id = base_labels[i]
class_name = CLASS_NAMES.get(class_id, f"class_{class_id}")
png_path = os.path.join(args.output_dir, f"{i+1:02d}_{class_name}_512x512.png")
else:
png_path = os.path.join(args.output_dir, f"upsampled_{i+1:03d}.png")
Image.fromarray(img).save(png_path)
print(f" β {png_path}")
# Also save the original 128x128 for comparison
print("\nπΈ Saving original 128x128 images for comparison...")
for i, img in enumerate(base_images):
if base_labels is not None:
class_id = base_labels[i]
class_name = CLASS_NAMES.get(class_id, f"class_{class_id}")
png_path = os.path.join(args.output_dir, f"{i+1:02d}_{class_name}_128x128_original.png")
else:
png_path = os.path.join(args.output_dir, f"original_{i+1:03d}_128x128.png")
Image.fromarray(img).save(png_path)
print(f" β {png_path}")
print(f"\nβ
Done! Upsampled {len(arr)} images from 128Γ128 to 512Γ512")
print(f"π Output directory: {args.output_dir}")
if __name__ == "__main__":
main()