MacOS M1环境PyCharm使用KerasCV遇ModuleNotFoundError问题求助
解决KerasCV导入
keras.engine模块找不到的问题 问题描述
在MacOS M1芯片(操作系统为macOS Ventura 13.4)的PyCharm虚拟环境(Python 3.10 + TensorFlow 2.13.0rc1)中运行KerasCV代码时,触发错误:
ModuleNotFoundError: No module named 'keras.engine'
相同代码在Google Colab可正常执行,代码如下:
import time import keras_cv from tensorflow import keras import matplotlib.pyplot as plt model = keras_cv.models.StableDiffusion(img_width=512, img_height=512) images = model.text_to_image("photograph of an astronaut riding a horse", batch_size=3) def plot_images(images): plt.figure(figsize=(20, 20)) for i in range(len(images)): ax = plt.subplot(1, len(images), i + 1) plt.imshow(images[i]) plt.axis("off") print(plot_images(images))
解决方案
对齐TensorFlow与KerasCV版本兼容性
TensorFlow 2.13.0rc1是预发布版本,可能和KerasCV存在适配问题。建议切换到稳定版TensorFlow:- 卸载当前版本:
pip uninstall tensorflow - 安装稳定版(如2.12.0):
pip install tensorflow==2.12.0 - 重新安装KerasCV:
pip install keras-cv
- 卸载当前版本:
移除独立版Keras避免冲突
确保环境中没有单独安装独立版keras包,避免和TensorFlow集成的Keras版本冲突:pip uninstall keras -y重建干净的虚拟环境
若上述方法无效,直接删除当前PyCharm虚拟环境,重新创建Python 3.10环境后依次安装依赖:pip install tensorflow==2.12.0 keras-cv matplotlib适配M1芯片的ARM架构依赖
M1芯片需安装针对ARM编译的TensorFlow版本,替换通用版:pip install tensorflow-macos==2.12.0 tensorflow-metal==0.8.0再安装KerasCV和matplotlib,确保所有依赖适配ARM架构。
内容的提问来源于stack exchange,提问作者Ata Tekeli
相关产品推荐
相关产品推荐

