Skip to content

Commit 977459d

Browse files
[Fix] add support for multi process build prompt in video dataset (#1162)
* add support for multi process build prompt in video dataset (success in megabench) * fix bugs and remove unnecessary video dataset config * fix megabench sampling config problem by @TianhaoLiang2000
1 parent 379f3f5 commit 977459d

17 files changed

+381
-267
lines changed

vlmeval/dataset/CGAVCounting/cg_av_counting.py

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,21 @@ def save_video_frames_clue(self, video,uid,timestamp_list):
5151
frames = []
5252
# 获取视频的帧率
5353
fps = vid.get_avg_fps()
54-
55-
for timestamp_sec in timestamp_list:
56-
# 计算视频帧对应的索引
57-
frame_idx = int(timestamp_sec * fps)
58-
59-
# 获取对应帧
60-
frame = vid[frame_idx]
61-
62-
# 将帧转换为PIL图像
63-
img = Image.fromarray(frame.asnumpy())
64-
frames.append(img)
65-
for im, pth in zip(frames, frame_paths):
66-
if not osp.exists(pth):
67-
im.save(pth)
54+
lock_path = osp.splitext(vid_path)[0] + '.lock'
55+
with portalocker.Lock(lock_path, 'w', timeout=30):
56+
for timestamp_sec in timestamp_list:
57+
# 计算视频帧对应的索引
58+
frame_idx = int(timestamp_sec * fps)
59+
60+
# 获取对应帧
61+
frame = vid[frame_idx]
62+
63+
# 将帧转换为PIL图像
64+
img = Image.fromarray(frame.asnumpy())
65+
frames.append(img)
66+
for im, pth in zip(frames, frame_paths):
67+
if not osp.exists(pth):
68+
im.save(pth)
6869
return frame_paths,frames[0].width,frames[0].height
6970

7071
def format_time(self,t):
@@ -322,36 +323,37 @@ def save_video_frames(self, video, uid, num_frames=8, fps=-1):
322323
# Save and validate frames
323324
valid_paths = []
324325
valid_indices = []
325-
326-
if not np.all([osp.exists(p) for p in frame_paths]):
327-
images = [vid[i].asnumpy() for i in indices]
328-
for i, (img_array, path) in enumerate(zip(images, frame_paths)):
329-
if osp.exists(path):
326+
lock_path = osp.splitext(vid_path)[0] + '.lock'
327+
with portalocker.Lock(lock_path, 'w', timeout=30):
328+
if not np.all([osp.exists(p) for p in frame_paths]):
329+
images = [vid[i].asnumpy() for i in indices]
330+
for i, (img_array, path) in enumerate(zip(images, frame_paths)):
331+
if osp.exists(path):
332+
try:
333+
with Image.open(path) as img:
334+
img.verify()
335+
valid_paths.append(path)
336+
valid_indices.append(indices[i])
337+
except Exception:
338+
continue
339+
else:
340+
try:
341+
img = Image.fromarray(img_array)
342+
img.save(path)
343+
img.verify()
344+
valid_paths.append(path)
345+
valid_indices.append(indices[i])
346+
except Exception:
347+
continue
348+
else:
349+
for i, path in enumerate(frame_paths):
330350
try:
331351
with Image.open(path) as img:
332352
img.verify()
333353
valid_paths.append(path)
334354
valid_indices.append(indices[i])
335355
except Exception:
336356
continue
337-
else:
338-
try:
339-
img = Image.fromarray(img_array)
340-
img.save(path)
341-
img.verify()
342-
valid_paths.append(path)
343-
valid_indices.append(indices[i])
344-
except Exception:
345-
continue
346-
else:
347-
for i, path in enumerate(frame_paths):
348-
try:
349-
with Image.open(path) as img:
350-
img.verify()
351-
valid_paths.append(path)
352-
valid_indices.append(indices[i])
353-
except Exception:
354-
continue
355357

356358
return valid_paths, valid_indices, vid_fps
357359

vlmeval/dataset/cgbench.py

Lines changed: 96 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -396,36 +396,37 @@ def save_video_frames(self, video, uid, clue_intervals=None, num_frames=8, fps=-
396396
# Save and validate frames
397397
valid_paths = []
398398
valid_indices = []
399-
400-
if not np.all([osp.exists(p) for p in frame_paths]):
401-
images = [vid[i].asnumpy() for i in indices]
402-
for i, (img_array, path) in enumerate(zip(images, frame_paths)):
403-
if osp.exists(path):
399+
lock_path = osp.splitext(vid_path)[0] + '.lock'
400+
with portalocker.Lock(lock_path, 'w', timeout=30):
401+
if not np.all([osp.exists(p) for p in frame_paths]):
402+
images = [vid[i].asnumpy() for i in indices]
403+
for i, (img_array, path) in enumerate(zip(images, frame_paths)):
404+
if osp.exists(path):
405+
try:
406+
with Image.open(path) as img:
407+
img.verify()
408+
valid_paths.append(path)
409+
valid_indices.append(indices[i])
410+
except Exception:
411+
continue
412+
else:
413+
try:
414+
img = Image.fromarray(img_array)
415+
img.save(path)
416+
img.verify()
417+
valid_paths.append(path)
418+
valid_indices.append(indices[i])
419+
except Exception:
420+
continue
421+
else:
422+
for i, path in enumerate(frame_paths):
404423
try:
405424
with Image.open(path) as img:
406425
img.verify()
407426
valid_paths.append(path)
408427
valid_indices.append(indices[i])
409428
except Exception:
410429
continue
411-
else:
412-
try:
413-
img = Image.fromarray(img_array)
414-
img.save(path)
415-
img.verify()
416-
valid_paths.append(path)
417-
valid_indices.append(indices[i])
418-
except Exception:
419-
continue
420-
else:
421-
for i, path in enumerate(frame_paths):
422-
try:
423-
with Image.open(path) as img:
424-
img.verify()
425-
valid_paths.append(path)
426-
valid_indices.append(indices[i])
427-
except Exception:
428-
continue
429430

430431
return valid_paths, valid_indices, vid_fps
431432

@@ -721,36 +722,37 @@ def save_video_frames(self, video, uid, clue_intervals=None, num_frames=8, fps=-
721722

722723
valid_paths = []
723724
valid_indices = []
724-
725-
if not np.all([osp.exists(p) for p in frame_paths]):
726-
images = [vid[i].asnumpy() for i in indices]
727-
for i, (img_array, path) in enumerate(zip(images, frame_paths)):
728-
if osp.exists(path):
725+
lock_path = osp.splitext(vid_path)[0] + '.lock'
726+
with portalocker.Lock(lock_path, 'w', timeout=30):
727+
if not np.all([osp.exists(p) for p in frame_paths]):
728+
images = [vid[i].asnumpy() for i in indices]
729+
for i, (img_array, path) in enumerate(zip(images, frame_paths)):
730+
if osp.exists(path):
731+
try:
732+
with Image.open(path) as img:
733+
img.verify()
734+
valid_paths.append(path)
735+
valid_indices.append(indices[i])
736+
except Exception:
737+
continue
738+
else:
739+
try:
740+
img = Image.fromarray(img_array)
741+
img.save(path)
742+
img.verify()
743+
valid_paths.append(path)
744+
valid_indices.append(indices[i])
745+
except Exception:
746+
continue
747+
else:
748+
for i, path in enumerate(frame_paths):
729749
try:
730750
with Image.open(path) as img:
731751
img.verify()
732752
valid_paths.append(path)
733753
valid_indices.append(indices[i])
734754
except Exception:
735755
continue
736-
else:
737-
try:
738-
img = Image.fromarray(img_array)
739-
img.save(path)
740-
img.verify()
741-
valid_paths.append(path)
742-
valid_indices.append(indices[i])
743-
except Exception:
744-
continue
745-
else:
746-
for i, path in enumerate(frame_paths):
747-
try:
748-
with Image.open(path) as img:
749-
img.verify()
750-
valid_paths.append(path)
751-
valid_indices.append(indices[i])
752-
except Exception:
753-
continue
754756

755757
return valid_paths, valid_indices, vid_fps
756758

@@ -1276,36 +1278,37 @@ def save_video_frames(self, video, uid, clue_intervals=None, num_frames=8, fps=-
12761278
# Save and validate frames
12771279
valid_paths = []
12781280
valid_indices = []
1279-
1280-
if not np.all([osp.exists(p) for p in frame_paths]):
1281-
images = [vid[i].asnumpy() for i in indices]
1282-
for i, (img_array, path) in enumerate(zip(images, frame_paths)):
1283-
if osp.exists(path):
1281+
lock_path = osp.splitext(vid_path)[0] + '.lock'
1282+
with portalocker.Lock(lock_path, 'w', timeout=30):
1283+
if not np.all([osp.exists(p) for p in frame_paths]):
1284+
images = [vid[i].asnumpy() for i in indices]
1285+
for i, (img_array, path) in enumerate(zip(images, frame_paths)):
1286+
if osp.exists(path):
1287+
try:
1288+
with Image.open(path) as img:
1289+
img.verify()
1290+
valid_paths.append(path)
1291+
valid_indices.append(indices[i])
1292+
except Exception:
1293+
continue
1294+
else:
1295+
try:
1296+
img = Image.fromarray(img_array)
1297+
img.save(path)
1298+
img.verify()
1299+
valid_paths.append(path)
1300+
valid_indices.append(indices[i])
1301+
except Exception:
1302+
continue
1303+
else:
1304+
for i, path in enumerate(frame_paths):
12841305
try:
12851306
with Image.open(path) as img:
12861307
img.verify()
12871308
valid_paths.append(path)
12881309
valid_indices.append(indices[i])
12891310
except Exception:
12901311
continue
1291-
else:
1292-
try:
1293-
img = Image.fromarray(img_array)
1294-
img.save(path)
1295-
img.verify()
1296-
valid_paths.append(path)
1297-
valid_indices.append(indices[i])
1298-
except Exception:
1299-
continue
1300-
else:
1301-
for i, path in enumerate(frame_paths):
1302-
try:
1303-
with Image.open(path) as img:
1304-
img.verify()
1305-
valid_paths.append(path)
1306-
valid_indices.append(indices[i])
1307-
except Exception:
1308-
continue
13091312

13101313
return valid_paths, valid_indices, vid_fps
13111314

@@ -1600,36 +1603,37 @@ def save_video_frames(self, video, uid, clue_intervals=None, num_frames=8, fps=-
16001603

16011604
valid_paths = []
16021605
valid_indices = []
1603-
1604-
if not np.all([osp.exists(p) for p in frame_paths]):
1605-
images = [vid[i].asnumpy() for i in indices]
1606-
for i, (img_array, path) in enumerate(zip(images, frame_paths)):
1607-
if osp.exists(path):
1606+
lock_path = osp.splitext(vid_path)[0] + '.lock'
1607+
with portalocker.Lock(lock_path, 'w', timeout=30):
1608+
if not np.all([osp.exists(p) for p in frame_paths]):
1609+
images = [vid[i].asnumpy() for i in indices]
1610+
for i, (img_array, path) in enumerate(zip(images, frame_paths)):
1611+
if osp.exists(path):
1612+
try:
1613+
with Image.open(path) as img:
1614+
img.verify()
1615+
valid_paths.append(path)
1616+
valid_indices.append(indices[i])
1617+
except Exception:
1618+
continue
1619+
else:
1620+
try:
1621+
img = Image.fromarray(img_array)
1622+
img.save(path)
1623+
img.verify()
1624+
valid_paths.append(path)
1625+
valid_indices.append(indices[i])
1626+
except Exception:
1627+
continue
1628+
else:
1629+
for i, path in enumerate(frame_paths):
16081630
try:
16091631
with Image.open(path) as img:
16101632
img.verify()
16111633
valid_paths.append(path)
16121634
valid_indices.append(indices[i])
16131635
except Exception:
16141636
continue
1615-
else:
1616-
try:
1617-
img = Image.fromarray(img_array)
1618-
img.save(path)
1619-
img.verify()
1620-
valid_paths.append(path)
1621-
valid_indices.append(indices[i])
1622-
except Exception:
1623-
continue
1624-
else:
1625-
for i, path in enumerate(frame_paths):
1626-
try:
1627-
with Image.open(path) as img:
1628-
img.verify()
1629-
valid_paths.append(path)
1630-
valid_indices.append(indices[i])
1631-
except Exception:
1632-
continue
16331637

16341638
return valid_paths, valid_indices, vid_fps
16351639

vlmeval/dataset/longvideobench.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,14 @@ def save_video_frames(self, video_path, video_llm=False):
222222
flag = np.all([osp.exists(p) for p in frame_paths])
223223

224224
if not flag:
225-
images = [vid[i].asnumpy() for i in indices]
226-
images = [Image.fromarray(arr) for arr in images]
227-
for im, pth in zip(images, frame_paths):
228-
if not osp.exists(pth) and not video_llm:
229-
im.save(pth)
225+
lock_path = osp.splitext(vid_path)[0] + '.lock'
226+
with portalocker.Lock(lock_path, 'w', timeout=30):
227+
if not np.all([osp.exists(p) for p in frame_paths]):
228+
images = [vid[i].asnumpy() for i in indices]
229+
images = [Image.fromarray(arr) for arr in images]
230+
for im, pth in zip(images, frame_paths):
231+
if not osp.exists(pth) and not video_llm:
232+
im.save(pth)
230233

231234
return frame_paths, indices, video_info
232235

0 commit comments

Comments
 (0)