微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用coremltools 4

如何解决使用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 举报,一经查实,本站将立刻删除。