18.4. FAQ

  • Q: SNPU模型的输入和输出必须都是short类型,如果模型的输入数据是float型,输出也需要float型,需如何处理?

  • A: 模型运行前将输入数据从float转成short,运行结束后把模型输出的short类型数据转换成float,转换方法可参考下面代码;

static short float32_to_int16(float in_data, int Q)
{
    int int_value = in_data * (1 << Q);
    return (int_value > 32767) ? 32767 : ((int_value < -32768) ? -32768 : int_value);
}

static float int16_to_float32(short in_data, int Q)
{
    return (float)in_data / (1 << Q);
}