Update README.md
Browse files
README.md
CHANGED
|
@@ -11,5 +11,52 @@ Long context capability can **zero-shot transfer** from language to vision.
|
|
| 11 |
|
| 12 |
LongVA can process **2000** frames or over **200K** visual tokens. It achieves **state-of-the-art** performance on Video-MME among 7B models.
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
|
|
|
|
| 11 |
|
| 12 |
LongVA can process **2000** frames or over **200K** visual tokens. It achieves **state-of-the-art** performance on Video-MME among 7B models.
|
| 13 |
|
| 14 |
+
# Usage
|
| 15 |
+
|
| 16 |
+
First follow the instructions in [our repo](https://github.com/EvolvingLMMs-Lab/LongVA) to install relevant packages.
|
| 17 |
+
|
| 18 |
+
```python
|
| 19 |
+
from longva.model.builder import load_pretrained_model
|
| 20 |
+
from longva.mm_utils import tokenizer_image_token, process_images
|
| 21 |
+
from longva.constants import IMAGE_TOKEN_INDEX
|
| 22 |
+
from PIL import Image
|
| 23 |
+
from decord import VideoReader, cpu
|
| 24 |
+
import torch
|
| 25 |
+
import numpy as np
|
| 26 |
+
# fix seed
|
| 27 |
+
torch.manual_seed(0)
|
| 28 |
+
|
| 29 |
+
model_path = "lmms-lab/LongVA-7B-DPO"
|
| 30 |
+
image_path = "local_demo/assets/lmms-eval.png"
|
| 31 |
+
video_path = "local_demo/assets/dc_demo.mp4"
|
| 32 |
+
max_frames_num = 16 # you can change this to several thousands so long you GPU memory can handle it :)
|
| 33 |
+
gen_kwargs = {"do_sample": True, "temperature": 0.5, "top_p": None, "num_beams": 1, "use_cache": True, "max_new_tokens": 1024}
|
| 34 |
+
tokenizer, model, image_processor, _ = load_pretrained_model(model_path, None, "llava_qwen", device_map="cuda:0")
|
| 35 |
+
|
| 36 |
+
#image input
|
| 37 |
+
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"
|
| 38 |
+
input_ids = tokenizer_image_token(prompt, tokenizer, IMAGE_TOKEN_INDEX, return_tensors="pt").unsqueeze(0).to(model.device)
|
| 39 |
+
image = Image.open(image_path).convert("RGB")
|
| 40 |
+
images_tensor = process_images([image], image_processor, model.config).to(model.device, dtype=torch.float16)
|
| 41 |
+
with torch.inference_mode():
|
| 42 |
+
output_ids = model.generate(input_ids, images=[images_tensor], image_sizes=[image.size], modalities=["image"], **gen_kwargs)
|
| 43 |
+
outputs = tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0].strip()
|
| 44 |
+
print(outputs)
|
| 45 |
+
print("-"*50)
|
| 46 |
+
|
| 47 |
+
#video input
|
| 48 |
+
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"
|
| 49 |
+
input_ids = tokenizer_image_token(prompt, tokenizer, IMAGE_TOKEN_INDEX, return_tensors="pt").unsqueeze(0).to(model.device)
|
| 50 |
+
vr = VideoReader(video_path, ctx=cpu(0))
|
| 51 |
+
total_frame_num = len(vr)
|
| 52 |
+
uniform_sampled_frames = np.linspace(0, total_frame_num - 1, max_frames_num, dtype=int)
|
| 53 |
+
frame_idx = uniform_sampled_frames.tolist()
|
| 54 |
+
frames = vr.get_batch(frame_idx).asnumpy()
|
| 55 |
+
video_tensor = image_processor.preprocess(frames, return_tensors="pt")["pixel_values"].to(model.device, dtype=torch.float16)
|
| 56 |
+
with torch.inference_mode():
|
| 57 |
+
output_ids = model.generate(input_ids, images=[video_tensor], modalities=["video"], **gen_kwargs)
|
| 58 |
+
outputs = tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0].strip()
|
| 59 |
+
print(outputs)
|
| 60 |
+
```
|
| 61 |
|
| 62 |
|