使用coremltools 4

问题描述

我无法使用ONNX转换为MLModel模型的灵活形状。源模型来自coremltools 4.0,但是我不能使用新的统一转换,因为coremltools目前不支持模型中使用的PyTorch层。

reflection_pad2d编译模型而没有任何警告或错误,并显示支持灵活的形状:

coremltools

但是在模型上运行预测失败并显示以下消息:

input {
  name: "input"
  type {
    imageType {
      width: 1024
      height: 1024
      colorSpace: BGR
      imageSizeRange {
        widthRange {
          lowerBound: 256
          upperBound: -1
        }
        heightRange {
          lowerBound: 256
          upperBound: -1
        }
      }
    }
  }
}
output {
  name: "output"
  type {
    imageType {
      width: 1024
      height: 1024
      colorSpace: RGB
      imageSizeRange {
        widthRange {
          lowerBound: 256
          upperBound: -1
        }
        heightRange {
          lowerBound: 256
          upperBound: -1
        }
      }
    }
  }
}

枚举形状将与模型一起使用,但是如果没有大约10k +枚举形状(这似乎不是解决方案),这是不够的。

该模型是一个完全卷积的网络,它似乎没有使用任何固定的形状(请参见规格输出),并且可以在MyApp[5773:4974761] [espresso] [Espresso::handle_ex_plan] exception=Invalid X-dimension 1/814 status=-7 MyApp[5773:4974761] [coreml] Error binding image input buffer input: -7 MyApp[5773:4974761] [coreml] Failure in bindInputsAndOutputs. prediction error: Error Domain=com.apple.CoreML Code=0 "Error binding image input buffer input." UserInfo={NSLocalizedDescription=Error binding image input buffer input.} 中使用不同的形状,因此看来必须有可能变得灵活形状以某种方式起作用。

我尝试通过图像输入/输出使用灵活的输入形状:

PyTorch

我还尝试过首先将模型转换为多数组,然后转换为图像,然后添加灵活的形状。

input_names=['input']
output_names=['output']
channels = 3
input_shape = ct.Shape(shape=(channels,ct.RangeDim(),ct.RangeDim()))
#also tried:
input_shape = ct.Shape(shape=(channels,ct.RangeDim(256,4096),4096)))
#and:
input_shape = ct.Shape(shape=(channels,-1),-1)))

model_input = ct.TensorType(shape=input_shape)
mlmodel = convert('torch_model.onnx',[model_input],image_input_names=input_names,image_output_names=output_names,...
)

spec = mlmodel.get_spec()

#tried with and without adding flexible shapes
spec = add_flexible_shapes(spec)

def add_flexible_shapes(spec):
    img_size_ranges = flexible_shape_utils.NeuralNetworkImageSizeRange(height_range=(256,width_range=(256,-1))
    #also tried:
    #img_size_ranges = flexible_shape_utils.NeuralNetworkImageSizeRange(height_range=(256,4096))
    flexible_shape_utils.update_image_size_range(spec,feature_name=input_names[0],size_range=img_size_ranges)
    flexible_shape_utils.update_image_size_range(spec,feature_name=output_names[0],size_range=img_size_ranges)
    return spec     

我已经查看了规范中的所有层,但没有看到任何使用固定形状的层(输入/输出层除外):

 torch.onnx.export(torch_model,example_input,'torch_model.onnx',input_names=input_names,output_names=output_names,verbose=True)
 mlmodel = ct.converters.onnx.convert(model='torch_model.onnx',...
 spec = mlmodel.get_spec()

 input = spec.description.input[0]
 input.type.imageType.colorSpace = ft.ImageFeatureType.RGB
 input.type.imageType.height = 1024
 input.type.imageType.width = 1024

 output = spec.description.output[0]
 output.type.imageType.colorSpace = ft.ImageFeatureType.RGB
 output.type.imageType.height = 1024
 output.type.imageType.width = 1024
                                     
 spec = add_flexible_shapes(spec)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...