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

torch default for Flip #2263

Closed
wants to merge 8 commits into from
Closed

Conversation

rijobro
Copy link
Contributor

@rijobro rijobro commented May 27, 2021

Description

Starts to address: #2231.

To base class convert to/from numpy as necessary, and then convert back to the original at the end. Use torch for the main functionality.

This is an example case for the rest of the transforms.

I also created a class to contain types frequently used in our transforms:

class TransformTypes:
    """Common transform types."""
    Images = Union[torch.Tensor, np.ndarray]
    ImageDict = Dict[Hashable, Images]

By using these, we should be better able to unify our transforms (some only have np.ndarray, some use torch.Tensor, some have both).

Status

Ready

Types of changes

  • Non-breaking change (fix or new feature that would not break existing functionality).
  • Integration tests passed locally by running ./runtests.sh -f -u --net --coverage.
  • Quick tests passed locally by running ./runtests.sh --quick --unittests.
  • In-line docstrings updated.
  • Documentation updated, tested make html command in the docs/ folder.

Signed-off-by: Richard Brown <33289025+rijobro@users.noreply.github.com>
@rijobro rijobro requested review from wyli, ericspod and Nic-Ma May 27, 2021 12:53
rijobro added 6 commits May 27, 2021 13:55
Signed-off-by: Richard Brown <33289025+rijobro@users.noreply.github.com>
Signed-off-by: Richard Brown <33289025+rijobro@users.noreply.github.com>
Signed-off-by: Richard Brown <33289025+rijobro@users.noreply.github.com>
Signed-off-by: Richard Brown <33289025+rijobro@users.noreply.github.com>
Signed-off-by: Richard Brown <33289025+rijobro@users.noreply.github.com>
orig_type = type(data)
assert orig_type in (torch.Tensor, np.ndarray)

if orig_type is np.ndarray and required_type is torch.Tensor:
Copy link
Member

Choose a reason for hiding this comment

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

We can put this conversion block into a separate utility function somewhere:

def convert_data_type(data, current_type, required_type):
    if current_type is np.ndarray and required_type is torch.Tensor:
        return torch.Tensor(data)
    elif current_type is torch.Tensor and required_type is np.ndarray:
        return data.detach().cpu().numpy()

    return data

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good point, done.

Signed-off-by: Richard Brown <33289025+rijobro@users.noreply.github.com>
@wyli
Copy link
Contributor

wyli commented Aug 26, 2021

Addressed by #2822

@wyli wyli closed this Aug 26, 2021
@rijobro rijobro deleted the torch_default_flip branch August 26, 2021 08:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants