Views
No views yet
1pip install einops torchvision accelerate
2pip install transformers==4.521from transformers import AutoProcessor, AutoModelForImageTextToText
2import torch
3from PIL import Image
4import requests
5from io import BytesIO
6
7ckpt = "allenai/MolmoAct-7B-D-LIBERO-Object-0812"
8
9# load the processor
10processor = AutoProcessor.from_pretrained(
11 ckpt,
12 trust_remote_code=True,
13 torch_dtype="bfloat16",
14 device_map="auto",
15 padding_side="left",
16)
17
18# load the model
19model = AutoModelForImageTextToText.from_pretrained(
20 ckpt,
21 trust_remote_code=True,
22 torch_dtype="bfloat16",
23 device_map="auto",
24)
25
26# task instruction
27instruction = "pick up the orange juice and place it in the basket"
28
29# strictly follow this reasoning prompt
30prompt = (
31 f"The task is {instruction}. "
32 "What is the action that the robot should take. "
33 f"To figure out the action that the robot should take to {instruction}, "
34 "let's think through it step by step. "
35 "First, what is the depth map for the first image? "
36 "Second, what is the trajectory of the end effector in the first image? "
37 "Based on the depth map of the first image and the trajectory of the end effector in the first image, "
38 "along with other images from different camera views as additional information, "
39 "what is the action that the robot should take?"
40)
41
42# apply chat template
43text = processor.apply_chat_template(
44 [
45 {
46 "role": "user",
47 "content": [dict(type="text", text=prompt)]
48 }
49 ],
50 tokenize=False,
51 add_generation_prompt=True,
52)
53
54# image observation (side + wrist)
55url1 = "https://huggingface.co/allenai/MolmoAct-7B-D-LIBERO-Object/resolve/main/example_1.png"
56url2 = "https://huggingface.co/allenai/MolmoAct-7B-D-LIBERO-Object/resolve/main/example_2.png"
57r1 = requests.get(url1, headers={"User-Agent": "python-requests"}, timeout=30)
58r1.raise_for_status()
59r2 = requests.get(url2, headers={"User-Agent": "python-requests"}, timeout=30)
60r2.raise_for_status()
61img1 = Image.open(BytesIO(r1.content)).convert("RGB")
62img2 = Image.open(BytesIO(r2.content)).convert("RGB")
63imgs = [img1, img2]
64
65# process the image and text
66inputs = processor(
67 images=[imgs],
68 text=text,
69 padding=True,
70 return_tensors="pt",
71)
72
73# move inputs to the correct device
74inputs = {k: v.to(model.device) for k, v in inputs.items()}
75
76# generate output
77with torch.inference_mode():
78 with torch.autocast("cuda", enabled=True, dtype=torch.bfloat16):
79 generated_ids = model.generate(**inputs, max_new_tokens=512)
80
81# only get generated tokens; decode them to text
82generated_tokens = generated_ids[:, inputs['input_ids'].size(1):]
83generated_text = processor.batch_decode(generated_tokens, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
84
85# print the generated text
86print(f"generated text: {generated_text}")
87
88# >>> The depth map of the first image is ... The trajectory of the end effector in the first image is ...
89# Based on these information, along with other images from different camera views as additional information,
90# the action that the robot should take is ...
91
92# parse out all depth perception tokens
93depth = model.parse_depth(generated_text)
94print(f"generated depth perception tokens: {depth}")
95
96# >>> [ "<DEPTH_START><DEPTH_1><DEPTH_2>...<DEPTH_END>" ]
97
98# parse out all visual reasoning traces
99trace = model.parse_trace(generated_text)
100print(f"generated visual reasoning trace: {trace}")
101
102# >>> [ [[242, 115], [140, 77], [94, 58], [140, 44], [153, 26]]] ]
103
104# parse out all 8 action chunks, unnormalizing with key "libero_object_no_noops_modified"
105action = model.parse_action(generated_text, unnorm_key="libero_object_no_noops_modified")
106print(f"generated action: {action}")
107
108# >>> [ [0.0732076061122558, 0.08228153779226191, -0.027760173818644346,
109# 0.15932856272248652, -0.09686601126895233, 0.043916773912953344,
110# 0.996078431372549], ... ]1@misc{molmoact2025,
2 title={MolmoAct: Action Reasoning Models that can Reason in Space},
3 author={Jason Lee and Jiafei Duan and Haoquan Fang and Yuquan Deng and Shuo Liu and Boyang Li and Bohan Fang and Jieyu Zhang and Yi Ru Wang and Sangho Lee and Winson Han and Wilbert Pumacay and Angelica Wu and Rose Hendrix and Karen Farley and Eli VanderBilt and Ali Farhadi and Dieter Fox and Ranjay Krishna},
4 year={2025},
5 eprint={2508.07917},
6 archivePrefix={arXiv},
7 primaryClass={cs.RO},
8 url={https://arxiv.org/abs/2508.07917}
9}