torchvision自带的transforms.to_tensor用法

import torch
import torchvision.transforms as tf
import numpy as np
raw_input = np.ones((224,224,3),dtype=np.uint8)
tt_tf = tf.ToTensor()
# tt_tf = torch.as_tensor
output_ = tt_tf(raw_input)

官方解释:

Convert a PIL Image or numpy.ndarray to tensor:
Converts a PIL Image or numpy.ndarray (H x W x C) in the range [0, 255] to a torch.FloatTensor of shape (C x H x W) in the range [0.0, 1.0] if the PIL Image belongs to one of the modes (L, LA, P, I, F, RGB, YCbCr, RGBA, CMYK, 1) or if the numpy.ndarray has dtype = np.uint8.

In the other cases, tensors are returned without scaling.

通俗解释:

  1. 转换为torch.FloatTensor
  2. 将通道提前
  3. 可以将特定类型的图片或者uint8的ndarray归一化(除255),其他不做归一化

tip:如果想只是转换为tensor,不做其他改变,可以使用 torch.as_array()

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值