Views
No views yet
1from llava.model.builder import load_pretrained_model
2from llava.mm_utils import get_model_name_from_path, process_images, tokenizer_image_token
3from llava.constants import (
4 IMAGE_TOKEN_INDEX, DEFAULT_IMAGE_TOKEN, DEFAULT_IM_START_TOKEN,
5 DEFAULT_IM_END_TOKEN, IGNORE_INDEX
6)
7from llava.conversation import conv_templates, SeparatorStyle
8from datetime import date
9from PIL import Image
10import copy
11import torch
12import requests
13# Model and device configurations
14pretrained = "mylesgoose/Llama-3.1-Minitron-4B-Llava-Nvidia-siglip-ov"
15model_name = "llava_llama3"
16device = "cuda"
17device_map = "auto"
18
19# Load the pretrained model
20tokenizer, model, image_processor, max_length = load_pretrained_model(
21 pretrained, None, model_name, device_map=device_map, attn_implementation="flash_attention_2"
22)
23
24# Set model to evaluation mode and tie weights
25model.eval()
26model.tie_weights()
27
28# Load and process the image
29url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/0052a70beed5bf71b92610a43a52df6d286cd5f3/diffusers/rabbit.jpg"
30image = Image.open(requests.get(url, stream=True).raw)
31image_tensor = process_images([image], image_processor, model.config)
32image_tensor = [_image.to(dtype=torch.float16, device=device) for _image in image_tensor]
33date_string: str = date.today().strftime("%d %b %Y")
34# Conversation template and question
35conv_template = "llava_llama_3"
36question = (
37 DEFAULT_IMAGE_TOKEN + "\nWhat is shown in this image"
38)
39
40# Set up conversation and prepare the prompt
41conv = copy.deepcopy(conv_templates[conv_template])
42conv.append_message(conv.roles[0], question)
43conv.append_message(conv.roles[1], None)
44prompt_question = conv.get_prompt()
45
46# Tokenize the prompt and prepare input for the model
47input_ids = tokenizer_image_token(
48 prompt_question, tokenizer, IMAGE_TOKEN_INDEX, return_tensors="pt"
49).unsqueeze(0).to(device)
50image_sizes = [image.size]
51
52
53# Generate output from the model
54cont = model.generate(
55 input_ids,
56 images=image_tensor,
57 image_sizes=image_sizes,
58 do_sample=True,
59 temperature=0.6,
60 #pad_token_id=128004,
61 #eos_token_id=None,
62 max_new_tokens=131072,
63)
64
65# Decode and print the generated output
66text_outputs = tokenizer.batch_decode(cont, skip_special_tokens=True)
67print(text_outputs)
68


1git clone https://github.com/LLaVA-VL/LLaVA-NeXT
2cd LLaVA-NeXT1conda create -n llava python=3.10 -y
2conda activate llava
3pip install --upgrade pip # Enable PEP 660 support.
4pip install -e ".[train]"cd PATH_TO/sglang1bash examples/usage/llava_video/srt_example_llava_v.sh K 0 YOUR_VIDEO_PATH YOUR_MODEL_PATH FRAMES_PER_VIDEO
2(e.g. bash examples/usage/llava_video/srt_example_llava_v.sh K 0 examples/usage/llava_video/videos/Q98Z4OTh8RwmDonc.mp4 lmms-lab/LLaVA-NeXT-Video-7B-DPO 16)bash examples/usage/llava_video/srt_example_llava_v.sh K 1 YOUR_VIDEO_PATH YOUR_MODEL_PATH FRAMES_PER_VIDEObash examples/usage/llava_video/srt_example_llava_v.sh K K-1 YOUR_VIDEO_PATH YOUR_MODEL_PATH FRAMES_PER_VIDEO1@article{li2024llava,
2 title={LLaVA-NeXT-Interleave: Tackling Multi-image, Video, and 3D in Large Multimodal Models},
3 author={Li, Feng and Zhang, Renrui and Zhang, Hao and Zhang, Yuanhan and Li, Bo and Li, Wei and Ma, Zejun and Li, Chunyuan},
4 journal={arXiv preprint arXiv:2407.07895},
5 year={2024}
6}
7
8@misc{li2024llavanext-ablations,
9 title={LLaVA-NeXT: What Else Influences Visual Instruction Tuning Beyond Data?},
10 url={https://llava-vl.github.io/blog/2024-05-25-llava-next-ablations/},
11 author={Li, Bo and Zhang, Hao and Zhang, Kaichen and Guo, Dong and Zhang, Yuanhan and Zhang, Renrui and Li, Feng and Liu, Ziwei and Li, Chunyuan},
12 month={May},
13 year={2024}
14}
15
16@misc{li2024llavanext-strong,
17 title={LLaVA-NeXT: Stronger LLMs Supercharge Multimodal Capabilities in the Wild},
18 url={https://llava-vl.github.io/blog/2024-05-10-llava-next-stronger-llms/},
19 author={Li, Bo and Zhang, Kaichen and Zhang, Hao and Guo, Dong and Zhang, Renrui and Li, Feng and Zhang, Yuanhan and Liu, Ziwei and Li, Chunyuan},
20 month={May},
21 year={2024}
22}
23
24@misc{zhang2024llavanext-video,
25 title={LLaVA-NeXT: A Strong Zero-shot Video Understanding Model},
26 url={https://llava-vl.github.io/blog/2024-04-30-llava-next-video/},
27 author={Zhang, Yuanhan and Li, Bo and Liu, haotian and Lee, Yong jae and Gui, Liangke and Fu, Di and Feng, Jiashi and Liu, Ziwei and Li, Chunyuan},
28 month={April},
29 year={2024}
30}
31
32@misc{liu2024llavanext,
33 title={LLaVA-NeXT: Improved reasoning, OCR, and world knowledge},
34 url={https://llava-vl.github.io/blog/2024-01-30-llava-next/},
35 author={Liu, Haotian and Li, Chunyuan and Li, Yuheng and Li, Bo and Zhang, Yuanhan and Shen, Sheng and Lee, Yong Jae},
36 month={January},
37 year={2024}
38}
39
40@misc{liu2023improvedllava,
41 title={Improved Baselines with Visual Instruction Tuning},
42 author={Liu, Haotian and Li, Chunyuan and Li, Yuheng and Lee, Yong Jae},
43 publisher={arXiv:2310.03744},
44 year={2023},
45}
46
47@misc{liu2023llava,
48 title={Visual Instruction Tuning},
49 author={Liu, Haotian and Li, Chunyuan and Wu, Qingyang and Lee, Yong Jae},
50 publisher={NeurIPS},
51 year={2023},
52}lmms-eval framework and its core contributors, including Peiyuan Zhang, Fanyi Pu, Joshua Adrian Cahyono, and Kairui Hu, for their support on the evaluation side.