-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinference.py
More file actions
161 lines (129 loc) · 5.28 KB
/
inference.py
File metadata and controls
161 lines (129 loc) · 5.28 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
import torch
import torch.nn.functional as F
from torchvision.utils import make_grid, save_image
from dit import DiT
from model import MeanFlow
import os
import argparse
import numpy as np
from PIL import Image
class DiTInference:
def __init__(self, checkpoint_path, device='cuda'):
self.device = device
# Load checkpoint
print(f"Loading checkpoint from {checkpoint_path}...")
checkpoint = torch.load(checkpoint_path, map_location=device)
# Extract config
config = checkpoint.get('config', {})
self.image_size = config.get('image_size', 29)
self.image_channels = config.get('image_channels', 1)
self.sigma_min = config.get('sigma_min', 1e-6)
# Initialize DiT model
print("Initializing DiT model...")
self.model = DiT(
input_size=self.image_size,
patch_size=4,
in_channels=self.image_channels,
dim=256,
depth=8,
num_heads=4,
num_classes=10,
learn_sigma=False,
class_dropout_prob=0.1,
).to(device)
# Load weights (prefer EMA if available)
if 'ema' in checkpoint:
print("Loading EMA weights...")
self.model.load_state_dict(checkpoint['ema'])
else:
print("Loading model weights...")
self.model.load_state_dict(checkpoint['model'])
self.model.eval()
# Initialize sampler with scheduler parameters from checkpoint
self.sampler = MeanFlow(
device=device,
channels=self.image_channels,
image_size=self.image_size,
num_classes=10,
cfg_drop_prob=0.1
)
print("Model loaded successfully!")
@torch.no_grad()
def sample(self, num_samples=16, class_labels=None, seed=None):
"""
Sample images from the model.
Args:
num_samples: Number of samples to generate
class_labels: List/tensor of class labels (0-9), or None for random
cfg_scale: Classifier-free guidance scale
num_steps: Number of sampling steps
seed: Random seed for reproducibility
Returns:
Generated images tensor in [0, 1]
"""
if seed is not None:
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
if class_labels is not None:
# Handle class labels
if isinstance(class_labels, int):
class_labels = torch.full((num_samples,), class_labels, device=self.device)
elif isinstance(class_labels, list):
class_labels = torch.tensor(class_labels, device=self.device)
else:
class_labels = class_labels.to(self.device)
# Ensure correct number of labels
if len(class_labels) < num_samples:
class_labels = class_labels.repeat(num_samples // len(class_labels) + 1)[:num_samples]
# Use the sampler's method
images = self.sampler.sample_each_class(
self.model,
10,
sample_steps=4
)
return images
def main():
parser = argparse.ArgumentParser(description='DiT Direct Image Inference')
parser.add_argument('--checkpoint', type=str, required=True,
help='Path to model checkpoint')
parser.add_argument('--output_dir', type=str, default='./outputs',
help='Directory to save outputs')
parser.add_argument('--device', type=str, default='cuda',
help='Device to use (cuda/cpu)')
parser.add_argument('--seed', type=int, default=42,
help='Random seed')
# Sampling parameters
parser.add_argument('--num_samples', type=int, default=64,
help='Number of samples to generate')
parser.add_argument('--class_label', type=int, default=None,
help='Specific class to sample (0-9), or None for random')
args = parser.parse_args()
# Initialize model
model = DiTInference(args.checkpoint, device=args.device)
# Create output directory
os.makedirs(args.output_dir, exist_ok=True)
images = model.sample(
num_samples=args.num_samples,
class_labels=args.class_label,
seed=args.seed
)
# Save individual images
for i, img in enumerate(images):
print('img shape: ', img.shape)
# change 1 channel to 3 channel
if img.shape[0] == 1: # then repeat
img = img.repeat(3, 1, 1)
print('max', img.max(), 'min ', img.min())
img = img.clamp(-1, 1)
# img = img.repeat
img_np = (((img.permute(1, 2, 0).cpu().numpy() + 1) / 2) * 255).astype(np.uint8)
img_np = np.clip(img_np, 0, 255)
Image.fromarray(img_np).save(
os.path.join(args.output_dir, f'sample_{i:04d}.png')
)
# Save grid
grid = make_grid(images, nrow=int(np.sqrt(args.num_samples)), normalize=False)
save_image(grid, os.path.join(args.output_dir, 'samples_grid.png'))
print(f"\nGenerated images saved to {args.output_dir}/")
if __name__ == "__main__":
main()