pytorch 模型的 Coreml 模型浮点输入

Coreml model float input for a pytorch model

我有一个 pytorch 模型,它将 3 x width x height 图像作为输入,像素值在 0-1

之间归一化

例如在pytorch中输入

img = io.imread(img_path)
input_img =  torch.from_numpy( np.transpose(img, (2,0,1)) ).contiguous().float()/255.0

我将此模型转换为 coreml 并导出了一个 mlmodel,该模型采用正确尺寸的输入

Image (Color width x height)

但是,我的预测不正确,因为模型期望 0-1 和 cvpixelbuffer 之间的浮点值是一个 int bwetween 0-255

我试着像这样规范化模型中的值,

z = x.mul(1.0/255.0) # div op is not supported for export yet

但是,当此操作在 coreml 级别的模型内部完成时,int * float 被转换为 int 并且所有值基本上都是 0

Cast op 不支持导出,例如 x = x.float()

如何确保我的输入的形状适合预测?本质上,我想获取 pixel rgb and float divide 255.0 并将其传递给模型进行推理?

我使用 coreml onnx 转换器的 preprocessing_args 解决了它,

preprocessing_args= {'image_scale' : (1.0/255.0)}

希望这对某人有所帮助