Change the image size and range
I want to ask for data transforms if I have an image of size 28 * 28 and I want to resize it to be 32 *32, I know that this could be done with transforms.Resize() but I'm sure how.
Also for the normalization, if I want it to be within the range of [-1,1], I did it previously if I want it to be within [0,1] using transforms.Normalize((0.485,0.456,0.406),(0.229,0.224,0.225))

Don't rage, it's gonna be fine. Resizing MNIST to 32x32 height x width can be done like so:

import tempfile

import torchvision

dataset = torchvision.datasets.MNIST(
    root=tempfile.gettempdir(),
    download=True,
    train=True,
    # Simply put the size you want in Resize (can be tuple for height, width)
    transform=torchvision.transforms.Compose(
        [torchvision.transforms.Resize(32), torchvision.transforms.ToTensor()]
    ),
)

print(dataset[0][0].shape) # 1, 32, 32 (channels, width, height)


When it comes to normalization, you can see PyTorch's per-channel normalization source here. It depends whether you want it per-channel or in another form, but something along those lines should work (see wikipedia for formula of the normalization, here it's applied per-channel):

import dataclasses


@dataclasses.dataclass
class Normalize:
    maximum: typing.Tuple
    minimum: typing.Tuple
    low: int = -1
    high: int = 1

    def __call__(self, tensor):
        maximum = torch.as_tensor(self.maximum, dtype=dtype, device=tensor.device)
        minimum = torch.as_tensor(self.minimum, dtype=dtype, device=tensor.device)
        return self.low + (
            (tensor - minimum[:, None, None]) * (self.high - self.low)
        ) / (maximum[:, None, None] - minimum[:, None, None])


You would have to provide Tuple of minimum values and Tuple of maximum values (one value per channel for both) just like for standard PyTorch's torchvision normalization though. You could calculate those from data, for MNIST you could calculate them like this:

def per_channel_op(data, op=torch.max):
    per_sample, _ = op(data, axis=0)
    per_width, _ = op(per_sample, axis=1)
    per_height, _ = op(per_width, axis=1)
    return per_height

# Unsqueeze to add superficial channel for MNIST
# Divide cause they are uint8 type by default
data = dataset.data.unsqueeze(1).float() / 255

# Maximum over samples
maximum = per_channel_op(data) # value per channel, here
minimum = per_channel_op(data, op=torch.min) # only one value cause MNIST


And finally, to apply normalization on MNIST (watch out, as those will only have -1, 1 values as all pixels are black and white, will act differently on datasets like CIFAR etc.):

dataset = torchvision.datasets.MNIST(
    root=tempfile.gettempdir(),
    download=True,
    train=True,
    # Simply put the size you want in Resize (can be tuple for height, width)
    transform=torchvision.transforms.Compose(
        [
            torchvision.transforms.Resize(32),
            torchvision.transforms.ToTensor(),
            # Apply with Lambda your custom transformation
            torchvision.transforms.Lambda(Normalize((maximum,), (minimum,))),
        ]
    ),
)

