-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_internal_vip-llava-13b-hf_model.py
324 lines (238 loc) · 13 KB
/
run_internal_vip-llava-13b-hf_model.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# source
# https://huggingface.co/llava-hf/vip-llava-13b-hf
import os
import csv
import shutil
import requests
from random import shuffle
from glob import glob
from PIL import Image
import time
import torch
from transformers import AutoProcessor, LlavaForConditionalGeneration, VipLlavaForConditionalGeneration
def main():
model_id = "llava-hf/vip-llava-13b-hf"
model = VipLlavaForConditionalGeneration.from_pretrained(
model_id,
torch_dtype=torch.float16,
low_cpu_mem_usage=True,
load_in_4bit=True,
output_hidden_states=True
)
processor = AutoProcessor.from_pretrained(model_id)
directory='/images'
files = [y for x in os.walk(directory) for y in glob(os.path.join(x[0], '*.jpg'))]
shuffle(files)
#os.makedirs('/RESULTS/FIRE_IMAGES/', mode=0o777, exist_ok=True)
CHECKPOINT, RESULTS, INTERNAL_REP, RGB_INTERNAL_REP, IR_INTERNAL_REP = load_checkpoint()
counter=len(CHECKPOINT)
for FILE in files:
if FILE not in CHECKPOINT:
user_prompt="Is there fire or not in the image?"
user_question="Please generate the number 1 if there is fire in the image, otherwise generate the number 0. Only one number has to be generated in your response."
image_file=FILE
pattern='###Assistant:'
response, time=run_llava(model, processor, user_question, user_prompt, image_file)
internal_rep, time=internal_run_llava(model, processor, user_question, user_prompt, image_file)
rgb_response, time=rgb_run_llava(model, processor, user_question, user_prompt, image_file)
rgb_internal_rep, rgb_time=internal_rgb_run_llava(model, processor, user_question, user_prompt, image_file)
ir_response, time=ir_run_llava(model, processor, user_question, user_prompt, image_file)
ir_internal_rep, ir_time=internal_ir_run_llava(model, processor, user_question, user_prompt, image_file)
response=response.split(pattern, 1)[1]
rgb_response=rgb_response.split(pattern, 1)[1]
ir_response=ir_response.split(pattern, 1)[1]
print(FILE)
print(counter)
print(response)
print('--------------->>>>>>>>>>>>>>>>>>')
RESULTS.append(FILE + ',' + response + ',' + rgb_response + ',' + ir_response)
CHECKPOINT.append(FILE)
INTERNAL_REP.append(internal_rep.to('cpu').detach())
RGB_INTERNAL_REP.append(rgb_internal_rep.to('cpu').detach())
IR_INTERNAL_REP.append(ir_internal_rep.to('cpu').detach())
counter += 1
if counter%10 == 0:
save_results(CHECKPOINT=CHECKPOINT, RESULTS=RESULTS, INTERNAL_REP=INTERNAL_REP, RGB_INTERNAL_REP=RGB_INTERNAL_REP, IR_INTERNAL_REP=IR_INTERNAL_REP)
else:
print('-------------------->>>>>>>>>>>>>>>>>>>>')
print('-------------------->>>>>>>>>>>>>>>>>>>>')
print('-------------------->>>>>>>>>>>>>>>>>>>>')
print('-------------------->>>>>>>>>>>>>>>>>>>>')
print('File is in checkpoint!!!!!!!!!!')
print('-------------------->>>>>>>>>>>>>>>>>>>>')
print('-------------------->>>>>>>>>>>>>>>>>>>>')
print('-------------------->>>>>>>>>>>>>>>>>>>>')
print('-------------------->>>>>>>>>>>>>>>>>>>>')
save_results(CHECKPOINT=CHECKPOINT, RESULTS=RESULTS, INTERNAL_REP=INTERNAL_REP, RGB_INTERNAL_REP=RGB_INTERNAL_REP, IR_INTERNAL_REP=IR_INTERNAL_REP)
def load_checkpoint(checkpoint_path='/RESULTS/checkpoint.txt',
output_path='/RESULTS/output.csv',
internal_rep_path='/RESULTS/internal_rep.pt',
rgb_internal_rep_path='/RESULTS/rgb_internal_rep.pt',
ir_internal_rep_path='/RESULTS/ir_internal_rep.pt'):
CHECKPOINT=[]
RESULTS=[]
INTERNAL_REP=[]
RGB_INTERNAL_REP=[]
IR_INTERNAL_REP=[]
if os.path.isfile(checkpoint_path):
with open(checkpoint_path) as file:
CHECKPOINT = [line.rstrip() for line in file]
if os.path.isfile(output_path):
with open(output_path) as file:
RESULTS = [line.rstrip() for line in file]
if os.path.isfile(internal_rep_path):
aux=torch.load(internal_rep_path)
for element in aux:
INTERNAL_REP.append(element.unsqueeze(0))
if os.path.isfile(rgb_internal_rep_path):
aux=torch.load(rgb_internal_rep_path)
for element in aux:
RGB_INTERNAL_REP.append(element.unsqueeze(0))
if os.path.isfile(ir_internal_rep_path):
aux=torch.load(ir_internal_rep_path)
for element in aux:
IR_INTERNAL_REP.append(element.unsqueeze(0))
return CHECKPOINT, RESULTS, INTERNAL_REP, RGB_INTERNAL_REP, IR_INTERNAL_REP
def save_results(CHECKPOINT, RESULTS, INTERNAL_REP, RGB_INTERNAL_REP, IR_INTERNAL_REP,
checkpoint_path='/RESULTS/checkpoint.txt',
output_path='/RESULTS/output.csv',
internal_rep_path='/RESULTS/internal_rep.pt',
rgb_internal_rep_path='/RESULTS/rgb_internal_rep.pt',
ir_internal_rep_path='/RESULTS/ir_internal_rep.pt'):
with open(checkpoint_path, 'w') as f:
for file_line in CHECKPOINT:
f.write(f"{file_line}\n")
os.chmod(checkpoint_path, 0o666)
if len(RESULTS) > 0:
with open(output_path,'w') as result_file:
for file_line in RESULTS:
result_file.write(f"{file_line}\n")
os.chmod(output_path, 0o666)
with torch.no_grad():
if len(INTERNAL_REP) > 0:
aux = torch.Tensor(len(INTERNAL_REP), INTERNAL_REP[0].shape[0], INTERNAL_REP[0].shape[1]).to(INTERNAL_REP[0].device)
torch.cat(INTERNAL_REP, out=aux)
torch.save(aux, internal_rep_path)
os.chmod(internal_rep_path, 0o666)
if len(RGB_INTERNAL_REP) > 0:
aux = torch.Tensor(len(RGB_INTERNAL_REP), RGB_INTERNAL_REP[0].shape[0], RGB_INTERNAL_REP[0].shape[1]).to(RGB_INTERNAL_REP[0].device)
torch.cat(RGB_INTERNAL_REP, out=aux)
torch.save(aux, rgb_internal_rep_path)
os.chmod(rgb_internal_rep_path, 0o666)
if len(IR_INTERNAL_REP) > 0:
aux = torch.Tensor(len(IR_INTERNAL_REP), IR_INTERNAL_REP[0].shape[0], IR_INTERNAL_REP[0].shape[1]).to(IR_INTERNAL_REP[0].device)
torch.cat(IR_INTERNAL_REP, out=aux)
torch.save(aux, ir_internal_rep_path)
os.chmod(ir_internal_rep_path, 0o666)
def run_llava(model, processor,
user_question='What are these?',
user_prompt="A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.",
image_file="http://images.cocodataset.org/val2017/000000039769.jpg"):
prompt = f"{user_prompt}.###Human: <image>\n{user_question}###Assistant:"
#prompt = f"{user_question}.###Human: <image>\n{user_prompt}###Assistant:"
#raw_image = Image.open(requests.get(image_file, stream=True).raw)
raw_image = Image.open(image_file)
inputs = processor(prompt, raw_image, return_tensors='pt').to(0, torch.float16)
start_t = time.time()
output = model.generate(**inputs, max_new_tokens=200, do_sample=False)
end_t = time.time()
#print(processor.decode(output[0][2:], skip_special_tokens=True))
#print('Time elapsed: ', end_t-start_t)
return processor.decode(output[0][2:], skip_special_tokens=True), end_t-start_t
def internal_run_llava(model, processor,
user_question="Can you explain the image in detail?",
user_prompt="A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.",
image_file="http://images.cocodataset.org/val2017/000000039769.jpg"):
#prompt = "USER: <image>\n" + user_prompt + "\nASSISTANT:"
prompt = f"{user_prompt}.###Human: <image>\n{user_question}###Assistant:"
raw_image = Image.open(image_file)
#raw_image = raw_image.resize((int(raw_image.size[0]/8),int(raw_image.size[1]/8)))
inputs = processor(prompt, raw_image, return_tensors='pt').to(0, torch.float16)
with torch.no_grad():
start_t = time.time()
output = model(input_ids = inputs['input_ids'], pixel_values = inputs['pixel_values'], attention_mask=inputs['attention_mask'], output_hidden_states=True)
end_t = time.time()
return output.hidden_states[-1].mean(1), end_t-start_t
def rgb_run_llava(model, processor,
user_question='What are these?',
user_prompt="A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.",
image_file="http://images.cocodataset.org/val2017/000000039769.jpg"):
#prompt = f"{user_question}.###Human: <image>\n{user_prompt}###Assistant:"
prompt = f"{user_prompt}.###Human: <image>\n{user_question}###Assistant:"
#raw_image = Image.open(requests.get(image_file, stream=True).raw)
im = Image.open(image_file)
width, height = im.size
left = 0
top = 0
right = width / 2
bottom = height
raw_image = im.crop((left, top, right, bottom))
inputs = processor(prompt, raw_image, return_tensors='pt').to(0, torch.float16)
start_t = time.time()
output = model.generate(**inputs, max_new_tokens=200, do_sample=False)
end_t = time.time()
#print(processor.decode(output[0][2:], skip_special_tokens=True))
#print('Time elapsed: ', end_t-start_t)
return processor.decode(output[0][2:], skip_special_tokens=True), end_t-start_t
def internal_rgb_run_llava(model, processor,
user_question="Can you explain the image in detail?",
user_prompt="A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.",
image_file="http://images.cocodataset.org/val2017/000000039769.jpg"):
#prompt = "USER: <image>\n" + user_prompt + "\nASSISTANT:"
prompt = f"{user_prompt}.###Human: <image>\n{user_question}###Assistant:"
im = Image.open(image_file)
#im = im.resize((int(im.size[0]/8),int(im.size[1]/8)))
width, height = im.size
left = 0
top = 0
right = width / 2
bottom = height
raw_image = im.crop((left, top, right, bottom))
with torch.no_grad():
inputs = processor(prompt, raw_image, return_tensors='pt').to(0, torch.float16)
start_t = time.time()
output = model(input_ids = inputs['input_ids'], pixel_values = inputs['pixel_values'], attention_mask=inputs['attention_mask'], output_hidden_states=True)
end_t = time.time()
return output.hidden_states[-1].mean(1), end_t-start_t
def ir_run_llava(model, processor,
user_question='What are these?',
user_prompt="A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.",
image_file="http://images.cocodataset.org/val2017/000000039769.jpg"):
#prompt = f"{user_question}.###Human: <image>\n{user_prompt}###Assistant:"
prompt = f"{user_prompt}.###Human: <image>\n{user_question}###Assistant:"
im = Image.open(image_file)
width, height = im.size
left = width / 2
top = 0
right = width
bottom = height
raw_image = im.crop((left, top, right, bottom))
inputs = processor(prompt, raw_image, return_tensors='pt').to(0, torch.float16)
start_t = time.time()
output = model.generate(**inputs, max_new_tokens=200, do_sample=False)
end_t = time.time()
return processor.decode(output[0][2:], skip_special_tokens=True), end_t-start_t
def internal_ir_run_llava(model, processor,
user_question="Can you explain the image in detail?",
user_prompt="A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.",
image_file="http://images.cocodataset.org/val2017/000000039769.jpg"):
#prompt = "USER: <image>\n" + user_prompt + "\nASSISTANT:"
prompt = f"{user_prompt}.###Human: <image>\n{user_question}###Assistant:"
im = Image.open(image_file)
#im = im.resize((int(im.size[0]/8),int(im.size[1]/8)))
width, height = im.size
left = width / 2
top = 0
right = width
bottom = height
raw_image = im.crop((left, top, right, bottom))
with torch.no_grad():
inputs = processor(prompt, raw_image, return_tensors='pt').to(0, torch.float16)
start_t = time.time()
output = model(input_ids = inputs['input_ids'], pixel_values = inputs['pixel_values'], attention_mask=inputs['attention_mask'], output_hidden_states=True)
end_t = time.time()
return output.hidden_states[-1].mean(1), end_t-start_t
# Using the special variable
# __name__
if __name__=="__main__":
main()