-
Notifications
You must be signed in to change notification settings - Fork 0
/
description_refiner.py
420 lines (314 loc) · 14.8 KB
/
description_refiner.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import fire
import random
import torch
import transformers
import os.path as osp
from PIL import Image
from tqdm import tqdm
from copy import deepcopy
from transformers import BlipProcessor, BlipForImageTextRetrieval
from fadata.utils import load_json, save_json, xywh_to_xyxy
from refine_prompts import *
from check_prompts import *
from utils import *
class BLIPScore:
def __init__(
self,
blip_model_path,
device=torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu"),
torch_dtype=torch.float16
) -> None:
self.device = device
self.torch_dtype = torch_dtype
self.processor = BlipProcessor.from_pretrained(blip_model_path)
self.model = BlipForImageTextRetrieval.from_pretrained(blip_model_path, torch_dtype=torch_dtype).to(device)
def __call__(self, images, texts, head='itm'):
inputs = self.processor(images, texts, return_tensors="pt", truncation=True, padding=True).to(self.device, self.torch_dtype)
with torch.no_grad():
if head == 'itm':
itm_scores = self.model(**inputs)[0].softmax(dim=-1)[:, 1]
return itm_scores.cpu()
else:
cosine_scores = self.model(**inputs, use_itm_head=False)[0]
return cosine_scores.cpu()
class AttributeClassifier:
templates_dict = {
"has": {
"none": ["{attr} {dobj} {noun}"],
"a": ["a {attr} {dobj} {noun}", "a {noun} has {attr} {dobj}"],
"the": ["the {attr} {dobj} {noun}", "the {noun} has {attr} {dobj}"],
"photo": [
"a photo of a {attr} {dobj} {noun}",
"a photo of an {noun} which has {attr} {dobj}",
"a photo of the {attr} {dobj} {noun}",
"a photo of the {noun} which has {attr} {dobj}",
],
},
"is": {
"none": ["{attr} {noun}"],
"a": ["a {attr} {noun}", "a {noun} is {attr}"],
"the": ["the {attr} {noun}", "the {noun} is {attr}"],
"photo": [
"a photo of a {attr} {noun}",
"a photo of a {noun} which is {attr}",
"a photo of the {attr} {noun}",
"a photo of the {noun} which is {attr}",
],
}
}
def __init__(self, score_fn, attributes_data, batch_size, obj_dim_attr_comb=None) -> None:
self.score_fn = score_fn
self.attributes_data = attributes_data
self.obj_dim_attr_comb = obj_dim_attr_comb
self.batch_size = batch_size
def build_template(self, object_word: str, dim: str, prompt_att: str = 'a'):
if prompt_att in self.templates_dict["is"].keys():
use_prompts = [prompt_att]
else: # use all prompts
use_prompts = ["a", "the", "none"]
all_att_templates = []
for att_dict in self.attributes_data:
att_w_type = att_dict["name"]
att_type, att_list = att_w_type.split(":")
if self.obj_dim_attr_comb is not None:
att_list = att_list.split("/")
tp = TYPE2DIM[dim]
comb = self.obj_dim_attr_comb[object_word][tp]
att_list = "/".join([att for att in att_list if att in comb])
if att_list == "":
continue
if att_type != dim:
continue
assert att_type == att_dict["type"]
is_has = att_dict["is_has_att"]
dobj_name = (
att_type.replace(" tone", "")
)
# extend the maturity to include other words
if att_list == "young/baby":
att_list += "/kid/kids/child/toddler/boy/girl"
elif att_list == "adult/old/aged":
att_list += "/teen/elder"
att_templates = []
for syn in att_list.split("/"):
for prompt in use_prompts:
for template in self.templates_dict[is_has][prompt]:
if is_has == "has":
att_templates.append(
template.format(
attr=syn, dobj=dobj_name, noun=object_word
).strip()
)
elif is_has == "is":
att_templates.append(
template.format(attr=syn, noun=object_word).strip()
)
all_att_templates.append(att_templates)
return all_att_templates
def __call__(self, image: Image.Image, object_label: str, dims: list):
complete_phrases = []
complete_confidences = []
dim2type = {v:k for k, v in TYPE2DIM.items()}
for dim in dims:
dim_templates = self.build_template(object_word=object_label, dim=dim2type[dim])
templates_texts = []
for temps in dim_templates:
templates_texts.extend(temps)
dim_scores = []
for batch_index in range(0, len(templates_texts), self.batch_size):
batch_texts = templates_texts[batch_index: batch_index + self.batch_size]
scores = self.score_fn([image] * len(batch_texts), batch_texts)
dim_scores.append(scores)
if len(dim_scores) != 0:
dim_scores = torch.cat(dim_scores)
max_value = torch.max(dim_scores).item()
max_index = torch.argmax(dim_scores).item()
max_score_words = templates_texts[max_index]
complete_confidences.append(max_value)
complete_phrases.append(max_score_words)
return complete_phrases, complete_confidences
def main(
llama_model_dir: str,
blip_model_dir: str,
image_root: str,
f_description: str,
f_result: str,
f_check_result: str = None,
f_ovad_anno: str = None,
complete: bool = False,
refine_control: bool = False,
complete_threshold: float = 0.5,
batch_size: int = 4,
n_shot: int = 4,
g_threshold: float = 0,
l_threshold: float = 0.1,
save_steps: int = 1,
f_obj_dim_attr_comb: str = None,
seed: int = 0,
f_update_refined_description: str = None
):
print(f"setting random seed to {seed}")
setup_seed(seed)
print(f"refine_control: {refine_control}")
print(f"complete: {complete}")
score_fn = BLIPScore(blip_model_dir)
if f_obj_dim_attr_comb is not None:
obj_dim_attr_comb = load_json(f_obj_dim_attr_comb)
else:
obj_dim_attr_comb = None
if complete:
completer = AttributeClassifier(score_fn, load_json(f_ovad_anno)["attributes"], batch_size, obj_dim_attr_comb=obj_dim_attr_comb)
pipeline = build_language_model(llama_model_dir)
if f_check_result is not None:
check_data = load_json(f_check_result)['check_items']
else:
check_data = None
if f_update_refined_description is not None:
data = load_json(f_update_refined_description)
else:
data = load_json(f_description)
result_data = []
for i, _item in enumerate(tqdm(data)):
region_bbox = _item['region_anno']['bbox']
xxyy_region_bbox = xywh_to_xyxy(deepcopy(region_bbox))
image_path = osp.join(image_root, _item['image'])
image = Image.open(image_path).convert("RGB")
cropped_image = image.copy()
cropped_image = cropped_image.crop(xxyy_region_bbox)
object_label = _item["object_label"]
all_dims = _item["dims"].split(', ')
if refine_control:
control_dims = _item["control_dims"].split(', ')
else:
control_dims = []
""" Check_dimension
"""
if check_data is not None:
check_dimensions = check_data[i]['check_dimensions']
check_tuples = check_data[i]['check_tuples']
else:
check_messages = [
{"role": "system", "content": check_task_instruction}
]
for example in check_few_shot_examples:
check_messages.extend(
[
{"role": "user", "content": example['user']},
{"role": "assistant", "content": example['assistant']}
]
)
check_question = check_question_template.format(description = _item['answer'], object_label = _item['object_label'])
check_messages.append({"role": "user", "content": check_question})
check_result = generation(check_messages, pipeline)
check_dimensions, check_tuples = check_parser(check_result)
if f_update_refined_description is not None:
if _item['edit_history'][-1]['edit_item'] == 'complete':
edit_history = _item['edit_history'][:-1]
else:
edit_history = _item['edit_history']
erased_dims = [eh['tuple'].split(' (')[0] for eh in edit_history if eh['edit_item'] == 'erase']
cur_desp = edit_history[-1]['desp']
else:
erased_dims = []
edit_history = []
cur_desp = _item["answer"]
if len(check_dimensions) != 0:
global_score = score_fn([image], [cur_desp]).item()
local_score = score_fn([cropped_image], [cur_desp]).item()
if global_score > 0.9 and local_score > 0.9:
continue
edit_history = [{
"edit_item": "no_edit",
"l_score": local_score,
"g_score": global_score,
"desp": cur_desp,
"tuple": ""
}]
for (edim, etp) in zip(check_dimensions, check_tuples):
erase_messages = [
{"role": "system", "content": erase_task_instruction}
]
for example in random.sample(erase_few_shot_examples, n_shot):
erase_messages.extend(
[
{"role": "user", "content": example['user']},
{"role": "assistant", "content": example['assistant']}
]
)
question = erase_question_template.format(dimension=f"{edim} {etp}", description=cur_desp)
erase_messages.append({"role": "user", "content": question})
edit_desp = generation(erase_messages, pipeline)
e_global_score = score_fn([image], [edit_desp]).item()
e_local_score = score_fn([cropped_image], [edit_desp]).item()
if refine_control and edim not in control_dims:
cur_desp = edit_desp
global_score = e_global_score
local_score = e_local_score
edit_history.append({
"edit_item": "erase",
"l_score": local_score,
"g_score": global_score,
"desp": cur_desp,
"tuple": f"{edim} {etp}"
})
erased_dims.append(edim)
continue
if ((e_global_score - global_score) > g_threshold) and ((e_local_score - local_score) > l_threshold):
cur_desp = edit_desp
global_score = e_global_score
local_score = e_local_score
edit_history.append({
"edit_item": "erase",
"l_score": local_score,
"g_score": global_score,
"desp": cur_desp,
"tuple": f"{edim} {etp}"
})
erased_dims.append(edim)
else:
continue
""" Complete
"""
if complete:
if refine_control:
erased_control_dims = set(control_dims).difference(set(erased_dims))
complete_dims = list(set(control_dims).difference(check_dimensions).union(erased_control_dims))
else:
if f_obj_dim_attr_comb is None:
complete_dims = list(set(all_dims).difference(check_dimensions).union(set(erased_dims)))
else:
complete_dims = list(set(all_dims).difference(check_dimensions).union(set(erased_dims).intersection(all_dims)))
complete_phrases, confidences = completer(cropped_image, object_label=object_label, dims=complete_dims)
filtered_complete_phrases = []
filtered_confidences = []
for cdim, phrase, conf in zip(complete_dims, complete_phrases, confidences):
if conf > complete_threshold:
filtered_complete_phrases.append(f"{cdim}: {phrase}")
filtered_confidences.append(conf)
if len(filtered_complete_phrases) > 0:
complete_messages = [
{"role": "system", "content": complete_task_instruction}
]
question = complete_question_template.format(description=cur_desp, phrases=" \n".join(filtered_complete_phrases))
complete_messages.append({"role": "user", "content": question})
complete_desp = complete_parser(generation(complete_messages, pipeline))
global_score = score_fn([image], [complete_desp]).item()
local_score = score_fn([cropped_image], [complete_desp]).item()
edit_history.append({
"edit_item": "complete",
"l_score": local_score,
"g_score": global_score,
"desp": complete_desp,
"complete_phrases": filtered_complete_phrases,
"confidences": filtered_confidences,
})
if len(edit_history) > 1:
copy_item = deepcopy(_item)
copy_item["edit_history"] = edit_history
result_data.append(copy_item)
if len(result_data) % save_steps == 0 and len(result_data) > 0:
print(f"Saving intermediate result to {f_result}")
save_json(f_result, result_data, indent=4)
save_json(f_result, result_data, indent=4)
if __name__ == "__main__":
fire.Fire(main)