Delete test_molmo2.py
Browse files- test_molmo2.py +0 -133
test_molmo2.py
DELETED
|
@@ -1,133 +0,0 @@
|
|
| 1 |
-
import argparse
|
| 2 |
-
from PIL import Image
|
| 3 |
-
import requests
|
| 4 |
-
|
| 5 |
-
import torch
|
| 6 |
-
|
| 7 |
-
from transformers import AutoProcessor, AutoModelForImageTextToText
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
video_path = "https://storage.googleapis.com/oe-training-public/demo_videos/many_penguins.mp4"
|
| 11 |
-
image1_path = "https://picsum.photos/id/237/536/354"
|
| 12 |
-
image2_path = "https://vllm-public-assets.s3.us-west-2.amazonaws.com/vision_model_images/cherry_blossom.jpg"
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
def main():
|
| 16 |
-
parser = argparse.ArgumentParser(
|
| 17 |
-
description="Test Molmo2 HF-compatible model."
|
| 18 |
-
)
|
| 19 |
-
parser.add_argument("checkpoint_dir", help="Location of Molmo2 checkpoint.")
|
| 20 |
-
args = parser.parse_args()
|
| 21 |
-
|
| 22 |
-
processor = AutoProcessor.from_pretrained(
|
| 23 |
-
args.checkpoint_dir,
|
| 24 |
-
trust_remote_code=True,
|
| 25 |
-
dtype="auto",
|
| 26 |
-
device_map="auto",
|
| 27 |
-
padding_side="left",
|
| 28 |
-
)
|
| 29 |
-
|
| 30 |
-
model = AutoModelForImageTextToText.from_pretrained(
|
| 31 |
-
args.checkpoint_dir,
|
| 32 |
-
trust_remote_code=True,
|
| 33 |
-
dtype="auto",
|
| 34 |
-
device_map="auto",
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
-
single_image_messages = [
|
| 38 |
-
{
|
| 39 |
-
"role": "user",
|
| 40 |
-
"content": [
|
| 41 |
-
dict(type="text", text="Describe this image."),
|
| 42 |
-
dict(type="image", image=Image.open(requests.get(image1_path, stream=True).raw)),
|
| 43 |
-
]
|
| 44 |
-
}
|
| 45 |
-
]
|
| 46 |
-
|
| 47 |
-
multi_image_messages = [
|
| 48 |
-
{
|
| 49 |
-
"role": "user",
|
| 50 |
-
"content": [
|
| 51 |
-
dict(type="text", text="Compare these images."),
|
| 52 |
-
dict(
|
| 53 |
-
type="image",
|
| 54 |
-
image=Image.open(requests.get(image1_path, stream=True).raw),
|
| 55 |
-
),
|
| 56 |
-
dict(
|
| 57 |
-
type="image",
|
| 58 |
-
image=Image.open(requests.get(image2_path, stream=True).raw),
|
| 59 |
-
),
|
| 60 |
-
],
|
| 61 |
-
}
|
| 62 |
-
]
|
| 63 |
-
|
| 64 |
-
video_messages = [
|
| 65 |
-
{
|
| 66 |
-
"role": "user",
|
| 67 |
-
"content": [
|
| 68 |
-
dict(type="text", text="Which animal appears in the video?"),
|
| 69 |
-
dict(type="video", video=video_path),
|
| 70 |
-
]
|
| 71 |
-
}
|
| 72 |
-
]
|
| 73 |
-
|
| 74 |
-
single_image_inputs = processor.apply_chat_template(
|
| 75 |
-
single_image_messages,
|
| 76 |
-
tokenize=True,
|
| 77 |
-
add_generation_prompt=True,
|
| 78 |
-
return_tensors="pt",
|
| 79 |
-
return_dict=True,
|
| 80 |
-
)
|
| 81 |
-
|
| 82 |
-
single_image_inputs = {k: v.to(model.device) for k, v in single_image_inputs.items()}
|
| 83 |
-
|
| 84 |
-
with torch.inference_mode():
|
| 85 |
-
with torch.autocast("cuda", enabled=True, dtype=torch.bfloat16):
|
| 86 |
-
single_image_generated_ids = model.generate(**single_image_inputs, max_new_tokens=448)
|
| 87 |
-
single_image_generated_tokens = single_image_generated_ids[:, single_image_inputs['input_ids'].size(1):]
|
| 88 |
-
single_image_generated_text = processor.post_process_image_text_to_text(
|
| 89 |
-
single_image_generated_tokens, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
| 90 |
-
)[0]
|
| 91 |
-
print(single_image_generated_text)
|
| 92 |
-
|
| 93 |
-
multi_image_inputs = processor.apply_chat_template(
|
| 94 |
-
multi_image_messages,
|
| 95 |
-
tokenize=True,
|
| 96 |
-
add_generation_prompt=True,
|
| 97 |
-
return_tensors="pt",
|
| 98 |
-
return_dict=True,
|
| 99 |
-
)
|
| 100 |
-
|
| 101 |
-
multi_image_inputs = {k: v.to(model.device) for k, v in multi_image_inputs.items()}
|
| 102 |
-
|
| 103 |
-
with torch.inference_mode():
|
| 104 |
-
with torch.autocast("cuda", enabled=True, dtype=torch.bfloat16):
|
| 105 |
-
multi_image_generated_ids = model.generate(**multi_image_inputs, max_new_tokens=448)
|
| 106 |
-
multi_image_generated_tokens = multi_image_generated_ids[:, multi_image_inputs['input_ids'].size(1):]
|
| 107 |
-
multi_image_generated_text = processor.post_process_image_text_to_text(
|
| 108 |
-
multi_image_generated_tokens, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
| 109 |
-
)[0]
|
| 110 |
-
print(multi_image_generated_text)
|
| 111 |
-
|
| 112 |
-
video_inputs = processor.apply_chat_template(
|
| 113 |
-
video_messages,
|
| 114 |
-
tokenize=True,
|
| 115 |
-
add_generation_prompt=True,
|
| 116 |
-
return_tensors="pt",
|
| 117 |
-
return_dict=True,
|
| 118 |
-
)
|
| 119 |
-
|
| 120 |
-
video_inputs = {k: v.to(model.device) for k, v in video_inputs.items()}
|
| 121 |
-
|
| 122 |
-
with torch.inference_mode():
|
| 123 |
-
with torch.autocast("cuda", enabled=True, dtype=torch.bfloat16):
|
| 124 |
-
video_generated_ids = model.generate(**video_inputs, max_new_tokens=2048)
|
| 125 |
-
video_generated_tokens = video_generated_ids[:, video_inputs['input_ids'].size(1):]
|
| 126 |
-
video_generated_text = processor.post_process_image_text_to_text(
|
| 127 |
-
video_generated_tokens, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
| 128 |
-
)[0]
|
| 129 |
-
print(video_generated_text)
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
if __name__ == "__main__":
|
| 133 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|