Sitemap
3 min readJan 4, 2024

--

Variational Autoencoders are one of the most used unsupervised technique which is very much used in the domain of the computer vision. They are very much used in the generation of the images from the lower representation of the input images ,But in this Blog I am going to tell how you can do the image classification task using the VAE on the galaxy10 dataset. Galaxy10 is a dataset contains 25753 69x69 pixels colored galaxy images (g, r and i band) separated in 10 classes. Galaxy10 images come from Sloan Digital Sky Survey and labels come from Galaxy Zoo.

Press enter or click to view image in full size

Lets me introduce the VAE in short.

What is VAE?

In traditional AE, we convert the input images to a low dimensional space which is called context vector , such that we can focus on the more specifically on the relevant parts of the images for the feature extraction unlike traditional CNN models. But in this case ,the latent space is continuous( as the output mean and variance of the probability distribution) ,not discrete like the traditional autoencoders ,as a result we can do the linear interpolation between the latent space features , resulting in the further reduction in the reconstruction loss(separability in the classes)in the model. We also here calculate another loss which is KL-divergence loss(sparsity in the latent space). After that we do sampling in the probability distribution of the encoder output and send it as a input to the decoder and trained the model to minimum reconstruction loss to generate the images.

Press enter or click to view image in full size

Image Classification using VAE

For this I have referred the below research paper http://cs231n.stanford.edu/reports/2017/pdfs/3.pdf.

Press enter or click to view image in full size
The part of the paper.

Basically what it wants to say ,we trained the VAE to the minimum reconstruction loss ,then frozen the weights and extract the encoder part , on the base of the encoder part we use a dense layer followed by a softmax layer to do the task, Now there is a confusion where to add the dense layer on the top of the flatten vector which we get from the encoder or at the top of the sampling layer . Now I have tried both of ways and using the sampling layer I just get 55% accuracy during the training ,but using the second approach I get 79% accuracy on the galaxy10 dataset.

Get Kousik Barnwal’s stories in your inbox

Join Medium for free to get updates from this writer.

Why this happens?

According to me ,the reason would be the Sampling layer in a VAE introduces stochasticity in the latent space due to the sampling of latent variables. This variability might make the learned features less consistent for classification. Also randomness introduces information loss which leads to degradation in the model.

Conclusion

I had used a custom CNN model on this dataset ,where I get 72 % accuracy with 66% validation accuracy , but using VAE ,we get 78% accuracy and 72 % validation accuracy which is the improvement of the previous .We definitely get more better results by adding more dense layer ,doing more hyperparameter tuning, making encoder architecture more complex etc. The github link for the notebook is below : https://github.com/Kou2004/VAE_Classification.git

That’s it in this blog. You can definitely do experiments with the model above to get better results and let me know your results ,you can email me at kousikbarnwal@gmail.com. Keep learning and keep growing !!!

--

--

Kousik Barnwal
Kousik Barnwal

Written by Kousik Barnwal

An analytics ,statistics, product data science enthusiast in Marketing domain.