Search code examples
pytorch

How to judge a torch.tensor dtype is int or not?


I want to check a Tensor convert from an image is normalized or not, i.e. the dtype is int or float. Is there convenient way to achieve this goal? I do not want an enumerated condition check like a.dtype == torch.int or a.dtype == torch.int32 or a.dtype ==torch.uint8 .... . Or is there another way to check an image tensor normalized or not?


Solution

  • As pointed out by jasonharper, you can use torch.is_floating_point to check your tensor:

    if torch.is_floating_point(my_tensor):
      # the tensor is "normalized"...
    else:
      # the tensor is an integer and needs to be normalized...