Views
No views yet
| Step | Grayscale Image (Masked) | Restored Grayscale Image | Fully Restored RGB Image |
|---|---|---|---|
| Image | ![]() | ![]() | ![]() |
1import torch
2import numpy as np
3
4from PIL import Image
5from diffusers.utils import load_image
6from transformers import AutoConfig, AutoModel, ModelCard
7
8img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png"
9mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png"
10
11image_gray = load_image(img_url).resize((512, 512)).convert('L').convert('RGB') # image must be 3 channel
12mask_image = load_image(mask_url).resize((512, 512))
13mask = (np.array(mask_image)>128)*1
14image_gray_masked = Image.fromarray(((1-mask) * np.array(image_gray)).astype(np.uint8))
15
16# Load the gray-inpaint model
17gray_inpaintor = AutoModel.from_pretrained(
18 'jwengr/stable-diffusion-2-gray-inpaint-to-rgb',
19 subfolder='gray-inpaint',
20 trust_remote_code=True,
21)
22
23# Load the gray2rgb model
24gray2rgb = AutoModel.from_pretrained(
25 'jwengr/stable-diffusion-2-gray-inpaint-to-rgb',
26 subfolder='gray2rgb',
27 trust_remote_code=True,
28)
29
30# Move models to GPU
31gray_inpaintor.to('cuda')
32gray2rgb.to('cuda')
33
34# Enable memory-efficient attention
35# gray2rgb.unet.enable_xformers_memory_efficient_attention()
36# gray_inpaintor.unet.enable_xformers_memory_efficient_attention()
37
38with torch.autocast('cuda',dtype=torch.bfloat16):
39 with torch.no_grad():
40 # each model's input image should be one of PIL.Image, List[PIL.Image], preprocessed tensor (B,3,H,W). Image must be 3 channel
41 image_gray_restored = gray_inpaintor(image_gray_masked, num_inference_steps=250, seed=10)[0].convert('L') # you can pass 'mask' arg explicitly. mask : Tensor (B,1,512,512)
42 image_restored = gray2rgb(image_gray_restored.convert('RGB'))