【问题标题】:ImportError: cannot import name 'relu6' and AttributeError: module 'keras.applications.mobilenet' has no attribute 'relu6'ImportError:无法导入名称“relu6”和 AttributeError:模块“keras.applications.mobilenet”没有属性“relu6”
【发布时间】:2019-03-09 00:18:49
【问题描述】:

我正在尝试使用以下代码将 h5 keras 模型转换为 .mlmodel 文件类型:

from keras.models import load_model
import keras
from keras.applications.mobilenet import MobileNet
from keras.layers import DepthwiseConv2D
# convert the model to coreml format
print("[INFO] converting model")

from keras.utils.generic_utils import CustomObjectScope

with CustomObjectScope({'relu6': keras.applications.mobilenet.relu6,'DepthwiseConv2D':     keras.applications.mobilenet.DepthwiseConv2D}):
   model = load_model('/Users/nikhil.c/aslModel.h5', custom_objects={
               'relu6': MobileNet})
coreml_model = coremltools.converters.keras.convert("/Users/nikhil.c        /aslModel.h5",
        input_names="image",
        image_input_names="image",
        image_scale=1/255.0,
        class_labels= ["hello", "hi", "you"],
        is_bgr=True)

# save the model to disk
coremltools.utils.save_spec(coreml_model, 'aslModel.mlmodel')

我在使用CustomObjectScope 之前最初收到此错误:

ImportError: cannot import name 'relu6'

我通过CustomObjectScope 修复了它,但现在我收到了错误:

AttributeError: module 'keras.applications.mobilenet' has no attribute 'relu6'. 

我通常不会在堆栈溢出中发帖,所以如果您需要更多信息,请告诉我。

【问题讨论】:

    标签: python keras


    【解决方案1】:

    您拥有的代码适用于较旧的 Keras 版本,我检查的最新 Keras (2.2.2) 已在 keras.layers 中集成了 ReLU 和 DepthWiseConv2D,因此您只需导入此代码即可使用 MobileNet:

    import keras
    from keras.applications import MobileNet
    

    对于较新版本的 MobileNet,同一包中还有 MobileNetV2

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-16
      • 2021-02-01
      • 2016-07-15
      相关资源
      最近更新 更多