【发布时间】:2019-07-14 10:40:03
【问题描述】:
我正在处理OCR model。我的最终目标是将 OCR 代码转换为 coreML 并将其部署到 ios 中。
我查看并运行了几个 github 源代码,即:
当您查看它们时,它们都将loss 实现为custom layer with lambda layer。
当我想将其转换为 coreML 时,问题就开始了。
我要转换为 CoreMl 的代码:
import coremltools
def convert_lambda(layer):
# Only convert this Lambda layer if it is for our swish function.
if layer.function == ctc_lambda_func:
params = NeuralNetwork_pb2.CustomLayerParams()
# The name of the Swift or Obj-C class that implements this layer.
params.className = "x"
# The desciption is shown in Xcode's mlmodel viewer.
params.description = "A fancy new loss"
return params
else:
return None
print("\nConverting the model:")
# Convert the model to Core ML.
coreml_model = coremltools.converters.keras.convert(
model,
# 'weightswithoutstnlrchangedbackend.best.hdf5',
input_names="image",
image_input_names="image",
output_names="output",
add_custom_layers=True,
custom_conversion_functions={"Lambda": convert_lambda},
)
但它会引发错误
Converting the model:
Traceback (most recent call last):
File "/home/sgnbx/Downloads/projects/CRNN-with-STN-master/CRNN_with_STN.py", line 201, in <module>
custom_conversion_functions={"Lambda": convert_lambda},
File "/home/sgnbx/anaconda3/envs/tf_gpu/lib/python3.6/site-packages/coremltools/converters/keras/_keras_converter.py", line 760, in convert
custom_conversion_functions=custom_conversion_functions)
File "/home/sgnbx/anaconda3/envs/tf_gpu/lib/python3.6/site-packages/coremltools/converters/keras/_keras_converter.py", line 556, in convertToSpec
custom_objects=custom_objects)
File "/home/sgnbx/anaconda3/envs/tf_gpu/lib/python3.6/site-packages/coremltools/converters/keras/_keras2_converter.py", line 255, in _convert
if input_names[idx] in input_name_shape_dict:
IndexError: list index out of range
Input name length mismatch
我有点不确定我是否可以解决这个问题,因为我没有找到任何与此错误相关的内容来解决。
另一方面,OCR 的大多数代码都具有自定义损失功能,我可能再次面临同样的问题。
所以最后我有两个问题:
- 你知道如何解决这个错误
-
我的主要问题你知道
KERAS中的任何OCR 源代码(因为我必须将其转换为coreMl)并且没有自定义损失函数,因此可以转换为CoreMl 没有问题?
提前致谢:)
只是为了让我的问题彻底:
这是我正在工作的源代码中的自定义损失函数:
def ctc_lambda_func(args):
iy_pred, ilabels, iinput_length, ilabel_length = args
# the 2 is critical here since the first couple outputs of the RNN
# tend to be garbage:
iy_pred = iy_pred[:, 2:, :] # no such influence
return backend.ctc_batch_cost(ilabels, iy_pred, iinput_length, ilabel_length)
loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name='ctc')
([fc_2, labels, input_length, label_length])
然后在编译中使用它:
model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer=sgd)
【问题讨论】:
标签: keras deep-learning ocr coreml text-recognition