Skip to main content

Stable Diffusion

tip

Recommended tutorials before starting to generate images with Stable Diffusion.

Stable Diffusion is a deep learning text-to-image model that is primarily used to generate images from a text prompt. For example, given the prompt "The Easter bunny riding a motorcycle in New York City", the model has generated the following images.

Stochastic has released a GitHub repository to perform fast real-time inference using this model. For experienced Deep Learning users we recommend to have a look to it.

Start generating images using Google Colab

If you don't have a GPU in your local machine, don't worry, you can run a quick demo using Google Colab. Click on the link to open it. You will find three code cells in it.

  1. Install the stochasticx library.
!pip install -q stochasticx
  1. Initialize the model
from stochasticx.local.stable_diffusion import init, generate_images

model = init()
  1. Start generating images. Feel free to change the prompt to generate whatever you want.
generate_images(
model,
prompt=["Riding a horse"]
)[0]
info

The generate_images function accepts several parameters.

  • prompt: the prompt.
  • img_height: height of the generated image, defaults to 512.
  • img_width: width of the generated image, defaults to 512.
  • num_inference_steps: the number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference, defaults to 50.
  • guidance_scale: guidance scale, defaults to 7.5.
  • num_images_per_prompt: the number of images to generate per prompt, defaults to 1.
  • seed: Seed to make generation deterministic, defaults to None.

Start generating images locally

If you have a computer with GPU, you can run the Stable-Diffusion model using the stochasticx CLI.

Before starting, you will have to sign up in the Stochastic Platform and install Docker and the stochasticx library.

Once you have installed the requirements, you can start generating images.

  1. Do login
stochasticx login
Output
Enter your email: 
Password:

[+] Login successfully
  1. Deploy the model. It might take some minutes.
stochasticx stable-diffusion deploy
Output
[+] Deploying Stable Diffusion model
[+] If it is the first time you deploy the model, it might take some minutes to deploy it.
[+] Stable Diffusion running in the port 5000.
[+] Using GPU: True
[+] Run the following command to start generating
stochasticx stable-diffusion inference --prompt "An astronaut riding a horse"
info

The CLI command stochasticx stable-diffusion deploy accepts the following options:

  • port: port in which the application will be listening.
  • type: model optimization. If anything is specified, the best option is chosen based on the available hardware.
  1. Check the logs.
stochasticx stable-diffusion logs
  1. Generate the images. By default, the generated images are saved in the directory ./generated_imges.
stochasticx stable-diffusion inference --prompt "An astronaut riding a horse"
Output
[+] Generating images...
[+] Time needed to generate the images: 2.93 seconds
[+] Images saved in the following path: generated_images/
info

The CLI command stochasticx stable-diffusion inference accepts the following options:

  • prompt: the prompt.
  • img_height: height of the generated image, defaults to 512.
  • img_width: width of the generated image, defaults to 512.
  • num_inference_steps: the number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference, defaults to 50.
  • guidance_scale: guidance scale, defaults to 7.5.
  • num_images_per_prompt: the number of images to generate per prompt, defaults to 1.
  • seed: Seed to make generation deterministic, defaults to None.
  • saving_path: directory where the generated images will be saved
  1. Stop your deployment.
stochasticx stable-diffusion stop

Deploy the Stable-Diffusion model in the Stochastic cloud

If you want to deploy a highly reliable and accelerated Stable-Diffusion model in a production environment, you can use the Stochastic platform. To do that, you only have to install the stochasticx library and sign up in the Stochastic Platform

  1. Do login
stochasticx login
Output
Enter your email: 
Password:

[+] Login successfully
  1. Deploy the model. It might take some minutes.
stochasticx stable-diffusion deploy --type cloud
Output
[+] Deploying the Stable-Diffusion model. It might take some minutes
[+] List your deployments with the following command: stochasticx stable-diffusion list
  1. You can list you cloud deployments.
stochasticx stable-diffusion ls
Output
[+] Deployments

ID: 635937bf49408e002617108d ; Status: running ; Model name: stable-diffusion ; API key: bnNHGWkNpi9T854m67GNVc3NC6iYTqlW ; Endpoint: http://infer.stochastic.ai:8000/635937bf49408e002617108d
  1. Start generating images
stochasticx stable-diffusion inference --cloud_id 635937bf49408e002617108d --prompt "A person riding a horse"
Output
[+] Generating images...
[+] Time needed to generate the images: 7.55306625366211 seconds
[+] Images saved in the following path: generated_images
info

The CLI command stochasticx stable-diffusion infererence accepts the following options:

  • prompt: the prompt.
  • img_height: height of the generated image, defaults to 512.
  • img_width: width of the generated image, defaults to 512.
  • num_inference_steps: the number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference, defaults to 50.
  • guidance_scale: guidance scale, defaults to 7.5.
  • num_images_per_prompt: the number of images to generate per prompt, defaults to 1.
  • seed: Seed to make generation deterministic, defaults to None.
  • saving_path: directory where the generated images will be saved
  1. Stop your deployment.
stochasticx stable-diffusion stop --cloud_id 635937bf49408e002617108d
Output
[+] Stopping and removing stable-diffusion model
[+] Removed