-
Notifications
You must be signed in to change notification settings - Fork 516
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
Allow ragged_batches to be configured when using Torch models with custom transforms #4512
Conversation
WalkthroughThe new changes in Changes
Possibly related issues
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Outside diff range and nitpick comments (7)
fiftyone/utils/torch.py (7)
Line range hint
31-34
: Imports should be placed at the top of the file.- import torch - import torchvision - from torchvision.transforms import functional as F - from torch.utils.data import DatasetMove these imports to the top of the file to adhere to PEP 8 standards for better readability and maintenance.
Line range hint
167-167
: Unnecessary open mode parameters in file open operation.- with open(req_path, "r") as f: + with open(req_path) as f:The default mode for
open()
is 'r' (read), so specifying it is redundant.
Line range hint
283-286
: Simplify conditional assignments using ternary operators.- if isinstance(arg, torch.Tensor): - args = arg.unsqueeze(0) - else: - args = [arg] + args = arg.unsqueeze(0) if isinstance(arg, torch.Tensor) else [arg] - if isinstance(img, torch.Tensor): - imgs = img.unsqueeze(0) - else: - imgs = [img] + imgs = img.unsqueeze(0) if isinstance(img, torch.Tensor) else [img] - if isinstance(img, torch.Tensor): - imgs = img.unsqueeze(0) - else: - imgs = [img] + imgs = img.unsqueeze(0) if isinstance(img, torch.Tensor) else [img] - if sample is not None: - samples = [sample] - else: - samples = None + samples = [sample] if sample is not None else NoneThese changes make the code more concise and maintainable by using Python's ternary conditional operator.
Also applies to: 666-669, 879-882, 884-887
Line range hint
892-892
: Remove unnecessary inheritance fromobject
.- class ToPILImage(object): + class ToPILImage: - class MinResize(object): + class MinResize: - class MaxResize(object): + class MaxResize: - class PatchSize(object): + class PatchSize: - class SaveLayerTensor(object): + class SaveLayerTensor: - class OutputProcessor(object): + class OutputProcessor:In Python 3, all classes implicitly inherit from
object
, so explicitly deriving fromobject
is redundant and not needed.Also applies to: 904-904, 942-942, 980-980, 1006-1006, 1047-1047
Line range hint
1042-1042
: Use format specifiers instead of percent format for string formatting.- raise ValueError("No layer found with name %s" % layer_name) + raise ValueError(f"No layer found with name {layer_name}") - raise ValueError("Image '%s' has no patches" % filepath) + raise ValueError(f"Image '{filepath}' has no patches") - raise ValueError("Patches field '%s' has unsupported type %s" % (patches_field, label_type)) + raise ValueError(f"Patches field '{patches_field}' has unsupported type {label_type}") - raise ValueError("Either `image_paths` or `samples` must be provided") + raise ValueError("Either 'image_paths' or 'samples' must be provided")Using f-strings provides a more readable, concise, and Pythonic way of formatting strings.
Also applies to: 1916-1916, 1939-1940, 1961-1961
Line range hint
1446-1446
: Avoid using bare 'except' statements.- except: + except Exception as e:Catching
Exception
is generally safer than a bareexcept
as it avoids accidentally catching system-exiting exceptions such asSystemExit
andKeyboardInterrupt
.
Line range hint
1689-1692
: Use ternary operators for conditional assignments.- if etau.is_str(targets): - targets = samples.values(targets) - else: - targets = list(targets) + targets = samples.values(targets) if etau.is_str(targets) else list(targets) - if str_targets: - targets = _to_bytes_array(targets) - else: - targets = np.array(targets) + targets = _to_bytes_array(targets) if str_targets else np.array(targets)Using ternary operators can simplify the code and improve readability.
Also applies to: 1695-1698
Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Files selected for processing (1)
- fiftyone/utils/torch.py (3 hunks)
Additional context used
Ruff
fiftyone/utils/torch.py
31-31: Module level import not at top of file (E402)
32-32: Module level import not at top of file (E402)
33-33: Module level import not at top of file (E402)
34-34: Module level import not at top of file (E402)
167-167: Unnecessary open mode parameters (UP015)
Remove open mode parameters
283-286: Use ternary operator
args = arg.unsqueeze(0) if isinstance(arg, torch.Tensor) else [arg]
instead ofif
-else
-block (SIM108)Replace
if
-else
-block withargs = arg.unsqueeze(0) if isinstance(arg, torch.Tensor) else [arg]
666-669: Use ternary operator
imgs = img.unsqueeze(0) if isinstance(img, torch.Tensor) else [img]
instead ofif
-else
-block (SIM108)Replace
if
-else
-block withimgs = img.unsqueeze(0) if isinstance(img, torch.Tensor) else [img]
879-882: Use ternary operator
imgs = img.unsqueeze(0) if isinstance(img, torch.Tensor) else [img]
instead ofif
-else
-block (SIM108)Replace
if
-else
-block withimgs = img.unsqueeze(0) if isinstance(img, torch.Tensor) else [img]
884-887: Use ternary operator
samples = [sample] if sample is not None else None
instead ofif
-else
-block (SIM108)Replace
if
-else
-block withsamples = [sample] if sample is not None else None
892-892: Class
ToPILImage
inherits fromobject
(UP004)Remove
object
inheritance
904-904: Class
MinResize
inherits fromobject
(UP004)Remove
object
inheritance
942-942: Class
MaxResize
inherits fromobject
(UP004)Remove
object
inheritance
980-980: Class
PatchSize
inherits fromobject
(UP004)Remove
object
inheritance
1006-1006: Class
SaveLayerTensor
inherits fromobject
(UP004)Remove
object
inheritance
1042-1042: Use format specifiers instead of percent format (UP031)
Replace with format specifiers
1047-1047: Class
OutputProcessor
inherits fromobject
(UP004)Remove
object
inheritance
1446-1446: Do not use bare
except
(E722)
1689-1692: Use ternary operator
targets = samples.values(targets) if etau.is_str(targets) else list(targets)
instead ofif
-else
-block (SIM108)Replace
if
-else
-block withtargets = samples.values(targets) if etau.is_str(targets) else list(targets)
1695-1698: Use ternary operator
targets = _to_bytes_array(targets) if str_targets else np.array(targets)
instead ofif
-else
-block (SIM108)Replace
if
-else
-block withtargets = _to_bytes_array(targets) if str_targets else np.array(targets)
1916-1916: Use format specifiers instead of percent format (UP031)
Replace with format specifiers
1939-1940: Use format specifiers instead of percent format (UP031)
Replace with format specifiers
1961-1961: Use format specifiers instead of percent format (UP031)
Replace with format specifiers
Hey @dimidagd sorry for the delayed review. At CVPR, so things are pretty hectic. The implementation looks sound and doesn't break anything for me. Do you have an example transform I could use to test this out? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
Resolves #4508
Completes #4509 as per #4509 (review).
Summary by CodeRabbit
ragged_batches
parameter to the batch inference functionality, allowing for the handling of tensors of different sizes.