以下是一份详细的、力求最简化的 OpenCLAW 安装与快速使用指南。

核心思想
OpenCLAW 不是一个简单的 pip install 就能直接运行的包,它的“即装即用”指的是在正确配置好基础环境后,可以非常方便地调用其模型进行推理。
准备工作(只需做一次)
-
安装 Conda(推荐) 为了避免包冲突,强烈建议使用 Conda(Miniconda 或 Anaconda)创建独立的 Python 环境。
# 创建并激活一个名为 openclaw 的 Python 3.9 环境 conda create -n openclaw python=3.9 conda activate openclaw
-
安装 PyTorch 根据你的 CUDA 版本(如果使用 GPU)或选择 CPU 版本,前往 PyTorch 官网 获取安装命令。 对于 CUDA 11.8:
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
对于仅 CPU:
pip install torch torchvision torchaudio
-
安装 Jupyter(可选但推荐) OpenCLAW 的示例通常在 Jupyter Notebook 中运行。
pip install jupyter
安装 OpenCLAW 及核心依赖
-
克隆仓库
git clone https://github.com/OpenGVLab/OpenCLAW.git cd OpenCLAW
-
安装项目依赖
# 安装基础依赖 pip install -r requirements.txt # 安装 timm(一个 PyTorch 视觉模型库) pip install timm # 安装 OpenCLAW 包本身(以可编辑模式安装,便于修改) pip install -e .
快速验证安装(“即用”部分)
安装完成后,你可以通过以下代码快速测试,这才是“即用”的体现:
import torch
from PIL import Image
from openclaw.models import build_model
from openclaw.datasets.transforms import get_transform
model_name = 'openclaw_small' # 可选:openclaw_tiny, openclaw_small, openclaw_base
model = build_model(model_name, pretrained=True)
model.eval()
# 2. 准备图像和文本
# 图像处理
transform = get_transform(is_train=False, input_size=model.image_size)
image = Image.open('path/to/your/image.jpg').convert('RGB') # 替换为你的图片路径
image_tensor = transform(image).unsqueeze(0) # 增加批次维度
# 文本处理(这里以简单的问答为例)
questions = ["What is the main object in the image?"]
answers = [""] # 对于生成任务,答案留空
# 3. 进行推理(图像问答)
with torch.no_grad():
# 注意:实际的 forward 函数可能需要根据任务调整,请参考项目中的 demo 或 scripts
# 这里是一个示意,具体请查看项目文档或源码中的示例
output = model.generate(image_tensor, questions)
print("模型输出:", output)
更简单的启动方式:使用 Demo Notebook
项目通常提供了最直接的示例:
- 在
OpenCLAW项目根目录下,启动 Jupyter:jupyter notebook
- 打开
demo或examples文件夹下的.ipynb文件(demo_image_qa.ipynb)。 - 按照 Notebook 中的步骤,上传你的图片,修改问题,即可直接运行看到效果。这才是真正的“即装即用”体验。
注意事项(可能导致无法“即用”的原因)
- 模型权重下载:第一次运行
pretrained=True时,会自动从云端下载权重文件(通常几百 MB 到几 GB),请确保网络通畅。 - CUDA 版本匹配:PyTorch 的 CUDA 版本必须与系统安装的 NVIDIA 驱动支持的 CUDA 版本兼容。
- 缺少组件:如果遇到类似
“No module named ‘transformers’”的错误,手动安装即可:pip install transformers
- 图像路径:确保代码中的图片路径正确。
总结步骤
对于想快速上手的用户,最直接的命令流如下:
# 1. 创建环境 conda create -n openclaw python=3.9 conda activate openclaw # 2. 安装PyTorch(以CPU为例) pip install torch torchvision torchaudio # 3. 获取代码并安装 git clone https://github.com/OpenGVLab/OpenCLAW.git cd OpenCLAW pip install -r requirements.txt pip install timm pip install -e . # 4. 启动Demo jupyter notebook # 然后在浏览器中打开提供的链接,找到并运行demo notebook
按照以上步骤,你就能在配置好环境后,实现 OpenCLAW 的 “即装即用”,核心在于利用 Conda 管理环境,并跟随项目提供的 Notebook 示例进行操作。