我的Keras训练好的模型文件想转换成CoreML的模型文件,然后转换出现了各种问题,花了我两天的时间全力去解决,包括去修改库文件代码,包括版本问题等等,最后还是这个转换的脚本为问题,其实仔细起来几行代码就能搞定,
我错误的脚本:
This gives an error: ValueError: Unknown activation function:relu6
还出现过各种
为了解决上述问题,我直接上转换代码,希望大家不要在这坑里很久:
import keras
import coremltools.converters.keras as k
from keras.utils.generic_utils import CustomObjectScope
def convert():
with CustomObjectScope({'relu6': keras.applications.mobilenet.relu6,
'DepthwiseConv2D': keras.applications.mobilenet.DepthwiseConv2D}):
model = k.convert("temp.h5",
input_names=['input'],
output_names=['output'],
model_precision='float16')
model.save('temp.mlmodel')
convert()
最后就生成了mlmodel文件了哈,辛苦了两天解决这个问题,stack overflow上基本上没有解决问题的方案,Apple的官方更没有解决的办法,大家有问题可以问我,尽量解答。