Spaces:
Paused
Paused
Create ref_in.py
Browse files- tasks/ref_in.py +77 -0
tasks/ref_in.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# --------------------------------------------------------
|
2 |
+
# X-Decoder -- Generalized Decoding for Pixel, Image, and Language
|
3 |
+
# Copyright (c) 2022 Microsoft
|
4 |
+
# Licensed under The MIT License [see LICENSE for details]
|
5 |
+
# Written by Jianwei Yang ([email protected]), Xueyan Zou ([email protected])
|
6 |
+
# --------------------------------------------------------
|
7 |
+
|
8 |
+
import torch
|
9 |
+
import numpy as np
|
10 |
+
from PIL import Image
|
11 |
+
from utils.inpainting import pad_image
|
12 |
+
from torchvision import transforms
|
13 |
+
from utils.visualizer import Visualizer
|
14 |
+
from diffusers import StableDiffusionInpaintPipeline
|
15 |
+
from detectron2.utils.colormap import random_color
|
16 |
+
from detectron2.data import MetadataCatalog
|
17 |
+
from scipy import ndimage
|
18 |
+
|
19 |
+
|
20 |
+
t = []
|
21 |
+
t.append(transforms.Resize(512, interpolation=Image.BICUBIC))
|
22 |
+
transform = transforms.Compose(t)
|
23 |
+
metadata = MetadataCatalog.get('ade20k_panoptic_train')
|
24 |
+
|
25 |
+
pipe = StableDiffusionInpaintPipeline.from_pretrained(
|
26 |
+
# "stabilityai/stable-diffusion-2-inpainting",
|
27 |
+
"runwayml/stable-diffusion-inpainting",
|
28 |
+
revision="fp16",
|
29 |
+
torch_dtype=torch.float16,
|
30 |
+
).to("cuda")
|
31 |
+
|
32 |
+
def crop_image(input_image):
|
33 |
+
crop_w, crop_h = np.floor(np.array(input_image.size) / 64).astype(int) * 64
|
34 |
+
im_cropped = Image.fromarray(np.array(input_image)[:crop_h, :crop_w])
|
35 |
+
return im_cropped
|
36 |
+
|
37 |
+
def referring_inpainting(model, image, texts, inpainting_text, *args, **kwargs):
|
38 |
+
model.model.metadata = metadata
|
39 |
+
texts = [[texts if texts.strip().endswith('.') else (texts.strip() + '.')]]
|
40 |
+
image_ori = crop_image(transform(image))
|
41 |
+
|
42 |
+
with torch.no_grad():
|
43 |
+
width = image_ori.size[0]
|
44 |
+
height = image_ori.size[1]
|
45 |
+
image = np.asarray(image_ori)
|
46 |
+
image_ori_np = np.asarray(image_ori)
|
47 |
+
images = torch.from_numpy(image.copy()).permute(2,0,1).cuda()
|
48 |
+
|
49 |
+
batch_inputs = [{'image': images, 'height': height, 'width': width, 'groundings': {'texts': texts}}]
|
50 |
+
outputs = model.model.evaluate_grounding(batch_inputs, None)
|
51 |
+
visual = Visualizer(image_ori_np, metadata=metadata)
|
52 |
+
|
53 |
+
grd_mask = (outputs[0]['grounding_mask'] > 0).float().cpu().numpy()
|
54 |
+
for idx, mask in enumerate(grd_mask):
|
55 |
+
color = random_color(rgb=True, maximum=1).astype(np.int32).tolist()
|
56 |
+
demo = visual.draw_binary_mask(mask, color=color, text=texts[idx])
|
57 |
+
res = demo.get_image()
|
58 |
+
|
59 |
+
if inpainting_text not in ['no', '']:
|
60 |
+
# if we want to do inpainting
|
61 |
+
image_crop = image_ori
|
62 |
+
struct2 = ndimage.generate_binary_structure(2, 2)
|
63 |
+
mask_dilated = ndimage.binary_dilation(grd_mask[0], structure=struct2, iterations=3).astype(grd_mask[0].dtype)
|
64 |
+
mask = Image.fromarray(mask_dilated * 255).convert('RGB')
|
65 |
+
image_and_mask = {
|
66 |
+
"image": image_crop,
|
67 |
+
"mask": mask,
|
68 |
+
}
|
69 |
+
width = image_crop.size[0]; height = image_crop.size[1]
|
70 |
+
images_inpainting = pipe(prompt = inpainting_text.strip(), image=image_and_mask['image'], mask_image=image_and_mask['mask'], height=height, width=width).images[0]
|
71 |
+
# put images_inpainting back to original image
|
72 |
+
# image_ori.paste(images_inpainting)
|
73 |
+
torch.cuda.empty_cache()
|
74 |
+
return Image.fromarray(res) ,'' , images_inpainting
|
75 |
+
else:
|
76 |
+
torch.cuda.empty_cache()
|
77 |
+
return image_ori, 'text', Image.fromarray(res)
|