# Doubt with torch.from\_numpy with uint16 and how to\_tensor manage these kinds of images

**URL:** <https://discuss.pytorch.org/t/doubt-with-torch-from-numpy-with-uint16-and-how-to-tensor-manage-these-kinds-of-images/86410>\
**Category:** vision\
**Created:** [June 22, 2020, 1:58pm UTC](https://discuss.pytorch.org/t/doubt-with-torch-from-numpy-with-uint16-and-how-to-tensor-manage-these-kinds-of-images/86410 "2020-06-22T13:58:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dhorka](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@Dhorka](https://discuss.pytorch.org/u/Dhorka)\
**Post date:** [June 22, 2020, 1:58pm UTC](https://discuss.pytorch.org/t/doubt-with-torch-from-numpy-with-uint16-and-how-to-tensor-manage-these-kinds-of-images/86410/1 "2020-06-22T13:58:22Z")

</div>

Hi,

I have a doubt related to the function torch.from\_numpy. I’m trying to convert a numpy array that contains uint16 and I’m getting the following error:

> TypeError: can’t convert np.ndarray of type numpy.uint16. The only supported types are: float64, float32, float16, int64, int32, int16, int8, uint8, and bool.

I suppose one way to solve that is to convert my uint16 numpy array to a numpy array with int32. Is there another way to solve this issue? Am I doing something wrong?

On the other hand, I was wondering how **to\_tensor method from torchvision** is able to transform uint16 images. I checked the [source code](https://github.com/pytorch/vision/blob/cd2b7f071cbfb207400e70deaf4b1b2c46c3e47c/torchvision/transforms/functional.py#L72) and I realized that there is a conversion to int16… However, in the official [documentation](https://pillow.readthedocs.io/en/stable/handbook/concepts.html#concept-modes) of PIL library specifies that the I;16 mode indicate that the pixel contains an unsigned int16 pixel. Is there a bug on this spcific line? Am I missing something?

Thanks,

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [June 22, 2020, 2:25pm UTC](https://discuss.pytorch.org/t/doubt-with-torch-from-numpy-with-uint16-and-how-to-tensor-manage-these-kinds-of-images/86410/2 "2020-06-22T14:25:01Z")

</div>

Hi,

I think the answer here depends on the use case.  
If you want to be use not to loose any precision, then yes you want to convert it to an int32 before converting to pytorch.  
If you don’t want to use double the space, you can use int16 and it should work just fine as long as your numbers are not too big.

Do you encounter any issue with the choice made by torchvision?

---

<div class="post-metadata">

**Author:** ![Dhorka](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@Dhorka](https://discuss.pytorch.org/u/Dhorka)\
**Post date:** [June 22, 2020, 2:32pm UTC](https://discuss.pytorch.org/t/doubt-with-torch-from-numpy-with-uint16-and-how-to-tensor-manage-these-kinds-of-images/86410/3 "2020-06-22T14:32:16Z")

</div>

> [@albanD](#):
>
> Do you encounter any issue with the choice made by torchvision?

Yes, it my case my numbers are big and I need all the space.

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [June 22, 2020, 2:35pm UTC](https://discuss.pytorch.org/t/doubt-with-torch-from-numpy-with-uint16-and-how-to-tensor-manage-these-kinds-of-images/86410/4 "2020-06-22T14:35:51Z")

</div>

In that case, I guess the simplest is going to be make sure your dataset returns Tensors directly. And convert them to int32 before converting them to torch Tensors.

---

<div class="post-metadata">

**Author:** ![Dhorka](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@Dhorka](https://discuss.pytorch.org/u/Dhorka)\
**Post date:** [June 22, 2020, 2:59pm UTC](https://discuss.pytorch.org/t/doubt-with-torch-from-numpy-with-uint16-and-how-to-tensor-manage-these-kinds-of-images/86410/5 "2020-06-22T14:59:03Z")

</div>

I’m going to do that. Thanks. However, maybe It will be a good idea to document this decision about how it is managed uint16 by ToTensor() method.

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [June 22, 2020, 3:04pm UTC](https://discuss.pytorch.org/t/doubt-with-torch-from-numpy-with-uint16-and-how-to-tensor-manage-these-kinds-of-images/86410/6 "2020-06-22T15:04:49Z")

</div>

cc @fmassa what do you think?

---

<div class="post-metadata">

**Author:** ![Arun\_Kumar4](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/arun_kumar4/32/41523_2.png) [@Arun\_Kumar4](https://discuss.pytorch.org/u/Arun_Kumar4)\
**Post date:** [September 1, 2021, 9:28am UTC](https://discuss.pytorch.org/t/doubt-with-torch-from-numpy-with-uint16-and-how-to-tensor-manage-these-kinds-of-images/86410/7 "2021-09-01T09:28:14Z")

</div>

i have to change uint16 to int64 but i have range values of 0 -9 for y varaible inj neural network  
tell me solution for this please

---

<div class="post-metadata">

**Author:** ![Arun\_Kumar4](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/arun_kumar4/32/41523_2.png) [@Arun\_Kumar4](https://discuss.pytorch.org/u/Arun_Kumar4)\
**Post date:** [September 30, 2021, 5:32pm UTC](https://discuss.pytorch.org/t/doubt-with-torch-from-numpy-with-uint16-and-how-to-tensor-manage-these-kinds-of-images/86410/8 "2021-09-30T17:32:59Z")

</div>

I have uint16 target image between 0 and 9 values how to use in pytorch dataloader
