forked from cloneofsimo/minRF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrf.py
161 lines (134 loc) · 5.06 KB
/
rf.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
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
# implementation of Rectified Flow for simple minded people like me.
import argparse
import torch
class RF:
def __init__(self, model, ln=True):
self.model = model
self.ln = ln
def forward(self, x, cond):
b = x.size(0)
if self.ln:
nt = torch.randn((b,)).to(x.device)
t = torch.sigmoid(nt)
else:
t = torch.rand((b,)).to(x.device)
texp = t.view([b, *([1] * len(x.shape[1:]))])
z1 = torch.randn_like(x)
zt = (1 - texp) * x + texp * z1
vtheta = self.model(zt, t, cond)
batchwise_mse = ((z1 - x - vtheta) ** 2).mean(dim=list(range(1, len(x.shape))))
tlist = batchwise_mse.detach().cpu().reshape(-1).tolist()
ttloss = [(tv, tloss) for tv, tloss in zip(t, tlist)]
return batchwise_mse.mean(), ttloss
@torch.no_grad()
def sample(self, z, cond, null_cond=None, sample_steps=50, cfg=2.0):
b = z.size(0)
dt = 1.0 / sample_steps
dt = torch.tensor([dt] * b).to(z.device).view([b, *([1] * len(z.shape[1:]))])
images = [z]
for i in range(sample_steps, 0, -1):
t = i / sample_steps
t = torch.tensor([t] * b).to(z.device)
vc = self.model(z, t, cond)
if null_cond is not None:
vu = self.model(z, t, null_cond)
vc = vu + cfg * (vc - vu)
z = z - dt * vc
images.append(z)
return images
if __name__ == "__main__":
# train class conditional RF on mnist.
import numpy as np
import torch.optim as optim
from PIL import Image
from torch.utils.data import DataLoader
from torchvision import datasets, transforms
from torchvision.utils import make_grid
from tqdm import tqdm
import wandb
from dit import DiT_Llama
parser = argparse.ArgumentParser(description="use cifar?")
parser.add_argument("--cifar", action="store_true")
args = parser.parse_args()
CIFAR = args.cifar
if CIFAR:
dataset_name = "cifar"
fdatasets = datasets.CIFAR10
transform = transforms.Compose(
[
transforms.ToTensor(),
transforms.RandomCrop(32),
transforms.RandomHorizontalFlip(),
transforms.Normalize((0.5,), (0.5,)),
]
)
channels = 3
model = DiT_Llama(
channels, 32, dim=256, n_layers=10, n_heads=8, num_classes=10
).cuda()
else:
dataset_name = "mnist"
fdatasets = datasets.MNIST
transform = transforms.Compose(
[
transforms.ToTensor(),
transforms.Pad(2),
transforms.Normalize((0.5,), (0.5,)),
]
)
channels = 1
model = DiT_Llama(
channels, 32, dim=64, n_layers=6, n_heads=4, num_classes=10
).cuda()
model_size = sum(p.numel() for p in model.parameters() if p.requires_grad)
print(f"Number of parameters: {model_size}, {model_size / 1e6}M")
rf = RF(model)
optimizer = optim.Adam(model.parameters(), lr=5e-4)
criterion = torch.nn.MSELoss()
mnist = fdatasets(root="./data", train=True, download=True, transform=transform)
dataloader = DataLoader(mnist, batch_size=256, shuffle=True, drop_last=True)
wandb.init(project=f"rf_{dataset_name}")
for epoch in range(100):
lossbin = {i: 0 for i in range(10)}
losscnt = {i: 1e-6 for i in range(10)}
for i, (x, c) in tqdm(enumerate(dataloader)):
x, c = x.cuda(), c.cuda()
optimizer.zero_grad()
loss, blsct = rf.forward(x, c)
loss.backward()
optimizer.step()
wandb.log({"loss": loss.item()})
# count based on t
for t, l in blsct:
lossbin[int(t * 10)] += l
losscnt[int(t * 10)] += 1
# log
for i in range(10):
print(f"Epoch: {epoch}, {i} range loss: {lossbin[i] / losscnt[i]}")
wandb.log({f"lossbin_{i}": lossbin[i] / losscnt[i] for i in range(10)})
rf.model.eval()
with torch.no_grad():
cond = torch.arange(0, 16).cuda() % 10
uncond = torch.ones_like(cond) * 10
init_noise = torch.randn(16, channels, 32, 32).cuda()
images = rf.sample(init_noise, cond, uncond)
# image sequences to gif
gif = []
for image in images:
# unnormalize
image = image * 0.5 + 0.5
image = image.clamp(0, 1)
x_as_image = make_grid(image.float(), nrow=4)
img = x_as_image.permute(1, 2, 0).cpu().numpy()
img = (img * 255).astype(np.uint8)
gif.append(Image.fromarray(img))
gif[0].save(
f"contents/sample_{epoch}.gif",
save_all=True,
append_images=gif[1:],
duration=100,
loop=0,
)
last_img = gif[-1]
last_img.save(f"contents/sample_{epoch}_last.png")
rf.model.train()