# Is there an encode\_png method for cuda tensors?

**URL:** <https://discuss.pytorch.org/t/is-there-an-encode-png-method-for-cuda-tensors/123634>\
**Category:** vision\
**Created:** [June 8, 2021, 9:13pm UTC](https://discuss.pytorch.org/t/is-there-an-encode-png-method-for-cuda-tensors/123634 "2021-06-08T21:13:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pablo1](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pablo1/32/39501_2.png) [@Pablo1](https://discuss.pytorch.org/u/Pablo1)\
**Post date:** [June 8, 2021, 9:13pm UTC](https://discuss.pytorch.org/t/is-there-an-encode-png-method-for-cuda-tensors/123634/1 "2021-06-08T21:13:29Z")

</div>

Hello,

Is there a way to get the size of a cuda tensor as if it were encoded as PNG?

I’ve tried this way:

```auto
inv_trans = transforms.Compose([transforms.Normalize(mean = [ 0., 0., 0.],
                                                     std = [1/0.229, 1/0.224, 1/0.225]),
                                transforms.Normalize(mean = [-0.485, -0.456, -0.406],
                                                     std = [1., 1., 1.]),
                                lambda x: x*255
                               ])
def get_size_as_png(input_tensor):
  k = inv_trans(input_tensor)
  a = torchvision.io.encode_png(k.type(torch.uint8)) 
  return sys.getsizeof(a.storage())

```

But when I have tensors in cuda I get the following error:

```auto
RuntimeError: Input tensor should be on CPU

```

Many thanks in advance

---

<div class="post-metadata">

**Author:** ![eqy](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/eqy/32/46233_2.png) [@eqy](https://discuss.pytorch.org/u/eqy)\
**Post date:** [June 8, 2021, 11:37pm UTC](https://discuss.pytorch.org/t/is-there-an-encode-png-method-for-cuda-tensors/123634/2 "2021-06-08T23:37:34Z")

</div>

You can call `cpu_tensor = mytensor.cpu()` to get a copy of the data in host memory.

---

<div class="post-metadata">

**Author:** ![Pablo1](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pablo1/32/39501_2.png) [@Pablo1](https://discuss.pytorch.org/u/Pablo1)\
**Post date:** [June 8, 2021, 11:51pm UTC](https://discuss.pytorch.org/t/is-there-an-encode-png-method-for-cuda-tensors/123634/3 "2021-06-08T23:51:14Z")

</div>

Thanks for your response,

I’m looking for the size of the tensor as if it were a PNG image, not the size of the tensor
