Bug in int8 ndarray to tensor conversion or in my understanding?

torch.tensor(ndarray) works fine for me. However, the labels are a binary mask so I’m storing them as an ndarray of numpy int8. The following code:
label_dict[key] = torch.Tensor(label)
fails with:
RuntimeError: tried to construct a tensor from a nested float sequence, but found an item of type numpy.int8 at index (0, 0, 0)

If label is converted with .astype(np.uint8) ahead of time, I get:
RuntimeError: tried to construct a tensor from a nested int sequence, but found an item of type numpy.uint8 at index (0, 0, 0)

I can obviously preallocate an appropriately sized CharTensor and then copy myself, but it seems like that shouldn’t be necessary.

Trying to work around it with a ByteTensor as dst and ndarray of uint8 as follows:

def copy_tensor(dst, src):
    for z in range(Z_MAX):
        for y in range(Y_MAX):
            for x in range(X_MAX):
                val = src[z][y][x]
                dst[z][y][x] = val

yields:
RuntimeError: can’t assign a numpy.uint8 to a scalar value of type int

The labels masks are 192x160x192, so it really would be nicer to keep them as single byte tensors.

this does work - albeit slowly. And it’s actually so slow as to be useless.

def copy_tensor(dst, src):
    for z in range(Z_MAX):
        for y in range(Y_MAX):
            for x in range(X_MAX):
                val = src[z][y][x]
                if val != 0:
                    dst[z][y][x] = 1

It appears that the only ndarray to tensor conversion that actually works is floating point.

Hi,

You can use the torch.from_numpy(ndarray) function to convert numpy array from numpy.
If you use it, you will see that int8 is not supported in torch, the error message by the from_numpy function will tell you which types are supported. In your case, I think you want to use the uint8 type (that is supported).

When you use torch.Tensor(ndarray) (which is a shorthand for torch.FloatTensor(ndarray) if you didn’t changed the default tensor type), it will try to build a FloatTensor from the ndarray, which is not an array of float in your case but int8.
If you want to build a tensor of uint8 elements, you will need to build it with the right type: torch.ByteTensor(ndarray).
I would advice using the from_numpy function as it finds the corresponding tensor type itself, while if you directly build the tensor, you will have to specify the type yourself.

2 Likes

Converting to np.uint8 and then calling from_numpy works. Thanks.

1 Like

Hi, torch.from_numpy(ndarray) works on uint8,
but if I want to covert the array to int8 what should I do.
I tried to use the torch.CharTensor but getting error
RuntimeError: tried to construct a tensor from a nested int sequence, but found an item of type numpy.int8 at index (0, 0, 0, 0)

Thank you very much

1 Like