-
Notifications
You must be signed in to change notification settings - Fork 34
/
train_image_only_with_unet.py
112 lines (84 loc) · 2.73 KB
/
train_image_only_with_unet.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
from shutil import rmtree
from pathlib import Path
import torch
from torch import tensor, nn
from torch.nn import Module
from torch.utils.data import Dataset, DataLoader
from torch.optim import Adam
from einops import rearrange
import torchvision
import torchvision.transforms as T
from torchvision.utils import save_image
from transfusion_pytorch import Transfusion, print_modality_sample
rmtree('./results', ignore_errors = True)
results_folder = Path('./results')
results_folder.mkdir(exist_ok = True, parents = True)
# functions
def divisible_by(num, den):
return (num % den) == 0
# encoder / decoder
class Encoder(Module):
def forward(self, x):
x = rearrange(x, '... 1 (h p1) (w p2) -> ... (p1 p2) h w', p1 = 2, p2 = 2)
return x * 2 - 1
class Decoder(Module):
def forward(self, x):
x = rearrange(x, '... (p1 p2) h w -> ... 1 (h p1) (w p2)', p1 = 2, p2 = 2, h = 14)
return ((x + 1) * 0.5).clamp(min = 0., max = 1.)
model = Transfusion(
num_text_tokens = 10,
dim_latent = 4,
channel_first_latent = True,
modality_default_shape = (14, 14),
modality_encoder = Encoder(),
modality_decoder = Decoder(),
pre_post_transformer_enc_dec = (
nn.Conv2d(4, 64, 3, 2, 1),
nn.ConvTranspose2d(64, 4, 3, 2, 1, output_padding = 1),
),
add_pos_emb = True,
modality_num_dim = 2,
velocity_consistency_loss_weight = 0.1,
transformer = dict(
dim = 64,
depth = 4,
dim_head = 32,
heads = 8
)
).cuda()
ema_model = model.create_ema()
class MnistDataset(Dataset):
def __init__(self):
self.mnist = torchvision.datasets.MNIST(
'./data',
download = True
)
def __len__(self):
return len(self.mnist)
def __getitem__(self, idx):
pil, labels = self.mnist[idx]
digit_tensor = T.PILToTensor()(pil)
return (digit_tensor / 255).float()
def cycle(iter_dl):
while True:
for batch in iter_dl:
yield batch
dataset = MnistDataset()
dataloader = DataLoader(dataset, batch_size = 32, shuffle = True)
iter_dl = cycle(dataloader)
optimizer = Adam(model.parameters(), lr = 8e-4)
# train loop
for step in range(1, 100_000 + 1):
loss = model(next(iter_dl), velocity_consistency_ema_model = ema_model)
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), 0.5)
optimizer.step()
optimizer.zero_grad()
ema_model.update()
print(f'{step}: {loss.item():.3f}')
if divisible_by(step, 500):
image = ema_model.generate_modality_only(batch_size = 64)
save_image(
rearrange(image, '(gh gw) 1 h w -> 1 (gh h) (gw w)', gh = 8).detach().cpu(),
str(results_folder / f'{step}.png')
)