PIL imagenp.array(PIL.Image)torch.tensor로 바꾸거나 vise versa 할 떄는 scaling을 조심해야함

가령 uint8 -> float32 would map the [0, 255] range into [0, 1] (and vice-versa).

PIL → Tensor

import torchvision.transforms as transforms
from PIL import Image

# load image in RGB mode (png files contains additional alpha channel)
img = Image.open('golf.png').convert('RGB')

# set up transformation to resize the image
resize = transforms.Resize([224, 224])
img = resize(img)
**to_tensor = transforms.ToTensor()**

# apply transformation and convert to Pytorch tensor
tensor = to_tensor(img)
# torch.Size([3, 224, 224])

# add another dimension at the front to get NCHW shape
tensor = tensor.unsqueeze(0)
# torch.Size([1, 3, 224, 224])

Conversions

Transforms discription scaling
**ToPILImage([mode])** tensor or ndarray → PIL Image x
v2.ToPILImage alias of ToImagePIL
**v2.ToImagePIL([mode])** [BETA] tensor or ndarray → PIL Image x
**ToTensor()** PIL Image or ndarray → tensor o
**v2.ToTensor()** [BETA] Convert a PIL Image or ndarray to tensor o
**PILToTensor()** PIL Image → tensor of the same type x
**v2.PILToTensor()** [BETA] Convert a PIL Image to a tensor of the same type x
**v2.ToImageTensor()** [BETA] Convert a tensor, ndarray, or PIL Image to Image ; x
**ConvertImageDtype(dtype)** Convert a tensor image to the given dtype o
**v2.ConvertDtype([dtype])** [BETA] Convert input image or video to the given dtype o
v2.ConvertImageDtype alias of ConvertDtype
**v2.ToDtype(dtype)** [BETA] Converts the input to a specific dtype - x
**v2.ConvertBoundingBoxFormat(format)** [BETA] Convert bounding box coordinates to the given format, eg from "CXCYWH" to "XYXY".