Upload root_scripts/test_single_q.py with huggingface_hub
Browse files
root_scripts/test_single_q.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import os, re
|
| 6 |
+
|
| 7 |
+
MODEL_PATH = "/workspace/rl4phyx/models/Qwen2.5-VL-3B-Instruct"
|
| 8 |
+
IMAGE_PATH = "/workspace/rl4phyx/RL4Phyx/ZeroSearch/One-Shot-RLVR/data/train/physics_vlm/mechanics/100.png"
|
| 9 |
+
|
| 10 |
+
PROMPT = """Look at the image and answer the physics question.
|
| 11 |
+
|
| 12 |
+
A patient with a dislocated shoulder is put into a traction apparatus as shown in figure. The pulls A and B have equal magnitudes and must combine to produce an outward traction force of 12.8 N on the patient's arm.
|
| 13 |
+
|
| 14 |
+
Question: How large should these pulls be?
|
| 15 |
+
|
| 16 |
+
Options:
|
| 17 |
+
A:"7.55N", B:"5.55N", C:"7.65N", D:"6.65N"
|
| 18 |
+
|
| 19 |
+
Please analyze the problem step by step and provide your final answer within \\boxed{}."""
|
| 20 |
+
|
| 21 |
+
print("Loading model...", flush=True)
|
| 22 |
+
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
| 23 |
+
MODEL_PATH, torch_dtype=torch.bfloat16, device_map="auto",
|
| 24 |
+
attn_implementation="sdpa"
|
| 25 |
+
)
|
| 26 |
+
processor = AutoProcessor.from_pretrained(MODEL_PATH)
|
| 27 |
+
print("Model loaded!", flush=True)
|
| 28 |
+
|
| 29 |
+
image = Image.open(IMAGE_PATH).convert("RGB")
|
| 30 |
+
print(f"Image loaded: {image.size}", flush=True)
|
| 31 |
+
|
| 32 |
+
results = []
|
| 33 |
+
for trial in range(10):
|
| 34 |
+
messages = [{"role": "user", "content": [
|
| 35 |
+
{"type": "image", "image": image},
|
| 36 |
+
{"type": "text", "text": PROMPT}
|
| 37 |
+
]}]
|
| 38 |
+
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 39 |
+
inputs = processor(text=[text], images=[image], return_tensors="pt", padding=True).to(model.device)
|
| 40 |
+
|
| 41 |
+
with torch.no_grad():
|
| 42 |
+
out = model.generate(**inputs, max_new_tokens=1024, temperature=0.7, do_sample=True, top_p=0.9)
|
| 43 |
+
|
| 44 |
+
response = processor.decode(out[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
|
| 45 |
+
|
| 46 |
+
boxed = re.search(r'\\boxed\{(.+?)\}', response)
|
| 47 |
+
answer = boxed.group(1) if boxed else "N/A"
|
| 48 |
+
|
| 49 |
+
# Check if correct (GT is A / 7.55N)
|
| 50 |
+
correct = "A" in answer or "7.55" in answer
|
| 51 |
+
results.append({"trial": trial+1, "answer": answer, "correct": correct})
|
| 52 |
+
|
| 53 |
+
print(f"\n=== Trial {trial+1}/10 ===")
|
| 54 |
+
print(f"Extracted answer: {answer}")
|
| 55 |
+
print(f"Correct: {correct}")
|
| 56 |
+
print(f"Full response:\n{response}")
|
| 57 |
+
print(f"{'='*50}", flush=True)
|
| 58 |
+
|
| 59 |
+
print(f"\n\n{'='*60}")
|
| 60 |
+
print(f"SUMMARY: GT = A (7.55N)")
|
| 61 |
+
print(f"{'='*60}")
|
| 62 |
+
correct_count = sum(1 for r in results if r["correct"])
|
| 63 |
+
for r in results:
|
| 64 |
+
mark = "OK" if r["correct"] else "WRONG"
|
| 65 |
+
print(f" Trial {r['trial']:2d}: {r['answer']:20s} [{mark}]")
|
| 66 |
+
print(f"\nAccuracy: {correct_count}/10 ({correct_count*10}%)")
|
| 67 |
+
print(f"{'='*60}", flush=True)
|