Core ML coremltools属性错误:模块';keras.applications.mobilenet';没有属性';relu6';

Core ML coremltools属性错误:模块';keras.applications.mobilenet';没有属性';relu6';,keras,coremltools,relu,Keras,Coremltools,Relu,我们正在尝试将.h5 Keras模型转换为.mlmodel模型,我的代码如下: from keras.models import load_model import keras from keras.applications import MobileNet from keras.layers import DepthwiseConv2D from keras.utils.generic_utils import CustomObjectScope with CustomObjectScop

我们正在尝试将.h5 Keras模型转换为.mlmodel模型,我的代码如下:

from keras.models import load_model
import keras
from keras.applications import MobileNet
from keras.layers import DepthwiseConv2D

from keras.utils.generic_utils import CustomObjectScope

with CustomObjectScope({'relu6': keras.applications.mobilenet.relu6,'DepthwiseConv2D': keras.applications.mobilenet.DepthwiseConv2D}):
    model = load_model('CNN_tourist_11.h5', custom_objects={'relu6': MobileNet})

output_labels = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

coreml_model = coremltools.converters.keras.convert("CNN_tourist_11.h5",
                                                    input_names="image",
                                                    image_input_names="image",
                                                    class_labels= output_labels,)

coremltools.utils.save_spec(coreml_model, 'place10.mlmodel')
我们查找6天前提出的类似问题,我们还导入了MobileNet,但它仍然显示此错误:

AttributeError: module 'keras.applications.mobilenet' has no attribute 'relu6'
我的Tensorflow版本是1.10.0 而Keras版本是2.2.2


如果有人能告诉我们为什么会不断显示此错误,我们将不胜感激。在最新的Keras版本中,MobileNet的组件已与Keras的其余层集成,因此它们不再作为MobileNet包的一部分提供。然后您需要将代码更改为:

from keras.models import load_model
import keras
from keras.applications import MobileNet
from keras.layers import DepthwiseConv2D

model = load_model('CNN_tourist_11.h5')

output_labels = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

coreml_model = coremltools.converters.keras.convert("CNN_tourist_11.h5",
                                                    input_names="image",
                                                    image_input_names="image",
                                                    class_labels= output_labels,)

coremltools.utils.save_spec(coreml_model, 'place10.mlmodel')
我修改了我的代码:

from keras.models import load_model
import keras
from keras.applications import MobileNet
from keras.layers import DepthwiseConv2D

#from keras.utils.generic_utils import CustomObjectScope

#with CustomObjectScope({'relu6': keras.applications.mobilenet.relu6,'DepthwiseConv2D': keras.applications.mobilenet.DepthwiseConv2D}):
    #model = load_model('CNN_tourist_11.h5', custom_objects={'relu6': MobileNet})

output_labels = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

coreml_model = coremltools.converters.keras.convert("CNN_tourist_11.h5",
                                                    input_names="image",
                                                    image_input_names="image",
                                                    class_labels= output_labels,)

coremltools.utils.save_spec(coreml_model, 'place10.mlmodel')
打印输出的某些部分:

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/coremltools/converters/keras/_layers2.py in <module>()
      8 if _keras.__version__ >= _StrictVersion('2.2.0'):
      9     from keras.layers import DepthwiseConv2D
---> 10     from keras_applications.mobilenet import relu6
     11 else:
     12     from keras.applications.mobilenet import DepthwiseConv2D, relu6

ImportError: cannot import name 'relu6'
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/coremltools/converters/keras//u layers2.py in()
8如果版本('2.2.0'):
9从keras.layers导入DepthwiseConv2D
--->10来自keras_applications.mobilenet import relu6
11其他:
12来自keras.applications.mobilenet import DepthwiseConv2D,relu6
ImportError:无法导入名称“relu6”

实际上,这个问题是由@Rex自己在评论中回答的。我只想把它说得清楚简短,作为遇到同样问题的人的“答案”

这个问题与keras无关,而是与coremltools有关。您需要在coremltools文件中找到一个
\u layers2.py
,并从keras\u application.mobilenet import relu6中注释掉

  • [您的PYTHON安装目录]/lib/python3.6/site packages/coremltools/converters/keras/\u layers2.py上查找
    \u layer2.py

  • 注释掉这行,如下所示:

    if _keras.__version__ >= _StrictVersion('2.2.0'):
        from keras.layers import DepthwiseConv2D
    # Modified by KF 10/16/2018
    #    from keras_applications.mobilenet import relu6
    else:
       from keras.applications.mobilenet import DepthwiseConv2D, relu6
    
然后,在代码中,删除所有与Keras相关的导入,它们不相关

对于
'Sequential'对象没有属性“SerializeToString”
错误,请使用
coreml\u model.save
而不是
tools.utils.save\u spec()


问题已解决。

此问题的可能重复项以前已经回答过,请在提问之前,尝试搜索类似的问题,看看答案是否已经在这里。@MatiasValdenegro我在6天前看到了这个问题,我按照您的建议“从keras.applications导入MobileNet”,但仍然不起作用。对于类似的问题,我很抱歉,但我确实查找了问题,谢谢。然后,您需要详细说明哪些问题不起作用,为您的问题添加尽可能详细的内容。@MatiasValdenegro我已经修改了我的问题,感谢您的提醒。@MatiasValdenegro我修改了我的代码,下面是打印输出,我通过删除keras/_layers2.py中的以下两行来解决问题:“from keras_applications.mobilenet import relu6”和“from keras.applications.mobilenet import DepthwiseConv2D,relu6”,但如果我遇到其他问题:“MLModel”对象没有属性“SerializeToString”ha,但是谢谢你的帮助。我的Keras版本是2.2.4,tensorflow 1.10.0,仍然面临着同样的问题,有人能帮忙吗?@KFZ请提出你自己的问题,根本不清楚什么是“同样的问题”。@MatiasValdenegro逐字逐句地将同样的问题与这个问题进行比较。唯一不同的是我提到的版本。如果我再问一个问题,你会说请不要问重复的问题。@KFZ Matias Valdenegro几乎是对的,但如果你键入代码,他会将其粘贴到“无法导入名称‘relu6’”,你只需转到你安装的Keras套装,在Keras文件中,有一个文件名“\u layers2.py”。在“_layers2.py”中,删除“来自keras_applications.mobilenet import relu6”和“来自keras.applications.mobilenet import DepthwiseConv2D,relu6”,然后问题将得到解决。
# from keras.models import load_model
# import keras
# from keras.applications import MobileNet
# from keras.layers import DepthwiseConv2D

output_labels = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

coreml_model = coremltools.converters.keras.convert("CNN_tourist_11.h5",
                                                input_names="image",
                                                image_input_names="image",
                                                class_labels= output_labels,)

#coremltools.utils.save_spec(coreml_model, 'place10.mlmodel')
coreml_model.save('place10.mlmodel')