Skip to content

Commit

Permalink
Merge in HW 3
Browse files Browse the repository at this point in the history
  • Loading branch information
wilson1yan authored Feb 27, 2020
2 parents f1079a6 + 8a778bd commit 0b0e7b9
Show file tree
Hide file tree
Showing 50 changed files with 1,760 additions and 5 deletions.
106 changes: 102 additions & 4 deletions deepul/hw3_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,108 @@ def q1_save_results(part, dset_id, fn):
fname=f'results/q1_{part}_dset{dset_id}_sample_without_noise.png')


def visualize_colored_shapes():
data_dir = get_data_dir(3)
train_data, test_data = load_pickled_data(join(data_dir, 'shapes_colored.pkl'))
idxs = np.random.choice(len(train_data), replace=False, size=(100,))
images = train_data[idxs]
show_samples(images, title='Colored Shapes Samples')


def visualize_svhn():
data_dir = get_data_dir(3)
train_data, test_data = load_pickled_data(join(data_dir, 'svhn.pkl'))
idxs = np.random.choice(len(train_data), replace=False, size=(100,))
images = train_data[idxs]
show_samples(images, title='SVHN Samples')


def visualize_cifar10():
data_dir = get_data_dir(3)
train_data, test_data = load_pickled_data(join(data_dir, 'cifar10.pkl'))
idxs = np.random.choice(len(train_data), replace=False, size=(100,))
images = train_data[idxs]
show_samples(images, title='CIFAR10 Samples')


def q2_save_results(part, dset_id, fn):
assert part in ['a', 'b'] and dset_id in [1, 2]
data_dir = get_data_dir(3)
if dset_id == 1:
train_data, test_data = load_pickled_data(join(data_dir, 'svhn.pkl'))
else:
train_data, test_data = load_pickled_data(join(data_dir, 'cifar10.pkl'))

train_losses, test_losses, samples, reconstructions, interpolations = fn(train_data, test_data, dset_id)
samples, reconstructions, interpolations = samples.astype('float32'), reconstructions.astype('float32'), interpolations.astype('float32')
print(f'Final -ELBO: {test_losses[-1, 0]:.4f}, Recon Loss: {test_losses[-1, 1]:.4f}, '
f'KL Loss: {test_losses[-1, 2]:.4f}')
plot_vae_training_plot(train_losses, test_losses, f'Q2({part}) Dataset {dset_id} Train Plot',
f'results/q2_{part}_dset{dset_id}_train_plot.png')
show_samples(samples, title=f'Q2({part}) Dataset {dset_id} Samples',
fname=f'results/q2_{part}_dset{dset_id}_samples.png')
show_samples(reconstructions, title=f'Q2({part}) Dataset {dset_id} Reconstructions',
fname=f'results/q2_{part}_dset{dset_id}_reconstructions.png')
show_samples(interpolations, title=f'Q2({part}) Dataset {dset_id} Interpolations',
fname=f'results/q2_{part}_dset{dset_id}_interpolations.png')






def q3_save_results(dset_id, fn):
assert dset_id in [1, 2]
data_dir = get_data_dir(3)
if dset_id == 1:
train_data, test_data = load_pickled_data(join(data_dir, 'svhn.pkl'))
else:
train_data, test_data = load_pickled_data(join(data_dir, 'cifar10.pkl'))

vqvae_train_losses, vqvae_test_losses, pixelcnn_train_losses, pixelcnn_test_losses, samples, reconstructions = fn(train_data, test_data, dset_id)
samples, reconstructions = samples.astype('float32'), reconstructions.astype('float32')
print(f'VQ-VAE Final Test Loss: {vqvae_test_losses[-1]:.4f}')
print(f'PixelCNN Prior Final Test Loss: {pixelcnn_test_losses[-1]:.4f}')
save_training_plot(vqvae_train_losses, vqvae_test_losses,f'Q3 Dataset {dset_id} VQ-VAE Train Plot',
f'results/q3_dset{dset_id}_vqvae_train_plot.png')
save_training_plot(pixelcnn_train_losses, pixelcnn_test_losses,f'Q3 Dataset {dset_id} PixelCNN Prior Train Plot',
f'results/q3_dset{dset_id}_pixelcnn_train_plot.png')
show_samples(samples, title=f'Q3 Dataset {dset_id} Samples',
fname=f'results/q3_dset{dset_id}_samples.png')
show_samples(reconstructions, title=f'Q3 Dataset {dset_id} Reconstructions',
fname=f'results/q3_dset{dset_id}_reconstructions.png')


def q4_a_save_results(dset_id, fn):
assert dset_id in [1, 2]
data_dir = get_data_dir(3)
if dset_id == 1:
train_data, test_data = load_pickled_data(join(data_dir, 'svhn.pkl'))
else:
train_data, test_data = load_pickled_data(join(data_dir, 'cifar10.pkl'))

vqvae_train_losses, vqvae_test_losses, pixelcnn_train_losses, pixelcnn_test_losses, samples, reconstructions = fn(train_data, test_data, dset_id)
samples, reconstructions = samples.astype('float32'), reconstructions.astype('float32')
print(f'VQ-VAE Final Test Loss: {vqvae_test_losses[-1]:.4f}')
print(f'PixelCNN Prior Final Test Loss: {pixelcnn_test_losses[-1]:.4f}')
save_training_plot(vqvae_train_losses, vqvae_test_losses,f'Q4(a) Dataset {dset_id} VQ-VAE Train Plot',
f'results/q4_a_dset{dset_id}_vqvae_train_plot.png')
save_training_plot(pixelcnn_train_losses, pixelcnn_test_losses,f'Q4(a) Dataset {dset_id} PixelCNN Prior Train Plot',
f'results/q4_a_dset{dset_id}_pixelcnn_train_plot.png')
show_samples(samples, title=f'Q4(a) Dataset {dset_id} Samples',
fname=f'results/q4_a_dset{dset_id}_samples.png')
show_samples(reconstructions, title=f'Q4(a) Dataset {dset_id} Reconstructions',
fname=f'results/q4_a_dset{dset_id}_reconstructions.png')


def q4_b_save_results(fn):
part = 'b'
data_dir = get_data_dir(3)
train_data, test_data = load_pickled_data(join(data_dir, 'mnist.pkl'))

train_losses, test_losses, samples, reconstructions = fn(train_data, test_data)
samples, reconstructions = samples.astype('float32') * 255, reconstructions.astype('float32') * 255
print(f'Final -ELBO: {test_losses[-1, 0]:.4f}, Recon Loss: {test_losses[-1, 1]:.4f}, '
f'KL Loss: {test_losses[-1, 2]:.4f}')
plot_vae_training_plot(train_losses, test_losses, f'Q4({part}) Train Plot',
f'results/q4_{part}_train_plot.png')
show_samples(samples, title=f'Q4({part}) Samples',
fname=f'results/q4_{part}_samples.png')
show_samples(reconstructions, title=f'Q4({part}) Reconstructions',
fname=f'results/q4_{part}_reconstructions.png')
7 changes: 6 additions & 1 deletion deepul/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,9 @@ def load_pickled_data(fname, include_labels=False):


def get_data_dir(hw_number):
return join('deepul', 'homeworks', f'hw{hw_number}', 'data')
return join('deepul', 'homeworks', f'hw{hw_number}', 'data')


def quantize(images, n_bits):
images = np.floor(images / 256. * 2 ** n_bits)
return images.astype('uint8')
15 changes: 15 additions & 0 deletions homeworks/hw3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Homework 3: Latent Variable Models

**Due March 10, 11:59pm on Gradescope**

In this homework, you will implement and train a variety of different latent variable models models. The homework consists of two components:
* `hw3_notebook.ipynb` : Contains all of the coding questions, and will automatically generate and display results for you after completing each question.
You will submit the notebook to Gradescope after completing the homework.
Open it on Colab by clicking on the file, and then "Open in Colab" at the top.
**Submit a PDF version of the notebook to the code (Print -> Preview -> Save) on Gradescope in the assignment with (code)**
* `hw3_latex` : Contains LaTeX files and figures needed to generate your PDF submission to Gradescope. Copy the images saved from the notebook into the `figures/` folder and fill out the test losses.
**Submit the Latex PDF in the assignment with (PDF)**

You can open the notebook in Google Colab to get access to a free GPU, or you can link Colab to a local runtime to run it on your own GPU.

**Note that for this homework, the data files are not in the git repo. If you would like to download them, you can use this [link](https://drive.google.com/open?id=1lWjGICwgzgcBDejo9S5g69hLAf0O3lGF)
Binary file added homeworks/hw3/hw3_latex/figures/placeholder.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added homeworks/hw3/hw3_latex/figures/q4_b_samples.png
Binary file added homeworks/hw3/hw3_latex/hw3_latex_template.pdf
Binary file not shown.
Loading

0 comments on commit 0b0e7b9

Please sign in to comment.