What is CFG Scale Stable Diffusion and How to Use It?
Published: 2024-07-03 09:30:15

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](What is CFG Scale Stable Diffusion and How to Use It?)** (txt2img) and **[image-to-image](What is CFG Scale Stable Diffusion and How to Use It?)** (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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nhg4qiv6xc4s5syy04h7.png)

## 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. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q3moej0q7pcokxk2tgff.png)

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. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pnhsy2v3ov2qayz5p260.png)

## 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](https://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](https://novita.ai/reference/image_generator/text_to_image.html)**", "**[Image to Image](https://novita.ai/reference/image_generator/image_to_image.html)**", and so on.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i69bwwqa5z1cw9m6fzrq.png)
- 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](https://novita.ai/)** and Stable Diffusion 3.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wsko9ijedlp4qaz3sst7.png)
- 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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tasqj5sngnzg1g2lmhk2.png)

### 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](https://novita.ai/)**): 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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2wm3eaoeb2mzth7xihzo.png)

## 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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iai0y0gtcfvwlaalilyj.png)

### 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](https://blogs.novita.ai/enhance-your-generator-stable-diffusion-image-to-image-mastery/)**". You can take your case study on it.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/axyqfwdhcod74qvpjsk2.png)

## 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](https://blogs.novita.ai/what-is-cfg-scale-stable-diffusion-and-how-to-use-it/?utm_source=dev_image&utm_medium=article&utm_campaign=cfg)
> [Novita AI](https://novita.ai/?utm_source=dev_image&utm_medium=article&utm_campaign=what-is-cfg-scale-stable-diffusion-and-how-to-use-it) 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.