(pic)
Convert a ``PIL Image`` or ``numpy.ndarray`` to tensor.

    See :class:`~paddlevision.transforms.ToTensor` for more details.

    Args:
        pic (PIL Image or numpy.ndarray): Image to be converted to tensor.

    Returns:
        Tensor: Converted image.
Convert a ``PIL Image`` or ``numpy.ndarray`` to tensor.
Convert a PIL Image or numpy . ndarray to tensor .
def to_tensor(pic):
    """Convert a ``PIL Image`` or ``numpy.ndarray`` to tensor.

    See :class:`~paddlevision.transforms.ToTensor` for more details.

    Args:
        pic (PIL Image or numpy.ndarray): Image to be converted to tensor.

    Returns:
        Tensor: Converted image.
    """
    if not (F_pil._is_pil_image(pic) or _is_numpy(pic)):
        raise TypeError('pic should be PIL Image or ndarray. Got {}'.format(
            type(pic)))

    if _is_numpy(pic) and not _is_numpy_image(pic):
        raise ValueError('pic should be 2/3 dimensional. Got {} dimensions.'.
                         format(pic.ndim))

    default_float_dtype = paddle.get_default_dtype()

    if isinstance(pic, np.ndarray):
        # handle numpy array
        if pic.ndim == 2:
            pic = pic[:, :, None]
        img = paddle.to_tensor(pic.transpose((2, 0, 1)))
        # backward compatibility
        if not img.dtype == default_float_dtype:
            img = img.astype(dtype=default_float_dtype)
            return img.divide(paddle.full_like(img, 255))
        else:
            return img

    if accimage is not None and isinstance(pic, accimage.Image):
        nppic = np.zeros(
            [pic.channels, pic.height, pic.width], dtype=np.float32)
        pic.copyto(nppic)
        return paddle.to_tensor(nppic).astype(dtype=default_float_dtype)

    # handle PIL Image
    mode_to_nptype = {'I': np.int32, 'I;16': np.int16, 'F': np.float32}
    img = paddle.to_tensor(
        np.array(
            pic, mode_to_nptype.get(pic.mode, np.uint8), copy=True))

    if pic.mode == '1':
        img = 255 * img
    img = img.reshape([pic.size[1], pic.size[0], len(pic.getbands())])

    if not img.dtype == default_float_dtype:
        img = img.astype(dtype=default_float_dtype)
        # put it from HWC to CHW format
        img = img.transpose((2, 0, 1))
        return img.divide(paddle.full_like(img, 255))
    else:
        # put it from HWC to CHW format
        img = img.transpose((2, 0, 1))
        return img
def to_tensor ( pic ) : if not ( F_pil . _is_pil_image ( pic ) or _is_numpy ( pic ) ) : raise TypeError ( 'pic should be PIL Image or ndarray. Got {}' . format ( type ( pic ) ) ) if _is_numpy ( pic ) and not _is_numpy_image ( pic ) : raise ValueError ( 'pic should be 2/3 dimensional. Got {} dimensions.' . format ( pic . ndim ) ) default_float_dtype = paddle . get_default_dtype ( ) if isinstance ( pic , np . ndarray ) : # handle numpy array if pic . ndim == 2 : pic = pic [ : , : , None ] img = paddle . to_tensor ( pic . transpose ( ( 2 , 0 , 1 ) ) ) # backward compatibility if not img . dtype == default_float_dtype : img = img . astype ( dtype = default_float_dtype ) return img . divide ( paddle . full_like ( img , 255 ) ) else : return img if accimage is not None and isinstance ( pic , accimage . Image ) : nppic = np . zeros ( [ pic . channels , pic . height , pic . width ] , dtype = np . float32 ) pic . copyto ( nppic ) return paddle . to_tensor ( nppic ) . astype ( dtype = default_float_dtype ) # handle PIL Image mode_to_nptype = { 'I' : np . int32 , 'I;16' : np . int16 , 'F' : np . float32 } img = paddle . to_tensor ( np . array ( pic , mode_to_nptype . get ( pic . mode , np . uint8 ) , copy = True ) ) if pic . mode == '1' : img = 255 * img img = img . reshape ( [ pic . size [ 1 ] , pic . size [ 0 ] , len ( pic . getbands ( ) ) ] ) if not img . dtype == default_float_dtype : img = img . astype ( dtype = default_float_dtype ) # put it from HWC to CHW format img = img . transpose ( ( 2 , 0 , 1 ) ) return img . divide ( paddle . full_like ( img , 255 ) ) else : # put it from HWC to CHW format img = img . transpose ( ( 2 , 0 , 1 ) ) return img