PY007 commited on
Commit
74620c2
β€’
1 Parent(s): 9ec0a94

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +55 -0
README.md ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # LongVA
2
+ <p align="center">
3
+ <img src="https://i.postimg.cc/4xFmj8wd/v-niah.png" width="800">
4
+ </p>
5
+ <p align="center">
6
+ 🌐 <a href="https://lmms-lab.github.io/posts/longva/" target="_blank">Blog</a> | πŸ“ƒ <a href="https://arxiv.org/abs/2406.16852" target="_blank">Paper</a> | πŸ€— <a href="https://huggingface.co/collections/lmms-lab/longva-667538e09329dbc7ea498057" target="_blank">Hugging Face</a> | πŸŽ₯ <a href="https://longva-demo.lmms-lab.com/" target="_blank">Demo</a>
7
+ </p>
8
+ Long context capability can **zero-shot transfer** from language to vision.
9
+
10
+ LongVA can process **2000** frames or over **200K** visual tokens. It achieves **state-of-the-art** performance on Video-MME among 7B models.
11
+
12
+ # Usage
13
+
14
+ First follow the instructions in [our repo](https://github.com/EvolvingLMMs-Lab/LongVA) to install relevant packages.
15
+
16
+ ```python
17
+ from longva.model.builder import load_pretrained_model
18
+ from longva.mm_utils import tokenizer_image_token, process_images
19
+ from longva.constants import IMAGE_TOKEN_INDEX
20
+ from PIL import Image
21
+ from decord import VideoReader, cpu
22
+ import torch
23
+ import numpy as np
24
+ # fix seed
25
+ torch.manual_seed(0)
26
+ model_path = "lmms-lab/LongVA-7B-DPO"
27
+ image_path = "local_demo/assets/lmms-eval.png"
28
+ video_path = "local_demo/assets/dc_demo.mp4"
29
+ max_frames_num = 16 # you can change this to several thousands so long you GPU memory can handle it :)
30
+ gen_kwargs = {"do_sample": True, "temperature": 0.5, "top_p": None, "num_beams": 1, "use_cache": True, "max_new_tokens": 1024}
31
+ tokenizer, model, image_processor, _ = load_pretrained_model(model_path, None, "llava_qwen", device_map="cuda:0")
32
+ #image input
33
+ prompt = "<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n<image>\nDescribe the image in details.<|im_end|>\n<|im_start|>assistant\n"
34
+ input_ids = tokenizer_image_token(prompt, tokenizer, IMAGE_TOKEN_INDEX, return_tensors="pt").unsqueeze(0).to(model.device)
35
+ image = Image.open(image_path).convert("RGB")
36
+ images_tensor = process_images([image], image_processor, model.config).to(model.device, dtype=torch.float16)
37
+ with torch.inference_mode():
38
+ output_ids = model.generate(input_ids, images=[images_tensor], image_sizes=[image.size], modalities=["image"], **gen_kwargs)
39
+ outputs = tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0].strip()
40
+ print(outputs)
41
+ print("-"*50)
42
+ #video input
43
+ prompt = "<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n<image>\nGive a detailed caption of the video as if I am blind.<|im_end|>\n<|im_start|>assistant\n"
44
+ input_ids = tokenizer_image_token(prompt, tokenizer, IMAGE_TOKEN_INDEX, return_tensors="pt").unsqueeze(0).to(model.device)
45
+ vr = VideoReader(video_path, ctx=cpu(0))
46
+ total_frame_num = len(vr)
47
+ uniform_sampled_frames = np.linspace(0, total_frame_num - 1, max_frames_num, dtype=int)
48
+ frame_idx = uniform_sampled_frames.tolist()
49
+ frames = vr.get_batch(frame_idx).asnumpy()
50
+ video_tensor = image_processor.preprocess(frames, return_tensors="pt")["pixel_values"].to(model.device, dtype=torch.float16)
51
+ with torch.inference_mode():
52
+ output_ids = model.generate(input_ids, images=[video_tensor], modalities=["video"], **gen_kwargs)
53
+ outputs = tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0].strip()
54
+ print(outputs)
55
+ ```