-
Notifications
You must be signed in to change notification settings - Fork 4
/
generate_gpt.py
89 lines (74 loc) · 2.7 KB
/
generate_gpt.py
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
import torch
import click
from train_gpt import ImageGPT, GPTConfig
import numpy as np
from PIL import Image
import os
from cosmos_tokenizer.image_lib import ImageTokenizer
@click.command()
@click.option("--checkpoint", required=True, help="Path to model checkpoint")
@click.option("--num_samples", default=5, help="Number of samples to generate")
@click.option("--temperature", default=0.9, help="Sampling temperature")
@click.option("--top_k", default=100, help="Top-k sampling parameter")
@click.option(
"--class_ids",
default="1,130,933,833",
help="Comma-separated list of class IDs to generate",
)
@click.option(
"--output_dir",
default="generated_images",
help="Output directory for generated images",
)
def generate(checkpoint, num_samples, temperature, top_k, class_ids, output_dir):
# Load checkpoint
ckpt = torch.load(checkpoint, map_location="cpu")
config = ckpt["config"]
# Initialize model
model = ImageGPT(config)
model.load_state_dict(ckpt["model"], strict=True)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device)
model.eval()
# Initialize decoder
decoder = ImageTokenizer(
checkpoint_dec="tokenize_dataset/pretrained_ckpts/Cosmos-Tokenizer-DI8x8/decoder.jit"
).to(device)
# Create output directory
os.makedirs(output_dir, exist_ok=True)
# Parse class IDs
class_ids = [int(x) for x in class_ids.split(",")]
print(
f"Generating {len(class_ids)} samples with temperature={temperature}, top_k={top_k}"
)
with torch.no_grad() and torch.cuda.amp.autocast(dtype=torch.bfloat16):
# Generate samples
test_classes = torch.tensor(class_ids, device=device)
samples = model.generate(
test_classes,
max_tokens=1024, # 32x32 image
temperature=temperature,
top_k=None,
)
# Decode and save each sample
for i, (sample, class_id) in enumerate(zip(samples, class_ids)):
# Reshape to [1, 32, 32] for decoder
tokens = sample.reshape(1, 32, 32)
# Decode the image
reconstructed = decoder.decode(tokens)
# Convert to PIL image
img = (
((reconstructed[0].cpu().float() + 1) * 127.5)
.clamp(0, 255)
.to(torch.uint8)
)
img = img.permute(1, 2, 0).numpy()
img = Image.fromarray(img)
# Save the image
save_path = os.path.join(
output_dir, f"generated_{i:03d}_class{class_id}.png"
)
img.save(save_path)
print(f"Saved image: {save_path}")
if __name__ == "__main__":
generate()