Exemple #1
0
    def reparametrize(self, mu, log_var):
        """" Does the reparametrize trick on the beta-VAE:
            $z = mean + eps * sigma$ where 'eps' is sampled from N(0, 1).

        Arguments: 
            - mu: mean of the distribution
            - log_var: log value of the variance

        Returns:
            - output: Reparametrized data (mu + eps * exp(log_var/2))
        
        """
        eps = to_var(torch.randn(mu.size(0), mu.size(1)))
        output = mu + eps * torch.exp(log_var / 2)  # 2 for convert var to std
        return output
 def reparametrize(self, mu, log_var):
     """"z = mean + eps * sigma where eps is sampled from N(0, 1)."""
     eps = to_var(torch.randn(mu.size(0), mu.size(1)))
     output = mu + eps * torch.exp(log_var/2)    # 2 for convert var to std
     return output
SOUND_LENGTH = np.shape(DATASET.__getitem__(9)[0])[0]

DATA_LOADER = torch.utils.data.DataLoader(dataset=DATASET,
                                          batch_size=BATCH_SIZE,
                                          shuffle=True)

#%% Saving original image
# Saving an item from the dataset to debug
DATA_ITER = iter(DATA_LOADER)
FIXED_X, _ = next(DATA_ITER)

FIXED_X = torch.Tensor(FIXED_X.type(torch.FloatTensor))
FIXED_X = FIXED_X.view(BATCH_SIZE, N_FFT, -1).unsqueeze(1)
_, _, HEIGHT, WIDTH = FIXED_X.size()

FIXED_X = to_var(FIXED_X)
torchvision.utils.save_image(FIXED_X.data.cpu(), './data/CNN/real_images.png')

#%% CREATING THE Beta-VAE
reload(CNN_VAE)
betaVAE = CNN_VAE.CNN(height=HEIGHT,
                      width=WIDTH,
                      h_dim=H_DIM,
                      z_dim=Z_DIM,
                      kernel_size=5)

# BETA: Regularisation factor
# 0: Maximum Likelihood
# 1: Bayes solution
BETA = 4
# Data loader
DATA_LOADER = torch.utils.data.DataLoader(dataset=DATASET,
                                          batch_size=100,
                                          shuffle=True)

#%%
""" TRAINING THE VAE MODEL """

ITER_PER_EPOCH = len(DATA_LOADER)
DATA_ITER = iter(DATA_LOADER)

# fixed inputs for debugging
FIXED_X, _ = next(DATA_ITER)
torchvision.utils.save_image(FIXED_X.cpu(), './data/MNIST/real_images.png')
#FIXED_X = to_var(FIXED_X.view(FIXED_X.size(0), -1))
FIXED_X = to_var(torch.Tensor(FIXED_X)).view(FIXED_X.size(0), 28,
                                             -1).unsqueeze(1)

DATA_ITER = enumerate(DATA_LOADER)

#%% CREATING THE Beta-VAE
_, _, HEIGHT, WIDTH = FIXED_X.size()

H_DIM = 400
Z_DIM = 10
CONV1D_OUT = 10
reload(CNN_VAE)
betaVAE = CNN_VAE.CNN(height=28,
                      width=28,
                      h_dim=H_DIM,
                      z_dim=Z_DIM,
                      kernel_size=5)
Exemple #5
0
BETA = 10

# GPU computing if available
if torch.cuda.is_available():
    betaVAE.cuda()
    print('GPU acceleration enabled')

# OPTIMIZER
OPTIMIZER = torch.optim.Adam(betaVAE.parameters(), lr=0.001)

ITER_PER_EPOCH = len(DATA_LOADER)
NB_EPOCH = 2
DATA_ITER = iter(DATA_LOADER)

# fixed inputs for debugging
FIXED_Z = to_var(torch.randn(100, 20))
FIXED_X, _ = next(DATA_ITER)
torchvision.utils.save_image(FIXED_X.cpu(), './data/real_images.png')
FIXED_X = to_var(FIXED_X.view(FIXED_X.size(0), -1))

#%%
""" TRAINING """
for epoch in range(NB_EPOCH):
    for i, (images, _) in enumerate(DATA_LOADER):

        images = to_var(images.view(images.size(0), -1))
        out, mu, log_var = betaVAE(images)

        # Compute reconstruction loss and KL-divergence
        reconst_loss = F.binary_cross_entropy(out, images, size_average=False)
        kl_divergence = torch.sum(0.5 *
DATA_LOADER = torch.utils.data.DataLoader(dataset=DATASET,
                                          batch_size=BATCH_SIZE,
                                          shuffle=True)

#%% Saving original image
FIXED_INDEX = randint(BATCH_SIZE)

# Saving an item from the dataset to debug
DATA_ITER = iter(DATA_LOADER)
FIXED_X, _ = next(DATA_ITER)
FIXED_X = torch.Tensor(FIXED_X.float()).view(FIXED_X.size(0), -1).squeeze()
HEIGHT, WIDTH = FIXED_X.size()

#%% SAVING fixed x as an image
FIXED_X = to_var(FIXED_X)
reconst_images = FIXED_X.view(BATCH_SIZE, 1, N_FFT, -1)
torchvision.utils.save_image(reconst_images.data.cpu(),
                             './data/CQT/original_images.png')

#%% CREATING THE Beta-VAE
betaVAE = modVAE.VAE(image_size=WIDTH, z_dim=Z_DIM, h_dim=H_DIM)

# BETA: Regularisation factor
# 0: Maximum Likelihood
# 1: Bayes solution
BETA = 4

# GPU computing if available
if torch.cuda.is_available():
    betaVAE.cuda()