Repository navigation
Expand file tree
/
Copy patheval_data_loader.py
More file actions
211 lines (171 loc) · 7.03 KB
/
Copy patheval_data_loader.py
File metadata and controls
211 lines (171 loc) · 7.03 KB
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
import json
import os
import random
from PIL import Image
from torch.utils.data import Dataset
from torchvision import transforms
def load_json_or_jsonl(filepath):
if filepath.endswith(".json"):
all_data = json.load(open(filepath, "r", encoding="utf-8"))
elif filepath.endswith(".jsonl"):
all_data = []
with open(filepath, "r", encoding="utf-8") as f:
for line in f:
all_data.append(json.loads(line.strip()))
return all_data
class COCODataSet(Dataset):
def __init__(self, data_path, trans, debug_number=-1):
self.data_path = data_path
self.trans = trans
img_files = os.listdir(self.data_path)
random.shuffle(img_files)
self.img_files = img_files
if debug_number != -1:
self.img_files = self.img_files[:debug_number]
def __len__(self):
return len(self.img_files)
def __getitem__(self, index):
img_file = self.img_files[index]
img_id = int(img_file.split(".jpg")[0][-6:])
image = Image.open(os.path.join(self.data_path, img_file)).convert("RGB")
image = self.trans(image)
image_path = os.path.join(self.data_path, img_file) # qwenvl use image_path
return {"img_id": img_id, "image": image, "image_path": image_path}
class MMHALDataSet(Dataset): # Abandon
def __init__(self, data_path, json_file, trans, debug_number=-1):
self.data_path = data_path
self.trans = trans
json_data = json.load(open(json_file, 'r'))
self.all_data = []
for item in json_data:
item["image_src"] = os.path.join(self.data_path, os.path.basename(item["image_src"]))
self.all_data.append(item)
# import ipdb; ipdb.set_trace()
random.shuffle(self.all_data)
if debug_number != -1:
self.all_data = self.all_data[:debug_number]
def __len__(self):
return len(self.img_files)
def __getitem__(self, index):
data_item = self.all_data[index]
question_type = question_type
question_topic = question_topic
image_id = image_id
image_src = image_src
image_content = image_content
question = question
gt_answer = gt_answer
model_answer = model_answer
# image = Image.open(os.path.join(self.data_path, img_file)).convert("RGB")
# image = self.trans(image)
image_path = os.path.join(self.data_path, img_file) # qwenvl use image_path
return {"img_id": img_id, "image_path": image_src, "question": question}
class POPEChatDataSet(Dataset):
def __init__(self, pope_path, data_path, trans):
self.pope_path = pope_path
self.data_path = data_path
self.trans = trans
image_list, query_list, label_list = [], [], []
for q in open(pope_path, "r"):
line = json.loads(q)
image_list.append(line["image"])
query_list.append(line["text"])
label_list.append(line["label"])
for i in range(len(label_list)):
for j in range(len(label_list[i])):
if label_list[i][j] == "no":
label_list[i][j] = 0
else:
label_list[i][j] = 1
assert len(image_list) == len(query_list)
assert len(image_list) == len(label_list)
self.image_list = image_list
self.query_list = query_list
self.label_list = label_list
def __len__(self):
return len(self.label_list)
def __getitem__(self, index):
image_path = os.path.join(self.data_path, self.image_list[index])
raw_image = Image.open(image_path).convert("RGB")
if self.trans is not None:
image = self.trans(raw_image)
else:
image = transforms.ToTensor()(raw_image)
query = self.query_list[index]
label = self.label_list[index]
return {"image": image, "query": query, "label": label, "image_path": image_path}
class MMEDataSet(Dataset):
def __init__(self, data_path, trans, debug_number=-1):
self.data_path = data_path
self.trans = trans
self.items = []
dims = os.listdir(data_path)
for dim in os.listdir(data_path):
dim_dir = os.path.join(data_path, dim)
if os.path.isdir(os.path.join(dim_dir, "images")):
# artwork / color / existence ...
img_dir = os.path.join(dim_dir, "images")
qa_dir = os.path.join(dim_dir, "questions_answers_YN")
for file in os.listdir(img_dir):
if file.endswith(".txt"):
continue
elif not (
file.endswith(".jpg")
or file.endswith(".png")
or file.endswith(".jpeg")
):
import ipdb; ipdb.set_trace()
stem = os.path.splitext(file)[0]
self.items.append({
"dimension": dim,
"image_path": os.path.join(img_dir, file),
"qa_path": os.path.join(qa_dir, stem + ".txt"),
"img_name": file,
})
else:
# commonsense_reasoning / numerical_calculation ...
for file in os.listdir(dim_dir):
if file.endswith(".txt"):
continue
elif not (
file.endswith(".jpg")
or file.endswith(".png")
or file.endswith(".jpeg")
):
import ipdb; ipdb.set_trace()
stem = os.path.splitext(file)[0]
self.items.append({
"dimension": dim,
"image_path": os.path.join(dim_dir, file),
"qa_path": os.path.join(dim_dir, stem + ".txt"),
"img_name": file,
})
random.shuffle(self.items)
if debug_number != -1:
self.items = self.items[:debug_number]
def __len__(self):
return len(self.items)
def __getitem__(self, index):
item = self.items[index]
image = Image.open(item["image_path"]).convert("RGB")
image_tensor = self.trans(image)
# img_id = str(item['img_name'].split(".")[0])
questions = []
answers = []
with open(item["qa_path"], "r") as f:
for line in f:
line = line.strip()
if not line:
continue
q, a = line.rsplit("\t", 1)
questions.append(q.strip())
answers.append(a.strip())
return {
"image": image_tensor,
# "img_id": img_id,
"image_path": item["image_path"],
"img_name": item["img_name"],
"dimension": item["dimension"],
"questions": questions, # 长度一般为2
"answers": answers, # 对应GT
}