Skip to content

[proto] Clean-up Label.to_categories #6419

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

Merged
merged 3 commits into from
Aug 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions torchvision/prototype/features/_label.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from __future__ import annotations

from typing import Any, cast, Optional, Sequence, Union
from typing import Any, Optional, Sequence, Union

import torch
from torchvision.prototype.utils._internal import apply_recursively
from torch.utils._pytree import tree_map

from ._feature import _Feature

Expand Down Expand Up @@ -43,10 +43,10 @@ def from_category(
return cls(categories.index(category), categories=categories, **kwargs)

def to_categories(self) -> Any:
if not self.categories:
raise RuntimeError()
if self.categories is None:
raise RuntimeError("Label does not have categories")

return apply_recursively(lambda idx: cast(Sequence[str], self.categories)[idx], self.tolist())
return tree_map(lambda idx: self.categories[idx], self.tolist())
Copy link
Member

Choose a reason for hiding this comment

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

We have a lot of calls to apply_recursively, should we update them all?

Copy link
Member

@NicolasHug NicolasHug Aug 15, 2022

Choose a reason for hiding this comment

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

but also, _pytree looks private - should we actually use it ?

Copy link
Collaborator Author

@vfdev-5 vfdev-5 Aug 15, 2022

Choose a reason for hiding this comment

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

Yes, in transforms I already replaced almost all of them (remains only BatchMultiCrop)



class OneHotLabel(_Feature):
Expand Down