-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter_attributes.py
227 lines (171 loc) · 7.54 KB
/
filter_attributes.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
from collections import defaultdict
import fire
import os.path as osp
from pprint import pprint
from PIL import Image
from tqdm import tqdm
from copy import deepcopy
from fadata.utils import load_json, save_json
from refine_prompts import *
from check_prompts import *
from utils import *
filter_task_instruction = """
You task is to select the attribute tags can be used to describe the object in the real world.
Each time the user will give an object name and a attribute dimenion and all attributes tags of the dimension.
Please choose from the labels provided and do not make up your own tag.
"""
filter_question_template = "object name: {object_label}, dimension: {dim}, tags: {tags}"
filter_few_shot_examples = [
{
"user": "object name: tv, dimension: cleanliness, tags: clean, neat, unclean, dirt, dirty, muddy",
"assistant": "clean, neat, unclean, dirt, dirty, muddy"
},
{
"user": "object name: bus, dimension: material, tags: asphalt, cement, clay, concrete, stucco, ceramic, brick, porcelain, glass, leather, metal, metallic, aluminum, brass, copper-zinc, iron, stainless steel, steel, silver, paper, cardboard, polymers, plastic, rubber, styrofoam, polymer, stone, granite, cobblestone, gravel, marble, pebbled, rocky, sandy, textile, cloth, fabric, denim, cotton, jean, silk, plush, wood, wooden, bamboo, hardwood",
"assistant": "iron, copper-zinc, steel, silver, aluminum, stainless steel, metallic, metal, brass"
}
]
def filter_parser(result):
if "null" not in result:
return result.split(', ')
else:
return "null"
TYPE2DIM = {
'cleanliness': 'cleanliness',
'color': 'color',
'face expression': 'face expression',
'gender': 'gender',
'hair type': 'hair type',
'length': 'length',
'material': 'material',
'maturity': 'maturity',
'pattern': 'pattern',
'position': 'pose',
'size': 'size',
'state': 'state',
'texture': 'texture',
'optical property': 'transparency',
}
class AttributeTagFilter:
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}",
],
"tag": ["{attr}"]
},
"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}",
],
"tag": ["{attr}"]
}
}
def __init__(self, attributes_data) -> None:
self.attributes_data = attributes_data
def build_template(self, object_word: str, dim: str, prompt_att: str = 'tag'):
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 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 main(
llama_model_dir: str,
f_question: str,
f_result: str,
f_ovad_anno: str = None,
):
question_items = load_json(f_question)
obj_dim_attr_comb = defaultdict(dict)
for _item in question_items:
for attribute_label in _item['attribute_label']:
attribute_dim, labels = attribute_label.split(':')
labels = labels.split('/')
if attribute_dim in obj_dim_attr_comb[_item['object_label']]:
obj_dim_attr_comb[_item['object_label']][attribute_dim].extend(labels)
else:
obj_dim_attr_comb[_item['object_label']][attribute_dim] = labels
obj_dim_attr_comb[_item['object_label']][attribute_dim] = list(set(obj_dim_attr_comb[_item['object_label']][attribute_dim]))
obj_dim_attr_comb = dict(obj_dim_attr_comb)
save_json("gt_obj_dim_attr_comb.json", obj_dim_attr_comb, indent=4)
exit()
object_labels = set([_item['object_label'] for _item in question_items])
dims = list(DIMS.keys())
dim2type = {v:k for k, v in TYPE2DIM.items()}
tag_filter = AttributeTagFilter(load_json(f_ovad_anno)["attributes"])
pipeline = build_language_model(llama_model_dir)
result_data = defaultdict(dict)
# from IPython import embed; embed()
for object_label in tqdm(object_labels):
for dim in dims:
if dim not in obj_dim_attr_comb[object_label].keys():
continue
filter_messages = [
{"role": "system", "content": filter_task_instruction}
]
for example in filter_few_shot_examples:
filter_messages.extend(
[
{"role": "user", "content": example['user']},
{"role": "assistant", "content": example['assistant']}
]
)
dim_tags = tag_filter.build_template(object_label, dim=dim2type[dim])
flatten_dim_tags = []
for tags in dim_tags:
flatten_dim_tags.extend(tags)
dim_tags = flatten_dim_tags
question = filter_question_template.format(object_label=object_label, dim=dim, tags=", ".join(dim_tags))
filter_messages.append({"role": "user", "content": question})
filter_result = generation(filter_messages, pipeline)
valid_tags = filter_parser(filter_result)
result_data[object_label][dim] = valid_tags
print(question)
print(valid_tags)
# from IPython import embed; embed()
save_json("obj_attr_combinations.json", dict(result_data), indent=4)
if __name__ == "__main__":
fire.Fire(main)