Spaces:
Runtime error
Runtime error
File size: 22,116 Bytes
fdffde6 844a1e9 fdffde6 e82e643 fdffde6 020e358 86468ab 020e358 fdffde6 020e358 fdffde6 020e358 fdffde6 8f10f7b 098738d fdffde6 8f10f7b e82e643 fdffde6 8f10f7b fdffde6 020e358 fdffde6 86468ab 020e358 fdffde6 f22f2c6 fdffde6 674dd26 fdffde6 7f11231 098738d 7f11231 fdffde6 1fb7e67 fdffde6 e82e643 3553543 e82e643 fdffde6 e82e643 7f11231 86468ab e82e643 fdffde6 a8772aa fdffde6 e511b8d fdffde6 674dd26 fdffde6 |
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 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 |
import argparse
import time
from PIL import Image
import torch
import numpy as np
import transformers
from transformers import AutoTokenizer, AutoModelForCausalLM, LlamaTokenizer
from transformers import StoppingCriteria, StoppingCriteriaList
import dataclasses
from enum import auto, Enum
from typing import List, Tuple, Any
import string
import cv2
import gradio as gr
from huggingface_hub import hf_hub_download, login
from open_flamingo.src.factory import create_model_and_transforms
class SeparatorStyle(Enum):
"""Different separator style."""
SINGLE = auto()
TWO = auto()
@dataclasses.dataclass
class Conversation:
"""A class that keeps all conversation history."""
system: str
roles: List[str]
messages: List[List[str]]
offset: int
# system_img: List[Image.Image] = []
sep_style: SeparatorStyle = SeparatorStyle.SINGLE
sep: str = "###"
sep2: str = None
skip_next: bool = False
conv_id: Any = None
def get_prompt(self):
if self.sep_style == SeparatorStyle.SINGLE:
ret = self.system + self.sep
for role, message in self.messages:
if message:
ret += role + ": " + message + self.sep
else:
ret += role + ":"
return ret
elif self.sep_style == SeparatorStyle.TWO:
seps = [self.sep, self.sep2]
ret = self.system + seps[0]
for i, (role, message) in enumerate(self.messages):
if message:
ret += role + ": " + message + seps[i % 2]
else:
ret += role + ":"
return ret
else:
raise ValueError(f"Invalid style: {self.sep_style}")
def append_message(self, role, message):
self.messages.append([role, message])
def to_gradio_chatbot(self):
ret = []
for i, (role, msg) in enumerate(self.messages[self.offset:]):
if i % 2 == 0:
ret.append([msg, None])
else:
ret[-1][-1] = msg
return ret
def copy(self):
return Conversation(
system=self.system,
# system_img=self.system_img,
roles=self.roles,
messages=[[x, y] for x, y in self.messages],
offset=self.offset,
sep_style=self.sep_style,
sep=self.sep,
sep2=self.sep2,
conv_id=self.conv_id)
def dict(self):
return {
"system": self.system,
# "system_img": self.system_img,
"roles": self.roles,
"messages": self.messages,
"offset": self.offset,
"sep": self.sep,
"sep2": self.sep2,
"conv_id": self.conv_id,
}
class StoppingCriteriaSub(StoppingCriteria):
def __init__(self, stops=[], encounters=1):
super().__init__()
self.stops = stops
def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor):
for stop in self.stops:
if torch.all((stop == input_ids[0][-len(stop):])).item():
return True
return False
CONV_VISION = Conversation(
system="Give the following image: <Img>ImageContent</Img>. "
"You will be able to see the image once I provide it to you. Please answer my questions.",
roles=("Human", "Assistant"),
messages=[],
offset=2,
sep_style=SeparatorStyle.SINGLE,
sep="###",
)
def get_outputs(
model,
batch_images,
attention_mask,
max_generation_length,
min_generation_length,
num_beams,
length_penalty,
input_ids,
image_start_index_list=None,
image_nums=None,
bad_words_ids=None,
):
# and torch.cuda.amp.autocast(dtype=torch.float16)
with torch.inference_mode():
outputs = model(
vision_x=batch_images,
lang_x=input_ids,
attention_mask=attention_mask,
labels=None,
image_nums=image_nums,
image_start_index_list=image_start_index_list,
added_bbox_list=None,
add_box=False,
)
# outputs = model.generate(
# batch_images,
# input_ids,
# attention_mask=attention_mask,
# max_new_tokens=max_generation_length,
# min_length=min_generation_length,
# num_beams=num_beams,
# length_penalty=length_penalty,
# image_start_index_list=image_start_index_list,
# image_nums=image_nums,
# bad_words_ids=bad_words_ids,
# )
return outputs
def generate(
idx,
image,
text,
image_processor,
tokenizer,
flamingo,
vis_embed_size=256,
rank=0,
world_size=1,
):
if image is None:
raise gr.Error("Please upload an image.")
flamingo.eval()
loc_token_ids = []
for i in range(1000):
loc_token_ids.append(int(tokenizer(f"<loc_{i}>", add_special_tokens=False)["input_ids"][-1]))
media_token_id = tokenizer("<|#image#|>", add_special_tokens=False)["input_ids"][-1]
endofmedia_token_id = tokenizer("<|#endofimage#|>", add_special_tokens=False)["input_ids"][-1]
pad_token_id = tokenizer(tokenizer.pad_token, add_special_tokens=False)["input_ids"][-1]
bos_token_id = tokenizer(tokenizer.bos_token, add_special_tokens=False)["input_ids"][-1]
prebox_token_id = tokenizer("<|#prebox#|>", add_special_tokens=False)["input_ids"][-1]
image_ori = image
image = image.convert("RGB")
width = image.width
height = image.height
image = image.resize((224, 224))
batch_images = image_processor(image).unsqueeze(0).unsqueeze(1).unsqueeze(0)
if idx == 1:
prompt = [f"{tokenizer.bos_token}<|#image#|>{tokenizer.pad_token * vis_embed_size}<|#endofimage#|><|#object#|> {text.rstrip('.').strip()}<|#endofobject#|><|#visual#|>"]
bad_words_ids = None
max_generation_length = 5
else:
prompt = [f"<|#image#|>{tokenizer.pad_token * vis_embed_size}<|#endofimage#|>{text.rstrip('.')}"]
bad_words_ids = loc_word_ids
max_generation_length = 300
encodings = tokenizer(
prompt,
padding="longest",
truncation=True,
return_tensors="pt",
max_length=2000,
)
input_ids = encodings["input_ids"]
attention_mask = encodings["attention_mask"]
image_start_index_list = ((input_ids == media_token_id).nonzero(as_tuple=True)[-1] + 1).tolist()
image_start_index_list = [[x] for x in image_start_index_list]
image_nums = [1] * len(input_ids)
outputs = get_outputs(
model=flamingo,
batch_images=batch_images,
attention_mask=attention_mask,
max_generation_length=max_generation_length,
min_generation_length=4,
num_beams=1,
length_penalty=1.0,
input_ids=input_ids,
bad_words_ids=bad_words_ids,
image_start_index_list=image_start_index_list,
image_nums=image_nums,
)
boxes = outputs["boxes"]
scores = outputs["scores"]
if len(scores) > 0:
box = boxes[scores.argmax()]/224
print(f"{box}")
if len(boxes)>0:
open_cv_image = np.array(image_ori)
# Convert RGB to BGR
open_cv_image = open_cv_image[:, :, ::-1].copy()
box = box*[width,height,width,height]
# for box in boxes:
open_cv_image = cv2.rectangle(open_cv_image, box[:2].astype(int), box[2:].astype(int), (255, 0, 0), 2)
out_image = Image.fromarray(cv2.cvtColor(open_cv_image, cv2.COLOR_BGR2RGB))
return f"Output:{box}", out_image
else:
gen_text = tokenizer.batch_decode(outputs)
return (f"{gen_text}")
def preprocess_conv(data):
conversation = ""
BEGIN_SIGNAL = "### "
END_SIGNAL = "\n"
for idx, d in enumerate(data):
from_str = d["from"]
if from_str.lower() == "human":
from_str = "Human"
elif from_str.lower() == "gpt":
from_str = "Assistant"
else:
from_str = 'unknown'
conversation += (BEGIN_SIGNAL + from_str + ": " + d["value"] + END_SIGNAL)
return conversation
def preprocess_image(sample, image_processor):
image = image_processor(sample)
if isinstance(image, transformers.image_processing_utils.BatchFeature):
image = torch.tensor(image["pixel_values"][0])
return image
class Chat:
def __init__(self, model, vis_processor, tokenizer, vis_embed_size ):
self.model = model
self.vis_processor = vis_processor
self.tokenizer = tokenizer
self.vis_embed_size = vis_embed_size
self.conv = []
# stop_words_ids = [torch.tensor([835]).to(self.device),
# torch.tensor([2277, 29937]).to(self.device)] # '###' can be encoded in two different ways.
# self.stopping_criteria = StoppingCriteriaList([StoppingCriteriaSub(stops=stop_words_ids)])
def ask(self, text, conv,radio):
if radio in ["Cap"]:
conv.append({
"from": "human",
"value": "",
})
elif radio in ["VQA"]:
conv.append({
"from": "human",
"value": f"Answer the question using a single word or phrase. {text}",
})
elif radio in ["REC"]:
conv.append({
"from": "human",
"value": f"Please provide the bounding box coordinate of the region this sentence describes: {text}.",
})
else:
conv.append({
"from": "human",
"value": text,
})
# if len(conv.messages) > 0 and conv.messages[-1][0] == conv.roles[0] \
# and conv.messages[-1][1][-6:] == '</Img>': # last message is image.
# conv.messages[-1][1] = ' '.join([conv.messages[-1][1], text])
# else:
# conv.append_message(conv.roles[0], text)
def answer(self, conv, img_list, radio, text_input, max_new_tokens=200, num_beams=5, min_length=1, top_p=0.9,
repetition_penalty=1.0, length_penalty=1, temperature=1, max_length=2000):
# conv.append_message(conv.roles[1], None)
# embs = self.get_context_emb(conv, img_list)
#
# # current_max_len = embs.shape[1] + max_new_tokens + 100
# # begin_idx = max(0, current_max_len - max_length)
# # embs = embs[:, begin_idx:]
# outputs = self.model.llama_model.generate(
# inputs_embeds=embs,
# max_new_tokens=max_new_tokens,
# stopping_criteria=self.stopping_criteria,
# num_beams=num_beams,
# min_length=min_length,
# top_p=top_p,
# repetition_penalty=repetition_penalty,
# length_penalty=length_penalty,
# temperature=temperature,
# )
# output_token = outputs[0]
# if output_token[0] == 0:
# output_token = output_token[1:]
# output_text = self.model.llama_tokenizer.decode(output_token, add_special_tokens=False)
# output_text = output_text.split('###')[0] # remove the stop sign '###'
# output_text = output_text.split('Assistant:')[-1].strip()
# conv.messages[-1][1] = output_text
visual_token = "<|#visual#|>"
previsual_token = "<|#previsual#|>"
box_token = "<|#box#|>"
prebox_token = "<|#prebox#|>"
end_token = "<|#endofobject#|>"
object_token = "<|#object#|>"
end_of_attr_token = "<|#endofattr#|>"
preend_of_attr_token = "<|#preendofattr#|>"
media_token_id = self.tokenizer("<|#image#|>", add_special_tokens=False)["input_ids"][-1]
box_token_id = self.tokenizer("<|#box#|>", add_special_tokens=False)["input_ids"][-1]
endofobject_token_id = self.tokenizer("<|#endofobject#|>", add_special_tokens=False)["input_ids"][-1]
endofattr_token_id = self.tokenizer("<|#endofattr#|>", add_special_tokens=False)["input_ids"][-1]
endofmedia_token_id = self.tokenizer("<|#endofimage#|>", add_special_tokens=False)["input_ids"][-1]
visual_token_id = self.tokenizer("<|#visual#|>", add_special_tokens=False)["input_ids"][-1]
previsual_token_id = self.tokenizer("<|#previsual#|>", add_special_tokens=False)["input_ids"][-1]
prebox_token_id = self.tokenizer("<|#prebox#|>", add_special_tokens=False)["input_ids"][-1]
size = 224
self.model.eval()
# "/gpfs/u/home/LMCG/LMCGljnn/scratch-shared/cdl/tmp_img/chat_vis/chat19.png"
# image_path = input("Please enter the image path: ")
image = img_list[0].convert("RGB")
image_ori = image
image = image.resize((size, size))
print(f"image size: {image.size}")
batch_images = preprocess_image(image, self.vis_processor).unsqueeze(0).unsqueeze(1).unsqueeze(0)
# conversation = []
human_sentence = None
if radio in ["Cap","VQA"]:
conv.append({
"from": "gpt",
"value": "",
})
elif radio in ["REC"]:
conv.append(
{
"from": "gpt",
"value": object_token + text_input + end_token + visual_token,
}
)
else:
conv.append({
"from": "gpt",
"value": "",
})
# while True:
# human_sentence = input("### Human: ")
# if human_sentence == "#end#":
# break
# conversation.append({
# "from": "human",
# "value": human_sentence,
# })
# conversation.append({
# "from": "gpt",
# "value": "",
# })
text = preprocess_conv(conv).strip()
caption = f"<|#image#|>{self.tokenizer.pad_token * self.vis_embed_size}<|#endofimage#|>{text}"
encodings = self.tokenizer(
caption,
padding="longest",
truncation=True,
return_tensors="pt",
max_length=2000,
)
input_ids = encodings["input_ids"]
attention_mask = encodings["attention_mask"]
image_start_index_list = ((input_ids == media_token_id).nonzero(as_tuple=True)[-1] + 1).tolist()
image_start_index_list = [[x] for x in image_start_index_list]
image_nums = [1] * len(input_ids)
added_bbox_list = []
with torch.inference_mode():
text_outputs = self.model.generate(
batch_images,
input_ids,
attention_mask=attention_mask,
max_new_tokens=20,
# min_new_tokens=8,
num_beams=1,
# length_penalty=0,
image_start_index_list=image_start_index_list,
image_nums=image_nums,
added_bbox_list=added_bbox_list if len(added_bbox_list) != 0 else None,
)
# and torch.cuda.amp.autocast(dtype=torch.float16)
with torch.no_grad():
outputs = self.model(
vision_x=batch_images,
lang_x=input_ids,
attention_mask=attention_mask,
image_nums=image_nums,
image_start_index_list=image_start_index_list,
added_bbox_list=None,
add_box=False,
)
boxes = outputs["boxes"]
scores = outputs["scores"]
if len(scores) > 0:
box = boxes[scores.argmax()] / 224
print(f"{box}")
out_image = None
if len(boxes)>0:
width, height = image_ori.size
open_cv_image = np.array(image_ori)
# Convert RGB to BGR
open_cv_image = open_cv_image[:, :, ::-1].copy()
box = box * [width, height, width, height]
# for box in boxes:
open_cv_image = cv2.rectangle(open_cv_image, box[:2].astype(int), box[2:].astype(int), (255, 0, 0), 2)
out_image = Image.fromarray(cv2.cvtColor(open_cv_image, cv2.COLOR_BGR2RGB))
# output_token = outputs[0, input_ids.shape[1]:]
# output_text = tokenizer.decode(output_token, skip_special_tokens=True).strip()
# conv[-1]["value"] = output_text
# # conv.messages[-1][1] = output_text
# print(
# f"### Assistant: {tokenizer.decode(outputs[0, input_ids.shape[1]:], skip_special_tokens=True).strip()}")
output_text = self.tokenizer.decode(text_outputs[0])
output_text = re.findall(r'Assistant:(.+)', output_text)[-1]
return output_text, out_image
def upload_img(self, image, conv, img_list):
img_list.append(image)
# if isinstance(image, str): # is a image path
# raw_image = Image.open(image).convert('RGB')
# image = image.resize((224, 224))
# image = self.vis_processor(raw_image).unsqueeze(0).unsqueeze(1).unsqueeze(0)
# elif isinstance(image, Image.Image):
# raw_image = image
# image = image.resize((224, 224))
# image = self.vis_processor(raw_image).unsqueeze(0).unsqueeze(1).unsqueeze(0)
# elif isinstance(image, torch.Tensor):
# if len(image.shape) == 3:
# image = image.unsqueeze(0)
# # image = image.to(self.device)
#
# # image_emb, _ = self.model.encode_img(image)
# img_list.append(image_emb)
# conv.append_message(conv.roles[0], "<Img><ImageHere></Img>")
msg = "Received."
# self.conv.append_message(self.conv.roles[1], msg)
return msg
# def get_context_emb(self, conv, img_list):
# prompt = conv.get_prompt()
# prompt_segs = prompt.split('<ImageHere>')
# assert len(prompt_segs) == len(img_list) + 1, "Unmatched numbers of image placeholders and images."
# seg_tokens = [
# self.model.llama_tokenizer(
# seg, return_tensors="pt", add_special_tokens=i == 0).to(self.device).input_ids
# # only add bos to the first seg
# for i, seg in enumerate(prompt_segs)
# ]
# seg_embs = [self.model.llama_model.model.embed_tokens(seg_t) for seg_t in seg_tokens]
# mixed_embs = [emb for pair in zip(seg_embs[:-1], img_list) for emb in pair] + [seg_embs[-1]]
# mixed_embs = torch.cat(mixed_embs, dim=1)
# return mixed_embs
def evaluate_exp(
model,
tokenizer,
image_processor,
vis_embed_size=None,
rank=0,
world_size=1,
id=0,
add_visual=True,
):
media_token_id = tokenizer("<|#image#|>", add_special_tokens=False)["input_ids"][-1]
box_token_id = tokenizer("<|#box#|>", add_special_tokens=False)["input_ids"][-1]
endofobject_token_id = tokenizer("<|#endofobject#|>", add_special_tokens=False)["input_ids"][-1]
endofattr_token_id = tokenizer("<|#endofattr#|>", add_special_tokens=False)["input_ids"][-1]
endofmedia_token_id = tokenizer("<|#endofimage#|>", add_special_tokens=False)["input_ids"][-1]
visual_token_id = tokenizer("<|#visual#|>", add_special_tokens=False)["input_ids"][-1]
previsual_token_id = tokenizer("<|#previsual#|>", add_special_tokens=False)["input_ids"][-1]
prebox_token_id = tokenizer("<|#prebox#|>", add_special_tokens=False)["input_ids"][-1]
size = image_processor.size["shortest_edge"]
model.eval()
# "/gpfs/u/home/LMCG/LMCGljnn/scratch-shared/cdl/tmp_img/chat_vis/chat19.png"
image_path = input("Please enter the image path: ")
image = Image.open(image_path).convert("RGB")
image = image.resize((size, size))
print(f"image size: {image.size}")
batch_images = preprocess_image(image, image_processor).unsqueeze(0).unsqueeze(1).unsqueeze(0)
conversation = []
human_sentence = None
while True:
human_sentence = input("### Human: ")
if human_sentence == "#end#":
break
conversation.append({
"from": "human",
"value": human_sentence,
})
conversation.append({
"from": "gpt",
"value": "",
})
text = preprocess_conv(conversation).strip()
caption = f"<|#image#|>{tokenizer.pad_token*vis_embed_size}<|#endofimage#|>{text}"
encodings = tokenizer(
caption,
padding="longest",
truncation=True,
return_tensors="pt",
max_length=2000,
)
input_ids = encodings["input_ids"].to("cuda")
attention_mask = encodings["attention_mask"].to("cuda")
image_start_index_list = ((input_ids == media_token_id).nonzero(as_tuple=True)[-1] + 1).tolist()
image_start_index_list = [[x] for x in image_start_index_list]
image_nums = [1] * len(input_ids)
with torch.no_grad() and torch.cuda.amp.autocast(dtype=torch.float16):
outputs = model.generate(
batch_images,
input_ids,
attention_mask=attention_mask,
max_new_tokens=100,
# min_new_tokens=8,
num_beams=1,
image_start_index_list=image_start_index_list,
image_nums=image_nums,
)
print(f"### Assistant: {tokenizer.decode(outputs[0, input_ids.shape[1]:], skip_special_tokens=True).strip()}")
|