Image generation using Stable Diffusion model
In DataSphere, you can deploy a neural network based on the Stable Diffusion model and generate images based on text descriptions.
Stable Diffusion
In this tutorial, you will generate an image based on a text description using a KerasCV
To generate an image using the Stable Diffusion model:
If you no longer need the resources you created, delete them.
Getting started
Before getting started, register in Yandex Cloud, set up a community, and link your billing account to it.
- On the DataSphere home page
, click Try for free and select an account to log in with: Yandex ID or your working account in the identity federation (SSO). - Select the Yandex Cloud Organization organization you are going to use in Yandex Cloud.
- Create a community.
- Link your billing account to the DataSphere community you are going to work in. Make sure that you have a billing account linked and its status is
ACTIVE
orTRIAL_ACTIVE
. If you do not have a billing account yet, create one in the DataSphere interface.
Note
If you use an identity federation to access Yandex Cloud, billing details might be unavailable to you. In this case, contact your Yandex Cloud organization administrator.
Required paid resources
The cost of using the model includes a fee for running code cells (see DataSphere pricing).
Prepare the infrastructure
Create a project
- Open the DataSphere home page
. - In the left-hand panel, select
Communities. - Select the community to create a project in.
- On the community page, click
Create project. - In the window that opens, enter
Stable Diffusion
as your project name and add a description (optional). - Click Create.
Create a notebook and install the libraries
Note
In this tutorial, all computations use the g1.1 configuration. However, you can run the model on other configurations as well.
-
In the DataSphere interface, open the project you created.
-
Create a new notebook:
- In the top panel of the project window, click File → New → Notebook.
- In the window that opens, select DataSphere Kernel.
-
Update the TensorFlow
library. Paste the below code into the cell and click :%pip install tensorflow==2.11.0
-
Install the KerasCV library:
%pip install keras_cv==0.4.2
-
Install the NumPy
library, version 1.21:%pip install numpy==1.21
-
Install the Protocol Buffers library, version 3.20:
%pip install protobuf==3.20
-
Restart the kernel by clicking Kernel → Restart kernel in the top panel of the project window.
Create a model and generate an image
-
Import the libraries to the project:
#!g1.1 import time import keras_cv from tensorflow import keras import matplotlib.pyplot as plt
-
Create a model:
#!g1.1 model = keras_cv.models.StableDiffusion(img_width=512, img_height=512)
-
Generate an image by its description:
#!g1.1 images = model.text_to_image("photograph of an astronaut riding a banana with old dragon", batch_size=3)
-
Create an image plotting function:
#!g1.1 def plot_images(images): plt.figure(figsize=(20, 20)) for i in range(len(images)): ax = plt.subplot(1, len(images), i + 1) plt.imshow(images[i]) plt.axis("off")
-
Display the resulting image:
#!g1.1 plot_images(images)
Result:
How to delete the resources you created
If you no longer plan to use the Stable Diffusion
project, delete it.