What is CFG Scale Stable Diffusion and How to Use It?

What is CFG Scale Stable Diffusion and How to Use It?

·

6 min read

Understanding the CFG scale in Stable Diffusion. Learning how to use it to enhance image quality in our blog.

Introduction

The CFG scale, also known as the Classifier Free Guidance scale, plays a crucial role in controlling the adherence of Stable Diffusion to your text prompt, which can be used in both text-to-image (txt2img) and image-to-image (img2img) generations.

In this blog, we’ll give you a comprehensive introduction to the CFG scale in Stable Diffusion, including its relation with Stable Diffusion and the technology behind it. Moreover, we’ll show you a detailed guide on how to use it in Stable Diffusion and how to avoid common mistakes. Let’s dive into the world of the CFG scale now!

Understanding CFG Scale in Stable Diffusion

In Stable Diffusion, the acronym CFG represents the “Classifier Free Guidance” scale, which plays a crucial role in determining the quality of the output images.

Evolution of the CFG (Classifier Free Guidance)

Initially, diffusion models used an explicit classifier to guide the generation process, involving training a classifier on noisy images to categorize and guide the generation of specific classes, such as cats or dogs. However, this required an extra model. So comes Classifier-Free Guidance, using image captions to train a conditional diffusion model.

What is the CFG Scale?

The CFG scale, or Configuration scale, is a parameter that controls the intensity of the diffusion process. It determines how much the pixel values are spread or dispersed, that’s to say, it determines the extent to which Stable Diffusion follows your prompt.

How Does the CFG Scale Work in Stable Diffusion?

By default, the CFG scale value is set to 7, striking a balance between creative freedom and prompt guidance.

Relation Between CFG Scale and Stable Diffusion

Stable diffusion is a concept in the field of image processing and computer graphics that refers to the process of spreading or dispersing pixel values across an image. This technique is often used to create a variety of effects, such as blurring, sharpening, and edge detection. The process is governed by a set of parameters, one of which is the CFG scale.

How Does the CFG Scale Affect Image Quality?

The CFG Scale determines the coefficient applied to the prompt words in the diffusion process. A lower CFG scale value can preserve more details but might not achieve the desired diffusion effect. On the other hand, a higher CFG scale value can create a strong diffusion effect but might result in the loss of image details. Therefore, finding the right balance is key to achieving high-quality output images.

Adjusting the CFG scale in stable diffusion depends on the desired outcome. If the goal is to create a subtle diffusion effect, a lower CFG scale value would be appropriate. Conversely, if the aim is to create a strong diffusion effect, a higher CFG scale value would be needed.

While using the Stable Diffusion Web UI, CFG is limited to positive numbers ranging from 1 to 30. However, when utilizing Stable Diffusion via a Terminal, CFG can be set as high as 999 and can even take negative values which indicates the desire for Stable Diffusion to generate content opposite to your text prompt.

How to Use the CFG Scale in Stable Diffusion?

To learn how to use the CFG scale in Stable Diffusion, you should have the Stable Diffusion model in your project. In this section, we’ll teach you how to use it step by step from integrating stable Diffusion into your program.

Step-by-Step Guide

The benefit of getting Stable Diffusion by Integrating API rather than downloading it is that you are able to train and make some adjustments to the models according to your needs.

  • Step 1: Open the Novita AI website and create an account on it.

  • Step 2: Navigate to the “API” and find the one you want. Novita AI features various APIs like “Text to Image”, “Image to Image”, and so on.

  • Step 3: Get the API key and integrate it into your project.

  • Step 4: Turn to your Stable Diffusion interface.

  • Step 5: Select a Stable Diffusion model you want from the list and enter the prompts of your image. Novita AI provides many models including Stable Diffusion XL and Stable Diffusion 3.

  • Step 6: Adjust the CFG scale value and generate the image.

  • Step 7: Experiment with different CFG scale values to uncover the specific one that brings out the most impressive result.

Hardware Considerations About Using the CFG Scale

The performance and outcome of Stable Diffusion can be influenced by the hardware used.

  • Graphics Processing Unit (GPU): A powerful GPU is essential for running Stable Diffusion efficiently. The model leverages the GPU for the computationally intensive tasks involved in image generation.

  • Random Access Memory (RAM): Adequate system RAM is important for overall system responsiveness and the ability to handle large datasets. A minimum of 16GB RAM is recommended, with 32GB for more demanding tasks.

  • Operating System: Stable Diffusion is compatible with various operating systems, including Windows, macOS, and Linux. However, the specific version and updates may affect compatibility and performance.

Use Cases of the CFG Scale for Stable Diffusion

The CFG Scale in Stable Diffusion allows users to fine-tune the image generation process according to their needs.

Optimizing Image Quality

Users can adjust the CFG Scale to optimize image quality. A value of 7 is often recommended, as it provides a good balance between realism and fidelity to the input prompt.

Negative Prompts

The CFG Scale can be used in conjunction with negative prompts, which can help create images that exclude certain elements while still adhering to the main text prompt.

Case Study

By adjusting the CFG scale value in the case study, we can observe how different levels of guidance affect the generated images, further understanding the importance of the CFG scale’s role in achieving high-fidelity output images.

Additionally, Novita AI also provides a playground of “image-to-image”. You can take your case study on it.

Conclusion

In conclusion, the CFG scale is a vital parameter in stable diffusion that controls the intensity of the diffusion process. Understanding how to adjust the CFG scale based on the desired outcome and the quality of the original image can significantly improve the results of stable diffusion. As with many things in image processing and computer graphics, finding the optimal CFG scale value often involves a process of trial and error and depends on the specific requirements of each project.

Originally published at Novita AI

Novita AI is the all-in-one cloud platform that empowers your AI ambitions. With seamlessly integrated APIs, serverless computing, and GPU acceleration, we provide the cost-effective tools you need to rapidly build and scale your AI-driven business. Eliminate infrastructure headaches and get started for free — Novita AI makes your AI dreams a reality.