Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion torchvision/prototype/transforms/functional/_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,15 @@ def equalize(inpt: features.InputTypeJIT) -> features.InputTypeJIT:
return equalize_image_pil(inpt)


invert_image_tensor = _FT.invert
def invert_image_tensor(image: torch.Tensor):
_FT._assert_image_tensor(image)

if image.dtype == torch.uint8:
return image.bitwise_not()
else:
return _FT._max_value(image.dtype) - image
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I couldn't believe it, but this seems to be fastest way:

def scalar_sub(image):
    return _max_value(image.dtype) - image


def new_full(image):
    return image.new_full(image.shape, _max_value(image.dtype)).sub_(image)


def full_like(image):
    return torch.full_like(image, _max_value(image.dtype)).sub_(image)
[-------------------- invert float or signed integer tensors -------------------]
                                         |  scalar_sub  |  new_full  |  full_like
1 threads: ----------------------------------------------------------------------
      (3, 256, 256)    / int32   / cpu   |      20      |     28     |      26   
      (3, 256, 256)    / int32   / cuda  |       5      |      8     |       7   
      (3, 256, 256)    / int64   / cpu   |      88      |    109     |     107   
      (3, 256, 256)    / int64   / cuda  |      13      |     22     |      22   
      (3, 256, 256)    / float64 / cpu   |      35      |     49     |      49   
      (3, 256, 256)    / float64 / cuda  |      12      |     21     |      21   
      (5, 3, 256, 256) / int32   / cpu   |      76      |    112     |     110   
      (5, 3, 256, 256) / int32   / cuda  |      34      |     67     |      67   
      (5, 3, 256, 256) / int64   / cpu   |     439      |    510     |     524   
      (5, 3, 256, 256) / int64   / cuda  |      68      |    133     |     133   
      (5, 3, 256, 256) / float64 / cpu   |     140      |    215     |     220   
      (5, 3, 256, 256) / float64 / cuda  |      68      |    133     |     133   

Times are in microseconds (us).



invert_image_pil = _FP.invert


Expand Down