Views
No views yet
conda create --name transformers4571 python=3.11
conda activate transformers4571
pip install transformers==4.57.1
pip install torch pillow einops torchvision accelerate decord2logits_processor=model.build_logit_processor_from_inputs(model_inputs)
to enforce points tokens are generated in a valid way.return_pointing_metadata flag.
Then model.extract_video_points does the decoding, it
returns a list of (timestamps, object_id, pixel_x, pixel_y) output points.1from transformers import AutoProcessor, AutoModelForImageTextToText
2import torch
3import numpy as np
4
5checkpoint_dir = "allenai/MolmoPoint-Vid-4B"
6
7model = AutoModelForImageTextToText.from_pretrained(
8 checkpoint_dir,
9 trust_remote_code=True,
10 dtype="auto",
11 device_map="auto",
12)
13
14processor = AutoProcessor.from_pretrained(
15 checkpoint_dir,
16 trust_remote_code=True,
17 padding_side="left",
18)
19
20video_path = "https://storage.googleapis.com/oe-training-public/demo_videos/many_penguins.mp4"
21video_messages = [
22 {
23 "role": "user",
24 "content": [
25 dict(type="text", text="Point to the penguins"),
26 dict(type="video", video=video_path),
27 ]
28 }
29]
30
31inputs = processor.apply_chat_template(
32 video_messages,
33 tokenize=True,
34 add_generation_prompt=True,
35 return_tensors="pt",
36 return_dict=True,
37 padding=True,
38 return_pointing_metadata=True
39)
40metadata = inputs.pop("metadata")
41inputs = {k: v.to("cuda") for k, v in inputs.items()}
42
43with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16):
44 output = model.generate(
45 **inputs,
46 logits_processor=model.build_logit_processor_from_inputs(inputs),
47 max_new_tokens=200
48 )
49
50 generated_tokens = output[:, inputs['input_ids'].size(1):]
51 generated_text = processor.post_process_image_text_to_text(generated_tokens, skip_special_tokens=False, clean_up_tokenization_spaces=False)[0]
52 video_points = model.extract_video_points(
53 generated_text,
54 metadata["token_pooling"],
55 metadata["subpatch_mapping"],
56 metadata["timestamps"],
57 metadata["video_size"]
58 )
59
60 # points as a list of [object_id, image_num, x, y]
61 # For tracking, object_id uniquely identifies objects that might appear multiple frames.
62 print(np.array(video_points))
63# expected:
64[[ 1. 9. 188.86666667 177.65925926]
65 [ 2. 15.5 197.66666667 288.35555556]
66 [ 3. 17. 153.26666667 327.7037037 ]
67 [ 4. 23.5 46.6 406.87407407]
68 [ 5. 23.5 91. 406.87407407]
69 [ 6. 23.5 135.53333333 438.4 ]
70 [ 7. 23.5 233.26666667 477.98518519]
71 [ 8. 25. 184.33333333 280.2962963 ]
72 [ 9. 25. 268.86666667 232.88888889]
73 [ 10. 27. 171. 335.76296296]
74 [ 11. 30. 193.26666667 304. ]
75 [ 12. 32. 64.33333333 201.36296296]
76 [ 13. 32. 202.2 414.6962963 ]
77 [ 14. 38.5 184.33333333 383.17037037]
78 [ 15. 40.5 335.53333333 82.84444444]
79 [ 16. 40.5 117.66666667 201.36296296]
80 [ 17. 40.5 95.53333333 501.68888889]
81 [ 18. 47. 259.93333333 304. ]
82 [ 19. 47. 153.26666667 501.68888889]]