How to Use Generative Compression for Image Compression with TensorFlow

Apr 7, 2024 | Data Science

This article will guide you through the process of implementing learned image compression using Generative Adversarial Networks (GANs) as described by Agustsson et al. in their paper, Generative Adversarial Networks for Extreme Learned Image Compression. It offers a method to compress images while retaining perceptual quality. Let’s break it down!

Set Up Your Environment

Before diving into the code, you need to ensure your environment is set up properly. The code depends on TensorFlow, specifically version 1.8 or higher. Here’s how to get started:

  • Clone the Repository:
  • bash
    # Clone the repository
    $ git clone https://github.com/Justin-Tang/generative-compression.git
    $ cd generative-compression
    
  • Check Command Line Arguments:
  • bash
    # To train the model
    $ python3 train.py -h
    

Running the Model

Once you’re set up, you can proceed to train the model:

bash
# Run the training
$ python3 train.py -opt momentum --name my_network

During training, a batch size of 1 is used and tensorboard summaries will be automatically written periodically. Checkpoints are saved every 10 epochs.

Compressing an Image

To compress a single image, execute the following command:

bash
# Compress a single image
$ python3 compress.py -r path_to_model_checkpoint -i path_to_image -o path_to_output_image

The output will be a side-by-side comparison with the original image under the specified path defined in directories.samples located in config.py.

Understanding the Code

To put the code into perspective, imagine you are an artist who has been asked to recreate a famous painting, but with a limited palette. Each time you step back, you evaluate your work and make adjustments. In this case, the painting represents an image and the limited palette corresponds to the compression channels.

The neural network acts like you, learning from the similarities and differences between the original and the compressed image, constantly adjusting (training) until it can produce a beautiful recreation with far fewer colors. This is why GANs are so powerful—they not only learn to compress but also ensure that the end result retains visual integrity, even if some detail is lost.

Results

The results are visually striking! The model seeks to maintain perceptual quality even at higher levels of compression. You can find representations of globally compressed images in the Cityscapes dataset showing how the model interprets greenery and structures with remarkable adaptiveness.

Troubleshooting Common Issues

If you encounter issues, here are some troubleshooting steps:

  • Error loading models: Ensure you are using TensorFlow 1.8 or later. Issues may arise if the model was originally trained on a different version.
  • Model training not starting: Recheck the command you used to train. Ensure correct path specifications and permissions are set for the directory.
  • Image compression doesn’t look right: Ensure that the hyperparameters in config.py match those used during training. Pay attention to the noise sampling settings if applicable.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox