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

Adds ImageToTensor module and resize for non-batched images #978

Merged
merged 3 commits into from
Apr 26, 2021

Conversation

edgarriba
Copy link
Member

Description

This PR implements resize to work with non batch and adds ImageToTensor module.

Status

Ready/Work in progress/Hold

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (fix or new feature that would cause existing functionality to change)
  • New tests added to cover the changes
  • Docstrings/Documentation updated

PR Checklist

PR Implementer

This is a small checklist for the implementation details of this PR.

If there are any questions regarding code style or other conventions check out our
summary.

  • Did you discuss the functionality or any breaking changes before ?
  • Pass all tests: did you test in local ? make test
  • Unittests: did you add tests for your new functionality ?
  • Documentations: did you build documentation ? make build-docs
  • Implementation: is your code well commented and follow conventions ? make lint
  • Docstrings & Typing: has your code documentation and typing ? make mypy
  • Update notebooks & documentation if necessary

KorniaTeam

KorniaTeam workflow
  • Assign correct label
  • Assign PR to a reviewer
  • Does this PR close an Issue? (add closes #IssueNumber at the bottom if
    not already in description)

Reviewer

Reviewer workflow
  • Do all tests pass? (Unittests, Typing, Linting, Documentation, Environment)
  • Does the implementation follow kornia design conventions?
  • Is the documentation complete enough ?
  • Are the tests covering simple and corner cases ?

@edgarriba edgarriba added enhancement 🚀 Improvement over an existing functionality module: utils labels Apr 25, 2021
kornia/geometry/transform/affwarp.py Outdated Show resolved Hide resolved
test/geometry/transform/test_affine.py Show resolved Hide resolved


class ImageToTensor(nn.Module):
"""Converts a numpy image to a PyTorch 4d tensor image.
Copy link
Member

Choose a reason for hiding this comment

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

By numpy image, are we assuming it is uint8 or floating points within 0~1?

Copy link
Member Author

Choose a reason for hiding this comment

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

both - it just permutes, we don;t modify dtype

Copy link
Member

Choose a reason for hiding this comment

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

That's the problem. If I am a user, I would expect this function returns a value as a Kornia image, which is shaped as BCHW and ranged from 0-1.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm fine with it, but tensor_to_image doesn't do that. And this is just the module of that function

Copy link
Member Author

Choose a reason for hiding this comment

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

probably we could provide an extra operator TensorToDtype or similar.

Copy link
Member Author

Choose a reason for hiding this comment

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

@shijianjian another option is the following:

def image_to_tensor(image: np.ndarray, keepdim: bool = True, device: Optional[torch.device] = None, dtype: Optional[torch.dtype] = None) -> torch.Tensor:
    ...
    tensor = ...
    if dtype is None:
        dtype = tensor.dtype
    return tensor.to(device, dtype)

Copy link
Member

Choose a reason for hiding this comment

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

What I meant is the conversion from 8bit image, 12bit image, or 16bit image to 0-1 scale.

Copy link
Member Author

Choose a reason for hiding this comment

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

for that we have normalize. With the definition below you could do the job

nn.Sequential(
  ImageToTensor(dtype=torch.float32),
  Normalise(mean=0., std=255.),
)

I wouldn't change the default behavior of image_to_tensor since it might a large set of users

@edgarriba edgarriba added the 1 Priority 1 🚨 High priority label Apr 26, 2021
@edgarriba edgarriba changed the title Adds ImageToTensor module Adds ImageToTensor module and resize for non-batched images Apr 26, 2021
@edgarriba edgarriba merged commit 02de87f into master Apr 26, 2021
@edgarriba edgarriba deleted the feat/resize branch April 26, 2021 20:36
edgarriba added a commit that referenced this pull request May 30, 2021
* adapt resize for non batch

* implement ImageToTensor module

* handle length 2 and 3
edgarriba added a commit to edgarriba/kornia that referenced this pull request Jul 6, 2021
* adapt resize for non batch

* implement ImageToTensor module

* handle length 2 and 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1 Priority 1 🚨 High priority enhancement 🚀 Improvement over an existing functionality module: utils
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants