Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show tensors shaped [H, W, 1, 1] as images (and more!) #2075

Merged
merged 11 commits into from
May 10, 2023

Conversation

Wumpf
Copy link
Member

@Wumpf Wumpf commented May 9, 2023

Ignore trailing tensor dimension when determining whether a tensor is an image.

Fixes #1871

There's a surprising amount of nuance in order to support single channel 1x1 images & line-like images.

image

Checklist

PR Build Summary: https://build.rerun.io/pr/2075

… an image.

Fixes #1871
Quite a bit of nuance to support single channel 1x1 images & line-like images.
@Wumpf Wumpf force-pushed the andreas/handle-trailing-dimensions branch from 10205f3 to 2efeb12 Compare May 9, 2023 16:44
@Wumpf Wumpf added the 📺 re_viewer affects re_viewer itself label May 9, 2023
Copy link
Member

@emilk emilk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even more nuance required!

.enumerate()
.rev()
.find(|(_, dim)| dim.size != 1)
.map_or(&self.shape[0..1], |(i, _)| &self.shape[..(i + 1)])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm starting to think we should have a struct Shape(Vec<TensorDimension>). It would also solve the formatting of shapes for which we currently use the ugly Debug formatting in a bunch of places (one more in this PR).

Oh well, out of scope for this PR

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a comment about this to #1992

crates/re_log_types/src/component_types/tensor.rs Outdated Show resolved Hide resolved
Comment on lines +422 to +423
match shape_short.len() {
1 => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if shape_short would return just the integers (not the names) we could make this match a lot nicer;

Suggested change
match shape_short.len() {
1 => {
match shape_short {
[h] => {},
[h, w] => {},
[h, w, c] => {},

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

…but it would require an allocation, or that we change how shape is stored (see #1992 (comment)), so let's not right now

crates/re_log_types/src/component_types/tensor.rs Outdated Show resolved Hide resolved
crates/re_log_types/src/component_types/tensor.rs Outdated Show resolved Hide resolved
crates/re_viewer_context/src/gpu_bridge/tensor_to_gpu.rs Outdated Show resolved Hide resolved
crates/re_log_types/src/component_types/tensor.rs Outdated Show resolved Hide resolved
crates/re_log_types/src/component_types/tensor.rs Outdated Show resolved Hide resolved
crates/re_log_types/src/component_types/tensor.rs Outdated Show resolved Hide resolved
crates/re_log_types/src/component_types/tensor.rs Outdated Show resolved Hide resolved
crates/re_log_types/src/component_types/tensor.rs Outdated Show resolved Hide resolved
crates/re_log_types/src/component_types/tensor.rs Outdated Show resolved Hide resolved
@Wumpf
Copy link
Member Author

Wumpf commented May 9, 2023

(almost done addressing everything, but logging off now; finishing tomorrow)

@Wumpf Wumpf requested a review from emilk May 10, 2023 08:46
@Wumpf
Copy link
Member Author

Wumpf commented May 10, 2023

nuancing away! This one might warrant a short re-review.
Still testing via run_all meanwhile

Comment on lines +503 to +510
dim => self.image_height_width_channels().and_then(|_| {
self.get(
&[x, y, channel]
.into_iter()
.chain(std::iter::repeat(0).take(dim - 3))
.collect::<Vec<u64>>(),
)
}),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should return None if using get_with_image_coords on a tensor that is non-image like, e.g. 8x7x6x5x4x3

So maybe just check if self.short_shape().len() <= 3 in this branch

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the check in here implied by self.image_height_width_channels().and_then is actually stronger and would also reject e.g. 3x3x5
So I think this is alright this way (albeit more expensive)

@Wumpf Wumpf merged commit 3601d2e into main May 10, 2023
15 checks passed
@Wumpf Wumpf deleted the andreas/handle-trailing-dimensions branch May 10, 2023 12:20
jprochazk pushed a commit that referenced this pull request May 11, 2023
* Ignore trailing tensor dimension when determining whether a tensor is an image.
Fixes #1871
Quite a bit of nuance to support single channel 1x1 images & line-like images.

* fix image preview for images other than M x N x C and M x N

* comment fix

* better shape_short comment

* handle empty tensors

* is_shaped_like_an_image is now defined via image_height_width_channels, improve comment on both

* any Nx1x... image now now treated as image

* rename to get_with_image_coords, make it more restrictive

* tensor_to_gpu height_width_depth utility uses now tensor.image_height_width_channels

* change behavior of is_vector
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📺 re_viewer affects re_viewer itself
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Show tensors shaped [H, W, 1, 1] as images
3 participants