-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_gpt4v.py
197 lines (166 loc) · 7.23 KB
/
test_gpt4v.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
import yaml
from utils import *
from iqa_models.gpt4v import gpt4v_single_nr_batch
from iqa_models.gpt4v import gpt4v_single_fr_batch
from iqa_models.gpt4v import gpt4v_double_fr_batch
from iqa_models.gpt4v import gpt4v_multiple_fr_batch
from iqa_models.gpt4v import gpt4v_double_nr_batch
from iqa_models.gpt4v import gpt4v_multiple_nr_batch
from prompts.gpt4v_prompt import get_prompt
from data.helpers_sample import parse_sampled_datasets
DATASET_NAME_IQA_TYPE = {
"FR_KADID": "FR", "AUG_KADID": "FR", "TQD": "FR", "SPCD": "FR",
"NR_KADID": "NR", "SPAQ": "NR", "AGIQA3K": "NR"
}
DATASET_MEAN_METRIC = {
"FR_KADID": True, "AUG_KADID": True, "TQD": True, "SPCD": False,
"NR_KADID": False, "SPAQ": False, "AGIQA3K": False
}
def read_yaml(path):
with open(path, 'r') as file:
data = file.read()
result = yaml.load(data, Loader=yaml.FullLoader)
return result
class GPT4VTester():
def __init__(self,
nr_image_pair_num, nr_image_group_num, fr_image_pair_num, fr_image_group_num):
setting_dict = read_yaml('./settings.yaml')
self.key = setting_dict["KEY"]
self.dataset_name = setting_dict["DATASET_NAME"]
self.dis_data_path = setting_dict["DIS_DATA_PATH"]
self.ref_data_path = setting_dict["REF_DATA_PATH"]
self.psy_pattern = setting_dict["PSYCHOPHYSICAL_PATTERN"]
self.nlp_pattern = setting_dict["NLP_PATTERN"]
self.iqa_type = DATASET_NAME_IQA_TYPE[self.dataset_name]
self.mean_metric = DATASET_MEAN_METRIC[self.dataset_name]
if self.nlp_pattern == "ic":
self.ic_path = setting_dict["IC_PATH"]
else:
self.ic_path = []
self.nr_image_pair_num = nr_image_pair_num
self.nr_image_group_num = nr_image_group_num
self.fr_image_pair_num = fr_image_pair_num
self.fr_image_group_num = fr_image_group_num
self.text_prompt = get_prompt(
psy_pattern=self.psy_pattern, nlp_pattern=self.nlp_pattern, iqa_type=self.iqa_type
)
# For saving GPT-4V outputs
self.save_folder_path = "./Results/GPT-4V_{}/".format(self.dataset_name)
if not os.path.exists(self.save_folder_path):
os.makedirs(self.save_folder_path)
self.save_file_name = "GPT-4V_{}_{}_{}.txt".format(
self.dataset_name, self.iqa_type, self.psy_pattern + "-" + self.nlp_pattern)
self.define_dataset()
def define_dataset(self,):
self.image_path_list, self.image_name_list, self.mos_list = parse_sampled_datasets(
dataset_name=self.dataset_name,
iqa_type=self.iqa_type,
dis_data_path=self.dis_data_path,
ref_data_path=self.ref_data_path
)
print("Image num {}".format(len(self.image_path_list)))
print("score num {}".format(len(self.mos_list)))
def testing_gpt4v(self, ):
if self.psy_pattern == "single" and self.iqa_type == "NR":
self.test_nr_single_stimulus()
elif self.psy_pattern == "double" and self.iqa_type == "NR":
self.test_nr_double_stimulus()
elif self.psy_pattern == "multiple" and self.iqa_type == "NR":
self.test_nr_multiple_stimulus()
elif self.psy_pattern == "single" and self.iqa_type == "FR":
self.test_fr_single_stimulus()
elif self.psy_pattern == "double" and self.iqa_type == "FR":
self.test_fr_double_stimulus()
elif self.psy_pattern == "multiple" and self.iqa_type == "FR":
self.test_fr_multiple_stimulus()
def test_nr_single_stimulus(self, ):
gpt4v_single_nr_batch(
key=self.key,
dataset_name=self.dataset_name,
image_path_list=self.image_path_list,
image_name_list=self.image_name_list,
mos_list=self.mos_list,
text_prompt=self.text_prompt,
save_folder_path=self.save_folder_path,
save_file_name=self.save_file_name,
prompt_pattern_str=self.psy_pattern + "-" + self.nlp_pattern,
ic_path=self.ic_path
)
def test_nr_double_stimulus(self, ):
gpt4v_double_nr_batch(
key=self.key,
dataset_name=self.dataset_name,
image_path_list=self.image_path_list,
image_name_list=self.image_name_list,
mos_list=self.mos_list,
text_prompt=self.text_prompt,
save_folder_path=self.save_folder_path,
save_file_name=self.save_file_name,
nr_image_pair_num=self.nr_image_pair_num,
prompt_pattern_str=self.psy_pattern + "-" + self.nlp_pattern,
ic_path=self.ic_path
)
def test_nr_multiple_stimulus(self, ):
gpt4v_multiple_nr_batch(
key=self.key,
dataset_name=self.dataset_name,
image_path_list=self.image_path_list,
image_name_list=self.image_name_list,
mos_list=self.mos_list,
text_prompt=self.text_prompt,
save_folder_path=self.save_folder_path,
save_file_name=self.save_file_name,
nr_image_group_num=self.nr_image_group_num,
prompt_pattern_str=self.psy_pattern + "-" + self.nlp_pattern,
ic_path=self.ic_path
)
def test_fr_single_stimulus(self, ):
gpt4v_single_fr_batch(
key=self.key,
dataset_name=self.dataset_name,
image_path_list=self.image_path_list,
image_name_list=self.image_name_list,
mos_list=self.mos_list,
text_prompt=self.text_prompt,
save_folder_path=self.save_folder_path,
save_file_name=self.save_file_name,
mean_metric=self.mean_metric,
prompt_pattern_str=self.psy_pattern + "-" + self.nlp_pattern,
ic_path=self.ic_path
)
def test_fr_double_stimulus(self, ):
gpt4v_double_fr_batch(
key=self.key,
dataset_name=self.dataset_name,
image_path_list=self.image_path_list,
image_name_list=self.image_name_list,
mos_list=self.mos_list,
text_prompt=self.text_prompt,
save_folder_path=self.save_folder_path,
save_file_name=self.save_file_name,
fr_image_pair_num=self.fr_image_pair_num,
mean_metric=self.mean_metric,
prompt_pattern_str=self.psy_pattern + "-" + self.nlp_pattern,
ic_path=self.ic_path
)
def test_fr_multiple_stimulus(self, ):
gpt4v_multiple_fr_batch(
key=self.key,
dataset_name=self.dataset_name,
image_path_list=self.image_path_list,
image_name_list=self.image_name_list,
mos_list=self.mos_list,
text_prompt=self.text_prompt,
save_folder_path=self.save_folder_path,
save_file_name=self.save_file_name,
fr_image_group_num=self.fr_image_group_num,
mean_metric=self.mean_metric,
prompt_pattern_str=self.psy_pattern + "-" + self.nlp_pattern,
ic_path=self.ic_path
)
if __name__ == "__main__":
setup_seed(20)
Tester = GPT4VTester(
nr_image_pair_num=5, nr_image_group_num=2, fr_image_pair_num=5, fr_image_group_num=2,
)
Tester.testing_gpt4v()