首页 > 解决方案 > ValueError:图像的输入形状无效。在 keras 模型中转换为 core ml

问题描述

我已经用暗网训练了我的 YOLOv3 网络来识别图像中的一些对象。一切正常。我想在 iOS 应用程序中使用权重文件,所以按照一些教程,我从暗网权重文件 ancd 获得了 keras h5 模型,我检查了 h5 模型是否也可以正常工作。好的。最后一步,使用 coremltools 我尝试将 h5 模型转换为可在 xcode 下使用的 coreml 模型。这里我有问题......最后一次转换是用这个小 py 脚本执行的:

import coremltools

....

coreml_model = coremltools.converters.keras.convert('yolorcgz.h5', input_names='image', class_labels=output_labels, image_input_names='image', input_name_shape_dict={'image': [1, 416, 416, 3]})

coreml_model.input_description['image'] = 'Takes a photo'
coreml_model.output_description['output'] = 'Prediction of obj in the photo'

coreml_model.author = 'SW Team'
coreml_model.license = 'Public Domain'
coreml_model.short_description = "YOLOv3 network trained for obj recognition"

coreml_model.save('yolorcgz.mlmodel')

当我运行脚本时,我总是遇到这个错误:

Traceback (most recent call last):
  File "coreml.py", line 9, in <module>
coreml_model =    coremltools.converters.keras.convert('yolorcgz.h5',input_name_shape_dict={'input1': [1, 416, 416, 3]})
  File "/usr/local/lib/python2.7/dist-packages/coremltools/converters/keras/_keras_converter.py", line 760, in convert
custom_conversion_functions=custom_conversion_functions)
  File "/usr/local/lib/python2.7/dist-packages/coremltools/converters/keras/_keras_converter.py", line 556, in convertToSpec
custom_objects=custom_objects)
  File "/usr/local/lib/python2.7/dist-packages/coremltools/converters/keras/_keras2_converter.py", line 305, in _convert
raise ValueError(errMsg)
ValueError: Invalid input shape for image.
Please provide a finite height (H), width (W) & channel value (C) using input_name_shape_dict arg with key = 'image' and value = [None,H,W,C]
Converted .mlmodel can be modified to have flexible input shape using     coremltools.models.neural_network.flexible_shape_utils

关于什么可能出错的任何想法?非常感谢

标签: tensorflowkerasdeep-learningyolocoremltools

解决方案


我遇到了同样的问题。input_name_shape_dict={'image': [1, 416, 416, 3]}我通过更改为来修复它input_name_shape_dict={'image': [None, 416, 416, 3]}

我正在使用 Keras 2.3.1 和 Tensorflow 1.14.0,这对我有用。


推荐阅读