【发布时间】: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'.
我通常不会在堆栈溢出中发帖,所以如果您需要更多信息,请告诉我。
【问题讨论】: