But for 2D tensors, the transforms.ToPILImage() and transforms.ToTensor() are needed.

Yes, I see you point here. Yes, images as tensors should be 3D tensors. In case of 2D tensors, I’d recommend to update the way to construct the input as 3D tensor, instead of 2D tensor. And in this case, I would say that if you deal already with tensors and your custom transformation do not require specifically PIL.Image as input, you can safely remove ToPILImage and ToTensor.

Also, for 2D tensor returns float32 while 3D tensor returns float64 after respective transformation.

Probably, it depends on how do you get the input as tensor. If you wish to change dtype of the tensor, this can be done with ConvertImageDtype, https://pytorch.org/docs/stable/torchvision/transforms.html#transforms-on-torch-tensor-only 2
Normally, transformations should not alter dtype, for example
import torch
from torchvision import transforms


for dtype in [torch.float32, torch.float64]:

    M = torch.randint(low=0, high=2, size=(6, 64, 64), dtype=dtype)
    gt_trans = transforms.Compose([
        transforms.Resize((32, 32)),
    ])
    res = gt_trans(M)
    assert res.dtype == M.dtype, "{} vs {}".format(res.dtype, M.dtype)

If it is not the case for you use-case and it is related to torchvision, this should be a bug and please file an issue . Thanks!