-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlvlm_zs_predict.py
More file actions
120 lines (106 loc) · 4.21 KB
/
lvlm_zs_predict.py
File metadata and controls
120 lines (106 loc) · 4.21 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import requests
import base64
import os
import re
import pandas as pd
import numpy as np
import ast
from pandarallel import pandarallel
from tqdm import tqdm
import json
import time
import argparse
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
def get_response(image_path, base_url, api_key, model_name, detail="low", max_tokens=200, temperature=1.2, n=10):
base64_image = encode_image(image_path)
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
payload = {
"model": model_name,
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": """Suppose you are an expert in geo-localization, you have the ability to give two number GPS coordination given an image.
Please give me the location of the given image.
Remember, you must have an answer, just output your best guess, don't answer me that you can't give a location.
Your answer should be in the following JSON format without any other information: {"latitude": float,"longitude": float}.
Your answer should be in the following JSON format without any other information: {"latitude": float,"longitude": float}."""
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}",
"detail": detail
}
}
]
}
],
"max_tokens": max_tokens,
"temperature": temperature,
"n": n
}
response = requests.post(base_url, headers=headers, json=payload, timeout=(30,60))
ans = []
for choice in response.json()['choices']:
try:
ans.append(choice['message']['content'])
except:
ans.append('{"latitude": 0.0,"longitude": 0.0}')
return ans
def process_row(row, base_url, api_key, model_name, root_path, image_path):
image_path = os.path.join(root_path, image_path, row["IMG_ID"])
try:
response = get_response(image_path, base_url, api_key, model_name)
except Exception as e:
response = "None"
print(e)
row['response'] = response
return row
def run(args):
api_key = args.api_key
model_name = args.model_name
base_url = args.base_url
root_path = args.root_path
text_path = args.text_path
image_path = args.image_path
result_path = args.result_path
process = args.process
if os.path.exists(os.path.join(root_path, result_path)):
df = pd.read_csv(os.path.join(root_path, result_path))
df_rerun = df[df['response'].isna()]
print('Need Rerun:', df_rerun.shape[0])
df_rerun = df_rerun.parallel_apply(lambda row: process_row(row, base_url, api_key, model_name, root_path, image_path), axis=1)
df.update(df_rerun)
df.to_csv(os.path.join(root_path, result_path), index=False)
else:
df = pd.read_csv(os.path.join(root_path, text_path))
df = df.parallel_apply(lambda row: process_row(row, base_url, api_key, model_name, root_path, image_path), axis=1)
df.to_csv(os.path.join(root_path, result_path), index=False)
if __name__ == '__main__':
args = argparse.ArgumentParser()
model_name = "xxx"
api_key = "sk-xxx"
base_url = "xxx"
root_path = "xxx/dataset/yfcc4k"
text_path = "yfcc4k_places365.csv"
image_path = "yfcc4k"
result_path = "llm_predict_results_zs_{}.csv".format(model_name)
pandarallel.initialize(progress_bar=True, nb_workers=32)
args.add_argument('--api_key', type=str, default=api_key)
args.add_argument('--model_name', type=str, default=model_name)
args.add_argument('--base_url', type=str, default=base_url)
args.add_argument('--root_path', type=str, default=root_path)
args.add_argument('--text_path', type=str, default=text_path)
args.add_argument('--image_path', type=str, default=image_path)
args.add_argument('--result_path', type=str, default=result_path)
args = args.parse_args()
print(args)
run(args)