-
Notifications
You must be signed in to change notification settings - Fork 7.1k
[prototype] Minor speed and nit optimizations on Transform Classes #6837
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
Changes from 1 commit
49f7e5a
5ea065c
99b1685
17d8184
5e0be6e
08ae56f
7b8be17
5f7e1ee
b0b9b55
ee31969
88328f5
e15f536
53f12bb
843bcc9
8e6af8d
11af094
4f90ce1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,4 +1,4 @@ | ||||||
from typing import Any, cast, Dict, Optional, Union | ||||||
from typing import Any, Dict, Optional, Union | ||||||
|
||||||
import numpy as np | ||||||
import PIL.Image | ||||||
|
@@ -13,7 +13,7 @@ class DecodeImage(Transform): | |||||
_transformed_types = (features.EncodedImage,) | ||||||
|
||||||
def _transform(self, inpt: torch.Tensor, params: Dict[str, Any]) -> features.Image: | ||||||
return cast(features.Image, F.decode_image_with_pil(inpt)) | ||||||
return F.decode_image_with_pil(inpt) # type: ignore[no-any-return] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has to be here, because it seems
doesn't "forward" the type annotations 🙄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In all other places we took the decision to silence with ignore rather than cast, do we really need the cast here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nono, I was just explaining why we need the ignore for future me that is looking confused at the blame why we introduced it in the first place. |
||||||
|
||||||
|
||||||
class LabelToOneHot(Transform): | ||||||
|
@@ -27,7 +27,7 @@ def _transform(self, inpt: features.Label, params: Dict[str, Any]) -> features.O | |||||
num_categories = self.num_categories | ||||||
if num_categories == -1 and inpt.categories is not None: | ||||||
num_categories = len(inpt.categories) | ||||||
output = one_hot(inpt, num_classes=num_categories) | ||||||
output = one_hot(inpt.as_subclass(torch.Tensor), num_classes=num_categories) | ||||||
datumbox marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
return features.OneHotLabel(output, categories=inpt.categories) | ||||||
|
||||||
def extra_repr(self) -> str: | ||||||
|
@@ -50,7 +50,7 @@ class ToImageTensor(Transform): | |||||
def _transform( | ||||||
self, inpt: Union[torch.Tensor, PIL.Image.Image, np.ndarray], params: Dict[str, Any] | ||||||
) -> features.Image: | ||||||
return cast(features.Image, F.to_image_tensor(inpt)) | ||||||
return F.to_image_tensor(inpt) # type: ignore[no-any-return] | ||||||
|
||||||
|
||||||
class ToImagePIL(Transform): | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,7 +97,7 @@ def _check_padding_mode_arg(padding_mode: Literal["constant", "edge", "reflect", | |
|
||
|
||
def query_bounding_box(flat_inputs: List[Any]) -> features.BoundingBox: | ||
bounding_boxes = {inpt for inpt in flat_inputs if isinstance(inpt, features.BoundingBox)} | ||
bounding_boxes = [inpt for inpt in flat_inputs if isinstance(inpt, features.BoundingBox)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can use a list here instead of a set. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a perf optimization? Otherwise I would prefer the set since
Functionally, the only difference is that with a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's both perf and code correctness. What we want here is to ensure there is only 1 bbox in the input. If there were two, even if they were the same exact copy, our transforms that rely on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Are
Agreed.
You are right that, I was not clear enough. What I meant was: we are not checking for duplicate images, videos, masks, ... anywhere. Either we should do that (and that seems like the right thing to do, but that probably has perf implications) or we shouldn't single out bounding boxes here. No strong opinion though. A single mitigation is probably better than none. But we should still discuss if we need to check this for everything. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discussed offline, On the other hand the So this is primarily a bug fix to restore the right semantics on the function and also has secondary positive performance implications. |
||
if not bounding_boxes: | ||
raise TypeError("No bounding box was found in the sample") | ||
elif len(bounding_boxes) > 1: | ||
|
Uh oh!
There was an error while loading. Please reload this page.