Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Can't use TFMOT version 0.8.0 due to missing dependency

未关闭
#1,117 1 条评论 11 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
48/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
python, tensorflow

调研方向

从 setup.py 第 35-41 行开始,将声明的依赖项与 commit 95331880046399a90d7d7533480c159d997aa143 进行比较。复现一次全新的 TensorFlow 2.15.0.post1 和 TFMOT 0.8.0 安装,然后验证无需手动安装 tf_keras 即可成功导入 tensorflow_model_optimization。

由索引模型根据 Issue 内容生成。

描述

bug

Describe the bug
Can't use latest TFMOT 0.8.0 with the latest TF 2.15.0.post1 on a fresh install because of missing tf_keras dependency. This complicates installations of downstream packages that depend on TFMOT.

System information

TensorFlow version (installed from source or binary): 2.15.0.post1 (installed via pip)

TensorFlow Model Optimization version (installed from source or binary): 0.8.0 (via pip)

Python version: 3.10

Describe the expected behavior

Importing the package should succeed

Describe the current behavior

Importing TFMOT fails

Code to reproduce the issue

import tensorflow_model_optimization

results in:

2024-02-12 22:31:33.815729: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2024-02-12 22:31:33.825475: I external/local_tsl/tsl/cuda/cudart_stub.cc:31] Could not find cuda drivers on your machine, GPU will not be used.
2024-02-12 22:31:33.851921: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:9261] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
2024-02-12 22:31:33.851950: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:607] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
2024-02-12 22:31:33.852716: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
2024-02-12 22:31:33.856955: I external/local_tsl/tsl/cuda/cudart_stub.cc:31] Could not find cuda drivers on your machine, GPU will not be used.
2024-02-12 22:31:33.857108: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-02-12 22:31:34.387760: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
WARNING:tensorflow:Your environment has TF_USE_LEGACY_KERAS set to True, but you do not have the tf_keras package installed. You must install it in order to use the legacy tf.keras. Install it via: `pip install tf_keras`
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/vloncar/soft/conda/envs/temp-3.10/lib/python3.10/site-packages/tensorflow_model_optimization/__init__.py", line 86, in <module>
    from tensorflow_model_optimization.python.core.api import clustering
  File "/home/vloncar/soft/conda/envs/temp-3.10/lib/python3.10/site-packages/tensorflow_model_optimization/python/core/api/__init__.py", line 16, in <module>
    from tensorflow_model_optimization.python.core.api import clustering
  File "/home/vloncar/soft/conda/envs/temp-3.10/lib/python3.10/site-packages/tensorflow_model_optimization/python/core/api/clustering/__init__.py", line 16, in <module>
    from tensorflow_model_optimization.python.core.api.clustering import keras
  File "/home/vloncar/soft/conda/envs/temp-3.10/lib/python3.10/site-packages/tensorflow_model_optimization/python/core/api/clustering/keras/__init__.py", line 19, in <module>
    from tensorflow_model_optimization.python.core.clustering.keras.cluster import cluster_scope
  File "/home/vloncar/soft/conda/envs/temp-3.10/lib/python3.10/site-packages/tensorflow_model_optimization/python/core/clustering/keras/cluster.py", line 22, in <module>
    from tensorflow_model_optimization.python.core.clustering.keras import cluster_wrapper
  File "/home/vloncar/soft/conda/envs/temp-3.10/lib/python3.10/site-packages/tensorflow_model_optimization/python/core/clustering/keras/cluster_wrapper.py", line 23, in <module>
    from tensorflow_model_optimization.python.core.clustering.keras import clustering_centroids
  File "/home/vloncar/soft/conda/envs/temp-3.10/lib/python3.10/site-packages/tensorflow_model_optimization/python/core/clustering/keras/clustering_centroids.py", line 22, in <module>
    from tensorflow_model_optimization.python.core.keras.compat import keras
  File "/home/vloncar/soft/conda/envs/temp-3.10/lib/python3.10/site-packages/tensorflow_model_optimization/python/core/keras/compat.py", line 41, in <module>
    keras = _get_keras_instance()
  File "/home/vloncar/soft/conda/envs/temp-3.10/lib/python3.10/site-packages/tensorflow_model_optimization/python/core/keras/compat.py", line 33, in _get_keras_instance
    version_fn = getattr(tf.keras, 'version', None)
  File "/home/vloncar/soft/conda/envs/temp-3.10/lib/python3.10/site-packages/tensorflow/python/util/lazy_loader.py", line 146, in __getattr__
    self._initialize()
  File "/home/vloncar/soft/conda/envs/temp-3.10/lib/python3.10/site-packages/tensorflow/python/util/lazy_loader.py", line 138, in _initialize
    raise ImportError(  # pylint: disable=raise-missing-from
ImportError: Keras cannot be imported. Check that it is installed.

Additional context
It's clear that the solution is to install tf_keras and indeed everything works as expected afterwards. However, this should happen automatically. Commit 95331880046399a90d7d7533480c159d997aa143 should have fixed that, however the requirement is not present in setup.py file and thus is not installed when using package from PyPI.

When using TF 2.14.1 everything works as expected without additional installations.

主要语言
Python
星标
1.6k
派生
349
PR 合并指标
30 天内没有已合并 PR

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

tensorflow/model-optimization 的其他 Issue

查看 tensorflow/model-optimization 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。