-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcaption.py
283 lines (251 loc) · 8.6 KB
/
caption.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import argparse
import json
import os
from datetime import datetime
from pathlib import Path
from utils.download import download
from utils.llava import Llava
from utils.logger import Logger
DEFAULT_SYSTEM_MESSAGE = """
You are an assistant who describes the content and composition of images.
Describe only what you see in the image, not what you think the image is about.
Be factual and literal. Do not use metaphors or similes. Be concise.
"""
DEFAULT_USER_PROMPT = """
Please describe this image in 30 to 50 words.
"""
def main(args):
# Set logger
workspace_path = os.getcwd()
data_dir_path = Path(args.data_path)
log_file_path = data_dir_path.parent if os.path.exists(data_dir_path.parent) else workspace_path
if args.custom_caption_save_path:
log_file_path = Path(args.custom_caption_save_path)
log_time = datetime.now().strftime('%Y%m%d_%H%M%S')
# caption_failed_list_file = f'Caption_failed_list_{log_time}.txt'
if os.path.exists(data_dir_path):
log_name = os.path.basename(data_dir_path)
else:
print(f'{data_dir_path} NOT FOUND!!!')
raise FileNotFoundError
if args.save_logs:
log_file = f'Caption_{log_name}_{log_time}.log' if log_name else f'test_{log_time}.log'
log_file = os.path.join(log_file_path, log_file) \
if os.path.exists(log_file_path) else os.path.join(os.getcwd(), log_file)
else:
log_file = None
if str(args.log_level).lower() in 'debug, info, warning, error, critical':
my_logger = Logger(args.log_level, log_file).logger
my_logger.info(f'Set log level to "{args.log_level}"')
else:
my_logger = Logger('INFO', log_file).logger
my_logger.warning('Invalid log level, set log level to "INFO"!')
if args.save_logs:
my_logger.info(f'Log file will be saved as "{log_file}".')
# Check custom models path
config_file = os.path.join(Path(__file__).parent, 'configs', 'default.json') \
if args.config == "default.json" else Path(args.config)
if args.custom_model_path is not None and args.custom_mmproj_path is not None:
# Use custom model and mmproj path
my_logger.warning('custom_model_path and custom_mmproj_path are enabled')
if not (os.path.isfile(args.custom_model_path) and str(args.custom_model_path).endswith('.gguf')):
my_logger.error(f'{args.custom_model_path} is not a gguf file!')
raise FileNotFoundError
elif not (os.path.isfile(args.custom_mmproj_path) and str(args.custom_mmproj_path).endswith('.gguf')):
my_logger.error(f'{args.custom_mmproj_path} is not a gguf file!')
raise FileNotFoundError
model_path, mmproj_path = args.custom_model_path, args.custom_mmproj_path
else:
if args.custom_model_path is not None and args.custom_mmproj_path is None:
my_logger.warning(f'custom_model_path has been set, but custom_mmproj_path not set. '
f'Will ignore these setting!')
elif args.custom_model_path is None and args.custom_mmproj_path is not None:
my_logger.warning(f'custom_mmproj_path has been set, but custom_model_path not set. '
f'Will ignore these setting!')
# Download llava model and mmproj
if os.path.exists(Path(args.models_save_path)):
models_save_path = Path(args.models_save_path)
else:
models_save_path = Path(os.path.join(Path(__file__).parent, args.models_save_path))
model_path, mmproj_path = download(
logger=my_logger,
config_file=config_file,
model_name=str(args.model_name),
model_site=str(args.model_site),
models_save_path=models_save_path,
use_sdk_cache=True if args.use_sdk_cache else False,
download_method=str(args.download_method)
)
# Load models
model_name = args.model_name
with open(config_file, 'r', encoding='utf-8') as config_json:
datas = json.load(config_json)
chat_format = datas[model_name]["chat_format"]
my_llava = Llava(
logger=my_logger,
args=args,
base_model_path=model_path,
mmproj_model_path=mmproj_path,
use_gpu=False if args.use_cpu else True,
chat_format=chat_format
)
my_llava.load_model()
# Inference
my_llava.inference()
# Unload models
my_llava.unload_model()
def setup_args() -> argparse.ArgumentParser:
args = argparse.ArgumentParser()
args.add_argument(
'data_path',
type=str,
help='path for data.'
)
args.add_argument(
'--recursive',
action='store_true',
help='Include recursive dirs'
)
args.add_argument(
'--config',
type=str,
default='default.json',
help='config json for llava models, default is "default.json"'
)
args.add_argument(
'--use_cpu',
action='store_true',
help='use cpu for inference.'
)
args.add_argument(
'--gpus',
type=int,
default=1,
help='how many gpus used for inference, default is 1'
)
args.add_argument(
'--split_in_gpus',
type=str,
help='weights to split model in multi-gpus for inference.'
)
args.add_argument(
'--n_ctx',
type=int,
default=2048,
help='Text context, set it larger if your image is large, default is 2048.'
)
args.add_argument(
'--image_size',
type=int,
default=1024,
help='resize image to suitable, default is 1024.'
)
args.add_argument(
'--model_name',
type=str,
default='llava-v1.6-34b.Q4_K_M',
help='model name for inference, default is "llava-v1.6-34b.Q4_K_M", please check configs/default.json'
)
args.add_argument(
'--model_site',
type=str,
choices=['huggingface', 'modelscope'],
default='huggingface',
help='download model from model site huggingface or modelscope, default is "huggingface".'
)
args.add_argument(
'--models_save_path',
type=str,
default="models",
help='path to save models, default is "models".'
)
args.add_argument(
'--use_sdk_cache',
action='store_true',
help='use sdk\'s cache dir to store models. \
if this option enabled, "--models_save_path" will be ignored.'
)
args.add_argument(
'--download_method',
type=str,
choices=["SDK", "URL"],
default='SDK',
help='download method via SDK or URL, default is "SDK".'
)
args.add_argument(
'--custom_model_path',
type=str,
default=None,
help='Input custom base model path, you should use --custom_mmproj_path together, '
'otherwise this will be ignored'
)
args.add_argument(
'--custom_mmproj_path',
type=str,
default=None,
help='Input custom mmproj model path, you should use --custom_model_path together, otherwise this will be '
'ignored'
)
args.add_argument(
'--custom_caption_save_path',
type=str,
default=None,
help='Input custom caption file save path.'
)
args.add_argument(
'--log_level',
type=str,
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
default='INFO',
help='set log level, default is "INFO"'
)
args.add_argument(
'--save_logs',
action='store_true',
help='save log file.'
)
args.add_argument(
'--caption_extension',
type=str,
default='.txt',
help='extension of caption file, default is ".txt"'
)
args.add_argument(
'--not_overwrite',
action='store_true',
help='not overwrite caption file if exist.'
)
args.add_argument(
'--system_message',
type=str,
default=DEFAULT_SYSTEM_MESSAGE,
help='system message for llava model.'
)
args.add_argument(
'--user_prompt',
type=str,
default=DEFAULT_USER_PROMPT,
help='user prompt for caption.'
)
args.add_argument(
'--temperature',
type=float,
default=0.4,
help='temperature for llava model.'
)
args.add_argument(
'--max_tokens',
type=int,
default=40,
help='max tokens for output.'
)
args.add_argument(
'--verbose',
action='store_true',
help='llama-cpp-python verbose mode.'
)
return args
if __name__ == "__main__":
args = setup_args()
args = args.parse_args()
main(args)