-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Add image2image example script. #231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7c4eea7
boom boom
patil-suraj e47b5f9
reorganise examples
patil-suraj f8b01da
add image2image in example inference
patil-suraj d5f644a
add readme
patil-suraj 7ced86d
fix example
patil-suraj 88a68c5
update colab url
patil-suraj 338ecd5
Apply suggestions from code review
patil-suraj 17c2212
fix init_timestep
patil-suraj 41ad08a
Merge branch 'img2img' of https://github.com/huggingface/diffusers in…
patil-suraj e4a1a8f
update colab url
patil-suraj c0c3a19
update main readme
patil-suraj a311826
rename readme
patil-suraj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
import inspect | ||
from typing import List, Optional, Union | ||
|
||
import numpy as np | ||
import torch | ||
|
||
import PIL | ||
from diffusers import AutoencoderKL, DDIMScheduler, DiffusionPipeline, PNDMScheduler, UNet2DConditionModel | ||
from diffusers.pipelines.stable_diffusion import StableDiffusionSafetyChecker | ||
from tqdm.auto import tqdm | ||
from transformers import CLIPFeatureExtractor, CLIPTextModel, CLIPTokenizer | ||
|
||
|
||
def preprocess(image): | ||
w, h = image.size | ||
w, h = map(lambda x: x - x % 32, (w, h)) # resize to integer multiple of 32 | ||
image = image.resize((w, h), resample=PIL.Image.LANCZOS) | ||
image = np.array(image).astype(np.float32) / 255.0 | ||
image = image[None].transpose(0, 3, 1, 2) | ||
image = torch.from_numpy(image) | ||
return 2.0 * image - 1.0 | ||
|
||
|
||
class StableDiffusionImg2ImgPipeline(DiffusionPipeline): | ||
def __init__( | ||
self, | ||
vae: AutoencoderKL, | ||
text_encoder: CLIPTextModel, | ||
tokenizer: CLIPTokenizer, | ||
unet: UNet2DConditionModel, | ||
scheduler: Union[DDIMScheduler, PNDMScheduler], | ||
safety_checker: StableDiffusionSafetyChecker, | ||
feature_extractor: CLIPFeatureExtractor, | ||
): | ||
super().__init__() | ||
scheduler = scheduler.set_format("pt") | ||
self.register_modules( | ||
vae=vae, | ||
text_encoder=text_encoder, | ||
tokenizer=tokenizer, | ||
unet=unet, | ||
scheduler=scheduler, | ||
safety_checker=safety_checker, | ||
feature_extractor=feature_extractor, | ||
) | ||
|
||
@torch.no_grad() | ||
def __call__( | ||
self, | ||
prompt: Union[str, List[str]], | ||
init_image: torch.FloatTensor, | ||
strength: float = 0.8, | ||
num_inference_steps: Optional[int] = 50, | ||
guidance_scale: Optional[float] = 7.5, | ||
eta: Optional[float] = 0.0, | ||
generator: Optional[torch.Generator] = None, | ||
output_type: Optional[str] = "pil", | ||
): | ||
|
||
if isinstance(prompt, str): | ||
batch_size = 1 | ||
elif isinstance(prompt, list): | ||
batch_size = len(prompt) | ||
else: | ||
raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}") | ||
|
||
# set timesteps | ||
accepts_offset = "offset" in set(inspect.signature(self.scheduler.set_timesteps).parameters.keys()) | ||
extra_set_kwargs = {} | ||
offset = 0 | ||
if accepts_offset: | ||
offset = 1 | ||
extra_set_kwargs["offset"] = 1 | ||
|
||
self.scheduler.set_timesteps(num_inference_steps, **extra_set_kwargs) | ||
|
||
# encode the init image into latents and scale the latents | ||
init_latents = self.vae.encode(init_image.to(self.device)).sample() | ||
init_latents = 0.18215 * init_latents | ||
|
||
# prepare init_latents noise to latents | ||
init_latents = torch.cat([init_latents] * batch_size) | ||
|
||
# get the original timestep using init_timestep | ||
init_timestep = int(num_inference_steps * strength) + offset | ||
init_timestep = min(init_timestep, num_inference_steps) | ||
timesteps = self.scheduler.timesteps[-init_timestep] | ||
timesteps = torch.tensor([timesteps] * batch_size, dtype=torch.long, device=self.device) | ||
|
||
# add noise to latents using the timesteps | ||
noise = torch.randn(init_latents.shape, generator=generator, device=self.device) | ||
init_latents = self.scheduler.add_noise(init_latents, noise, timesteps) | ||
|
||
# get prompt text embeddings | ||
text_input = self.tokenizer( | ||
prompt, | ||
padding="max_length", | ||
max_length=self.tokenizer.model_max_length, | ||
truncation=True, | ||
return_tensors="pt", | ||
) | ||
text_embeddings = self.text_encoder(text_input.input_ids.to(self.device))[0] | ||
|
||
# here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) | ||
# of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` | ||
# corresponds to doing no classifier free guidance. | ||
do_classifier_free_guidance = guidance_scale > 1.0 | ||
# get unconditional embeddings for classifier free guidance | ||
if do_classifier_free_guidance: | ||
max_length = text_input.input_ids.shape[-1] | ||
uncond_input = self.tokenizer( | ||
[""] * batch_size, padding="max_length", max_length=max_length, return_tensors="pt" | ||
) | ||
uncond_embeddings = self.text_encoder(uncond_input.input_ids.to(self.device))[0] | ||
|
||
# For classifier free guidance, we need to do two forward passes. | ||
# Here we concatenate the unconditional and text embeddings into a single batch | ||
# to avoid doing two forward passes | ||
text_embeddings = torch.cat([uncond_embeddings, text_embeddings]) | ||
|
||
# prepare extra kwargs for the scheduler step, since not all schedulers have the same signature | ||
# eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. | ||
# eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 | ||
# and should be between [0, 1] | ||
accepts_eta = "eta" in set(inspect.signature(self.scheduler.step).parameters.keys()) | ||
extra_step_kwargs = {} | ||
if accepts_eta: | ||
extra_step_kwargs["eta"] = eta | ||
|
||
latents = init_latents | ||
t_start = max(num_inference_steps - init_timestep + offset, 0) | ||
for i, t in tqdm(enumerate(self.scheduler.timesteps[t_start:])): | ||
# expand the latents if we are doing classifier free guidance | ||
latent_model_input = torch.cat([latents] * 2) if do_classifier_free_guidance else latents | ||
|
||
# predict the noise residual | ||
noise_pred = self.unet(latent_model_input, t, encoder_hidden_states=text_embeddings)["sample"] | ||
|
||
# perform guidance | ||
if do_classifier_free_guidance: | ||
noise_pred_uncond, noise_pred_text = noise_pred.chunk(2) | ||
noise_pred = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) | ||
|
||
# compute the previous noisy sample x_t -> x_t-1 | ||
latents = self.scheduler.step(noise_pred, t, latents, **extra_step_kwargs)["prev_sample"] | ||
|
||
# scale and decode the image latents with vae | ||
latents = 1 / 0.18215 * latents | ||
image = self.vae.decode(latents) | ||
|
||
image = (image / 2 + 0.5).clamp(0, 1) | ||
image = image.cpu().permute(0, 2, 3, 1).numpy() | ||
|
||
# run safety checker | ||
safety_cheker_input = self.feature_extractor(self.numpy_to_pil(image), return_tensors="pt").to(self.device) | ||
image, has_nsfw_concept = self.safety_checker(images=image, clip_input=safety_cheker_input.pixel_values) | ||
|
||
if output_type == "pil": | ||
image = self.numpy_to_pil(image) | ||
|
||
return {"sample": image, "nsfw_content_detected": has_nsfw_concept} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Inference Examples | ||
|
||
## Installing the dependencies | ||
|
||
Before running the scipts, make sure to install the library's dependencies: | ||
|
||
```bash | ||
pip install diffusers transformers ftfy | ||
``` | ||
|
||
## Image-to-Image text-guided generation with Stable Diffusion | ||
|
||
The `image_to_image.py` script implements `StableDiffusionImg2ImgPipeline`. It lets you pass a text prompt and an initial image to condition the generation of new images. This example also showcases how you can write custom diffusion pipelines using `diffusers`! | ||
|
||
### How to use it | ||
|
||
|
||
```python | ||
from torch import autocast | ||
import requests | ||
from PIL import Image | ||
from io import BytesIO | ||
|
||
from image_to_image import StableDiffusionImg2ImgPipeline, preprocess | ||
|
||
# load the pipeline | ||
device = "cuda" | ||
pipe = StableDiffusionImg2ImgPipeline.from_pretrained( | ||
"CompVis/stable-diffusion-v1-4", | ||
revision="fp16", | ||
torch_dtype=torch.float16, | ||
use_auth_token=True | ||
).to(device) | ||
|
||
# let's download an initial image | ||
url = "https://rg.gosu.cc/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg" | ||
|
||
response = requests.get(url) | ||
init_image = Image.open(BytesIO(response.content)).convert("RGB") | ||
init_image = init_image.resize((768, 512)) | ||
init_image = preprocess(init_image) | ||
|
||
prompt = "A fantasy landscape, trending on artstation" | ||
|
||
with autocast("cuda"): | ||
images = pipe(prompt=prompt, init_image=init_image, strength=0.75, guidance_scale=7.5)["sample"] | ||
|
||
images[0].save("fantasy_landscape.png") | ||
``` | ||
You can also run this example on colab [](https://colab.research.google.com/github/patil-suraj/Notebooks/blob/master/image_2_image_using_diffusers.ipynb) |
File renamed without changes.
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@anton-l Linked the examples here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect!