导入错误:无法导入名称“relu6”和 AttributeError:

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


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'. 

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


慕森卡
浏览 411回答 1
1回答

Cats萌萌

您拥有的代码适用于较旧的 Keras 版本,我检查的最新 Keras (2.2.2) 已经集成了 ReLU 和 DepthWiseConv2D keras.layers,因此您只需要导入它即可使用 MobileNet:import kerasfrom keras.applications import MobileNetMobileNetV2更新版本的 MobileNet也在同一个包中。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python