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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import notebook_login
|
| 2 |
+
import cv2
|
| 3 |
+
from google.colab.patches import cv2_imshow
|
| 4 |
+
import tempfile
|
| 5 |
+
|
| 6 |
+
notebook_login()
|
| 7 |
+
|
| 8 |
+
import inspect
|
| 9 |
+
from typing import List, Optional, Union
|
| 10 |
+
|
| 11 |
+
import numpy as np
|
| 12 |
+
import torch
|
| 13 |
+
|
| 14 |
+
import PIL
|
| 15 |
+
from diffusers import AutoencoderKL, DDIMScheduler, DiffusionPipeline, PNDMScheduler, UNet2DConditionModel
|
| 16 |
+
from diffusers.pipelines.stable_diffusion import StableDiffusionSafetyChecker
|
| 17 |
+
from tqdm.auto import tqdm
|
| 18 |
+
from transformers import CLIPFeatureExtractor, CLIPTextModel, CLIPTokenizer
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def preprocess_image(image):
|
| 22 |
+
w, h = image.size
|
| 23 |
+
w, h = map(lambda x: x - x % 32, (w, h)) # resize to integer multiple of 32
|
| 24 |
+
image = image.resize((w, h), resample=PIL.Image.LANCZOS)
|
| 25 |
+
image = np.array(image).astype(np.float32) / 255.0
|
| 26 |
+
image = image[None].transpose(0, 3, 1, 2)
|
| 27 |
+
image = torch.from_numpy(image)
|
| 28 |
+
return 2.0 * image - 1.0
|
| 29 |
+
|
| 30 |
+
def preprocess_mask(mask):
|
| 31 |
+
mask=mask.convert("L")
|
| 32 |
+
w, h = mask.size
|
| 33 |
+
w, h = map(lambda x: x - x % 32, (w, h)) # resize to integer multiple of 32
|
| 34 |
+
mask = mask.resize((w//8, h//8), resample=PIL.Image.NEAREST)
|
| 35 |
+
mask = np.array(mask).astype(np.float32) / 255.0
|
| 36 |
+
mask = np.tile(mask,(4,1,1))
|
| 37 |
+
mask = mask[None].transpose(0, 1, 2, 3)#what does this step do?
|
| 38 |
+
mask = 1 - mask #repaint white, keep black
|
| 39 |
+
mask = torch.from_numpy(mask)
|
| 40 |
+
return mask
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
class StableDiffusionInpaintingPipeline(DiffusionPipeline):
|
| 44 |
+
def __init__(
|
| 45 |
+
self,
|
| 46 |
+
vae: AutoencoderKL,
|
| 47 |
+
text_encoder: CLIPTextModel,
|
| 48 |
+
tokenizer: CLIPTokenizer,
|
| 49 |
+
unet: UNet2DConditionModel,
|
| 50 |
+
scheduler: Union[DDIMScheduler, PNDMScheduler],
|
| 51 |
+
safety_checker: StableDiffusionSafetyChecker,
|
| 52 |
+
feature_extractor: CLIPFeatureExtractor,
|
| 53 |
+
):
|
| 54 |
+
super().__init__()
|
| 55 |
+
scheduler = scheduler.set_format("pt")
|
| 56 |
+
self.register_modules(
|
| 57 |
+
vae=vae,
|
| 58 |
+
text_encoder=text_encoder,
|
| 59 |
+
tokenizer=tokenizer,
|
| 60 |
+
unet=unet,
|
| 61 |
+
scheduler=scheduler,
|
| 62 |
+
safety_checker=safety_checker,
|
| 63 |
+
feature_extractor=feature_extractor,
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
@torch.no_grad()
|
| 67 |
+
def __call__(
|
| 68 |
+
self,
|
| 69 |
+
prompt: Union[str, List[str]],
|
| 70 |
+
init_image: torch.FloatTensor,
|
| 71 |
+
mask_image: torch.FloatTensor,
|
| 72 |
+
strength: float = 0.8,
|
| 73 |
+
num_inference_steps: Optional[int] = 50,
|
| 74 |
+
guidance_scale: Optional[float] = 7.5,
|
| 75 |
+
eta: Optional[float] = 0.0,
|
| 76 |
+
generator: Optional[torch.Generator] = None,
|
| 77 |
+
output_type: Optional[str] = "pil",
|
| 78 |
+
):
|
| 79 |
+
|
| 80 |
+
if isinstance(prompt, str):
|
| 81 |
+
batch_size = 1
|
| 82 |
+
elif isinstance(prompt, list):
|
| 83 |
+
batch_size = len(prompt)
|
| 84 |
+
else:
|
| 85 |
+
raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}")
|
| 86 |
+
|
| 87 |
+
if strength < 0 or strength > 1:
|
| 88 |
+
raise ValueError(f"The value of strength should in [0.0, 1.0] but is {strength}")
|
| 89 |
+
|
| 90 |
+
# set timesteps
|
| 91 |
+
accepts_offset = "offset" in set(inspect.signature(self.scheduler.set_timesteps).parameters.keys())
|
| 92 |
+
extra_set_kwargs = {}
|
| 93 |
+
offset = 0
|
| 94 |
+
if accepts_offset:
|
| 95 |
+
offset = 1
|
| 96 |
+
extra_set_kwargs["offset"] = 1
|
| 97 |
+
|
| 98 |
+
self.scheduler.set_timesteps(num_inference_steps, **extra_set_kwargs)
|
| 99 |
+
|
| 100 |
+
#preprocess image
|
| 101 |
+
init_image = preprocess_image(init_image).to(self.device)
|
| 102 |
+
|
| 103 |
+
# encode the init image into latents and scale the latents
|
| 104 |
+
init_latents = self.vae.encode(init_image).sample()
|
| 105 |
+
init_latents = 0.18215 * init_latents
|
| 106 |
+
|
| 107 |
+
# prepare init_latents noise to latents
|
| 108 |
+
init_latents = torch.cat([init_latents] * batch_size)
|
| 109 |
+
init_latents_orig = init_latents
|
| 110 |
+
|
| 111 |
+
# preprocess mask
|
| 112 |
+
mask = preprocess_mask(mask_image).to(self.device)
|
| 113 |
+
mask = torch.cat([mask] * batch_size)
|
| 114 |
+
|
| 115 |
+
#check sizes
|
| 116 |
+
if not mask.shape == init_latents.shape:
|
| 117 |
+
raise ValueError(f"The mask and init_image should be the same size!")
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
# get the original timestep using init_timestep
|
| 121 |
+
init_timestep = int(num_inference_steps * strength) + offset
|
| 122 |
+
init_timestep = min(init_timestep, num_inference_steps)
|
| 123 |
+
timesteps = self.scheduler.timesteps[-init_timestep]
|
| 124 |
+
timesteps = torch.tensor([timesteps] * batch_size, dtype=torch.long, device=self.device)
|
| 125 |
+
|
| 126 |
+
# add noise to latents using the timesteps
|
| 127 |
+
noise = torch.randn(init_latents.shape, generator=generator, device=self.device)
|
| 128 |
+
init_latents = self.scheduler.add_noise(init_latents, noise, timesteps)
|
| 129 |
+
|
| 130 |
+
# get prompt text embeddings
|
| 131 |
+
text_input = self.tokenizer(
|
| 132 |
+
prompt,
|
| 133 |
+
padding="max_length",
|
| 134 |
+
max_length=self.tokenizer.model_max_length,
|
| 135 |
+
truncation=True,
|
| 136 |
+
return_tensors="pt",
|
| 137 |
+
)
|
| 138 |
+
text_embeddings = self.text_encoder(text_input.input_ids.to(self.device))[0]
|
| 139 |
+
|
| 140 |
+
# here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
|
| 141 |
+
# of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
|
| 142 |
+
# corresponds to doing no classifier free guidance.
|
| 143 |
+
do_classifier_free_guidance = guidance_scale > 1.0
|
| 144 |
+
# get unconditional embeddings for classifier free guidance
|
| 145 |
+
if do_classifier_free_guidance:
|
| 146 |
+
max_length = text_input.input_ids.shape[-1]
|
| 147 |
+
uncond_input = self.tokenizer(
|
| 148 |
+
[""] * batch_size, padding="max_length", max_length=max_length, return_tensors="pt"
|
| 149 |
+
)
|
| 150 |
+
uncond_embeddings = self.text_encoder(uncond_input.input_ids.to(self.device))[0]
|
| 151 |
+
|
| 152 |
+
# For classifier free guidance, we need to do two forward passes.
|
| 153 |
+
# Here we concatenate the unconditional and text embeddings into a single batch
|
| 154 |
+
# to avoid doing two forward passes
|
| 155 |
+
text_embeddings = torch.cat([uncond_embeddings, text_embeddings])
|
| 156 |
+
|
| 157 |
+
# prepare extra kwargs for the scheduler step, since not all schedulers have the same signature
|
| 158 |
+
# eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers.
|
| 159 |
+
# eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502
|
| 160 |
+
# and should be between [0, 1]
|
| 161 |
+
accepts_eta = "eta" in set(inspect.signature(self.scheduler.step).parameters.keys())
|
| 162 |
+
extra_step_kwargs = {}
|
| 163 |
+
if accepts_eta:
|
| 164 |
+
extra_step_kwargs["eta"] = eta
|
| 165 |
+
|
| 166 |
+
latents = init_latents
|
| 167 |
+
t_start = max(num_inference_steps - init_timestep + offset, 0)
|
| 168 |
+
for i, t in tqdm(enumerate(self.scheduler.timesteps[t_start:])):
|
| 169 |
+
# expand the latents if we are doing classifier free guidance
|
| 170 |
+
latent_model_input = torch.cat([latents] * 2) if do_classifier_free_guidance else latents
|
| 171 |
+
|
| 172 |
+
# predict the noise residual
|
| 173 |
+
noise_pred = self.unet(latent_model_input, t, encoder_hidden_states=text_embeddings)["sample"]
|
| 174 |
+
|
| 175 |
+
# perform guidance
|
| 176 |
+
if do_classifier_free_guidance:
|
| 177 |
+
noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
|
| 178 |
+
noise_pred = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
|
| 179 |
+
|
| 180 |
+
# compute the previous noisy sample x_t -> x_t-1
|
| 181 |
+
latents = self.scheduler.step(noise_pred, t, latents, **extra_step_kwargs)["prev_sample"]
|
| 182 |
+
|
| 183 |
+
#masking
|
| 184 |
+
init_latents_proper = self.scheduler.add_noise(init_latents_orig, noise, t)
|
| 185 |
+
latents = ( init_latents_proper * mask ) + ( latents * (1-mask) )
|
| 186 |
+
|
| 187 |
+
# scale and decode the image latents with vae
|
| 188 |
+
latents = 1 / 0.18215 * latents
|
| 189 |
+
image = self.vae.decode(latents)
|
| 190 |
+
|
| 191 |
+
image = (image / 2 + 0.5).clamp(0, 1)
|
| 192 |
+
image = image.cpu().permute(0, 2, 3, 1).numpy()
|
| 193 |
+
|
| 194 |
+
# run safety checker
|
| 195 |
+
safety_cheker_input = self.feature_extractor(self.numpy_to_pil(image), return_tensors="pt").to(self.device)
|
| 196 |
+
image, has_nsfw_concept = self.safety_checker(images=image, clip_input=safety_cheker_input.pixel_values)
|
| 197 |
+
|
| 198 |
+
if output_type == "pil":
|
| 199 |
+
image = self.numpy_to_pil(image)
|
| 200 |
+
|
| 201 |
+
return {"sample": image, "nsfw_content_detected": has_nsfw_concept}
|
| 202 |
+
|
| 203 |
+
device = "cuda"
|
| 204 |
+
model_path = "CompVis/stable-diffusion-v1-4"
|
| 205 |
+
|
| 206 |
+
pipe = StableDiffusionInpaintingPipeline.from_pretrained(
|
| 207 |
+
model_path,
|
| 208 |
+
revision="fp16",
|
| 209 |
+
torch_dtype=torch.float16,
|
| 210 |
+
use_auth_token=True
|
| 211 |
+
).to(device)
|
| 212 |
+
|
| 213 |
+
import gdown
|
| 214 |
+
def download_gdrive_url():
|
| 215 |
+
url = 'https://drive.google.com/u/0/uc?id=1PPO2MCttsmSqyB-vKh5C7SumwFKuhgyj&export=download'
|
| 216 |
+
output = 'haarcascade_frontalface_default.xml'
|
| 217 |
+
gdown.download(url, output, quiet=False)
|
| 218 |
+
|
| 219 |
+
from torch import autocast
|
| 220 |
+
def inpaint(p, init_image, mask_image=None, strength=0.75, guidance_scale=7.5, generator=None, num_samples=1, n_iter=1):
|
| 221 |
+
all_images = []
|
| 222 |
+
for _ in range(n_iter):
|
| 223 |
+
with autocast("cuda"):
|
| 224 |
+
images = pipe(
|
| 225 |
+
prompt=[p] * num_samples,
|
| 226 |
+
init_image=init_image,
|
| 227 |
+
mask_image=mask_image,
|
| 228 |
+
strength=strength,
|
| 229 |
+
guidance_scale=guidance_scale,
|
| 230 |
+
generator=generator,
|
| 231 |
+
num_inference_steps=75
|
| 232 |
+
)["sample"]
|
| 233 |
+
all_images.extend(images)
|
| 234 |
+
print(len(all_images))
|
| 235 |
+
return all_images[0]
|
| 236 |
+
|
| 237 |
+
def identify_face(user_image):
|
| 238 |
+
img = cv2.imread(user_image.name) # read the resized image in cv2
|
| 239 |
+
print(img.shape)
|
| 240 |
+
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # convert to grayscale
|
| 241 |
+
download_gdrive_url() #download the haarcascade face recognition stuff
|
| 242 |
+
haar_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
|
| 243 |
+
faces_rect = haar_cascade.detectMultiScale(gray_img, scaleFactor=1.1, minNeighbors=9)
|
| 244 |
+
for (x, y, w, h) in faces_rect[:1]:
|
| 245 |
+
mask = np.zeros(img.shape[:2], dtype="uint8")
|
| 246 |
+
print(mask.shape)
|
| 247 |
+
cv2.rectangle(mask, (x, y), (x+w, y+h), 255, -1)
|
| 248 |
+
print(mask.shape)
|
| 249 |
+
inverted_image = cv2.bitwise_not(mask)
|
| 250 |
+
return inverted_image
|
| 251 |
+
|
| 252 |
+
def sample_images(init_image, mask_image):
|
| 253 |
+
p = "4K UHD professional profile picture of a person wearing a suit for work"
|
| 254 |
+
strength=0.65
|
| 255 |
+
guidance_scale=10
|
| 256 |
+
num_samples = 1
|
| 257 |
+
n_iter = 1
|
| 258 |
+
|
| 259 |
+
generator = torch.Generator(device="cuda").manual_seed(random.randint(0, 1000000)) # change the seed to get different results
|
| 260 |
+
all_images = inpaint(p, init_image, mask_image, strength=strength, guidance_scale=guidance_scale, generator=generator, num_samples=num_samples, n_iter=n_iter)
|
| 261 |
+
return all_images
|
| 262 |
+
|
| 263 |
+
import gradio as gr
|
| 264 |
+
import random
|
| 265 |
+
# accept an image input
|
| 266 |
+
# trigger the set of functions to occur => identify face, generate mask, save the inverted face mask, sample for the inverted images
|
| 267 |
+
# output the sampled images
|
| 268 |
+
def main(user_image):
|
| 269 |
+
# accept the image as input
|
| 270 |
+
init_image = PIL.Image.open(user_image).convert("RGB")
|
| 271 |
+
# # resize the image to be (512, 512)
|
| 272 |
+
newsize = (512, 512)
|
| 273 |
+
init_image = init_image.resize(newsize)
|
| 274 |
+
init_image.save(user_image.name) # save the resized image
|
| 275 |
+
## identify the face + save the inverted mask
|
| 276 |
+
inverted_mask = identify_face(user_image)
|
| 277 |
+
fp = tempfile.NamedTemporaryFile(mode='wb', suffix=".png")
|
| 278 |
+
cv2.imwrite(fp.name, inverted_mask) # save the inverted image mask
|
| 279 |
+
pil_inverted_mask = PIL.Image.open(fp.name).convert("RGB")
|
| 280 |
+
print("type(init_image): ", type(init_image))
|
| 281 |
+
print("type(pil_inverted_mask): ", type(pil_inverted_mask))
|
| 282 |
+
# sample the new images
|
| 283 |
+
return sample_images(init_image, pil_inverted_mask)
|
| 284 |
+
|
| 285 |
+
demo = gr.Interface(main, gr.Image(type="file"), "image")
|
| 286 |
+
demo.launch(debug=True)
|