-
Notifications
You must be signed in to change notification settings - Fork 34
/
train_mnist_with_unet.py
144 lines (105 loc) · 3.35 KB
/
train_mnist_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
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
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
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
rmtree('./results', ignore_errors = True)
results_folder = Path('./results')
results_folder.mkdir(exist_ok = True, parents = True)
# constants
IMAGE_AFTER_TEXT = False
NUM_TRAIN_STEPS = 20_000
SAMPLE_EVERY = 500
# 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,
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,
channel_first_latent = True,
transformer = dict(
dim = 64,
depth = 4,
dim_head = 32,
heads = 8,
)
).to(device)
ema_model = model.create_ema()
class MnistDataset(Dataset):
def __init__(self):
self.mnist = torchvision.datasets.MNIST(
'./data/mnist',
download = True
)
def __len__(self):
return len(self.mnist)
def __getitem__(self, idx):
pil, labels = self.mnist[idx]
digit_tensor = T.PILToTensor()(pil)
output = tensor(labels), (digit_tensor / 255).float()
if not IMAGE_AFTER_TEXT:
return output
first, second = output
return second, first
def cycle(iter_dl):
while True:
for batch in iter_dl:
yield batch
def collate_fn(data):
data = [*map(list, data)]
return data
dataset = MnistDataset()
dataloader = model.create_dataloader(dataset, batch_size = 16, shuffle = True)
iter_dl = cycle(dataloader)
optimizer = Adam(model.parameters(), lr = 3e-4)
# train loop
for step in range(1, NUM_TRAIN_STEPS + 1):
model.train()
loss = model(next(iter_dl))
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}')
# eval
if divisible_by(step, SAMPLE_EVERY):
one_multimodal_sample = ema_model.sample(max_length = 384)
print_modality_sample(one_multimodal_sample)
if len(one_multimodal_sample) < 2:
continue
if IMAGE_AFTER_TEXT:
_, maybe_image, maybe_label = one_multimodal_sample
else:
maybe_label, maybe_image, *_ = one_multimodal_sample
filename = f'{step}.{maybe_label[1].item()}.png'
save_image(
maybe_image[1].cpu(),
str(results_folder / filename),
)