Google Colab中TensorFlow Object Detection API安装报错求助
在Google Colab中安装TensorFlow Object Detection API的PyYAML编译错误解决
问题背景
执行以下命令安装TensorFlow Object Detection API:
!git clone https://github.com/tensorflow/models.git %cd /content/models/research/ !protoc object_detection/protos/*.proto --python_out=. !cp object_detection/packages/tf2/setup.py . !python -m pip install .
触发PyYAML编译失败错误:
Collecting pyyaml<6.0,>=5.1 (from tf-models-official>=2.5.1->object-detection==0.1) Using cached PyYAML-5.4.1.tar.gz (175 kB) Installing build dependencies ... done error: subprocess-exited-with-error × Getting requirements to build wheel did not run successfully. │ exit code: 1 ╰─> See above for output. note: This error originates from a subprocess, and is likely not a problem with pip. Getting requirements to build wheel ... error error: subprocess-exited-with-error × Getting requirements to build wheel did not run successfully. │ exit code: 1 ╰─> See above for output. note: This error originates from a subprocess, and is likely not a problem with pip.
已尝试的无效操作:
- 无缓存安装:
!python -m pip install . --no-cache-dir - 重置/删除Colab运行时
- 卸载所有pip包:
!pip freeze > requirements.txt !pip uninstall -r requirements.txt -y - 单独安装最新版PyYAML(安装成功但原API安装脚本仍失败)
- 执行
!pip install tf-models-official>=2.5.1 - 执行
!pip install --upgrade pip setuptools
有效解决方案
方案1:预编译PyYAML安装优先
Colab默认环境缺少PyYAML编译依赖,直接安装预编译wheel包绕过编译步骤:
# 安装符合版本要求的预编译PyYAML !pip install pyyaml==5.4.1 --only-binary=:all: # 继续完成API安装 %cd /content/models/research/ !python -m pip install .
方案2:放宽PyYAML版本限制
修改setup.py中的依赖约束,允许使用更高版本的PyYAML(你已验证可成功安装最新版):
- 打开
/content/models/research/setup.py,找到依赖项中pyyaml<6.0,>=5.1的条目,修改为pyyaml>=5.1 - 保存后执行安装:
%cd /content/models/research/ !python -m pip install .
方案3:使用官方简化安装脚本
直接使用TensorFlow官方提供的Colab专用安装命令,自动处理依赖冲突:
# 克隆models仓库 !git clone https://github.com/tensorflow/models.git # 安装protoc编译工具 !apt-get install protobuf-compiler # 编译Object Detection的proto文件 %cd /content/models/research/ !protoc object_detection/protos/*.proto --python_out=. # 安装TF2版本的Object Detection API !pip install -q object-detection[tf2]
内容的提问来源于stack exchange,提问作者ela16
相关产品推荐
相关产品推荐

