【问题标题】:Is there a way to convert the quint8 pytorch format to np.uint8 format?有没有办法将 quint8 pytorch 格式转换为 np.uint8 格式?
【发布时间】:2020-10-23 16:05:50
【问题描述】:

我正在使用下面的代码在 pytorch 中获取量化的 unsiged int 8 格式。但是,我无法将quant 变量转换为np.uint8。有可能吗?

import torch

quant = torch.quantize_per_tensor(torch.tensor([-1.0, 0.352, 1.321, 2.0]), 0.1, 10, torch.quint8)

【问题讨论】:

    标签: python pytorch quantization


    【解决方案1】:

    这可以使用torch.int_repr()来完成

    import torch
    import numpy as np
    
    # generate a test float32 tensor
    float32_tensor = torch.tensor([-1.0, 0.352, 1.321, 2.0])
    print(f'{float32_tensor.dtype}\n{float32_tensor}\n')
    
    # convert to a quantized uint8 tensor. This format keeps the values in the range of
    # the float32 format, with the resolution of a uint8 format (256 possible values)
    quint8_tensor = torch.quantize_per_tensor(float32_tensor, 0.1, 10, torch.quint8)
    print(f'{quint8_tensor.dtype}\n{quint8_tensor}\n')
    
    # map the quantized data to the actual uint8 values (and then to an np array)
    uint8_np_ndarray = torch.int_repr(quint8_tensor).numpy()
    print(f'{uint8_np_ndarray.dtype}\n{uint8_np_ndarray}')
    

    输出

    torch.float32
    tensor([-1.0000,  0.3520,  1.3210,  2.0000])
    
    torch.quint8
    tensor([-1.0000,  0.4000,  1.3000,  2.0000], size=(4,), dtype=torch.quint8,
           quantization_scheme=torch.per_tensor_affine, scale=0.1, zero_point=10)
    
    uint8
    [ 0 14 23 30]
    

    【讨论】:

      猜你喜欢
      • 2012-05-03
      • 1970-01-01
      • 2020-01-10
      • 1970-01-01
      • 1970-01-01
      • 2022-07-06
      • 2021-09-22
      • 2019-04-15
      • 1970-01-01
      相关资源
      最近更新 更多