简体   繁体   English

pytorch模型的Coreml模型浮点输入

[英]Coreml model float input for a pytorch model

I have a pytorch model that takes 3 x width x height image as input with the pixel values normalized between 0-1 我有一个pytorch模型,将3 x width x height图像作为输入,并将像素值标准化为0-1

Eg, input in pytorch 例如,在pytorch中输入

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

I converted this model to coreml and exported an mlmodel which takes the input with correct dimensions 我将此模型转换为coreml并导出了一个mlmodel,该模型接受了正确尺寸的输入

Image (Color width x height)

However, my predictions are incorrect as the model is expecting a float value between 0-1 and cvpixelbuffer is a int bwetween 0-255 但是,我的预测是不正确的,因为模型期望浮点值介于0-1和cvpixelbuffer之间是介于0-255之间的整数

I tried to normalize the values inside the model like so, 我试图像这样规范化模型内部的值,

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

However, when this op is done inside model at coreml level, int * float is casted as int and all values are essentially 0 但是,当此操作在coreml级别的模型内部完成时,将int * float强制转换为int并且所有值基本上都是0

Cast op is not supported for export eg, x = x.float() 投射操作不受导出支持,例如x = x.float()

How can I make sure my input is properly shaped for prediction? 如何确定输入的形状正确以进行预测? Essentially, I want to take the pixel rgb and float divide 255.0 and pass it to the model for inference? 本质上,我想将pixel rgb and float divide 255.0并将其传递给模型进行推理?

I solved it using the coreml onnx coverter's preprocessing_args like so, 我使用coreml onnx掩盖程序的preprocessing_args解决了它,

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

Hope this helps someone 希望这可以帮助某人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM