Skip to content

Commit

Permalink
VAE CelebA
Browse files Browse the repository at this point in the history
  • Loading branch information
carlportz committed Jul 18, 2024
1 parent ba3c9a0 commit a942416
Show file tree
Hide file tree
Showing 3 changed files with 278 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ code/*/.hypothesis
.DS_Store
*/.DS_Store

# Pytorch data sets
# Pytorch data sets and models
src/codes/07-summary/data/
src/codes/07-summary/models/

23 changes: 13 additions & 10 deletions src/codes/07-summary/vae_celeba.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
from os import mkdir
from torch import optim
from torch.nn import functional as F
import torchvision
from torchvision.utils import save_image
from torchvision.datasets import CelebA
from torch.utils.data import DataLoader

import numpy as np
from tqdm import tqdm
import matplotlib.pyplot as plt


Expand All @@ -25,6 +25,7 @@

print(f'Using device: {device}')


class VAE(nn.Module):
def __init__(self, IMAGE_SIZE=150, LATENT_DIM=128):
super(VAE, self).__init__()
Expand Down Expand Up @@ -171,15 +172,16 @@ def test(epoch):
image_dim = 3 * IMAGE_SIZE * IMAGE_SIZE
LATENT_DIM = 128
EPOCHS = 20 # number of training epochs
BATCH_SIZE = 16 # for data loaders
BATCH_SIZE = 64 # for data loaders
CELEB_PATH = 'data/'

# data = torch.utils.data.DataLoader(
# torchvision.datasets.CelebA('./data',
# transform=torchvision.transforms.ToTensor(),
# download=True),
# batch_size=128,
# shuffle=True)
# Download the dataset once
data = torch.utils.data.DataLoader(
torchvision.datasets.CelebA(CELEB_PATH,
transform=torchvision.transforms.ToTensor(),
download=True),
batch_size=128,
shuffle=True)

celeb_transform = transforms.Compose([
transforms.Resize(IMAGE_SIZE, antialias=True),
Expand Down Expand Up @@ -208,12 +210,13 @@ def test(epoch):
optimizer = optim.Adam(model.parameters(), lr=1e-3)


for epoch in tqdm(range(1, EPOCHS + 1)):
for epoch in range(1, EPOCHS + 1):
train(epoch)
torch.save(model, f'{directory}/vae_model_{epoch}.pth')
test(epoch)
with torch.no_grad():
sample = torch.randn(64, LATENT_DIM).to(device)
sample = model.decode(sample).cpu()
save_image(sample.view(64, 3, IMAGE_SIZE, IMAGE_SIZE),
f'{directory}/sample_{str(epoch)}.png')
f'{directory}/sample_{str(epoch)}.png')
del sample
263 changes: 263 additions & 0 deletions src/codes/07-summary/vae_train.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
nohup: Eingabe wird ignoriert
Using device: cuda
Files already downloaded and verified
Train Epoch: 1 [0/162770 (0%)] Loss: 0.001511
Train Epoch: 1 [6400/162770 (4%)] Loss: 0.000363
Train Epoch: 1 [12800/162770 (8%)] Loss: 0.000266
Train Epoch: 1 [19200/162770 (12%)] Loss: 0.000226
Train Epoch: 1 [25600/162770 (16%)] Loss: 0.000207
Train Epoch: 1 [32000/162770 (20%)] Loss: 0.000189
Train Epoch: 1 [38400/162770 (24%)] Loss: 0.000180
Train Epoch: 1 [44800/162770 (28%)] Loss: 0.000162
Train Epoch: 1 [51200/162770 (31%)] Loss: 0.000142
Train Epoch: 1 [57600/162770 (35%)] Loss: 0.000149
Train Epoch: 1 [64000/162770 (39%)] Loss: 0.000156
Train Epoch: 1 [70400/162770 (43%)] Loss: 0.000144
Train Epoch: 1 [76800/162770 (47%)] Loss: 0.000164
Train Epoch: 1 [83200/162770 (51%)] Loss: 0.000145
Train Epoch: 1 [89600/162770 (55%)] Loss: 0.000121
Train Epoch: 1 [96000/162770 (59%)] Loss: 0.000133
Train Epoch: 1 [102400/162770 (63%)] Loss: 0.000128
Train Epoch: 1 [108800/162770 (67%)] Loss: 0.000154
Train Epoch: 1 [115200/162770 (71%)] Loss: 0.000116
Train Epoch: 1 [121600/162770 (75%)] Loss: 0.000137
Train Epoch: 1 [128000/162770 (79%)] Loss: 0.000123
Train Epoch: 1 [134400/162770 (83%)] Loss: 0.000133
Train Epoch: 1 [140800/162770 (86%)] Loss: 0.000125
Train Epoch: 1 [147200/162770 (90%)] Loss: 0.000134
Train Epoch: 1 [153600/162770 (94%)] Loss: 0.000117
Train Epoch: 1 [160000/162770 (98%)] Loss: 0.000124
====> Epoch: 1 Average loss: 0.0002
====> Test set loss: 0.0001
Train Epoch: 2 [0/162770 (0%)] Loss: 0.000116
Train Epoch: 2 [6400/162770 (4%)] Loss: 0.000132
Train Epoch: 2 [12800/162770 (8%)] Loss: 0.000123
Train Epoch: 2 [19200/162770 (12%)] Loss: 0.000122
Train Epoch: 2 [25600/162770 (16%)] Loss: 0.000134
Train Epoch: 2 [32000/162770 (20%)] Loss: 0.000122
Train Epoch: 2 [38400/162770 (24%)] Loss: 0.000121
Train Epoch: 2 [44800/162770 (28%)] Loss: 0.000130
Train Epoch: 2 [51200/162770 (31%)] Loss: 0.000116
Train Epoch: 2 [57600/162770 (35%)] Loss: 0.000121
Train Epoch: 2 [64000/162770 (39%)] Loss: 0.000113
Train Epoch: 2 [70400/162770 (43%)] Loss: 0.000120
Train Epoch: 2 [76800/162770 (47%)] Loss: 0.000123
Train Epoch: 2 [83200/162770 (51%)] Loss: 0.000112
Train Epoch: 2 [89600/162770 (55%)] Loss: 0.000108
Train Epoch: 2 [96000/162770 (59%)] Loss: 0.000122
Train Epoch: 2 [102400/162770 (63%)] Loss: 0.000126
Train Epoch: 2 [108800/162770 (67%)] Loss: 0.000125
Train Epoch: 2 [115200/162770 (71%)] Loss: 0.000117
Train Epoch: 2 [121600/162770 (75%)] Loss: 0.000142
Train Epoch: 2 [128000/162770 (79%)] Loss: 0.000115
Train Epoch: 2 [134400/162770 (83%)] Loss: 0.000125
Train Epoch: 2 [140800/162770 (86%)] Loss: 0.000109
Train Epoch: 2 [147200/162770 (90%)] Loss: 0.000108
Train Epoch: 2 [153600/162770 (94%)] Loss: 0.000112
Train Epoch: 2 [160000/162770 (98%)] Loss: 0.000115
====> Epoch: 2 Average loss: 0.0001
====> Test set loss: 0.0001
Train Epoch: 3 [0/162770 (0%)] Loss: 0.000118
Train Epoch: 3 [6400/162770 (4%)] Loss: 0.000111
Train Epoch: 3 [12800/162770 (8%)] Loss: 0.000120
Train Epoch: 3 [19200/162770 (12%)] Loss: 0.000109
Train Epoch: 3 [25600/162770 (16%)] Loss: 0.000132
Train Epoch: 3 [32000/162770 (20%)] Loss: 0.000110
Train Epoch: 3 [38400/162770 (24%)] Loss: 0.000107
Train Epoch: 3 [44800/162770 (28%)] Loss: 0.000116
Train Epoch: 3 [51200/162770 (31%)] Loss: 0.000112
Train Epoch: 3 [57600/162770 (35%)] Loss: 0.000121
Train Epoch: 3 [64000/162770 (39%)] Loss: 0.000118
Train Epoch: 3 [70400/162770 (43%)] Loss: 0.000113
Train Epoch: 3 [76800/162770 (47%)] Loss: 0.000110
Train Epoch: 3 [83200/162770 (51%)] Loss: 0.000112
Train Epoch: 3 [89600/162770 (55%)] Loss: 0.000104
Train Epoch: 3 [96000/162770 (59%)] Loss: 0.000102
Train Epoch: 3 [102400/162770 (63%)] Loss: 0.000113
Train Epoch: 3 [108800/162770 (67%)] Loss: 0.000107
Train Epoch: 3 [115200/162770 (71%)] Loss: 0.000106
Train Epoch: 3 [121600/162770 (75%)] Loss: 0.000123
Train Epoch: 3 [128000/162770 (79%)] Loss: 0.000115
Train Epoch: 3 [134400/162770 (83%)] Loss: 0.000112
Train Epoch: 3 [140800/162770 (86%)] Loss: 0.000104
Train Epoch: 3 [147200/162770 (90%)] Loss: 0.000125
Train Epoch: 3 [153600/162770 (94%)] Loss: 0.000117
Train Epoch: 3 [160000/162770 (98%)] Loss: 0.000100
====> Epoch: 3 Average loss: 0.0001
====> Test set loss: 0.0001
Train Epoch: 4 [0/162770 (0%)] Loss: 0.000106
Train Epoch: 4 [6400/162770 (4%)] Loss: 0.000119
Train Epoch: 4 [12800/162770 (8%)] Loss: 0.000101
Train Epoch: 4 [19200/162770 (12%)] Loss: 0.000107
Train Epoch: 4 [25600/162770 (16%)] Loss: 0.000104
Train Epoch: 4 [32000/162770 (20%)] Loss: 0.000110
Train Epoch: 4 [38400/162770 (24%)] Loss: 0.000110
Train Epoch: 4 [44800/162770 (28%)] Loss: 0.000111
Train Epoch: 4 [51200/162770 (31%)] Loss: 0.000110
Train Epoch: 4 [57600/162770 (35%)] Loss: 0.000113
Train Epoch: 4 [64000/162770 (39%)] Loss: 0.000107
Train Epoch: 4 [70400/162770 (43%)] Loss: 0.000109
Train Epoch: 4 [76800/162770 (47%)] Loss: 0.000122
Train Epoch: 4 [83200/162770 (51%)] Loss: 0.000098
Train Epoch: 4 [89600/162770 (55%)] Loss: 0.000094
Train Epoch: 4 [96000/162770 (59%)] Loss: 0.000102
Train Epoch: 4 [102400/162770 (63%)] Loss: 0.000111
Train Epoch: 4 [108800/162770 (67%)] Loss: 0.000109
Train Epoch: 4 [115200/162770 (71%)] Loss: 0.000117
Train Epoch: 4 [121600/162770 (75%)] Loss: 0.000104
Train Epoch: 4 [128000/162770 (79%)] Loss: 0.000096
Train Epoch: 4 [134400/162770 (83%)] Loss: 0.000115
Train Epoch: 4 [140800/162770 (86%)] Loss: 0.000111
Train Epoch: 4 [147200/162770 (90%)] Loss: 0.000097
Train Epoch: 4 [153600/162770 (94%)] Loss: 0.000114
Train Epoch: 4 [160000/162770 (98%)] Loss: 0.000097
====> Epoch: 4 Average loss: 0.0001
====> Test set loss: 0.0001
Train Epoch: 5 [0/162770 (0%)] Loss: 0.000115
Train Epoch: 5 [6400/162770 (4%)] Loss: 0.000109
Train Epoch: 5 [12800/162770 (8%)] Loss: 0.000100
Train Epoch: 5 [19200/162770 (12%)] Loss: 0.000108
Train Epoch: 5 [25600/162770 (16%)] Loss: 0.000105
Train Epoch: 5 [32000/162770 (20%)] Loss: 0.000100
Train Epoch: 5 [38400/162770 (24%)] Loss: 0.000098
Train Epoch: 5 [44800/162770 (28%)] Loss: 0.000095
Train Epoch: 5 [51200/162770 (31%)] Loss: 0.000106
Train Epoch: 5 [57600/162770 (35%)] Loss: 0.000093
Train Epoch: 5 [64000/162770 (39%)] Loss: 0.000110
Train Epoch: 5 [70400/162770 (43%)] Loss: 0.000110
Train Epoch: 5 [76800/162770 (47%)] Loss: 0.000118
Train Epoch: 5 [83200/162770 (51%)] Loss: 0.000115
Train Epoch: 5 [89600/162770 (55%)] Loss: 0.000103
Train Epoch: 5 [96000/162770 (59%)] Loss: 0.000111
Train Epoch: 5 [102400/162770 (63%)] Loss: 0.000101
Train Epoch: 5 [108800/162770 (67%)] Loss: 0.000097
Train Epoch: 5 [115200/162770 (71%)] Loss: 0.000105
Train Epoch: 5 [121600/162770 (75%)] Loss: 0.000109
Train Epoch: 5 [128000/162770 (79%)] Loss: 0.000096
Train Epoch: 5 [134400/162770 (83%)] Loss: 0.000113
Train Epoch: 5 [140800/162770 (86%)] Loss: 0.000100
Train Epoch: 5 [147200/162770 (90%)] Loss: 0.000107
Train Epoch: 5 [153600/162770 (94%)] Loss: 0.000101
Train Epoch: 5 [160000/162770 (98%)] Loss: 0.000107
====> Epoch: 5 Average loss: 0.0001
====> Test set loss: 0.0001
Train Epoch: 6 [0/162770 (0%)] Loss: 0.000117
Train Epoch: 6 [6400/162770 (4%)] Loss: 0.000107
Train Epoch: 6 [12800/162770 (8%)] Loss: 0.000107
Train Epoch: 6 [19200/162770 (12%)] Loss: 0.000102
Train Epoch: 6 [25600/162770 (16%)] Loss: 0.000113
Train Epoch: 6 [32000/162770 (20%)] Loss: 0.000102
Train Epoch: 6 [38400/162770 (24%)] Loss: 0.000105
Train Epoch: 6 [44800/162770 (28%)] Loss: 0.000099
Train Epoch: 6 [51200/162770 (31%)] Loss: 0.000102
Train Epoch: 6 [57600/162770 (35%)] Loss: 0.000106
Train Epoch: 6 [64000/162770 (39%)] Loss: 0.000101
Train Epoch: 6 [70400/162770 (43%)] Loss: 0.000103
Train Epoch: 6 [76800/162770 (47%)] Loss: 0.000104
Train Epoch: 6 [83200/162770 (51%)] Loss: 0.000102
Train Epoch: 6 [89600/162770 (55%)] Loss: 0.000108
Train Epoch: 6 [96000/162770 (59%)] Loss: 0.000103
Train Epoch: 6 [102400/162770 (63%)] Loss: 0.000102
Train Epoch: 6 [108800/162770 (67%)] Loss: 0.000104
Train Epoch: 6 [115200/162770 (71%)] Loss: 0.000105
Train Epoch: 6 [121600/162770 (75%)] Loss: 0.000097
Train Epoch: 6 [128000/162770 (79%)] Loss: 0.000094
Train Epoch: 6 [134400/162770 (83%)] Loss: 0.000088
Train Epoch: 6 [140800/162770 (86%)] Loss: 0.000120
Train Epoch: 6 [147200/162770 (90%)] Loss: 0.000098
Train Epoch: 6 [153600/162770 (94%)] Loss: 0.000108
Train Epoch: 6 [160000/162770 (98%)] Loss: 0.000106
====> Epoch: 6 Average loss: 0.0001
====> Test set loss: 0.0001
Train Epoch: 7 [0/162770 (0%)] Loss: 0.000103
Train Epoch: 7 [6400/162770 (4%)] Loss: 0.000094
Train Epoch: 7 [12800/162770 (8%)] Loss: 0.000116
Train Epoch: 7 [19200/162770 (12%)] Loss: 0.000096
Train Epoch: 7 [25600/162770 (16%)] Loss: 0.000090
Train Epoch: 7 [32000/162770 (20%)] Loss: 0.000101
Train Epoch: 7 [38400/162770 (24%)] Loss: 0.000100
Train Epoch: 7 [44800/162770 (28%)] Loss: 0.000101
Train Epoch: 7 [51200/162770 (31%)] Loss: 0.000094
Train Epoch: 7 [57600/162770 (35%)] Loss: 0.000105
Train Epoch: 7 [64000/162770 (39%)] Loss: 0.000102
Train Epoch: 7 [70400/162770 (43%)] Loss: 0.000097
Train Epoch: 7 [76800/162770 (47%)] Loss: 0.000101
Train Epoch: 7 [83200/162770 (51%)] Loss: 0.000106
Train Epoch: 7 [89600/162770 (55%)] Loss: 0.000096
Train Epoch: 7 [96000/162770 (59%)] Loss: 0.000108
Train Epoch: 7 [102400/162770 (63%)] Loss: 0.000095
Train Epoch: 7 [108800/162770 (67%)] Loss: 0.000099
Train Epoch: 7 [115200/162770 (71%)] Loss: 0.000099
Train Epoch: 7 [121600/162770 (75%)] Loss: 0.000107
Train Epoch: 7 [128000/162770 (79%)] Loss: 0.000107
Train Epoch: 7 [134400/162770 (83%)] Loss: 0.000106
Train Epoch: 7 [140800/162770 (86%)] Loss: 0.000095
Train Epoch: 7 [147200/162770 (90%)] Loss: 0.000095
Train Epoch: 7 [153600/162770 (94%)] Loss: 0.000099
Train Epoch: 7 [160000/162770 (98%)] Loss: 0.000100
====> Epoch: 7 Average loss: 0.0001
====> Test set loss: 0.0001
Train Epoch: 8 [0/162770 (0%)] Loss: 0.000102
Train Epoch: 8 [6400/162770 (4%)] Loss: 0.000099
Train Epoch: 8 [12800/162770 (8%)] Loss: 0.000105
Train Epoch: 8 [19200/162770 (12%)] Loss: 0.000093
Train Epoch: 8 [25600/162770 (16%)] Loss: 0.000104
Train Epoch: 8 [32000/162770 (20%)] Loss: 0.000103
Train Epoch: 8 [38400/162770 (24%)] Loss: 0.000114
Train Epoch: 8 [44800/162770 (28%)] Loss: 0.000089
Train Epoch: 8 [51200/162770 (31%)] Loss: 0.000096
Train Epoch: 8 [57600/162770 (35%)] Loss: 0.000101
Train Epoch: 8 [64000/162770 (39%)] Loss: 0.000095
Train Epoch: 8 [70400/162770 (43%)] Loss: 0.000100
Train Epoch: 8 [76800/162770 (47%)] Loss: 0.000101
Train Epoch: 8 [83200/162770 (51%)] Loss: 0.000106
Train Epoch: 8 [89600/162770 (55%)] Loss: 0.000091
Train Epoch: 8 [96000/162770 (59%)] Loss: 0.000096
Train Epoch: 8 [102400/162770 (63%)] Loss: 0.000108
Train Epoch: 8 [108800/162770 (67%)] Loss: 0.000101
Train Epoch: 8 [115200/162770 (71%)] Loss: 0.000098
Train Epoch: 8 [121600/162770 (75%)] Loss: 0.000106
Train Epoch: 8 [128000/162770 (79%)] Loss: 0.000103
Train Epoch: 8 [134400/162770 (83%)] Loss: 0.000113
Train Epoch: 8 [140800/162770 (86%)] Loss: 0.000104
Train Epoch: 8 [147200/162770 (90%)] Loss: 0.000108
Train Epoch: 8 [153600/162770 (94%)] Loss: 0.000108
Train Epoch: 8 [160000/162770 (98%)] Loss: 0.000099
====> Epoch: 8 Average loss: 0.0001
====> Test set loss: 0.0001
Train Epoch: 9 [0/162770 (0%)] Loss: 0.000094
Train Epoch: 9 [6400/162770 (4%)] Loss: 0.000101
Train Epoch: 9 [12800/162770 (8%)] Loss: 0.000097
Train Epoch: 9 [19200/162770 (12%)] Loss: 0.000092
Train Epoch: 9 [25600/162770 (16%)] Loss: 0.000102
Train Epoch: 9 [32000/162770 (20%)] Loss: 0.000094
Train Epoch: 9 [38400/162770 (24%)] Loss: 0.000100
Train Epoch: 9 [44800/162770 (28%)] Loss: 0.000096
Train Epoch: 9 [51200/162770 (31%)] Loss: 0.000112
Train Epoch: 9 [57600/162770 (35%)] Loss: 0.000106
Train Epoch: 9 [64000/162770 (39%)] Loss: 0.000090
Train Epoch: 9 [70400/162770 (43%)] Loss: 0.000097
Train Epoch: 9 [76800/162770 (47%)] Loss: 0.000098
Train Epoch: 9 [83200/162770 (51%)] Loss: 0.000110
Train Epoch: 9 [89600/162770 (55%)] Loss: 0.000100
Train Epoch: 9 [96000/162770 (59%)] Loss: 0.000106
Train Epoch: 9 [102400/162770 (63%)] Loss: 0.000107
Train Epoch: 9 [108800/162770 (67%)] Loss: 0.000093
Train Epoch: 9 [115200/162770 (71%)] Loss: 0.000106
Train Epoch: 9 [121600/162770 (75%)] Loss: 0.000100
Train Epoch: 9 [128000/162770 (79%)] Loss: 0.000086
Train Epoch: 9 [134400/162770 (83%)] Loss: 0.000092
Train Epoch: 9 [140800/162770 (86%)] Loss: 0.000106
Train Epoch: 9 [147200/162770 (90%)] Loss: 0.000106
Train Epoch: 9 [153600/162770 (94%)] Loss: 0.000099
Train Epoch: 9 [160000/162770 (98%)] Loss: 0.000103
====> Epoch: 9 Average loss: 0.0001
====> Test set loss: 0.0001
Train Epoch: 10 [0/162770 (0%)] Loss: 0.000092
Train Epoch: 10 [6400/162770 (4%)] Loss: 0.000105
Train Epoch: 10 [12800/162770 (8%)] Loss: 0.000108
Train Epoch: 10 [19200/162770 (12%)] Loss: 0.000103
Train Epoch: 10 [25600/162770 (16%)] Loss: 0.000095
Train Epoch: 10 [32000/162770 (20%)] Loss: 0.000094
Train Epoch: 10 [38400/162770 (24%)] Loss: 0.000085
Train Epoch: 10 [44800/162770 (28%)] Loss: 0.000100

0 comments on commit a942416

Please sign in to comment.