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:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Sure, I can do it. I’ll set the default value of
keepdim
toFalse
to maintain the current behaviour if the arg is not specified.fixed in #1168