Skip to content

Ezgii/Variational-Autoencoder

Repository files navigation

Project description

In this project, an Auto Encoder (AE) and a Variational Auto Encoder (VAE) are designed in Python.

Goals of the project

  • Explore the gym framework for training RL agents.
  • Apply my knowledge on VAE to learn image generation.
  • Train generative models to produce sample pixel observation images from gym environments.

Environment

OpenAI's Gym is a framework for training reinforcement learning agents. It provides a set of environments and a standardized interface for interacting with those.
In this project, I used the CartPole environment from gym.

Installation

Using conda (recommended)

  1. Install Anaconda

  2. Create the env
    conda create a1 python=3.8

  3. Activate the env
    conda activate a1

  4. install torch (steps from pytorch installation guide):

  • if you don't have an nvidia gpu or don't want to bother with cuda installation:
    conda install pytorch torchvision torchaudio cpuonly -c pytorch

  • if you have an nvidia gpu and want to use it:
    install cuda
    install torch with cuda:
    conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch

  1. other dependencies
    conda install -c conda-forge matplotlib gym opencv pyglet

Using pip

python3 -m pip install -r requirements.txt

Code

MyVAE.py - my VAE model
train_vae.py - script to collect pixel observations from gym environments using a random policy, and train the VAE model
sample_vae.py - samples from the VAE trained by train_vae.py

How to run the code

On terminal, write:

python3 MyVae.py

python3 train_vae.py

python3 sample_vae.py

Results

Below figure shows two images generated by sampling from the Auto Encoder (AE). It can be seen that sampling from AEs may result in garbage images. This is because AEs have a non-regularized latent space. Their entire latent space does not have a generative capability. Hence, AEs are mainly used for compression.

AE

Variational Autoencoders (VAE), on the other hand, provide the generative capability to the entire latent space. Below figure shows two images generated by sampling from the VAE.

VAE

About

An implementation of VAEs in Python.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages