Windows下pip安装tf-object-detection遇参数错误与依赖冲突求助
我在Windows上装TensorFlow Object Detection时碰到俩问题,跟论坛里同标题的错误都不一样,下面说下问题和解决步骤:
第一个坑:--use-feature=2020-resolver 参数无效
我执行了这条命令:
C:\Users\NAME\models\research>python -m pip install --use-feature=2020-resolver .
结果直接报错:
C:\Users\NAME\models\research>python -m pip install --use-feature=2020-resolver . Usage: C:\Users\NAME\AppData\Local\Programs\Python\Python311\python.exe -m pip install [options] <requirement specifier> [package-index-options] ... C:\Users\NAME\AppData\Local\Programs\Python\Python311\python.exe -m pip install [options] -r <requirements file> [package-index-options] ... C:\Users\NAME\AppData\Local\Programs\Python\Python311\python.exe -m pip install [options] [-e] <vcs project url> ... C:\Users\NAME\AppData\Local\Programs\Python\Python311\python.exe -m pip install [options] [-e] <local project path> ... C:\Users\NAME\AppData\Local\Programs\Python\Python311\python.exe -m pip install [options] <archive url/path> ... option --use-feature: invalid choice: '2020-resolver' (choose from 'fast-deps', 'truststore', 'no-binary-enable-wheel-cache')
原因很简单:新版pip(21.0及以上)默认就用2020版依赖解析器了,这个--use-feature=2020-resolver参数已经被删掉了,所以用了就会报错。
第二个坑:去掉参数后出现依赖冲突
我把参数删掉,执行python -m pip install .,结果又炸了,报依赖冲突:
ERROR: Cannot install object-detection and object-detection==0.1 because these package versions have conflicting dependencies. The conflict is caused by: tf-models-official 2.11.3 depends on tensorflow-addons tf-models-official 2.11.2 depends on tensorflow-addons tf-models-official 2.11.0 depends on opencv-python-headless==4.5.2.52 object-detection 0.1 depends on sacrebleu<=2.2.0 tf-models-official 2.10.1 depends on sacrebleu==2.2.0 tf-models-official 2.10.0 depends on opencv-python-headless==4.5.2.52 tf-models-official 2.9.2 depends on tensorflow-addons tf-models-official 2.9.1 depends on tensorflow-addons tf-models-official 2.9.0 depends on tensorflow-addons tf-models-official 2.8.0 depends on tensorflow-addons tf-models-official 2.7.2 depends on tensorflow-addons tf-models-official 2.7.1 depends on tensorflow-addons tf-models-official 2.7.0 depends on tensorflow-addons tf-models-official 2.6.1 depends on tensorflow-addons tf-models-official 2.6.0 depends on tensorflow-addons tf-models-official 2.5.1 depends on tensorflow-addons To fix this you could try to: 1. loosen the range of package versions you've specified 2. remove package versions to allow pip attempt to solve the dependency conflict ERROR: ResolutionImpossible: for help visit https://pip.pypa.io/en/latest/topics/dependency-resolution/#dealing-with-dependency-conflicts
我当前环境的pip freeze输出是:
absl-py==1.4.0 astunparse==1.6.3 cachetools==5.3.0 certifi==2022.12.7 charset-normalizer==3.1.0 contextlib2==21.6.0 contourpy==1.0.7 cycler==0.11.0 Cython==0.29.33 flatbuffers==23.3.3 fonttools==4.39.2 gast==0.4.0 google-auth==2.16.2 google-auth-oauthlib==0.4.6 google-pasta==0.2.0 grpcio==1.51.3 h5py==3.8.0 idna==3.4 jax==0.4.6 keras==2.12.0rc1 kiwisolver==1.4.4 libclang==15.0.6.1 lvis==0.5.3 lxml==4.9.2 Markdown==3.4.1 MarkupSafe==2.1.2 matplotlib==3.7.1 numpy==1.23.5 oauthlib==3.2.2 object-detection @ file:///C:/Users/NAME/models/research opencv-python==4.7.0.72 opt-einsum==3.3.0 packaging==23.0 pandas==1.5.3 Pillow==9.4.0 protobuf==4.22.1 pyasn1==0.4.8 pyasn1-modules==0.2.8 pycocotools==2.0.6 pygame==2.1.3.dev8 pyparsing==3.0.9 python-dateutil==2.8.2 pytz==2022.7.1 requests==2.28.2 requests-oauthlib==1.3.1 rsa==4.9 scipy==1.10.1 six==1.16.0 tensorboard==2.12.0 tensorboard-data-server==0.7.0 tensorboard-plugin-wit==1.8.1 tensorflow==2.12.0rc1 tensorflow-estimator==2.12.0rc0 tensorflow-intel==2.12.0rc1 tensorflow-io-gcs-filesystem==0.31.0 termcolor==2.2.0 tf-slim==1.1.0 typing_extensions==4.5.0 urllib3==1.26.15 Werkzeug==2.2.3 wrapt==1.14.1
一步步解决的方法
1. 先清掉冲突的包
先把已经装的object-detection和可能搞事情的tf-models-official卸载:
pip uninstall -y object-detection tf-models-official
2. 降级TensorFlow到稳定版
你现在用的是TensorFlow 2.12.0rc1(预发布版),这种版本最容易出依赖问题,换成稳定的2.11.0:
pip install tensorflow==2.11.0 tensorflow-estimator==2.11.0
3. 解决OpenCV版本冲突
你当前装的是opencv-python==4.7.0.72,但tf-models-official要求的是opencv-python-headless==4.5.2.52,先卸载旧的,装指定版本:
pip uninstall -y opencv-python pip install opencv-python-headless==4.5.2.52
4. 装tensorflow-addons
错误提示里说tf-models-official依赖这个包,先装上:
pip install tensorflow-addons
5. 安装本地的object-detection包
回到models/research目录,用--no-deps参数跳过自动依赖解析,避免再次触发冲突:
cd C:\Users\NAME\models\research python -m pip install --no-deps .
6. 验证是否装好了
跑个简单的命令测试下:
python -c "import object_detection; print('安装成功')"
怎么看本地包的依赖列表?
要看models/research里的object-detection依赖啥,直接看该目录下的setup.py文件,找install_requires字段,里面列的就是所有依赖;或者在该目录下执行:
pip show -f object-detection
如果还没安装,也可以用pip install --dry-run .来预览它要装的依赖。
内容的提问来源于stack exchange,提问作者DestroyerPrime

