Update ImageRewardDB.py
Browse files- ImageRewardDB.py +62 -18
ImageRewardDB.py
CHANGED
@@ -107,11 +107,14 @@ class ImageRewardDB(datasets.GeneratorBasedBuilder):
|
|
107 |
"validation": 2,
|
108 |
"test": 2
|
109 |
}
|
|
|
|
|
|
|
110 |
BUILDER_CONFIGS.append(
|
111 |
ImageRewardDBConfig(name=f"{num_k}k", part_ids=part_ids, description=f"This is a {num_k}k-scale ImageRewardDB")
|
112 |
)
|
113 |
BUILDER_CONFIGS.append(
|
114 |
-
ImageRewardDBConfig(name=f"{num_k}
|
115 |
)
|
116 |
|
117 |
DEFAULT_CONFIG_NAME = "8k" # It's not mandatory to have a default configuration. Just use one if it make sense.
|
@@ -130,6 +133,16 @@ class ImageRewardDB(datasets.GeneratorBasedBuilder):
|
|
130 |
"fidelity_rating": datasets.Sequence(datasets.Value("int8"))
|
131 |
}
|
132 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
else:
|
134 |
features = datasets.Features(
|
135 |
{
|
@@ -216,10 +229,10 @@ class ImageRewardDB(datasets.GeneratorBasedBuilder):
|
|
216 |
assert num_data_dirs == len(json_paths)
|
217 |
|
218 |
#Iterate throug all extracted zip folders for images
|
219 |
-
|
220 |
for index, json_path in enumerate(json_paths):
|
221 |
json_data = json.load(open(json_path, "r", encoding="utf-8"))
|
222 |
-
if "group" in self.config.name:
|
223 |
group_num = 0
|
224 |
image_path = []
|
225 |
rank = []
|
@@ -235,8 +248,7 @@ class ImageRewardDB(datasets.GeneratorBasedBuilder):
|
|
235 |
prompt = sample["prompt"]
|
236 |
classification = sample["classification"]
|
237 |
image_amount_in_total = sample["image_amount_in_total"]
|
238 |
-
|
239 |
-
image_path.append(os.path.join(data_dirs[index], str(sample["image_path"]).split("/")[-1]))
|
240 |
rank.append(sample["rank"])
|
241 |
overall_rating.append(sample["overall_rating"])
|
242 |
image_text_alignment_rating.append(sample["image_text_alignment_rating"])
|
@@ -244,19 +256,51 @@ class ImageRewardDB(datasets.GeneratorBasedBuilder):
|
|
244 |
group_num += 1
|
245 |
if group_num == image_amount_in_total:
|
246 |
group_num = 0
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
"
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
260 |
else:
|
261 |
for example in json_data:
|
262 |
image_path = os.path.join(data_dirs[index], str(example["image_path"]).split("/")[-1])
|
|
|
107 |
"validation": 2,
|
108 |
"test": 2
|
109 |
}
|
110 |
+
BUILDER_CONFIGS.append(
|
111 |
+
ImageRewardDBConfig(name=f"{num_k}k_group", part_ids=part_ids, description=f"This is a {num_k}k-scale groups of ImageRewardDB")
|
112 |
+
)
|
113 |
BUILDER_CONFIGS.append(
|
114 |
ImageRewardDBConfig(name=f"{num_k}k", part_ids=part_ids, description=f"This is a {num_k}k-scale ImageRewardDB")
|
115 |
)
|
116 |
BUILDER_CONFIGS.append(
|
117 |
+
ImageRewardDBConfig(name=f"{num_k}k_pair", part_ids=part_ids, description=f"This is a {num_k}k-scale pairs of ImageRewardDB")
|
118 |
)
|
119 |
|
120 |
DEFAULT_CONFIG_NAME = "8k" # It's not mandatory to have a default configuration. Just use one if it make sense.
|
|
|
133 |
"fidelity_rating": datasets.Sequence(datasets.Value("int8"))
|
134 |
}
|
135 |
)
|
136 |
+
elif "pair" in self.config.name:
|
137 |
+
features = datasets.Features(
|
138 |
+
{
|
139 |
+
"prompt_id": datasets.Value("string"),
|
140 |
+
"prompt": datasets.Value("string"),
|
141 |
+
"classification": datasets.Value("string"),
|
142 |
+
"img_better": datasets.Image(),
|
143 |
+
"img_worse": datasets.Image()
|
144 |
+
}
|
145 |
+
)
|
146 |
else:
|
147 |
features = datasets.Features(
|
148 |
{
|
|
|
229 |
assert num_data_dirs == len(json_paths)
|
230 |
|
231 |
#Iterate throug all extracted zip folders for images
|
232 |
+
metadata_table = pd.read_parquet(metadata_path)
|
233 |
for index, json_path in enumerate(json_paths):
|
234 |
json_data = json.load(open(json_path, "r", encoding="utf-8"))
|
235 |
+
if "group" in self.config.name or "pair" in self.config.name:
|
236 |
group_num = 0
|
237 |
image_path = []
|
238 |
rank = []
|
|
|
248 |
prompt = sample["prompt"]
|
249 |
classification = sample["classification"]
|
250 |
image_amount_in_total = sample["image_amount_in_total"]
|
251 |
+
image_path.append(sample["image_path"])
|
|
|
252 |
rank.append(sample["rank"])
|
253 |
overall_rating.append(sample["overall_rating"])
|
254 |
image_text_alignment_rating.append(sample["image_text_alignment_rating"])
|
|
|
256 |
group_num += 1
|
257 |
if group_num == image_amount_in_total:
|
258 |
group_num = 0
|
259 |
+
if "group" in self.config.name:
|
260 |
+
yield prompt_id, ({
|
261 |
+
"prompt_id": prompt_id,
|
262 |
+
"prompt": prompt,
|
263 |
+
"classification": classification,
|
264 |
+
"image": [{
|
265 |
+
"path": image_path[idx],
|
266 |
+
"bytes": open(image_path[idx], "rb").read()
|
267 |
+
} for idx in range(image_amount_in_total)],
|
268 |
+
"rank": rank,
|
269 |
+
"overall_rating": overall_rating,
|
270 |
+
"image_text_alignment_rating": image_text_alignment_rating,
|
271 |
+
"fidelity_rating": fidelity_rating,
|
272 |
+
})
|
273 |
+
else:
|
274 |
+
for idx in range(image_amount_in_total):
|
275 |
+
for idy in range(idx+1, image_amount_in_total):
|
276 |
+
if rank[idx] < rank[idy]:
|
277 |
+
yield prompt_id, ({
|
278 |
+
"prompt_id": prompt_id,
|
279 |
+
"prompt": prompt,
|
280 |
+
"classification": classification,
|
281 |
+
"img_better": {
|
282 |
+
"path": image_path[idx],
|
283 |
+
"bytes": open(image_path[idx], "rb").read()
|
284 |
+
},
|
285 |
+
"img_worse": {
|
286 |
+
"path": image_path[idy],
|
287 |
+
"bytes": open(image_path[idy], "rb").read()
|
288 |
+
}
|
289 |
+
})
|
290 |
+
elif rank[idx] > rank[idy]:
|
291 |
+
yield prompt_id, ({
|
292 |
+
"prompt_id": prompt_id,
|
293 |
+
"prompt": prompt,
|
294 |
+
"classification": classification,
|
295 |
+
"img_better": {
|
296 |
+
"path": image_path[idy],
|
297 |
+
"bytes": open(image_path[idy], "rb").read()
|
298 |
+
},
|
299 |
+
"img_worse": {
|
300 |
+
"path": image_path[idx],
|
301 |
+
"bytes": open(image_path[idx], "rb").read()
|
302 |
+
}
|
303 |
+
})
|
304 |
else:
|
305 |
for example in json_data:
|
306 |
image_path = os.path.join(data_dirs[index], str(example["image_path"]).split("/")[-1])
|