Notebooks
H
Hugging Face
Image 2 Image Using Diffusers

Image 2 Image Using Diffusers

hf-notebooksdiffusers

Open In Colab

Image2Image Pipeline for Stable Diffusion using 🧨 Diffusers

This notebook shows how to create a custom diffusers pipeline for text-guided image-to-image generation with Stable Diffusion model using 🤗 Hugging Face 🧨 Diffusers library.

For a general introduction to the Stable Diffusion model please refer to this colab.

[2]
Thu Sep  8 18:58:28 2022       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 460.32.03    Driver Version: 460.32.03    CUDA Version: 11.2     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  Tesla V100-SXM2...  Off  | 00000000:00:04.0 Off |                    0 |
| N/A   42C    P0    27W / 300W |      0MiB / 16160MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+
[3]
Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/
Requirement already satisfied: diffusers==0.11.1 in /usr/local/lib/python3.7/dist-packages (0.3.0)
Requirement already satisfied: transformers in /usr/local/lib/python3.7/dist-packages (4.21.3)
Requirement already satisfied: ftfy in /usr/local/lib/python3.7/dist-packages (6.1.1)
Requirement already satisfied: torch>=1.4 in /usr/local/lib/python3.7/dist-packages (from diffusers==0.11.1) (1.12.1+cu113)
Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from diffusers==0.11.1) (1.21.6)
Requirement already satisfied: Pillow in /usr/local/lib/python3.7/dist-packages (from diffusers==0.11.1) (7.1.2)
Requirement already satisfied: importlib-metadata in /usr/local/lib/python3.7/dist-packages (from diffusers==0.11.1) (4.12.0)
Requirement already satisfied: filelock in /usr/local/lib/python3.7/dist-packages (from diffusers==0.11.1) (3.8.0)
Requirement already satisfied: huggingface-hub>=0.8.1 in /usr/local/lib/python3.7/dist-packages (from diffusers==0.11.1) (0.9.1)
Requirement already satisfied: regex!=2019.12.17 in /usr/local/lib/python3.7/dist-packages (from diffusers==0.11.1) (2022.6.2)
Requirement already satisfied: requests in /usr/local/lib/python3.7/dist-packages (from diffusers==0.11.1) (2.23.0)
Requirement already satisfied: tqdm in /usr/local/lib/python3.7/dist-packages (from huggingface-hub>=0.8.1->diffusers==0.11.1) (4.64.0)
Requirement already satisfied: packaging>=20.9 in /usr/local/lib/python3.7/dist-packages (from huggingface-hub>=0.8.1->diffusers==0.11.1) (21.3)
Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.7/dist-packages (from huggingface-hub>=0.8.1->diffusers==0.11.1) (6.0)
Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.7/dist-packages (from huggingface-hub>=0.8.1->diffusers==0.11.1) (4.1.1)
Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in /usr/local/lib/python3.7/dist-packages (from packaging>=20.9->huggingface-hub>=0.8.1->diffusers==0.11.1) (3.0.9)
Requirement already satisfied: tokenizers!=0.11.3,<0.13,>=0.11.1 in /usr/local/lib/python3.7/dist-packages (from transformers) (0.12.1)
Requirement already satisfied: wcwidth>=0.2.5 in /usr/local/lib/python3.7/dist-packages (from ftfy) (0.2.5)
Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.7/dist-packages (from importlib-metadata->diffusers==0.11.1) (3.8.1)
Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests->diffusers==0.11.1) (3.0.4)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests->diffusers==0.11.1) (1.24.3)
Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests->diffusers==0.11.1) (2.10)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests->diffusers==0.11.1) (2022.6.15)

To use private and gated models on 🤗 Hugging Face Hub, login is required. If you are only using a public checkpoint (such as CompVis/stable-diffusion-v1-4 in this notebook), you can skip this step.

[4]
Login successful
Your token has been saved to /root/.huggingface/token
Authenticated through git-credential store but this isn't the helper defined on your machine.
You might have to re-authenticate when pushing to the Hugging Face Hub. Run the following command in your terminal in case you want to set this credential helper as the default

git config --global credential.helper store

Image2Image pipeline.

[5]

Load the pipeline

[7]
Downloading:   0%|          | 0.00/1.34k [00:00<?, ?B/s]
Downloading:   0%|          | 0.00/12.5k [00:00<?, ?B/s]
Downloading:   0%|          | 0.00/342 [00:00<?, ?B/s]
Downloading:   0%|          | 0.00/543 [00:00<?, ?B/s]
Downloading:   0%|          | 0.00/4.63k [00:00<?, ?B/s]
Downloading:   0%|          | 0.00/608M [00:00<?, ?B/s]
Downloading:   0%|          | 0.00/209 [00:00<?, ?B/s]
Downloading:   0%|          | 0.00/209 [00:00<?, ?B/s]
Downloading:   0%|          | 0.00/572 [00:00<?, ?B/s]
Downloading:   0%|          | 0.00/246M [00:00<?, ?B/s]
Downloading:   0%|          | 0.00/525k [00:00<?, ?B/s]
Downloading:   0%|          | 0.00/472 [00:00<?, ?B/s]
Downloading:   0%|          | 0.00/788 [00:00<?, ?B/s]
Downloading:   0%|          | 0.00/1.06M [00:00<?, ?B/s]
Downloading:   0%|          | 0.00/772 [00:00<?, ?B/s]
Downloading:   0%|          | 0.00/1.72G [00:00<?, ?B/s]
Downloading:   0%|          | 0.00/71.2k [00:00<?, ?B/s]
Downloading:   0%|          | 0.00/550 [00:00<?, ?B/s]
Downloading:   0%|          | 0.00/167M [00:00<?, ?B/s]
{'trained_betas'} was not found in config. Values will be initialized to default values.

Download an initial image and preprocess it so we can pass it to the pipeline.

[8]
Output

Define the prompt and run the pipeline.

[9]

Here, strength is a value between 0.0 and 1.0, that controls the amount of noise that is added to the input image. Values that approach 1.0 allow for lots of variations but will also produce images that are not semantically consistent with the input.

[13]
  0%|          | 0/38 [00:00<?, ?it/s]
[14]
Output
[15]
  0%|          | 0/26 [00:00<?, ?it/s]
[16]
Output

As you can see, when using a lower value for strength, the generated image is more closer to the original image

[17]
[20]
  0%|          | 0/37 [00:00<?, ?it/s]
[21]
Output