Skip to content

[AIR] Allow users to pass Callable[[torch.Tensor], torch.Tensor] to TorchVisionTransform - #32383

Merged
amogkam merged 5 commits into
ray-project:masterfrom
bveeramani:torch-preprocessor-update
Feb 10, 2023
Merged

[AIR] Allow users to pass Callable[[torch.Tensor], torch.Tensor] to TorchVisionTransform#32383
amogkam merged 5 commits into
ray-project:masterfrom
bveeramani:torch-preprocessor-update

Conversation

@bveeramani

@bveeramani bveeramani commented Feb 9, 2023

Copy link
Copy Markdown
Member

Signed-off-by: Balaji Veeramani balaji@anyscale.com

Why are these changes needed?

Transforms like RandomHorizontalFlip expect Torch tensors as input, but if you're applying the transform per-epoch, then you can't use ToTensor. To fix the problem, this PR updates TorchVisionPreprocessor to convert ndarray inputs to Torch tensors.

You can't use ToTensor to convert the ndarrays to Torch tensors because then you'd be applying ToTensor twice, and your images would get scaled incorrectly.

transform = ToTensor()
preprocessor = TorchVisionPreprocessor(["image"], transform=transform)

# You can't use `ToTensor` twice, because then images would be scaled incorrectly
per_epoch_transforms = Compose([ToTensor(), RandomHorizontalFlip(0.5)])
per_epoch_preprocessor = TorchVisionPreprocessor(["image"], transforms=per_epoch_transform) 

Related issue number

Checks

  • I've signed off every commit(by using the -s flag, i.e., git commit -s) in this PR.
  • I've run scripts/format.sh to lint the changes in this PR.
  • I've included any doc changes needed for https://docs.ray.io/en/master/.
  • I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/
  • Testing Strategy
    • Unit tests
    • Release tests
    • This PR is not tested :(

Signed-off-by: Balaji Veeramani <balaji@anyscale.com>
Signed-off-by: Balaji Veeramani <balaji@anyscale.com>
Signed-off-by: Balaji Veeramani <balaji@anyscale.com>
@bveeramani bveeramani changed the title [AIR] Allow users to pass Callable[[np.ndarray], torch.Tensor] to TorchVisionTransform [AIR] Allow users to pass Callable[[torch.Tensor], torch.Tensor] to TorchVisionTransform Feb 10, 2023

@amogkam amogkam left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lgtm!

Signed-off-by: Balaji Veeramani <balaji@anyscale.com>
Signed-off-by: Balaji Veeramani <balaji@anyscale.com>
@bveeramani bveeramani added the tests-ok The tagger certifies test failures are unrelated and assumes personal liability. label Feb 10, 2023
@bveeramani

Copy link
Copy Markdown
Member Author

Test failures are unrelated.

@amogkam

amogkam commented Feb 10, 2023

Copy link
Copy Markdown
Contributor

Can you please add a pr description?

@amogkam
amogkam merged commit faeb2cc into ray-project:master Feb 10, 2023
@bveeramani
bveeramani deleted the torch-preprocessor-update branch February 10, 2023 21:17
edoakes pushed a commit to edoakes/ray that referenced this pull request Mar 22, 2023
… `TorchVisionTransform` (ray-project#32383)

Transforms like RandomHorizontalFlip expect Torch tensors as input, but if you're applying the transform per-epoch, then you can't use ToTensor. To fix the problem, this PR updates TorchVisionPreprocessor to convert ndarray inputs to Torch tensors.

You can't use ToTensor to convert the ndarrays to Torch tensors because then you'd be applying ToTensor twice, and your images would get scaled incorrectly.

Signed-off-by: Balaji Veeramani <balaji@anyscale.com>
Signed-off-by: Edward Oakes <ed.nmi.oakes@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tests-ok The tagger certifies test failures are unrelated and assumes personal liability.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants