onnxruntime中传入特定数据类型,比如fp16,int8

typedef enum ONNXTensorElementDataType {
  ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED,
  ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT,   // maps to c type float
  ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT8,   // maps to c type uint8_t
  ONNX_TENSOR_ELEMENT_DATA_TYPE_INT8,    // maps to c type int8_t
  ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT16,  // maps to c type uint16_t
  ONNX_TENSOR_ELEMENT_DATA_TYPE_INT16,   // maps to c type int16_t
  ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32,   // maps to c type int32_t
  ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64,   // maps to c type int64_t
  ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING,  // maps to c++ type std::string
  ONNX_TENSOR_ELEMENT_DATA_TYPE_BOOL,
  ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT16,
  ONNX_TENSOR_ELEMENT_DATA_TYPE_DOUBLE,      // maps to c type double
  ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT32,      // maps to c type uint32_t
  ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT64,      // maps to c type uint64_t
  ONNX_TENSOR_ELEMENT_DATA_TYPE_COMPLEX64,   // complex with float32 real and imaginary components
  ONNX_TENSOR_ELEMENT_DATA_TYPE_COMPLEX128,  // complex with float64 real and imaginary components
  ONNX_TENSOR_ELEMENT_DATA_TYPE_BFLOAT16     // Non-IEEE floating-point format based on IEEE754 single-precision
} ONNXTensorElementDataType;

 需要自行转换数据类型后再放到vector中传入。

 

注意createTensor使用的是非模板版本

std::vector<uint8_t> mask_tensor_values;
for(int i = 0; i < mask_tensor_size; i++){
	mask_tensor_values.push_back((uint8_t)(true));
}
auto mask_memory_info = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
Ort::Value mask_tensor = Ort::Value::CreateTensor<bool>(mask_memory_info, reinterpret_cast<bool *>(mask_tensor_values.data()),mask_tensor_size, mask_node_dims.data(),  ONNX_TENSOR_ELEMENT_DATA_TYPE_INT8);

fp16 需要特殊处理,注意转入的长度:

 

    std::vector<uint16_t>  inputTensorValueFp16;

    //std::clock_t c_start = std::clock();
    for (auto fp32 : inputTensorValues)
    {
        inputTensorValueFp16.push_back(float32_to_float16(fp32));
    }

    //std::clock_t c_end = std::clock();

    //auto timevalue = c_end - c_start;
    //printf("time: %d\n", timevalue);
    // Note we are passing bytes count in this api, not number of elements -> sizeof(values)
    auto inputTensor = Ort::Value::CreateTensor(memoryInfo, inputTensorValueFp16.data(), inputTensorValueFp16.size()*sizeof(uint16_t), inputShape.data(),   inputShape.size(), ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT16);
       

后面扯点闲篇:

 

fp16的模型是用winmltools转换的,具体看我的另一篇blog: https://blog.csdn.net/znsoft/article/details/114538684

在你自己的python环境中:

pip install winmltools 

 

目前只在ubuntu20.04下安装通过,其它平台,比如ubuntu18.04,windows 10 /anaconda下都安装失败。。

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值