Views
No views yet

1from unsloth import FastVisionModel
2from PIL import Image
3from transformers import TextStreamer
4model, processor = FastVisionModel.from_pretrained(
5 'SakalYin\Qwen2-VL-2B-RobotArm-Camera',
6 load_in_4bit=True,
7 local_files_only=True,
8 attn_implementation="flash_attention_2",
9 )
10def resize_image(image_input, size=None):
11 """Load image from path or matrices and resize them"""
12 size = size if size else (854,480)
13 if isinstance(image_input, str):
14 image = Image.open(image_input)
15 else:
16 image = image_input
17 size = size if size else (854,480)
18 image = image.resize(size).convert('RGB')
19 return image
20image_path = "https://i.imgur.com/vAleq1e.png"
21size = (854, 480) # Recommeded Size
22system_message = "You are a Visual Language Model Trained to output robot arm end-effectors parameters. Base on the user requests, locate the appropriate object in the image and you must return the position and orientation to reach it in xml format."
23prompt = "Give me a cup.<camera>0.5,-0.75,1.8</camera>"
24image = resize_image(image_path, size=size)
25messages = [
26 {
27 "role": "system",
28 "content": [{"type": "text", "text": system_message if system_message else "You are a helpful assistant."}],
29 },
30 {"role": "user", "content": [
31 {"type": "image"},
32 {"type": "text", "text": prompt}]
33 }
34]
35input_text = processor.apply_chat_template(messages, add_generation_prompt=True)
36inputs = processor(
37 image,
38 input_text,
39 add_special_tokens=False,
40 return_tensors="pt",
41).to("cuda")
42# Generate text without using the streamer
43text_streamer = TextStreamer(processor, skip_prompt=True)
44generated_tokens = model.generate(
45 **inputs,
46 streamer=text_streamer,
47 max_new_tokens=128,
48 use_cache=True,
49 temperature=1.5,
50 min_p=0.1
51)
52# Decode output properly
53output_text = processor.decode(generated_tokens[0], skip_special_tokens=True).strip()| Model | Euclidean | X MSE | Y MSE | Z MSE | Roll Error | Pitch Error | Yaw Error |
|---|---|---|---|---|---|---|---|
| Qwen2VL-2B (Trained with Camera Location) | 0.1089 | 0.0505 | 0.0575 | 0.0363 | 6.8334 | 5.4204 | 6.7619 |
| Qwen2VL-2B | 0.3865 | 0.1239 | 0.3411 | 0.0000 | 2.1462 | 0.9029 | 1.1926 |
| Qwen2VL-7B | 0.0509 | 0.0305 | 0.0320 | 0.0008 | 0.4148 | 0.1066 | 0.1734 |
| LlaVA-NeXT 7B | 0.0480 | 0.0306 | 0.0296 | 0.0000 | 0.0000 | 0.0000 | 0.0000 |
| Llama Vision 11B | - | - | - | - | - | - | - |