DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Bridging UI, DevOps, and AI: A Full-Stack Engineer’s Approach to Resilient Systems
  • Build an AI Browser Agent With LLMs, Playwright, Browser Use
  • Working With Vision AI to Test Cloud Applications
  • AI-Powered Flashcard Application With Next.js, Clerk, Firebase, Material UI, and LLaMA 3.1

Trending

  • How to Format Articles for DZone
  • Prioritizing Cloud Security Risks: A Developer's Guide to Tackling Security Debt
  • Strategies for Securing E-Commerce Applications
  • Data Lake vs. Warehouse vs. Lakehouse vs. Mart: Choosing the Right Architecture for Your Business
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Cleaning Up AI-Generated Images With CodeFormer: A Complete Guide

Cleaning Up AI-Generated Images With CodeFormer: A Complete Guide

This article offers a hands-on journey through the world of AI image restoration and clean-up with CodeFormer and Node.js.

By 
mike labs user avatar
mike labs
·
May. 12, 23 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
5.6K Views

Join the DZone community and get the full member experience.

Join For Free

Sometimes, AI-generated photos come out a little bit…wonky. Maybe they’re low quality, or perhaps there are weird artifacts that make the image look less than perfect. But fear not! CodeFormer is here to save the day, helping you fix up those images in no time. In this guide, I’ll introduce you to the CodeFormer model, show you how it works, and explain how to use it to fix up a slightly warped AI-generated photo. I’ll walk you through the exact steps I used to clean up the weird image I got from another AI model, shown below:

This dude looks terrible and scary. Original generation from Arcane-Diffusion.

This image came from the arcane-diffusion model, which I was using for another blog post. I’ll show you how you can use the same workflow I followed to clean up your own generated images and even upscale them to look better. I’ll do this walkthrough using the Replicate Python SDK, but there are also many other languages supported on Replicate.

In this guide, I’ll also show you how we can use Replicate Codex to find similar models and decide which one we like. Replicate Codex is a free community tool that lets you search, filter, sort, and compare AI models and find the right one for your AI project.

CodeFormer

CodeFormer is ranked 6th on Replicate Codex. It’s super popular, with over 8M runs. So, it’s safe to say that the community really likes this tool  —  and that means you might like to use it too!

About CodeFormer

CodeFormer, created by sczhou, is a robust face restoration algorithm designed to work with both old photos and AI-generated faces. It’s an Image-to-Image model that costs $0.0055 per run, with an average run time of 10 seconds on Nvidia T4 GPU hardware.

The underlying technology of CodeFormer is based on a Transformer-based prediction network, which models global composition and context for code prediction. This allows the model to discover natural faces that closely approximate the target faces, even when the inputs are severely degraded. A controllable feature transformation module is also included, which enables a flexible trade-off between fidelity and quality. You can read more about how CodeFormer works on GitHub.

Understanding the Inputs and Outputs of CodeFormer

Before we start working on this project, let’s take a second to understand the inputs and outputs the model expects.

Inputs

  1. image (file): The input image you want to fix.
  2. codeformer_fidelity (number): A balance between quality (lower number) and fidelity (higher number). Default value: 0.5.
  3. background_enhance (boolean): Whether to enhance the background image with Real-ESRGAN. Default value: false.
  4. face_upsample (boolean): Whether to upsample restored faces for high-resolution AI-created images. Default value: false.
  5. upscale (integer): The final upsampling scale of the image. Default value: 2.

Outputs

The output of the model is a URI string representing the fixed image. It’s a JSON object and looks like the format below:

JSON
 
{
  "type": "string",
  "title": "Output",
  "format": "uri"
}


Now that we understand the inputs and outputs, let’s dive into using CodeFormer to fix up a warped AI-generated photo.

A Step-By-Step Guide To Using CodeFormer

If you’re not up for coding, you can interact directly with CodeFormer’s “demo” on Replicate via their UI. You can use this link to interact directly with the interface and try it out! This is a nice way to play with the model’s parameters and get some quick feedback and validation.

If you’re more technical and looking to eventually build a cool tool on top of CodeFormer, you can follow these simple steps to restore and enhance your images using the model on Replicate.

Make sure you have a Replicate account and your API key handy!

Step 1: Install the Python Client

This is pretty easy to do:

pip install replicate

Step 2: Set Up Your API Token

You can get this from Replicate in your account tab.

export REPLICATE_API_TOKEN=[token]

Step 3: Run the Model

You just need a few lines of code to do this with the Replicate Python SDK.

Python
 
import replicate
output = replicate.run(
    "sczhou/codeformer:7de2ea26c616d5bf2245ad0d5e24f0ff9a6204578a5c876db53142edd9d2cd56",
    input={"image": open("path/to/file", "rb")}
)
print(output)


In this step, we import the replicate library and call the run function to execute the CodeFormer model with the provided input image. The output will be printed as a URI string representing the fixed image.

Here’s the output image I got from this process, shown against the hideous input I started with. What an improvement!

Wow, this looks miles better. 

Taking It Further — Finding Other Image Restoration Models With Replicate Codex

Replicate Codex is a fantastic resource for discovering AI models that cater to various creative needs, including image generation, image-to-image conversion, and much more. It’s a fully searchable, filterable, tagged database of all the models on Replicate, and also allows you to compare models and sort by price or explore by the creator. It’s free, and it also has a digest email that will alert you when new models come out so you can try them.

If you’re interested in finding similar models to CodeFormer…

Step 1: Visit Replicate Codex

Head over to Replicate Codex to begin your search for similar models.

Step 2: Use the Search Bar

Use the search bar at the top of the page to search for models with specific keywords, such as “image restoration,” “face enhancement,” or “super-resolution.” This will show you a list of models related to your search query.

Step 3: Filter the Results

On the right side of the search results page, you’ll find several filters that can help you narrow down the list of models. You can filter and sort models by type (Image-to-Image, Text-to-Image, etc.), cost, popularity, or even specific creators.filter and sort

Finding the most popular restoration models on Replicate Codex.

By applying these filters, you can find the models that best suit your specific needs and preferences. For example, if you’re looking for an image restoration model that’s the most popular, you can just search and then sort by the number of runs. In that case, you’ll find the GFPGAN model, which I’ve also used to restore old photos — see my writeup here for more information. I’ve included an example restoration from GFPGAN below so you can get a sense of how it handles images too.

Input/Output

An example GFPGAN output  —  I find it works better on old photos.

Conclusion

In this guide, we explored the CodeFormer model, learned about its inputs and outputs, and demonstrated how to use it to fix warped AI-generated photos. We also discussed how to leverage the search and filter features in Replicate Codex to find similar models and compare their outputs, allowing us to broaden our horizons in the world of AI-powered image enhancement and restoration.

I hope this guide has inspired you to explore the creative possibilities of AI and bring your imagination to life. Thanks for reading. Happy image enhancing!

AI Image conversion UI

Published at DZone with permission of mike labs. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Bridging UI, DevOps, and AI: A Full-Stack Engineer’s Approach to Resilient Systems
  • Build an AI Browser Agent With LLMs, Playwright, Browser Use
  • Working With Vision AI to Test Cloud Applications
  • AI-Powered Flashcard Application With Next.js, Clerk, Firebase, Material UI, and LLaMA 3.1

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!