forked from lucidrains/DALLE-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 7
/
mixVAEcuda.py
executable file
·59 lines (46 loc) · 1.64 KB
/
mixVAEcuda.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
import torch
from torch import nn, optim
from torchvision import datasets, transforms
from torchvision.utils import save_image
from torch.utils.data import DataLoader
from dalle_pytorch import DiscreteVAE
imgSize = 256
load_epoch = 280
vae = DiscreteVAE(
image_size = imgSize,
num_layers = 3,
channels = 3,
num_tokens = 2048,
codebook_dim = 1024,
hidden_dim = 128
)
vae_dict = torch.load("./models/dvae-"+str(load_epoch)+".pth")
vae.load_state_dict(vae_dict)
vae.cuda()
batchSize = 12
n_epochs = 500
log_interval = 20
#images = torch.randn(4, 3, 256, 256)
t = transforms.Compose([
transforms.Resize(imgSize),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) #(0.267, 0.233, 0.234))
])
train_set = datasets.ImageFolder('./imagedata', transform=t, target_transform=None)
train_loader = DataLoader(dataset=train_set, num_workers=1, batch_size=batchSize, shuffle=True)
for batch_idx, (images, _) in enumerate(train_loader):
images = images.cuda()
codes = vae.get_codebook_indices(images)
sample1 = vae.decode(codes)
#save_image(sample.view(-1, 3, imgSize, imgSize),
# 'results/recon_sample_' + str(batch_idx) + '.png', normalize=True)
for i in range(0, 8):
j = i + 1
j = j % 8
codes[i,512:] = codes[j,512:]
sample2 = vae.decode(codes)
grid = torch.cat([images[:8], sample1[:8], sample2[:8]])
save_image(grid.view(-1, 3, imgSize, imgSize),
'mixed/mixed_epoch_' +str(load_epoch) + "_"+ str(batch_idx) + '.png', normalize=True)
#break