-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_attributes.py
98 lines (81 loc) · 2.4 KB
/
generate_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
import openai
from tqdm import tqdm
from tools import *
from cfg import *
dataset_name = 'caltech101'
task_name = {
'caltech101': 'object',
'dtd': 'texture',
'eurosat': 'remote sensing land cover',
'fgvc': 'aircraft model',
'food101': 'food',
'I': 'object',
'oxford_flowers': 'flower',
'oxford_pets': 'pet',
'resisc45': 'remote sensing scene',
'stanford_cars': 'fine-grained automobile',
'sun397': 'scene',
'ucf101': 'action'
}
openai.api_key = ""
json_name = 'attributes/gpt3/' + dataset_name + '_dist.json'
_, _, category_list, _ = build_loader(dataset_name, DOWNSTREAM_PATH)
all_responses = {}
vowel_list = ['A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u']
'''
Generating DistAttr
'''
for category in tqdm(category_list):
categoryname = category.replace('.', '')
if category[0] in vowel_list:
article = "an"
else:
article = "a"
instruct = []
instruct.append("Describe the unique appearance of " + article + " " + categoryname + " from the other " + task_name[dataset_name])
all_result = []
for curr_prompt in instruct:
response = openai.Completion.create(
engine="gpt-3.5-turbo-instruct",
prompt=curr_prompt,
temperature=.99,
max_tokens = 50,
n=25,
stop="."
)
for r in range(len(response["choices"])):
result = response["choices"][r]["text"]
if len(result) > 20:
all_result.append(result.replace("\n\n", "") + ".")
all_responses[category] = all_result
with open(json_name, 'w') as f:
json.dump(all_responses, f, indent=4)
'''
Generating DesAttr
'''
json_name = 'attributes/gpt3/' + dataset_name + '_des.json'
for category in tqdm(category_list):
categoryname = category.replace('.', '')
if category[0] in vowel_list:
article = "an"
else:
article = "a"
instruct = []
instruct.append("Describe the appearance of the " + task_name[dataset_name] + " " + categoryname)
all_result = []
for curr_prompt in instruct:
response = openai.Completion.create(
engine="gpt-3.5-turbo-instruct",
prompt=curr_prompt,
temperature=.99,
max_tokens = 50,
n=25,
stop="."
)
for r in range(len(response["choices"])):
result = response["choices"][r]["text"]
if len(result) > 20:
all_result.append(result.replace("\n\n", "") + ".")
all_responses[category] = all_result
with open(json_name, 'w') as f:
json.dump(all_responses, f, indent=4)