Views
No views yet
pip install git+https://github.com/LLaVA-VL/LLaVA-NeXT.git Exception: data did not match any variant of untagged enum ModelWrapper at line 757272 column 3transformers, try to update it:pip install -U transformers1RuntimeError: Error(s) in loading state_dict for CLIPVisionModel:
2 size mismatch for vision_model.embeddings.position_embedding.weight: copying a param with shape torch.Size([729, 1152]) from checkpoint, the shape in current model is torch.Size([730, 1152]).
3 You may consider adding `ignore_mismatched_sizes=True` in the model `from_pretrained` method.pip install git+https://github.com/inst-it/LLaVA-NeXT.gitgit clone https://github.com/LLaVA-VL/LLaVA-NeXT.gitline 17 of llava/model/multimodal_encoder/builder.py.1# Before modification:
2if is_absolute_path_exists or vision_tower.startswith("openai") or vision_tower.startswith("laion") or "ShareGPT4V" in vision_tower:
3
4# After modification:
5if "clip" in vision_tower or vision_tower.startswith("openai") or vision_tower.startswith("laion") or "ShareGPT4V" in vision_tower:1cd LLaVA-NeXT
2pip install --upgrade pip # Enable PEP 660 support.
3pip install -e ".[train]"1from llava.model.builder import load_pretrained_model
2from llava.constants import DEFAULT_IMAGE_TOKEN
3
4from llava.mm_utils import (
5 KeywordsStoppingCriteria,
6 get_model_name_from_path,
7 tokenizer_image_token,
8 process_images
9)
10from llava.conversation import SeparatorStyle, conv_templates
11from llava.eval.model_vqa import preprocess_qwen
12
13overwrite_config = {}
14overwrite_config["mm_spatial_pool_stride"] = 2
15overwrite_config["mm_spatial_pool_mode"] = 'bilinear'
16overwrite_config["mm_pooling_position"] = 'after'
17overwrite_config["mm_newline_position"] = 'no_token'
18
19model_path = "Inst-IT/LLaVA-Next-Inst-It-Qwen2-7B"
20model_name = get_model_name_from_path(model_path)
21
22tokenizer, model, image_processor, max_length = load_pretrained_model(
23 model_path=model_path,
24 model_base=None,
25 model_name=model_name,
26 device_map="auto",
27 torch_dtype='bfloat16',
28 overwrite_config=overwrite_config,
29 attn_implementation='sdpa')1import torch
2import requests
3from PIL import Image
4
5img_url = "https://github.com/inst-it/inst-it/blob/main/assets/demo/image.jpg?raw=true"
6image = Image.open(requests.get(img_url, stream=True).raw)
7image_tensor = process_images([image], image_processor, model.config).bfloat16()
8image_sizes = [image.size]
9
10question = "Describe this image."
11question = DEFAULT_IMAGE_TOKEN + "\n" + question
12
13conv_template = 'qwen_1_5'
14conv = conv_templates[conv_template].copy()
15conv.append_message(conv.roles[0], question)
16conv.append_message(conv.roles[1], None)
17prompt = conv.get_prompt()
18
19input_ids = preprocess_qwen([{'from': 'human','value': question},{'from': 'gpt','value': None}], tokenizer, has_image=True).cuda()
20
21pad_token_ids = tokenizer.pad_token_id if tokenizer.pad_token_id is not None else tokenizer.eos_token_id
22attention_masks = input_ids.ne(pad_token_ids).long().cuda()
23
24stop_str = conv.sep if conv.sep_style != SeparatorStyle.TWO else conv.sep2
25keywords = [stop_str]
26stopping_criteria = KeywordsStoppingCriteria(keywords, tokenizer, input_ids)
27
28with torch.inference_mode():
29 output_ids = model.generate(
30 inputs=input_ids,
31 images=image_tensor,
32 attention_mask=attention_masks,
33 modalities="image",
34 image_sizes=image_sizes,
35 use_cache=True,
36 stopping_criteria=[stopping_criteria],
37 max_new_tokens=4096
38 )
39
40pred = tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0].strip()
41print(pred)1import torch
2import requests
3from PIL import Image
4
5img_url = "https://github.com/inst-it/inst-it/blob/main/assets/demo/image_som.jpg?raw=true"
6image = Image.open(requests.get(img_url, stream=True).raw)
7image_tensor = process_images([image], image_processor, model.config).bfloat16()
8image_sizes = [image.size]
9
10# You can use [id] to refer to the instances that you are interested in
11question = "Describe [8] in detail."
12question = DEFAULT_IMAGE_TOKEN + "\n" + question
13
14conv_template = 'qwen_1_5'
15conv = conv_templates[conv_template].copy()
16conv.append_message(conv.roles[0], question)
17conv.append_message(conv.roles[1], None)
18prompt = conv.get_prompt()
19
20input_ids = preprocess_qwen([{'from': 'human','value': question},{'from': 'gpt','value': None}], tokenizer, has_image=True).cuda()
21
22pad_token_ids = tokenizer.pad_token_id if tokenizer.pad_token_id is not None else tokenizer.eos_token_id
23attention_masks = input_ids.ne(pad_token_ids).long().cuda()
24
25stop_str = conv.sep if conv.sep_style != SeparatorStyle.TWO else conv.sep2
26keywords = [stop_str]
27stopping_criteria = KeywordsStoppingCriteria(keywords, tokenizer, input_ids)
28
29with torch.inference_mode():
30 output_ids = model.generate(
31 inputs=input_ids,
32 images=image_tensor,
33 attention_mask=attention_masks,
34 modalities="image",
35 image_sizes=image_sizes,
36 use_cache=True,
37 stopping_criteria=[stopping_criteria],
38 max_new_tokens=4096
39 )
40
41pred = tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0].strip()
42print(pred)1import torch
2import requests
3from PIL import Image
4
5frame_urls = [
6 "https://github.com/inst-it/inst-it/blob/main/assets/demo/frame_1.jpg?raw=true",
7 "https://github.com/inst-it/inst-it/blob/main/assets/demo/frame_2.jpg?raw=true",
8 "https://github.com/inst-it/inst-it/blob/main/assets/demo/frame_3.jpg?raw=true",
9 "https://github.com/inst-it/inst-it/blob/main/assets/demo/frame_4.jpg?raw=true",
10 "https://github.com/inst-it/inst-it/blob/main/assets/demo/frame_5.jpg?raw=true",
11 "https://github.com/inst-it/inst-it/blob/main/assets/demo/frame_6.jpg?raw=true",
12 "https://github.com/inst-it/inst-it/blob/main/assets/demo/frame_7.jpg?raw=true",
13 "https://github.com/inst-it/inst-it/blob/main/assets/demo/frame_8.jpg?raw=true"
14]
15video = [Image.open(requests.get(frame_url, stream=True).raw) for frame_url in frame_urls]
16video = image_processor.preprocess(video, return_tensors="pt")["pixel_values"].cuda()
17video = video.bfloat16()
18videos = [video]
19
20question = "Describe the video." # overall video caption
21question = "What happens at frame <1>?" # caption a specific moment
22question = DEFAULT_IMAGE_TOKEN + "\n" + question
23
24conv_template = 'qwen_1_5'
25conv = conv_templates[conv_template].copy()
26conv.append_message(conv.roles[0], question)
27conv.append_message(conv.roles[1], None)
28prompt = conv.get_prompt()
29
30input_ids = preprocess_qwen([{'from': 'human','value': question},{'from': 'gpt','value': None}], tokenizer, has_image=True).cuda()
31
32pad_token_ids = tokenizer.pad_token_id if tokenizer.pad_token_id is not None else tokenizer.eos_token_id
33attention_masks = input_ids.ne(pad_token_ids).long().cuda()
34
35stop_str = conv.sep if conv.sep_style != SeparatorStyle.TWO else conv.sep2
36keywords = [stop_str]
37stopping_criteria = KeywordsStoppingCriteria(keywords, tokenizer, input_ids)
38
39with torch.inference_mode():
40 output_ids = model.generate(
41 inputs=input_ids,
42 images=videos,
43 attention_mask=attention_masks,
44 modalities="video",
45 use_cache=True,
46 stopping_criteria=[stopping_criteria],
47 max_new_tokens=4096
48 )
49
50pred = tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0].strip()
51print(pred)1import torch
2import requests
3from PIL import Image
4
5frame_urls = [
6 "https://github.com/inst-it/inst-it/blob/main/assets/demo/som_frame_1.jpg?raw=true",
7 "https://github.com/inst-it/inst-it/blob/main/assets/demo/som_frame_2.jpg?raw=true",
8 "https://github.com/inst-it/inst-it/blob/main/assets/demo/som_frame_3.jpg?raw=true",
9 "https://github.com/inst-it/inst-it/blob/main/assets/demo/som_frame_4.jpg?raw=true",
10 "https://github.com/inst-it/inst-it/blob/main/assets/demo/som_frame_5.jpg?raw=true",
11 "https://github.com/inst-it/inst-it/blob/main/assets/demo/som_frame_6.jpg?raw=true",
12 "https://github.com/inst-it/inst-it/blob/main/assets/demo/som_frame_7.jpg?raw=true",
13 "https://github.com/inst-it/inst-it/blob/main/assets/demo/som_frame_8.jpg?raw=true"
14]
15video = [Image.open(requests.get(frame_url, stream=True).raw) for frame_url in frame_urls]
16video = image_processor.preprocess(video, return_tensors="pt")["pixel_values"].cuda()
17video = video.bfloat16()
18videos = [video]
19
20# You can use [id] to refer to the instances that you are interested in
21question = "Is [3] visible at <1>?"
22question = DEFAULT_IMAGE_TOKEN + "\n" + question
23
24conv_template = 'qwen_1_5'
25conv = conv_templates[conv_template].copy()
26conv.append_message(conv.roles[0], question)
27conv.append_message(conv.roles[1], None)
28prompt = conv.get_prompt()
29
30input_ids = preprocess_qwen([{'from': 'human','value': question},{'from': 'gpt','value': None}], tokenizer, has_image=True).cuda()
31
32pad_token_ids = tokenizer.pad_token_id if tokenizer.pad_token_id is not None else tokenizer.eos_token_id
33attention_masks = input_ids.ne(pad_token_ids).long().cuda()
34
35stop_str = conv.sep if conv.sep_style != SeparatorStyle.TWO else conv.sep2
36keywords = [stop_str]
37stopping_criteria = KeywordsStoppingCriteria(keywords, tokenizer, input_ids)
38
39with torch.inference_mode():
40 output_ids = model.generate(
41 inputs=input_ids,
42 images=videos,
43 attention_mask=attention_masks,
44 modalities="video",
45 use_cache=True,
46 stopping_criteria=[stopping_criteria],
47 max_new_tokens=4096
48 )
49
50pred = tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0].strip()
51print(pred)1@article{peng2024inst,
2 title={Inst-IT: Boosting Multimodal Instance Understanding via Explicit Visual Prompt Instruction Tuning},
3 author={Peng, Wujian and Meng, Lingchen and Chen, Yitong and Xie, Yiweng and Liu, Yang and Gui, Tao and Xu, Hang and Qiu, Xipeng and Wu, Zuxuan and Jiang, Yu-Gang},
4 journal={arXiv preprint arXiv:2412.03565},
5 year={2024}
6}