-
Notifications
You must be signed in to change notification settings - Fork 99
/
run_inference_benchmark_general.py
83 lines (66 loc) · 3.02 KB
/
run_inference_benchmark_general.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
import os
import argparse
import json
from tqdm import tqdm
from video_chatgpt.eval.model_utils import initialize_model, load_video
from video_chatgpt.inference import video_chatgpt_infer
def parse_args():
"""
Parse command-line arguments.
"""
parser = argparse.ArgumentParser()
# Define the command-line arguments
parser.add_argument('--video_dir', help='Directory containing video files.', required=True)
parser.add_argument('--gt_file', help='Path to the ground truth file.', required=True)
parser.add_argument('--output_dir', help='Directory to save the model results JSON.', required=True)
parser.add_argument('--output_name', help='Name of the file for storing results JSON.', required=True)
parser.add_argument("--model-name", type=str, required=True)
parser.add_argument("--conv-mode", type=str, required=False, default='video-chatgpt_v1')
parser.add_argument("--projection_path", type=str, required=True)
return parser.parse_args()
def run_inference(args):
"""
Run inference on a set of video files using the provided model.
Args:
args: Command-line arguments.
"""
# Initialize the model
model, vision_tower, tokenizer, image_processor, video_token_len = initialize_model(args.model_name,
args.projection_path)
# Load the ground truth file
with open(args.gt_file) as file:
gt_contents = json.load(file)
# Create the output directory if it doesn't exist
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
output_list = [] # List to store the output results
conv_mode = args.conv_mode
video_formats = ['.mp4', '.avi', '.mov', '.mkv']
# Iterate over each sample in the ground truth file
for sample in tqdm(gt_contents):
video_name = sample['video_name']
sample_set = sample
question = sample['Q']
# Load the video file
for fmt in video_formats: # Added this line
temp_path = os.path.join(args.video_dir, f"{video_name}{fmt}")
if os.path.exists(temp_path):
video_path = temp_path
break
# Check if the video exists
if video_path is not None: # Modified this line
video_frames = load_video(video_path)
try:
# Run inference on the video and add the output to the list
output = video_chatgpt_infer(video_frames, question, conv_mode, model, vision_tower,
tokenizer, image_processor, video_token_len)
sample_set['pred'] = output
output_list.append(sample_set)
except Exception as e:
print(f"Error processing video file '{video_name}': {e}")
# Save the output list to a JSON file
with open(os.path.join(args.output_dir, f"{args.output_name}.json"), 'w') as file:
json.dump(output_list, file)
if __name__ == "__main__":
args = parse_args()
run_inference(args)