-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_vlm_newdata.py
103 lines (91 loc) · 3.12 KB
/
test_vlm_newdata.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
98
99
100
101
102
103
import base64
import json
from typing import Dict, List, Union
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 = os.getenv("TEAM_NAME")
TEAM_TRACK = os.getenv("TEAM_TRACK")
def main():
input_dir = Path(f"vlm/images_augmented")
# input_dir = Path(f"data")
results_dir = Path(f"vlm/images_augmented")
# results_dir = Path("results")
results_dir.mkdir(parents=True, exist_ok=True)
instances = []
truths = []
counter = 0
max_num_files = 600
with open(input_dir / "annotations.json", "r") as f:
data = json.load(f)
for instance in data[:max_num_files]:
with open(input_dir / 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 / "siglip_epoch10.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, Union[str, int]]], batch_size: int = 4
) -> List[Dict[str, Union[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"]
for i in range(len(_instances)):
if i < len(_results):
results.extend([{"key": _instances[i]["key"],"bbox": _results[i]}])
else:
results.extend([{"key": _instances[i]["key"],"bbox": [0, 0, 0, 0]}])
'''results.extend(
[
{
"key": _instances[i]["key"],
"bbox": _results[i],
}
for i in range(len(_instances))
]
)'''
return results
if __name__ == "__main__":
main()