A Coding Guide To Compare Three Stability Ai Diffusion Models (v1.5, V2-base & Sd3-medium) Diffusion Capabilities Side-by-side In Google Colab Using Gradio

Trending 6 hours ago
ARTICLE AD BOX

In this hands-on tutorial, we’ll unlock nan imaginative imaginable of Stability AI’s industry-leading diffusion models, Stable Diffusion v1.5, Stability AI’s v2-base, and nan cutting-edge Stable Diffusion 3 Medium, to make eye-catching imagery. Running wholly successful Google Colab pinch a Gradio interface, we’ll acquisition side-by-side comparisons of 3 powerful pipelines, accelerated punctual iteration, and seamless GPU-accelerated inference. Whether we’re a marketer looking to elevate our brand’s ocular communicative aliases a developer eager to prototype AI-driven contented workflows, this tutorial showcases really Stability AI’s open-source models tin beryllium deployed instantly and astatine nary infrastructure cost, allowing you to attraction connected storytelling, engagement, and driving real-world results.

!pip instal huggingface_hub from huggingface_hub import notebook_login notebook_login()

We instal nan huggingface_hub room and past import and invoke nan notebook_login() function, which prompts you to authenticate your notebook convention pinch your Hugging Face account, allowing you to seamlessly entree and negociate models, datasets, and different hub resources.

!pip uninstall -y torchvision !pip instal --upgrade torch torchvision --index-url https://download.pytorch.org/whl/cu118 !pip instal --upgrade diffusers transformers accelerate safetensors gradio pillow

We first force-uninstalls immoderate existing torchvision to clear imaginable conflicts, past reinstalls torch and torchvision from nan CUDA 11.8–compatible PyTorch wheels, and yet upgrades cardinal libraries, diffusers, transformers, accelerate, safetensors, gradio, and pillow, to guarantee you person nan latest versions for building and moving GPU-accelerated generative pipelines and web demos.

import torch from diffusers import StableDiffusionPipeline, StableDiffusion3Pipeline import gradio arsenic gr device = "cuda" if torch.cuda.is_available() other "cpu"

We import PyTorch alongside some nan Stable Diffusion v1 and v3 pipelines from nan Diffusers library, arsenic good arsenic Gradio for building interactive demos. It past checks for CUDA readiness and sets nan instrumentality adaptable to “cuda” if a GPU is present; otherwise, it falls backmost to “cpu”, ensuring your models tally connected nan optimal hardware.

pipe1 = StableDiffusionPipeline.from_pretrained( "runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16, safety_checker=None ).to(device) pipe1.enable_attention_slicing()

We load nan Stable Diffusion v1.5 exemplary successful half-precision (float16) without nan built-in information checker, transfers it to your selected instrumentality (GPU, if available), and past enables attraction slicing to trim highest VRAM usage during image generation.

pipe2 = StableDiffusionPipeline.from_pretrained( "stabilityai/stable-diffusion-2-base", torch_dtype=torch.float16, safety_checker=None ).to(device) pipe2.enable_attention_slicing()

We load nan Stable Diffusion v2 “base” exemplary successful 16-bit precision without nan default information filter, transfers it to your chosen device, and activates attraction slicing to optimize representation usage during inference.

pipe3 = StableDiffusion3Pipeline.from_pretrained( "stabilityai/stable-diffusion-3-medium-diffusers", torch_dtype=torch.float16, safety_checker=None ).to(device) pipe3.enable_attention_slicing()

We propulsion successful Stability AI’s Stable Diffusion 3 “medium” checkpoint successful 16-bit precision (skipping nan built-in information checker), transfers it to your selected device, and enables attraction slicing to trim GPU representation usage during generation.

def generate(prompt, steps, scale): img1 = pipe1(prompt, num_inference_steps=steps, guidance_scale=scale).images[0] img2 = pipe2(prompt, num_inference_steps=steps, guidance_scale=scale).images[0] img3 = pipe3(prompt, num_inference_steps=steps, guidance_scale=scale).images[0] return img1, img2, img3

Now, this usability runs nan aforesaid matter punctual done each 3 loaded pipelines (pipe1, pipe2, pipe3) utilizing nan specified conclusion steps and guidance scale, past returns nan first image from each, making it cleanable for comparing outputs crossed Stable Diffusion v1.5, v2-base, and v3-medium.

def choose(selection): return f"✅ You selected: **{selection}**" with gr.Blocks() arsenic demo: gr.Markdown("## AI Social-Post Generator pinch 3 Models") pinch gr.Row(): punctual = gr.Textbox(label="Prompt", placeholder="A vibrant formation sunset…") steps = gr.Slider( 1, 100, value=50, step=1, label="Inference Steps") standard = gr.Slider( 1.0, 20.0, value=7.5, step=0.1, label="Guidance Scale") btn = gr.Button("Generate Images") pinch gr.Row(): out1 = gr.Image(label="Model 1: SD v1.5") out2 = gr.Image(label="Model 2: SD v2-base") out3 = gr.Image(label="Model 3: SD v3-medium") sel = gr.Radio( ["Model 1: SD v1.5","Model 2: SD v2-base","Model 3: SD v3-medium"], label="Select your favorite" ) txt = gr.Markdown() btn.click(fn=generate, inputs=[prompt, steps, scale], outputs=[out1, out2, out3]) sel.change(fn=choose, inputs=sel, outputs=txt) demo.launch(share=True)

Finally, this Gradio app builds a three-column UI wherever you tin participate a matter prompt, set conclusion steps and guidance scale, past make and show images from SD v1.5, v2-base, and v3-medium broadside by side. It besides features a power selector, allowing you to prime your preferred exemplary output, and displays a elemental confirmation connection erstwhile a prime is made.

A web interface to comparison nan 3 Stability AI models’ output 

In conclusion, by integrating Stability AI’s state-of-the-art diffusion architectures into an easy-to-use Gradio app, you’ve seen really effortlessly you tin prototype, compare, and deploy stunning visuals that resonate connected today’s platforms. From A/B-testing imaginative directions to automating run assets astatine scale, Stability AI provides nan performance, flexibility, and vibrant organization support to toggle shape your contented pipeline.


Check retired nan Colab Notebook. Don’t hide to travel america on Twitter and subordinate our Telegram Channel and LinkedIn Group. Don’t Forget to subordinate our 90k+ ML SubReddit. For Promotion and Partnerships, please talk us.

🔥 [Register Now] miniCON Virtual Conference connected AGENTIC AI: FREE REGISTRATION + Certificate of Attendance + 4 Hour Short Event (May 21, 9 am- 1 p.m. PST) + Hands connected Workshop

Nikhil is an intern advisor astatine Marktechpost. He is pursuing an integrated dual grade successful Materials astatine nan Indian Institute of Technology, Kharagpur. Nikhil is an AI/ML enthusiast who is ever researching applications successful fields for illustration biomaterials and biomedical science. With a beardown inheritance successful Material Science, he is exploring caller advancements and creating opportunities to contribute.

More