我正在尝试修改本教程以使用自己的数据集/模型:
https://www.pyimagesearch.com/2018/04/23/running-keras-models-on-ios-with-coreml/
但是,当我尝试使用已序列化的模型并将其转换为Xcode的.mlmodel时,可以运行 coremlconverter.py 脚本:
# USAGE
# python coremlconverter.py --model pokedex.model --labelbin lb.pickle
# import necessary packages
from keras.models import load_model
import coremltools
import argparse
import pickle
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-m", "--model", required=True, help="path to trained model model")
ap.add_argument("-l", "--labelbin", required=True, help="path to label binarizer")
args = vars(ap.parse_args())
# load the class labels
print("[INFO] loading class labels from label binarizer")
lb = pickle.loads(open(args["labelbin"], "rb").read())
class_labels = lb.classes_.tolist()
print("[INFO] class labels: {}".format(class_labels))
# load the trained convolutional neural network
print("[INFO] loading model...")
model = load_model(args["model"])
# convert the model to coreml format
print("[INFO] converting model")
coreml_model = coremltools.converters.keras.convert(model,
input_names="image",
image_input_names="image",
image_scale=1/255.0,
class_labels=class_labels,
is_bgr=True)
# save the model to disk
output = args["model"].rsplit(".", 1)[0] + ".mlmodel"
print("[INFO] saving model as {}".format(output))
coreml_model.save(output)
我最终遇到以下回溯/错误:
Traceback (most recent call last):
File "coremlconverter.py", line 26, in <module> model = load_model(args["model"])
File "/Users/fraser/.virtualenvs/keras_tf/lib/python3.6/site-packages/keras/models.py", line 243, in load_model
model = model_from_config(model_config, custom_objects=custom_objects)
File "/Users/fraser/.virtualenvs/keras_tf/lib/python3.6/site-packages/keras/models.py", line 317, in model_from_config
return layer_module.deserialize(config, custom_objects=custom_objects)
File "/Users/fraser/.virtualenvs/keras_tf/lib/python3.6/site-packages/keras/layers/__init__.py", line 55, in deserialize
printable_module_name='layer')
File "/Users/fraser/.virtualenvs/keras_tf/lib/python3.6/site-packages/keras/utils/generic_utils.py", line 143, in deserialize_keras_object
list(custom_objects.items())))
File "/Users/fraser/.virtualenvs/keras_tf/lib/python3.6/site-packages/keras/models.py", line 1352, in from_config
layer = layer_module.deserialize(conf, custom_objects=custom_objects)
File "/Users/fraser/.virtualenvs/keras_tf/lib/python3.6/site-packages/keras/layers/__init__.py", line 55, in deserialize
printable_module_name='layer')
File "/Users/fraser/.virtualenvs/keras_tf/lib/python3.6/site-packages/keras/utils/generic_utils.py", line 145, in deserialize_keras_object
return cls.from_config(config['config'])
File "/Users/fraser/.virtualenvs/keras_tf/lib/python3.6/site-packages/keras/engine/topology.py", line 1269, in from_config
return cls(**config)
File "/Users/fraser/.virtualenvs/keras_tf/lib/python3.6/site-packages/keras/layers/core.py", line 483, in __init__
super(Flatten, self).__init__(**kwargs)
File "/Users/fraser/.virtualenvs/keras_tf/lib/python3.6/site-packages/keras/engine/topology.py", line 292, in __init__
raise TypeError('Keyword argument not understood:', kwarg)
TypeError: ('Keyword argument not understood:', 'data_format')
我不知道为什么会出现此错误,因为我训练有素的模型与本教程中的模型具有完全相同的体系结构。我要做的就是更改类标签的数量。
对于在何处/为什么失败以及如何进行修复的任何建议,将不胜感激。
答案 0 :(得分:0)
经过研究,我发现问题出在我用于序列化的tensorflow / keras版本上。我必须降级到较低的版本才能将其写入.mlmodel文件而不会出现错误。