许可协议: cc-by-4.0
数据集:
- RussRobin/SpatialQA
语言:
- 英文
标签:
- 具身智能
- 多模态大语言模型
- 视觉语言模型
- 空间理解
- Phi-2
任务标签: 视觉问答
SpatialBot是一款具备空间理解与推理能力的视觉语言模型,能精准解析深度图并执行高级任务。
本HF仓库提供融合版SpatialBot-3B模型,基于Phi-2和SigLIP架构开发。该模型在常规视觉语言任务及SpatialBench等空间理解基准测试中表现优异。
SpatialBot-3B使用指南
重要提示:2024年8月28日更新了仓库和快速启动代码。此前下载的用户请更新模型与代码。
- 首先安装依赖:
pip install torch transformers accelerate pillow numpy
- 运行模型:
import torch
import transformers
from transformers import AutoModelForCausalLM, AutoTokenizer
from PIL import Image
import warnings
import numpy as np
# 禁用部分警告
transformers.logging.set_verbosity_error()
transformers.logging.disable_progress_bar()
warnings.filterwarnings('ignore')
# 设置设备
device = 'cuda' # 或cpu
model_name = 'RussRobin/SpatialBot-3B'
offset_bos = 0
# 创建模型
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.float16, # CPU请使用float32
device_map='auto',
trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained(
model_name,
trust_remote_code=True)
# 文本提示
prompt = '点<0.5,0.2>的深度值是多少?请直接从深度图读取答案。'
text = f"好奇用户与AI助手的对话。助手提供专业、详尽且礼貌的解答。用户: <图像1>\n<图像2>\n{prompt} 助手:"
text_chunks = [tokenizer(chunk).input_ids for chunk in text.split('<图像1>\n<图像2>\n')]
input_ids = torch.tensor(text_chunks[0] + [-201] + [-202] + text_chunks[1][offset_bos:], dtype=torch.long).unsqueeze(0).to(device)
image1 = Image.open('rgb.jpg')
image2 = Image.open('depth.png')
channels = len(image2.getbands())
if channels == 1:
img = np.array(image2)
height, width = img.shape
three_channel_array = np.zeros((height, width, 3), dtype=np.uint8)
three_channel_array[:, :, 0] = (img // 1024) * 4
three_channel_array[:, :, 1] = (img // 32) * 8
three_channel_array[:, :, 2] = (img % 32) * 8
image2 = Image.fromarray(three_channel_array, 'RGB')
image_tensor = model.process_images([image1,image2], model.config).to(dtype=model.dtype, device=device)
# 生成结果
output_ids = model.generate(
input_ids,
images=image_tensor,
max_new_tokens=100,
use_cache=True,
repetition_penalty=1.0 # 增大该值可避免重复输出
)[0]
print(tokenizer.decode(output_ids[input_ids.shape[1]:], skip_special_tokens=True).strip())
论文:
https://arxiv.org/abs/2406.13642
GitHub仓库:
https://github.com/BAAI-DCAI/SpatialBot
基准测试集SpatialBench:
https://huggingface.co/datasets/RussRobin/SpatialBench
SpatialBot-3B的LoRA权重:
https://huggingface.co/RussRobin/SpatialBot-3B-LoRA