/
grounded_edit.py
335 lines (299 loc) · 15.5 KB
/
grounded_edit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import json
import omegaconf
import torch
import os
import argparse
import numpy as np
import yaml
# Grounding DINO
import GroundingDINO.groundingdino.datasets.transforms as T
from GroundingDINO.groundingdino.models import build_model
from GroundingDINO.groundingdino.util import box_ops
from GroundingDINO.groundingdino.util.slconfig import SLConfig
from GroundingDINO.groundingdino.util.utils import clean_state_dict, get_phrases_from_posmap
# stable diffusion
# from models.pipeline_stable_diffusion_inpaint import StableDiffusionInpaintPipeline
from diffusers import StableDiffusionInpaintPipeline, StableDiffusionInstructPix2PixPipeline, EulerAncestralDiscreteScheduler
# segment anything
from segment_anything import build_sam, SamPredictor
import cv2
import numpy as np
import matplotlib.pyplot as plt
# clip to filter ambuguite regions
import clip
# utils
from grounded_sam import load_image, load_model, show_box, generate_caption
import matplotlib.pyplot as plt
from PIL import Image, ImageDraw
from torchvision.ops import box_convert
from transformers import BlipProcessor, BlipForConditionalGeneration
# @torch.no_grad()
# def inpaint_img_with_lama_cleaner(
# img: np.ndarray,
# mask: np.ndarray,
# config_p: str,
# ckpt_p: str="./lama/configs/prediction/default.yaml",
# mod = 8
# ):
# assert len(mask.shape) == 2
# img = torch.from_numpy(img).float().div(255.)
# mask = torch.from_numpy(mask).float()
# predict_config = omegaconf.load(config_p)
# predict_config.model.path = ckpt_p
# device = torch.device(predict_config.device)
# train_config_path = os.path.join(
# predict_config.model.path, 'config.yaml')
# with open(train_config_path, 'r') as f:
# train_config = omegaconf.OmegaConf.create(yaml.safe_load(f))
# train_config.training_model.predict_only = True
# train_config.visualizer.kind = 'noop'
# checkpoint_path = os.path.join(
# predict_config.model.path, 'models',
# predict_config.model.checkpoint
# )
# model = load_checkpoint(
# train_config, checkpoint_path, strict=False, map_location='cpu')
# model.freeze()
# if not predict_config.get('refine', False):
# model.to(device)
# batch = {}
# batch['image'] = img.permute(2, 0, 1).unsqueeze(0)
# batch['mask'] = mask[None, None]
# unpad_to_size = [batch['image'].shape[2], batch['image'].shape[3]]
# batch['image'] = pad_tensor_to_modulo(batch['image'], mod)
# batch['mask'] = pad_tensor_to_modulo(batch['mask'], mod)
# batch = move_to_device(batch, device)
# batch['mask'] = (batch['mask'] > 0) * 1
# batch = model(batch)
# cur_res = batch[predict_config.out_key][0].permute(1, 2, 0)
# cur_res = cur_res.detach().cpu().numpy()
# if unpad_to_size is not None:
# orig_height, orig_width = unpad_to_size
# cur_res = cur_res[:orig_height, :orig_width]
# cur_res = np.clip(cur_res * 255, 0, 255).astype('uint8')
# return cur_res
# load open-world detection models
@torch.no_grad()
def get_grounding_output(model, image, caption, box_threshold, text_threshold, with_logits=True, device="cpu"):
caption = caption.lower()
caption = caption.strip()
if not caption.endswith("."):
caption = caption + "."
model = model.to(device)
image = image.to(device)
with torch.no_grad():
outputs = model(image[None], captions=[caption])
logits = outputs["pred_logits"].cpu().sigmoid()[0] # (num_query, 256)
boxes = outputs["pred_boxes"].cpu()[0] # (num_query, 4)
logits.shape[0]
# filter output box with > box_threshold (match with caption) Language-Guided Query Selection
logits_filt = logits.clone()
boxes_filt = boxes.clone()
filt_mask = logits_filt.max(dim=1)[0] > box_threshold
logits_filt = logits_filt[filt_mask] # num_filt, 256
boxes_filt = boxes_filt[filt_mask] # num_filt, 4
logits_filt.shape[0]
# get phrase
tokenlizer = model.tokenizer
tokenized = tokenlizer(caption)
# build pred
pred_phrases = []
scores = []
for logit, box in zip(logits_filt, boxes_filt):
# convert ids to token (filter stop-words in captions to get tokens)
pred_phrase = get_phrases_from_posmap(logit > text_threshold, tokenized, tokenlizer)
if with_logits:
pred_phrases.append(pred_phrase + f"({str(logit.max().item())[:4]})")
else:
pred_phrases.append(pred_phrase)
scores.append(logit.max().item())
return boxes_filt, pred_phrases, torch.Tensor(scores)
@torch.no_grad()
def mask_fusion(final_mask):
final_mask = np.sum(final_mask, axis=0)
mask_image = Image.fromarray(np.uint8(final_mask))
return mask_image
@torch.no_grad()
def generate_masks_with_grounding(image_pil, boxes):
mask = np.zeros_like(image_pil)
x0, y0, x1, y1 = boxes
mask[int(y0):int(y1), int(x0):int(x1), :] = 255
return mask
@torch.no_grad()
def retriev(elements, search_text):
preprocessed_images = [clip_preprocess(image).to(device) for image in elements]
tokenized_text = clip.tokenize([search_text]).to(device)
stacked_images = torch.stack(preprocessed_images)
image_features = clip_model.encode_image(stacked_images)
text_features = clip_model.encode_text(tokenized_text)
image_features /= image_features.norm(dim=-1, keepdim=True)
text_features /= text_features.norm(dim=-1, keepdim=True)
probs = 100. * image_features @ text_features.T
return probs[:, 0].softmax(dim=0)
# load sd image for image edit
MY_TOKEN = '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'
# # LOW_RESOURCE = False
# # NUM_DIFFUSION_STEPS = 50
# # GUIDANCE_SCALE = 7.5
# # MAX_NUM_WORDS = 77
device = torch.device('cuda:0') if torch.cuda.is_available() else torch.device('cpu')
ldm_stable_inpaint = StableDiffusionInpaintPipeline.from_pretrained("runwayml/stable-diffusion-inpainting", revision="fp16",
torch_dtype=torch.float16, use_auth_token=MY_TOKEN).to(device)
# stable_pix2pix = StableDiffusionInstructPix2PixPipeline.from_pretrained("timbrooks/instruct-pix2pix", torch_dtype=torch.float16, safety_checker=None).to(device)
# stable_pix2pix.scheduler = EulerAncestralDiscreteScheduler.from_config(stable_pix2pix.scheduler.config)
# #image and mask_image should be PIL images.
# #The mask structure is white for inpainting and black for keeping as is
# image = ldm_stable_inpaint(prompt=prompt, image=image, mask_image=mask_image).images[0]
# tokenizer = ldm_stable.tokenizer
if __name__ == "__main__":
parser = argparse.ArgumentParser("Grounded-Segment-Anything Demo", add_help=True)
parser.add_argument("--config", type=str, required=True, help="path to config file")
parser.add_argument(
"--grounded_checkpoint", type=str, required=True, help="path to checkpoint file"
)
parser.add_argument(
"--sam_checkpoint", type=str, required=True, help="path to checkpoint file"
)
parser.add_argument("--input_image", type=str, required=True, help="path to image file")
parser.add_argument("--text_prompt", type=str, required=True, help="text prompt")
parser.add_argument("--reverse", type=bool, default=False, required=False, help="whether reverse mask")
parser.add_argument("--edit_prompt", type=str, required=True, help="edit prompt")
parser.add_argument(
"--output_dir", "-o", type=str, default="outputs", required=True, help="output directory"
)
parser.add_argument("--box_threshold", type=float, default=0.3, help="box threshold")
parser.add_argument("--text_threshold", type=float, default=0.25, help="text threshold")
parser.add_argument("--device", type=str, default="cpu", help="running on cpu only!, default=False")
args = parser.parse_args()
# cfg
config_file = args.config # change the path of the model config file
grounded_checkpoint = args.grounded_checkpoint # change the path of the model
sam_checkpoint = args.sam_checkpoint
image_path = args.input_image
text_prompt = args.text_prompt
reverse = args.reverse
edit_prompt = args.edit_prompt
output_dir = args.output_dir
box_threshold = args.box_threshold
text_threshold = args.box_threshold
device = args.device
# make dir
os.makedirs(output_dir, exist_ok=True)
# load image and process with PIL
image_pil, image = load_image(image_path)
# load image for visualization
# image = cv2.imread(image_path)
# image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# load model
model = load_model(config_file, grounded_checkpoint, device=device)
# Load CLIP
clip_model, clip_preprocess = clip.load("ViT-B/32", device=device)
# run grounding dino model
total_boxes = []
total_predphrases = []
total_scores = []
boxes_filt, pred_phrases, pred_scores = get_grounding_output(model, image, text_prompt, box_threshold, text_threshold, device=device)
for valid_box, valid_phrase, valid_score in zip(boxes_filt, pred_phrases, pred_scores):
total_boxes.append(valid_box)
total_predphrases.append(valid_phrase)
total_scores.append(valid_score)
for i in range(len(total_boxes)):
size = image_pil.size
H, W = size[1], size[0]
boxes_filt = total_boxes[i] * torch.Tensor([W, H, W, H])
boxes_filt[:2] -= boxes_filt[2:] / 2
boxes_filt[2:] += boxes_filt[:2]
boxes_filt = boxes_filt.cpu()
total_boxes[i] = boxes_filt
# judge which to edit (objects or background)
# test for imagin
# scene_image = {"size": [(2048, 1365)], "objects": [{"value": 1, "label": "dog", "logit": 0.48, "box": [550.0, 600.0, 1050.0, 950.0]}, {"value": 2, "label": "cat", "logit": 0.45, "box": [1100.0, 600.0, 1600.0, 950.0]}, {"value": 3, "label": "grass", "logit": 0.80, "box": [0.0, 1000.0, 2048.0, 1365.0]}, {"value": 4, "label": "sky", "logit": 0.85, "box": [0.0, 0.0, 2048.0, 500.0]}]}
# if len(total_boxes) > 0:
# # print(image_pil.size)
# # initial_canvas = np.zeros(scene_image["size"][0]).T
# # np.expand_dims(initial_canvas, 2)
# # image_pil = Image.fromarray(np.uint8(initial_canvas))
# # print(image_pil.size)
# scene_image["objects"] = sorted(scene_image["objects"], key=lambda x: x["logit"], reverse=True)
# for object in scene_image["objects"]:
# image_mask = generate_masks_with_grounding(image_pil, np.array(object["box"]))
# phrase = object["label"]
# logit = object["logit"]
# mask_image = Image.fromarray(image_mask)
# mask_image.save(os.path.join(output_dir, f'mask_{phrase}_{logit}.jpg'))
# image_source_for_inpaint = image_pil.resize((512, 512))
# image_mask_for_inpaint = mask_image.resize((512, 512))
# image_inpainting = ldm_stable_inpaint(prompt=object["label"], image=image_source_for_inpaint, mask_image=image_mask_for_inpaint).images[0]
# image_inpainting = image_inpainting.resize((image_pil.size[0], image_pil.size[1]))
# image_inpainting.save(os.path.join(output_dir, f'edit_result_{phrase}_{logit}.jpg'))
# image_pil = image_inpainting.copy()
# ccc
# objects mask with bounding box
# if len(total_boxes) > 0:
# # Cut out all masks and select the most correct bounding box with clip
# cropped_boxes = []
# for box, pred_phrase in zip(total_boxes, total_predphrases):
# cropped_boxes.append(image_pil.crop(box.tolist()))
# idx = torch.argmax(retriev(cropped_boxes, text_prompt)).item()
# box = total_boxes[idx]
# image_mask = generate_masks_with_grounding(image_pil, box.numpy())
# mask_image = Image.fromarray(image_mask)
# phrase, logit = pred_phrase.split('(')
# logit = logit[:-1]
# mask_image.save(os.path.join(output_dir, f'mask_{phrase}_{logit}.jpg'))
# image_source_for_inpaint = image_pil.resize((512, 512))
# image_mask_for_inpaint = mask_image.resize((512, 512))
# image_inpainting = ldm_stable_inpaint(prompt=edit_prompt, image=image_source_for_inpaint, mask_image=image_mask_for_inpaint).images[0]
# image_inpainting = image_inpainting.resize((image_pil.size[0], image_pil.size[1]))
# image_inpainting.save(os.path.join(output_dir, f'edit_result_{phrase}_{logit}.jpg'))
# background edit with segmentation mask
predictor = SamPredictor(build_sam(checkpoint=sam_checkpoint))
image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
predictor.set_image(image)
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large", torch_dtype=torch.float16).to("cuda")
caption = generate_caption(image_pil, processor, blip_model)
print(caption)
edit_prompts_background = ['river', 'Open field', 'Sunny meadow', 'Backyard', 'Nature reserve', 'Beach', 'Forest', 'City park', 'Hilltop', 'Countryside', 'Pasture']
edit_prompts_object = ['Meadow flowers', 'Birds', 'Insects', 'Fence', 'Lake', 'Sand dunes', 'Buildings', 'Playground equipment', 'Hiking trail', 'Picnic table']
if len(total_boxes) > 0:
idx = torch.argmax(torch.stack(total_scores)).item()
boxes_filt = total_boxes[idx]
pred_phrase = total_predphrases[idx]
transformed_boxes = predictor.transform.apply_boxes_torch(boxes_filt, image.shape[:2])
masks, _, _ = predictor.predict_torch(
point_coords = None,
point_labels = None,
boxes = transformed_boxes,
multimask_output = False,
)
if reverse:
# need to reverse the mask to modift the background
mask_image = Image.fromarray(~masks[0][0].cpu().numpy())
else:
mask_image = Image.fromarray(masks[0][0].cpu().numpy())
phrase, logit = pred_phrase.split('(')
logit = logit[:-1]
mask_image.save(os.path.join(output_dir, f'mask_{phrase}_{logit}_nonreverse.jpg'))
image_source_for_inpaint = image_pil.resize((512, 512))
image_mask_for_inpaint = mask_image.resize((512, 512))
image_inpainting = ldm_stable_inpaint(prompt=edit_prompt, image=image_source_for_inpaint, mask_image=image_mask_for_inpaint).images[0]
image_inpainting = image_inpainting.resize((image_pil.size[0], image_pil.size[1]))
image_inpainting.save(os.path.join(output_dir, f'edit_result_{phrase}_{logit}_nonreverse.jpg'))
# instructPix2Pix for inpainting
# for edit_prompt in edit_prompts_object:
# image_inpainting_instruct = stable_pix2pix("change the background into "+edit_prompt, image=image_source_for_inpaint).images[0]
# image_inpainting_instruct = stable_pix2pix("add "+edit_prompt+" into suitable position in the the image", image=image_source_for_inpaint).images[0]
# lama-cleaner for removal objects
# image_inpainting_instruct = image_inpainting_instruct.resize((image_pil.size[0], image_pil.size[1]))
# image_inpainting_instruct.save(os.path.join(output_dir, f'edit_result_{phrase}_{logit}_{edit_prompt}.jpg'))
# draw output image
plt.figure(figsize=(10, 10))
plt.imshow(image_pil)
# non-ambiguity annotation
for boxes_filt, pred_phrases in zip(total_boxes, total_predphrases):
# for valid_box, valid_phrase in zip(boxes_filt, pred_phrases):
show_box(boxes_filt.numpy(), plt.gca(), pred_phrases, random_color=True)
plt.axis('off')
plt.savefig(os.path.join(output_dir, "test.jpg"), bbox_inches="tight")