Home > Mobile >  Error in converting torch tensor to numpy.ndarray
Error in converting torch tensor to numpy.ndarray

Time:12-04

var1 = tensor([[[[0., 1., 1.,  ..., 1., 0., 0.],
          [0., 0., 1.,  ..., 0., 0., 0.],
          [0., 0., 0.,  ..., 0., 0., 1.],
          ...,
          [0., 0., 0.,  ..., 1., 1., 1.],
          [0., 0., 0.,  ..., 1., 1., 1.],
          [0., 0., 0.,  ..., 1., 1., 1.]]]])

print(var1.size())
print(type(var1))
print(var1.dtype)

Output:

torch.Size([1, 1, 480, 640])
<class 'torch.Tensor'>
torch.float32

When I tried to convert torch tensor into numpy.ndarray, all values became zero.

nump_var1 = var1.argmax(dim=1).squeeze(0).cpu().numpy()
print(nump_var1)
print(nump_var1.shape)
print(type(nump_var1))
print(nump_var1.dtype)

Output:

    [[0 0 0 ... 0 0 0]
     [0 0 0 ... 0 0 0]
     [0 0 0 ... 0 0 0]
     ...
     [0 0 0 ... 0 0 0]
     [0 0 0 ... 0 0 0]
     [0 0 0 ... 0 0 0]]

(480, 640)
<class 'numpy.ndarray'>
int64

Can anyone point out the mistake I have made?

Thanks for the help.

CodePudding user response:

As hpaulj hinted at in a comment, var1.argmax(dim=1) will result in a zero tensor because you have var1.size(1) == 1.

CodePudding user response:

The error seems to occur when using argmax() on the tensor. The argmax() function returns the index of the maximum value of a tensor along a given dimension. In your code, you are using argmax() with the dim argument set to 1, which will return the indices of the maximum value along the second dimension of the tensor.

However, since your tensor has only one element in the second dimension, the argmax() function will always return 0 as the index of the maximum value, which means that all the values in your tensor will be set to 0 when you convert it to a NumPy array.

To avoid this issue, you can try using the numpy() function instead of the argmax() function to convert your tensor to a NumPy array. This will simply convert the tensor to a NumPy array without changing the values.

Here is how you can do it:

nump_var1 = var1.squeeze(0).cpu().numpy()

You can also remove the squeeze() function, as it is not necessary in this case. The squeeze() function is used to remove dimensions of size 1 from a tensor, but since your tensor has only one element in the first and second dimensions, the squeeze() function will not have any effect.

Here is the updated code:

var1 = tensor([[[[0., 1., 1.,  ..., 1., 0., 0.],
          [0., 0., 1.,  ..., 0., 0., 0.],
          [0., 0., 0.,  ..., 0., 0., 1.],
          ...,
          [0., 0., 0.,  ..., 1., 1., 1.],
          [0., 0., 0.,  ..., 1., 1., 1.],
          [0., 0., 0.,  ..., 1., 1., 1.]]]])

print(var1.size())
print(type(var1))
print(var1.dtype)

# Use the numpy() function to convert the tensor to a NumPy array
nump_var1 = var1.cpu().numpy()

print(nump_var1)
print(nump_var1.shape)
print(type(nump_var1))
print(nump_var1.dtype)

This should fix the issue and give you the expected output. Let me know if this helps.

  • Related