Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Deep Learning Tutorial
  • Data Analysis Tutorial
  • Python – Data visualization tutorial
  • NumPy
  • Pandas
  • OpenCV
  • R
  • Machine Learning Tutorial
  • Machine Learning Projects
  • Machine Learning Interview Questions
  • Machine Learning Mathematics
  • Deep Learning Project
  • Deep Learning Interview Questions
  • Computer Vision Tutorial
  • Computer Vision Projects
  • NLP
  • NLP Project
  • NLP Interview Questions
  • Statistics with Python
  • 100 Days of Machine Learning
Open In App
Next Article:
Implement Deep Autoencoder in PyTorch for Image Reconstruction
Next article icon

Implement Convolutional Autoencoder in PyTorch with CUDA

Last Updated : 24 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Autoencoders are a type of neural network architecture used for unsupervised learning tasks such as data compression, dimensionality reduction, and data denoising. The architecture consists of two main components: an encoder and a decoder. The encoder portion of the network compresses the input data into a lower-dimensional representation, while the decoder portion of the network reconstructs the original input data from this lower-dimensional representation.

A Convolutional Autoencoder (CAE) is an autoencoder a type of deep learning neural network architecture that is commonly used for unsupervised learning tasks, such as image compression and denoising. It is an extension of the traditional autoencoder architecture that incorporates convolutional layers into both the encoder and decoder portions of the network.

Same like the Autoencoder, the Convolutional Autoencoder architecture also consists of two main components: an encoder and a decoder. The encoder portion of the network processes the input image using convolutional layers and pooling operations to produce a lower-dimensional feature representation of the image. The decoder portion of the network takes this lower-dimensional feature representation and upsamples it back to the original input image size using deconvolutional layers. The final output of the network is a reconstructed image that is as close as possible to the original input image.

The training process for a Convolutional Autoencoder is similar to that of a traditional autoencoder. The network is trained to minimize the difference between the original input image and the reconstructed output image using a loss function such as mean squared error (MSE) or binary cross-entropy (BCE). Once trained, the encoder portion of the network can be used for feature extraction, and the decoder portion of the network can be used for image generation or reconstruction.

Convolutional Autoencoders have shown impressive results in a variety of computer vision tasks, including image compression, denoising, and feature extraction. They have also been used in various applications such as image retrieval, object recognition, and anomaly detection.

Implementation in Pytorch:

Algorithm

  1.  Load the dataset using PyTorch's ImageFolder class and define a dataloader.
  2.  Define the Convolutional Autoencoder architecture by creating an Autoencoder class that contains an encoder and decoder, each with convolutional and pooling layers.
  3. Initialize the autoencoder model and move it to the GPU if available using the to() method.
  4. Define the loss function and optimizer to use during training. Typically, mean squared error (MSE) loss is used, and the Adam optimizer is a popular choice for deep learning tasks.
  5. Set the number of epochs to train for and begin the training loop.
  6. In each epoch, iterate through the batches of the dataloader, move the data to the GPU, and perform forward propagation to obtain the autoencoder's output.
  7. Calculate the loss between the output and the input using the loss function.
  8. Perform backward propagation to calculate the gradients of the model parameters with respect to the loss.\
  9.  Use the optimizer to update the model parameters based on the calculated gradients.
  10. Print the loss after each epoch to monitor the training progress.
  11. Save the trained model to a file using the state_dict() method.

Code:

Python
import torch import torch.nn as nn import torch.optim as optim import torchvision.datasets as datasets import torchvision.transforms as transforms  # Define the autoencoder architecture class Autoencoder(nn.Module):     def __init__(self):         super(Autoencoder, self).__init__()         self.encoder = nn.Sequential(             nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1),             nn.ReLU(),             nn.MaxPool2d(kernel_size=2, stride=2),             nn.Conv2d(16, 8, kernel_size=3, stride=1, padding=1),             nn.ReLU(),             nn.MaxPool2d(kernel_size=2, stride=2)         )         self.decoder = nn.Sequential(             nn.ConvTranspose2d(8, 16,                                 kernel_size=3,                                 stride=2,                                 padding=1,                                 output_padding=1),             nn.ReLU(),             nn.ConvTranspose2d(16, 3,                                 kernel_size=3,                                 stride=2,                                 padding=1,                                 output_padding=1),             nn.Sigmoid()         )              def forward(self, x):         x = self.encoder(x)         x = self.decoder(x)         return x   # Initialize the autoencoder model = Autoencoder()  # Define transform transform = transforms.Compose([     transforms.Resize((64, 64)),     transforms.ToTensor(), ])  # Load dataset train_dataset = datasets.Flowers102(root='flowers',                                      split='train',                                      transform=transform,                                      download=True) test_dataset = datasets.Flowers102(root='flowers',                                     split='test',                                     transform=transform) # Define the dataloader train_loader = torch.utils.data.DataLoader(dataset=train_dataset,                                             batch_size=128,                                             shuffle=True) test_loader = torch.utils.data.DataLoader(dataset=test_dataset,                                            batch_size=128)  # Move the model to GPU device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') print(device) model.to(device)  # Define the loss function and optimizer criterion = nn.MSELoss() optimizer = optim.Adam(model.parameters(), lr=0.001)  # Train the autoencoder num_epochs = 50 for epoch in range(num_epochs):     for data in train_loader:         img, _ = data         img = img.to(device)         optimizer.zero_grad()         output = model(img)         loss = criterion(output, img)         loss.backward()         optimizer.step()     if epoch % 5== 0:         print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, loss.item()))  # Save the model torch.save(model.state_dict(), 'conv_autoencoder.pth') 

Output:

cuda Epoch [1/50], Loss: 0.0919 Epoch [6/50], Loss: 0.0746 Epoch [11/50], Loss: 0.0362 Epoch [16/50], Loss: 0.0239 Epoch [21/50], Loss: 0.0178 Epoch [26/50], Loss: 0.0154 Epoch [31/50], Loss: 0.0144 Epoch [36/50], Loss: 0.0124 Epoch [41/50], Loss: 0.0127 Epoch [46/50], Loss: 0.0101

Plot the original image with decoded image

Python3
with torch.no_grad():     for data, _ in test_loader:         data = data.to(device)         recon = model(data)         break          import matplotlib.pyplot as plt plt.figure(dpi=250) fig, ax = plt.subplots(2, 7, figsize=(15, 4)) for i in range(7):     ax[0, i].imshow(data[i].cpu().numpy().transpose((1, 2, 0)))     ax[1, i].imshow(recon[i].cpu().numpy().transpose((1, 2, 0)))     ax[0, i].axis('OFF')     ax[1, i].axis('OFF') plt.show() 

Output:

Convolutional Autoencoder decoded image with original image -Geeksforgeeks
Convolutional Autoencoder decoded image with original image

Next Article
Implement Deep Autoencoder in PyTorch for Image Reconstruction

S

sagarseth06
Improve
Article Tags :
  • Machine Learning
  • AI-ML-DS
  • Python-PyTorch
  • python
  • Deep-Learning
Practice Tags :
  • Machine Learning
  • python

Similar Reads

  • Overcomplete Autoencoders with PyTorch
    Neural networks are used in autoencoders to encode and decode data. They are utilized in many different applications, including data compression, natural language processing, and picture and audio recognition. Autoencoders work by learning a compressed representation of the input data that may be us
    7 min read
  • Implementing an Autoencoder in PyTorch
    Autoencoders are neural networks that learn to compress and reconstruct data. In this guide we’ll walk you through building a simple autoencoder in PyTorch using the MNIST dataset. This approach is useful for image compression, denoising and feature extraction. Implementation of Autoencoder in PyTor
    4 min read
  • Convolutional Variational Autoencoder in Tensorflow
    In the age of Generative AI, the creation of generative models is very crucial for learning and synthesizing complex data distributions within the dataset. By incorporating convolutional layers with Variational Autoencoders, we can create a such kind of generative model. In this article, we will dis
    10 min read
  • Implement Deep Autoencoder in PyTorch for Image Reconstruction
    Since the availability of staggering amounts of data on the internet, researchers and scientists from industry and academia keep trying to develop more efficient and reliable data transfer modes than the current state-of-the-art methods. Autoencoders are one of the key elements found in recent times
    8 min read
  • Deep Learning with PyTorch | An Introduction
    PyTorch in a lot of ways behaves like the arrays we love from Numpy. These Numpy arrays, after all, are just tensors. PyTorch takes these tensors and makes it simple to move them to GPUs for the faster processing needed when training neural networks. It also provides a module that automatically calc
    7 min read
  • Building a Convolutional Neural Network using PyTorch
    Convolutional Neural Networks (CNNs) are deep learning models used for image processing tasks. They automatically learn spatial hierarchies of features from images through convolutional, pooling and fully connected layers. In this article we'll learn how to build a CNN model using PyTorch. This incl
    6 min read
  • How to Define a Simple Convolutional Neural Network in PyTorch?
    In this article, we are going to see how to  Define a Simple Convolutional Neural Network in PyTorch using Python. Convolutional Neural Networks(CNN) is a type of Deep Learning algorithm which is highly instrumental in learning patterns and features in images. CNN has a unique trait which is its abi
    5 min read
  • Apply a 2D Convolution Operation in PyTorch
    A 2D Convolution operation is a widely used operation in computer vision and deep learning. It is a mathematical operation that applies a filter to an image, producing a filtered output (also called a feature map). In this article, we will look at how to apply a 2D Convolution operation in PyTorch.
    8 min read
  • Extending PyTorch with Custom Activation Functions
    In the context of deep learning and neural networks, activation functions are mathematical functions that are applied to the output of a neuron or a set of neurons. The output of the activation function is then passed on as input to the next layer of neurons. The purpose of an activation function is
    7 min read
  • Computing the Mean and Std of a Dataset in Pytorch
    PyTorch provides various inbuilt mathematical utilities to monitor the descriptive statistics of a dataset at hand one of them being mean and standard deviation. Mean, denoted by, is one of the Measures of central tendencies which is calculated by finding the average of the given dataset. Standard D
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences