Search code examples
pythonpytorchcomputer-visiontorchvision

Unable to convert Tensor to FloatTensor in Pytorch


I have:

def pytorchConvolution(img, kernel):    
    img=torch.from_numpy(img)
    kernel=torch.from_numpy(kernel)
    
    img.type(torch.FloatTensor)
    kernel.type(torch.FloatTensor)

    dtype_inputs = torch.quint8
    dtype_filters = torch.qint8
    
    scale, zero_point = 1.0, 0
    q_filters = torch.quantize_per_tensor(kernel, scale, zero_point, dtype_filters)
    q_inputs = torch.quantize_per_tensor(img, scale, zero_point, dtype_inputs)
    bias = torch.randn(8, dtype=torch.float)
     
    print(img.type())
    
    convolution2d=qF.conv2d(q_inputs,q_filters,bias)  
    return(convolution2d)

which is used here:

blur_filter = (1/250)*np.ones([5, 5])
img_blurred_py = pytorchConvolution(img, blur_filter)

However, upon running this code, I get the following output, meaning PyTorch did not convert the tensor to FloatTensor

torch.DoubleTensor

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Input In [190], in <cell line: 6>()
      4 img_blurred = convolution(img, blur_filter)
      5 img_blurred_c = convolutionImplantation(img, blur_filter)
----> 6 img_blurred_py = pytorchConvolution(img, blur_filter)
      8 plt.axis("off")
      9 plt.subplot(1, 2, 1)

Input In [189], in pytorchConvolution(img, kernel)
      9 dtype_filters = torch.qint8
     11 scale, zero_point = 1.0, 0
---> 12 q_filters = torch.quantize_per_tensor(kernel, scale, zero_point, dtype_filters)
     13 q_inputs = torch.quantize_per_tensor(img, scale, zero_point, dtype_inputs)
     14 bias = torch.randn(8, dtype=torch.float)

RuntimeError: Quantize only works on Float Tensor, got Double

enter image description here

I have set the default_tensor_type to FloatTensor, and tried to convert to other Tensor Types, however, PyTorch does not convert the tensor to any type. I need to convert both the tensors to FloatTensor to quantize them.


Solution

  • The type function does not operate in place:

    In [2]: a = torch.tensor([1,2])
    
    In [3]: a.dtype
    Out[3]: torch.int64
    
    In [4]: a.type(torch.FloatTensor)
    Out[4]: tensor([1., 2.])
    
    In [5]: a.dtype
    Out[5]: torch.int64
    

    You need to assign these back into the variable:

    In [7]: a = a.type(torch.FloatTensor)
    
    In [8]: a.dtype
    Out[8]: torch.float32