This repository has been archived by the owner on Sep 7, 2024. It is now read-only.
generated from TIL-24/til-24-base
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_vlm.py
97 lines (87 loc) · 3.03 KB
/
test_vlm.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import base64
import json
from typing import Dict, List
import pandas as pd
import requests
from tqdm import tqdm
from pathlib import Path
from scoring.vlm_eval import vlm_eval
from dotenv import load_dotenv
import os
load_dotenv()
TEAM_NAME = "dingdongs"#os.getenv("TEAM_NAME")
TEAM_TRACK = "novice"#os.getenv("TEAM_TRACK")
def main():
input_dir = Path(f"/home/jupyter/{TEAM_TRACK}")
#input_dir = Path(f"../../data/{TEAM_TRACK}/train")
results_dir = Path(f"/home/jupyter/{TEAM_NAME}")
#results_dir = Path("results")
results_dir.mkdir(parents=True, exist_ok=True)
instances = []
truths = []
counter = 0
with open(input_dir / "vlm.jsonl", "r") as f:
for line in f:
if line.strip() == "":
continue
instance = json.loads(line.strip())
with open(input_dir / "images" / instance["image"], "rb") as file:
image_bytes = file.read()
for annotation in instance["annotations"]:
instances.append(
{
"key": counter,
"caption": annotation["caption"],
"b64": base64.b64encode(image_bytes).decode("ascii"),
}
)
truths.append(
{
"key": counter,
"caption": annotation["caption"],
"bbox": annotation["bbox"],
}
)
counter += 1
assert len(truths) == len(instances)
results = run_batched(instances)
df = pd.DataFrame(results)
assert len(truths) == len(results)
df.to_csv(results_dir / "vlm_results.csv", index=False)
# calculate eval
eval_result = vlm_eval(
[truth["bbox"] for truth in truths],
[result["bbox"] for result in results],
)
print(f"[email protected]: {eval_result}")
def run_batched(
instances: List[Dict[str, str | int]], batch_size: int = 4
) -> List[Dict[str, str | int]]:
# split into batches
results = []
for index in tqdm(range(0, len(instances), batch_size)):
_instances = instances[index : index + batch_size]
response = requests.post(
"http://localhost:5004/identify",
data=json.dumps(
{
"instances": [
{field: _instance[field] for field in ("key", "caption", "b64")}
for _instance in _instances
]
}
),
)
_results = response.json()["predictions"]
results.extend(
[
{
"key": _instances[i]["key"],
"bbox": _results[i],
}
for i in range(len(_instances))
]
)
return results
if __name__ == "__main__":
main()