to_tensor

paddle.vision.transforms. to_tensor ( pic, data_format='CHW' ) [source]

Converts a PIL.Image or numpy.ndarray to paddle.Tensor.

See ToTensor for more details.

Parameters
  • pic (PIL.Image|np.ndarray) – Image to be converted to tensor.

  • data_format (str, optional) – Data format of output tensor, should be ‘HWC’ or ‘CHW’. Default: ‘CHW’.

Returns

Converted image. Data type is same as input img.

Return type

Tensor

Examples

import numpy as np
from PIL import Image
from paddle.vision.transforms import functional as F

fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8')

fake_img = Image.fromarray(fake_img)

tensor = F.to_tensor(fake_img)
print(tensor.shape)