Syzygianinfern0 commited on
Commit
f269277
·
1 Parent(s): 32371f9

Create CLI and Gradio versions

Browse files
Files changed (2) hide show
  1. evaluate.py +17 -27
  2. evaluate_demo.py +99 -0
evaluate.py CHANGED
@@ -1,9 +1,8 @@
 
1
  import pickle
2
  import warnings
3
  from pathlib import Path
4
 
5
- import gradio as gr
6
-
7
  from neus_v.smooth_scoring import smooth_confidence_scores
8
  from neus_v.utils import clear_gpu_memory
9
  from neus_v.veval.eval import evaluate_video_with_sequence_of_images
@@ -16,21 +15,21 @@ warnings.filterwarnings(
16
  )
17
 
18
  # Paths and parameters
19
- # WEIGHT_PATH = Path("/opt/mars/mnt/model_weights")
20
  WEIGHT_PATH = Path("/nas/mars/model_weights/")
21
  pickle_path = WEIGHT_PATH / "distributions.pkl"
22
  num_of_frame_in_sequence = 3
23
  model = "InternVL2-8B"
24
  device = 7
 
25
  # Load the vision-language model
26
  vision_language_model = InternVL(model_name=model, device=device)
 
27
  # Load distributions
28
  with open(pickle_path, "rb") as f:
29
  distributions = pickle.load(f)
30
  all_dimension_data = distributions.get(model).get("all_dimension")
31
 
32
 
33
- # TODO: Make paths better for public release
34
  def process_video(video_path, propositions, tl):
35
  """Process the video and compute the score_on_all."""
36
  proposition_set = parse_proposition_set(propositions.split(","))
@@ -63,36 +62,27 @@ def process_video(video_path, propositions, tl):
63
  return f"Error: {str(e)}"
64
 
65
 
66
- # Gradio interface
67
- def demo_interface(video, propositions, tl):
68
- """Wrapper for the Gradio interface."""
69
- return process_video(video, propositions, tl)
 
70
 
 
71
 
72
- def main():
73
- # Example data from the original script
 
 
74
  example_video_path_1 = "/nas/mars/dataset/teaser/A_storm_bursts_in_with_intermittent_lightning_and_causes_flooding_and_large_waves_crash_in.mp4"
75
  example_video_path_2 = "/nas/mars/dataset/teaser/The ocean waves gently lapping at the shore, until a storm bursts in, and then lightning flashes across the sky.mp4"
76
  example_propositions = "waves lapping,ocean shore,storm bursts in,lightning on the sky"
77
  example_tl = '("waves_lapping" & "ocean_shore") U ("storm_bursts_in" U "lightning_on_the_sky")'
78
 
79
- demo = gr.Interface(
80
- fn=demo_interface,
81
- inputs=[
82
- gr.Video(label="Upload Video"),
83
- gr.Textbox(label="List of Propositions (comma-separated)"),
84
- gr.Textbox(label="Temporal Logic Specification"),
85
- ],
86
- outputs=gr.Textbox(label="Score on All"),
87
- title="Video Evaluation with Temporal Logic",
88
- description="Upload a video and provide propositions and temporal logic to evaluate the score_on_all.",
89
- examples=[
90
- [example_video_path_1, example_propositions, example_tl],
91
- [example_video_path_2, example_propositions, example_tl],
92
- ],
93
- )
94
-
95
- demo.launch(allowed_paths=["/nas/mars/dataset/teaser"])
96
 
97
 
98
  if __name__ == "__main__":
 
1
+ import argparse
2
  import pickle
3
  import warnings
4
  from pathlib import Path
5
 
 
 
6
  from neus_v.smooth_scoring import smooth_confidence_scores
7
  from neus_v.utils import clear_gpu_memory
8
  from neus_v.veval.eval import evaluate_video_with_sequence_of_images
 
15
  )
16
 
17
  # Paths and parameters
 
18
  WEIGHT_PATH = Path("/nas/mars/model_weights/")
19
  pickle_path = WEIGHT_PATH / "distributions.pkl"
20
  num_of_frame_in_sequence = 3
21
  model = "InternVL2-8B"
22
  device = 7
23
+
24
  # Load the vision-language model
25
  vision_language_model = InternVL(model_name=model, device=device)
26
+
27
  # Load distributions
28
  with open(pickle_path, "rb") as f:
29
  distributions = pickle.load(f)
30
  all_dimension_data = distributions.get(model).get("all_dimension")
31
 
32
 
 
33
  def process_video(video_path, propositions, tl):
34
  """Process the video and compute the score_on_all."""
35
  proposition_set = parse_proposition_set(propositions.split(","))
 
62
  return f"Error: {str(e)}"
63
 
64
 
65
+ def main():
66
+ # parser = argparse.ArgumentParser(description="Process a video using temporal logic evaluation.")
67
+ # parser.add_argument("video", type=str, help="Path to the video file.")
68
+ # parser.add_argument("propositions", type=str, help="List of propositions (comma-separated).")
69
+ # parser.add_argument("tl", type=str, help="Temporal logic specification.")
70
 
71
+ # args = parser.parse_args()
72
 
73
+ # score = process_video(args.video, args.propositions, args.tl)
74
+ # print(f"Score on All: {score}")
75
+
76
+ # Example usage
77
  example_video_path_1 = "/nas/mars/dataset/teaser/A_storm_bursts_in_with_intermittent_lightning_and_causes_flooding_and_large_waves_crash_in.mp4"
78
  example_video_path_2 = "/nas/mars/dataset/teaser/The ocean waves gently lapping at the shore, until a storm bursts in, and then lightning flashes across the sky.mp4"
79
  example_propositions = "waves lapping,ocean shore,storm bursts in,lightning on the sky"
80
  example_tl = '("waves_lapping" & "ocean_shore") U ("storm_bursts_in" U "lightning_on_the_sky")'
81
 
82
+ print("Example 1:")
83
+ print(f"Score: {process_video(example_video_path_1, example_propositions, example_tl)}")
84
+ print("Example 2:")
85
+ print(f"Score: {process_video(example_video_path_2, example_propositions, example_tl)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
 
88
  if __name__ == "__main__":
evaluate_demo.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pickle
2
+ import warnings
3
+ from pathlib import Path
4
+
5
+ import gradio as gr
6
+
7
+ from neus_v.smooth_scoring import smooth_confidence_scores
8
+ from neus_v.utils import clear_gpu_memory
9
+ from neus_v.veval.eval import evaluate_video_with_sequence_of_images
10
+ from neus_v.veval.parse import parse_proposition_set, parse_tl_specification
11
+ from neus_v.vlm.internvl import InternVL
12
+
13
+ # Suppress specific warnings
14
+ warnings.filterwarnings(
15
+ "ignore", category=DeprecationWarning, message="Conversion of an array with ndim > 0 to a scalar is deprecated"
16
+ )
17
+
18
+ # Paths and parameters
19
+ # WEIGHT_PATH = Path("/opt/mars/mnt/model_weights")
20
+ WEIGHT_PATH = Path("/nas/mars/model_weights/")
21
+ pickle_path = WEIGHT_PATH / "distributions.pkl"
22
+ num_of_frame_in_sequence = 3
23
+ model = "InternVL2-8B"
24
+ device = 7
25
+ # Load the vision-language model
26
+ vision_language_model = InternVL(model_name=model, device=device)
27
+ # Load distributions
28
+ with open(pickle_path, "rb") as f:
29
+ distributions = pickle.load(f)
30
+ all_dimension_data = distributions.get(model).get("all_dimension")
31
+
32
+
33
+ # TODO: Make paths better for public release
34
+ def process_video(video_path, propositions, tl):
35
+ """Process the video and compute the score_on_all."""
36
+ proposition_set = parse_proposition_set(propositions.split(","))
37
+ tl_spec = parse_tl_specification(tl)
38
+ threshold = 0.349
39
+
40
+ try:
41
+ result = evaluate_video_with_sequence_of_images(
42
+ vision_language_model=vision_language_model,
43
+ confidence_as_token_probability=True,
44
+ video_path=video_path,
45
+ proposition_set=proposition_set,
46
+ tl_spec=tl_spec,
47
+ parallel_inference=False,
48
+ num_of_frame_in_sequence=num_of_frame_in_sequence,
49
+ threshold=threshold,
50
+ )
51
+ probability = result.get("probability")
52
+ score_on_all = float(
53
+ smooth_confidence_scores(
54
+ target_data=[probability],
55
+ prior_distribution=all_dimension_data,
56
+ )
57
+ )
58
+ clear_gpu_memory()
59
+ return score_on_all
60
+
61
+ except Exception as e:
62
+ clear_gpu_memory()
63
+ return f"Error: {str(e)}"
64
+
65
+
66
+ # Gradio interface
67
+ def demo_interface(video, propositions, tl):
68
+ """Wrapper for the Gradio interface."""
69
+ return process_video(video, propositions, tl)
70
+
71
+
72
+ def main():
73
+ # Example data from the original script
74
+ example_video_path_1 = "/nas/mars/dataset/teaser/A_storm_bursts_in_with_intermittent_lightning_and_causes_flooding_and_large_waves_crash_in.mp4"
75
+ example_video_path_2 = "/nas/mars/dataset/teaser/The ocean waves gently lapping at the shore, until a storm bursts in, and then lightning flashes across the sky.mp4"
76
+ example_propositions = "waves lapping,ocean shore,storm bursts in,lightning on the sky"
77
+ example_tl = '("waves_lapping" & "ocean_shore") U ("storm_bursts_in" U "lightning_on_the_sky")'
78
+
79
+ demo = gr.Interface(
80
+ fn=demo_interface,
81
+ inputs=[
82
+ gr.Video(label="Upload Video"),
83
+ gr.Textbox(label="List of Propositions (comma-separated)"),
84
+ gr.Textbox(label="Temporal Logic Specification"),
85
+ ],
86
+ outputs=gr.Textbox(label="Score on All"),
87
+ title="Video Evaluation with Temporal Logic",
88
+ description="Upload a video and provide propositions and temporal logic to evaluate the score_on_all.",
89
+ examples=[
90
+ [example_video_path_1, example_propositions, example_tl],
91
+ [example_video_path_2, example_propositions, example_tl],
92
+ ],
93
+ )
94
+
95
+ demo.launch(allowed_paths=["/nas/mars/dataset/teaser"])
96
+
97
+
98
+ if __name__ == "__main__":
99
+ main()