首页 > 编程语言 >Python,Maskrcnn训练,cannot import name ‘saving‘ from ‘keras.engine‘ ,等问题集合

Python,Maskrcnn训练,cannot import name ‘saving‘ from ‘keras.engine‘ ,等问题集合

时间:2024-08-02 23:24:59浏览次数:18  
标签:engine saving name keras py install pip mrcnn

Python版本=3.9,tensorflow=2.11.0,keras==2.11.0

问题一、module 'keras.engine' has no attribute 'Layer'

Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\20240801\代码\test.py", line 16, in <module>
    from mrcnn import model as modellib, utils
  File "D:\Data_Download\anaconda_env\python\lib\site-packages\mrcnn\model.py", line 255, in <module>
    class ProposalLayer(KE.Layer):
AttributeError: module 'keras.engine' has no attribute 'Layer'

问题二、cannot import name 'saving' from 'keras.engine' 

Traceback (most recent call last):
  File "test.py", line 237, in <module>
    "mrcnn_bbox", "mrcnn_mask"])
  File "D:\Data_Download\anaconda_env\代码\lib\site-packages\mrcnn\model.py", line 2097, in load_weights
    from keras.engine import saving
ImportError: cannot import name 'saving' from 'keras.engine' (D:\Data_Download\anaconda_env\python\lib\site-packages\keras\engine\__init__.py)

这两个问题是同一个问题导致的,

网上有教程让降低pip install tensorflow==1.13.1、pip install keras==2.0.8、pip install h5py==2.10.0,我还尝试退回了python3.7,实际上解决不了问题

实际问题是mrcnn模块的问题,我运行代码,提示找不到mrcnn库,

然后我就直接pip install mrcnn ,这不对的,需要卸载pip uninstall mrcnn

正确的安装方式是,到github上下载源文件 Mask_RCNN-master.zip,下载地址:https://github.com/akTwelve/Mask_RCNN

也可以到这里下载:https://download.csdn.net/download/qq_61523551/89605885

通过下面的代码安装

pip install Mask_RCNN-master.zip

问题三、OSError: Unable to synchronously open file

代码中,需要读取一个初始模型,mask_rcnn_coco.h5

然后,我的代码中有一段是,在线下载模型,我是下载到一半中断了,所以导致无法读取

# Local path to trained weights file
COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")


# Download COCO trained weights from Releases if needed
if not os.path.exists(COCO_MODEL_PATH):
     utils.download_trained_weights(COCO_MODEL_PATH)

解决方案是删除,mask_rcnn_coco.h5,执行代码重新下载,或者带github上下载,下载地址https://github.com/matterport/Mask_RCNN/releases

也可以到这里下载:​​​​​https://download.csdn.net/download/qq_61523551/89605903

问题四、跑起来之后,FutureWarning: Input image dtype is bool

这个直接调整scikit-image版本

pip install scikit-image==0.16.2

问题五、跑起来之后,'SGD' object has no attribute 'get_updates'

这个最经典!找了很久都没找到问题,最后发现是Maskrcnn源代码的问题,

打开代码,你的python路径\Lib\site-packages\mrcnn\model.py

大概2160行,按照下面修改代码,keras.optimizers.legacy.SGD

#修改前
# Optimizer object
optimizer = keras.optimizers.SGD(
            lr=learning_rate, momentum=momentum,
            clipnorm=self.config.GRADIENT_CLIP_NORM)


#修改后
# Optimizer object
optimizer = keras.optimizers.legacy.SGD(
            lr=learning_rate, momentum=momentum,
            clipnorm=self.config.GRADIENT_CLIP_NORM)

标签:engine,saving,name,keras,py,install,pip,mrcnn
From: https://blog.csdn.net/qq_61523551/article/details/140853795

相关文章