question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Option to avoid squeezing tensors with batch size = 1 when converting to arrays with tensor_to_image()

See original GitHub issue

🚀 Feature

Adding a boolean input argument keepdim to the tensor_to_image() function to avoid squeezing tensors with batch size 1.

Motivation

While the image_to_tensor() function has a keepdim argument that avoids turning (H, W, C ) images into (1, C, H, W) tensors, there is nothing similar in tensor_to_image() to avoid turning tensors (1, C, H, W) into arrays (H, W, C). Currently, there is no way of keeping the batch dimension when it is 1.

Take a look at this piece of code:

import torch
import kornia

t_img = torch.randn((1, 3, 100, 100))
img = kornia.tensor_to_image(t_img)  # shape 100x100x3

It would be nice to have something like this:

import torch
import kornia

t_img = torch.randn((1, 3, 100, 100))
img = kornia.tensor_to_image(t_img, keepdim=True)  # shape 1x100x100x3

Pitch

When keepdim=True, the tensor_to_image() function returns an array whose first dimension is the batch_size regardless of the batch_size being equal to 1 (as it is currently happening when batch_size > 1).

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
arubiorcommented, May 12, 2021

Sure, I can do it. I’ll set the default value of keepdim to False to maintain the current behaviour if the arg is not specified.

0reactions
edgarribacommented, Oct 10, 2021

fixed in #1168

Read more comments on GitHub >

github_iconTop Results From Across the Web

Numpy array loses a dimension converting to tf dataset
You need to set the batch size on the dataset so that it returns multiple examples instead of just one. This will also...
Read more >
4. Image Tensors - Learning TensorFlow.js [Book] - O'Reilly
Identify what makes a tensor an image tensor. Build some images by hand. Use fill methods to create large tensors. Convert existing images...
Read more >
Trying to infer the `batch_size` from an ambiguous collection
The batch size we found is 1. To avoid any miscalculations, use `self.log(..., batch_size=batch_size)`. "Trying to infer the `batch_size` ...
Read more >
Flatten, Reshape, and Squeeze Explained - Tensors for Deep ...
Let's jump in with reshaping operations. Tensor shape review. Suppose that we have the following tensor: > t = torch.tensor([ [1,1 ...
Read more >
Convert Images to Tensors in Pytorch and Tensorflow
N — batch size (number of images per batch); H — height of the image ... with its own built-in function called numpy()...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found