'pytorch int32 to int64 conversion
I'm trying to convert a simple image mask to int64
image = np.array([[1, 2], [3, 4]], dtype='int32')
transform = Compose([
torch.from_numpy,
ConvertImageDtype(torch.int64)
])
However, transform(image) yields
tensor([[ 4294967296, 8589934592],
[12884901888, 17179869184]])
Is there something wrong, or am I fundamentally misunderstanding something about how the conversion should work?
Solution 1:[1]
If you skip torch's conversion, the image is transformed correctly.
image = np.array([[1, 2], [3, 4]], dtype='int64')
transform = Compose([
torch.from_numpy
])
transform(image)
# tensor([[1, 2],
# [3, 4]])
Solution 2:[2]
I'm late but just in case…
The ConvertImageDtype docstring states:
Convert a tensor image to the given dtype and scale the values accordingly
In the source code, we can see this function calls a F.convert_image_dtype function which then calls a F_t.convert_image_dtype function where we can understand how the scaling is done:
...
input_max = _max_value(image.dtype)
...
output_max = _max_value(dtype)
...
factor = int((output_max + 1) // (input_max + 1))
...
return image * factor
For the int32 representation input_max == 2147483647 == 2**(32-1) -1.
For the int64 representation output_max == 9223372036854775807 == 2**(64-1) -1.
(The -1 in 32-1 and 64-1 is because one byte is used for the sign.
The final -1… I don't know, but we see that factor uses a +1 to "correct" it.)
So we get factor == 4294967296 which is used to scale your image…
Now, in the source code we can also see:
if image.is_floating_point():
...
# float to int <= actually written in the code
...
else:
...
# int to float <= actually written in the code
...
So basically the code assumes one won't try anything except "float to int" and "int to float" conversion, but does not strictly check it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alex Metsai |
| Solution 2 | François P. |
