
Stable Cascade is a text-to-image generation model developed by Stability AI based on the Würstchen architecture. The model uses a three-stage architecture in the A, B, C format for greater efficiency and image generation quality. As a result, all known model extensions such as finetuning, LoRA, ControlNet, IP-Adapter, and LCM are possible with Stable Cascade making it an efficient model you can adopt in multiple projects.
This guide explains how to achieve AI-generated images with the Stable Cascade model on a Vultr Cloud GPU server. You will install the required packages to create a development environment to run the Stable Cascade model. Then, generate images with Text-to-Image, Image-to-Image, and Image Variation methods in addition to exploring the model's performance benefits and limitations.
Before you begin:
80 GB VRAM.Stable Cascade consists of three models divided into stages that include Stage A, Stage B, and Stage C. Stage A and B support image compression similar to the role of the VAE (Variational Autoencoder) in Stable Diffusion while Stage C focuses on the generated resolution as described in the following sequence operations:
To implement the Stable Cascade model, access the Jupyter Lab interface, open a new terminal session, and clone the Stable Cascade model repository to your server. Then, download the model checkpoints in a Safetensors format, and install the required Python packages as described in the following steps.
Open a new JupyterLab terminal session.
Switch to the Jupyter user home directory.
Create a new directory to store the model generated images.
Clone the Stable Cascade repository using Git.
Switch to the new Stable Cascade directory.
Install all necessary dependencies using the requirements.txt file.
When successful, switch to the models directory.
Execute the download_models.sh file with the target dependency models to install on your server.
Switch back to the main Stable Cascade directory.
Print the working directory path to use in your model configurations.
Output:
To generate images with the Stable Cascade model, create a new Jupyter Notebook file to use as the implementation environment. Then, import the required modules and load the model configuration files to activate as described in the steps below.
Create a new Jupyter Notebook Python3 Kernel file and set its name to Stable Cascade Text-to-Image.
Import the required packages in a new code cell. Replace /home/jupyter/StableCascade/ with your actual model directory.
Below are the tasks performed by each of the imported packages:
utils module in the inference package.load_or_fail function from the utils module in the core package.WurstCoreC and WurstCoreB classes from the train module.Press Shift + Enter to run the code cell and import all packages.
Verify the system GPU availability status.
Output:
The above code queries the NVidia GPU availability status with the torch.cuda.is_available() function and displays the result with a print function. The output value 0 means a GPU device is available and ready to use while any other value switches the default device to CPU.
Load the Stage C model configuration file.
Within the above code, config_file stores the stage_c_3b.yaml stage path that contains settings and parameters for the Stage C model.
Load the Stage B model configuration file.
Within the above code, the config_file_b variable stores the stage_b_3b.yaml configuration path that contains settings and parameters for the Stage B model.
Load the model extras to set up Stage C.
The above code prepares the Stage C model with the following values:
setup_extras_pre(): Calls the core function in the WurstCoreC package.extras: Contains setup details.models: Contains the initialized generator, discriminator, and tokenizer.models.generator.eval(): Sets the generator to evaluation mode while .requires_grad_(False) disables gradient computation for its parameters, saving computational resources.Load the model extras to set up Stage B.
The above code prepares the Stage B model similar to Stage C with the following functions:
WurstCoreB.Models: Creates a new instance with the model details including models and models_b.models_b.generator.bfloat16(): Converts the generator parameters to 16-bit floating-point precision..eval().requires_grad_(False): Sets the model to evaluation mode, preventing gradient computation during inference.Create a batch size and a text prompt to describe the image generation process. Replace Anthropomorphic cat dressed as a pilot with your desired text prompt.
In the above code, the batch_size variable has a value of 4. This means that the code will generate images in 4 batches using the caption variable that includes a text prompt Anthropomorphic cat dressed as a pilot for generating the image.
Define the target image height and width.
The above code sets the height and width variables to 1024 pixels to set the generated image dimensions. The calculate_latent_sizes function computes latent sizes for Stage C and Stage B based on the specified height, width, and batch size.
Define the Stage C and Stage B parameters.
The above model parameters control the sampling process when generating an image.
Add the model conditions.
The above code prepares conditions and unconditional inputs for both Stage C and Stage B that are required when generating images conditioned to a specific caption.
Set TOKENIZERS_PARALLELISM environment variable to false to disable parallelism while generating the image.
Start the image generation process.
The above code generates images with two stages Stage C and Stage B using the specified models, conditions, and sampling configurations. The torch.cuda.amp.autocast performs automatic mixed-precision tasks to speed up the model computations.
View the generated images.
Based on the input text prompt Anthropomorphic cat dressed as a pilot, the model generates a photo-realistic image with the set resolution. For each text prompt, the model generates a different image.
Stable Cascade image-to-image generation works similar to the text-to-image model process with a three-stage architecture. Instead of a text prompt, image-to-image uses an input image and the involves adding noise to a specific point. Follow the steps below to generate images using the image-to-image procedure, add noise and start the model image generation process.
Click Kernel on the top navigation bar and select Shutdown Down All Kernels from the list of options to clear the GPU memory.
In a new code cell, define the batch size and the input image URL to use with the model. Replace https://imagizer.imageshack.com/img922/1920/iquupP.png with your desired input image URL.
Enter a text prompt to define the generated image with the target width and height to set the image dimensions. Replace a person riding a rodent and 1023 with your desired prompt, and target image dimensions respectively.
Define the Stage C and Stage B parameters.
Prepare the image generation conditions.
Set the TOKENIZERS_PARALLELISM environment variable to false to disable parallelism.
Generate the images based on your input image and prompt.
View the generated images.
Based on the input image, dimensions, and the text prompt a person riding a rodent. The model outputs a final image that closely matches your input values.
Image variation enables the Stable Cascade model to comprehend image embeddings and generate variations without a base prompt. Similar to the image-to-image generation process, image variation uses an input image but does not require a prompt to generate a final image as described in the steps below.
Click the Kernel menu option and select Shutdown Down All Kernels to clear the system GPU memory to run a new model process.
In a new code cell, define your target batch size and input image URL. Replace https://imagizer.imageshack.com/img923/8748/1Lo6Ii.png with your desired image URL to use with the model.
Define the target width and height to set the generated image dimensions.
Add the model Stage C parameters.
Prepare the model image generation conditions.
Disable parallelism.
Generate the images.
View the generated images.
Based on your input image, the Stable Cascade image variation model process outputs a generated image that matches your condition specifications.
All Stable Cascade image generation processes generate final images but do not save the result. Follow the steps below to export and save generated images to your data directory.
Import the required model packages to save generated images.
Set your target server directory to save the generated images. Replace /home/jupyter/notebooks/generated_images with your desired directory path.
Declare a new condition to create the directory if it does not exist on the server.
Create a new loop to scan each generated image in the batch and save it as a standalone file.
Within the above code, for i in range starts a new loop that iterates through each of the generated images. For each image:
current_image = sampled[i].vutils.make_grid function to convert the image tensor to a grid format suitable for visualization.index + 1 to avoid starting new image files from zero and ensure uniqueness for each filename.To download the exported Stable Cascade images to your local machine, use a file transfer protocol such as SFTP, SCP or Rsync.
In comparison with other image generation models, Stable Cascade consistently outperforms other variants in prompt alignment and aesthetic quality. Particularly, Stable Cascade (30 inference steps) exhibits superior performance compared to Playground v2 (50 inference steps), SDXL (50 inference steps), SDXL Turbo (1 inference step), and Würstchen v2 (30 inference steps) based on the initial model tests.
It's important to note that the model results are specifically related to text-to-image generation.
You have generated AI images using the Stable Cascade model on a Vultr Cloud GPU server with text-to-image, image-to-image, and image variation methods. Based on your model deployment needs, you can use Stable Cascade with multiple input prompts and images to finetune it to your environment. For more information and usage samples, visit the Stable Cascade model page on Hugging Face.
0 Comments
Be the first to comment and share your perspective with the community.