运行LLaVA图文模型报错:已装TensorFlow仍提示需安装框架求助
修复LLaVA模型加载时的RuntimeError问题
问题背景
在Intel Mac(Python 3.10.13)环境下,已安装TensorFlow 2.15.0,但运行HuggingFace的llava-hf/llava-1.5-7b-hf模型示例代码时,加载pipeline环节出现错误:
RuntimeError: At least one of TensorFlow 2.0 or PyTorch should be installed. To install TensorFlow 2.0, read the instructions at ... To install PyTorch, read the instructions at ...
示例代码:
from transformers import pipeline from PIL import Image import requests model_id = "llava-hf/llava-1.5-7b-hf" pipe = pipeline("image-to-text", model=model_id) url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg" image = Image.open(requests.get(url, stream=True).raw) prompt = "USER: <image>\nWhat does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud\nASSISTANT:" outputs = pipe(image, prompt=prompt, generate_kwargs={"max_new_tokens": 200}) print(outputs)
修复方案
1. 安装PyTorch(推荐)
LLaVA系列模型对PyTorch的支持更完善,优先建议安装:
pip3 install torch torchvision torchaudio
安装完成后重新运行代码即可,transformers会自动识别PyTorch环境。
2. 强制指定TensorFlow作为后端
如果坚持使用已安装的TensorFlow,需要在初始化pipeline时明确指定框架:
pipe = pipeline("image-to-text", model=model_id, framework="tf")
3. 验证TensorFlow环境有效性
确认当前Python环境能正确识别TensorFlow,运行以下命令:
python -c "import tensorflow; print(tensorflow.__version__)"
如果报错,说明TensorFlow未正确安装,需检查:
- 是否激活了正确的虚拟环境(如conda/venv)
- 重新安装TensorFlow:
pip uninstall tensorflow && pip install tensorflow==2.15.0
4. 检查虚拟环境隔离
如果使用了虚拟环境,确保激活了对应的环境后再安装依赖,避免全局环境和虚拟环境的包冲突。
内容的提问来源于stack exchange,提问作者Quinten
相关产品推荐
相关产品推荐

