torchvision.transforms.ToTensor详解 | 使用transforms.ToTensor()出现用户警告 | 图像的H W C 代表什么

看看torchvision.transforms.ToTensor做了什么:

简单来说就是把PIL.Image或ndarray从 (H x W x C)形状转换为 (C x H x W) 的tensor。

如果

  • 转换前numpy.ndarray的dtype = np.uint8
  • 转换前的PIL.Image是L, LA, P, I, F, RGB, YCbCr, RGBA, CMYK, 1 格式

还会将数值从 [0, 255] 归一化到[0,1]

不符合上边条件的话就不会归一化。

  • [h, w, c]:数组中最外层即hight,表示图像像素有几行;第二层元素width,表示图像像素几列,最后一层元素为每一个通道的数值。
  • [c, h, w]:数组中第一层元素为图像有一个通道,第二层元素为某个通道上的一行像素,第三层为该通道上某列的像素值。
    举个栗子:
    •  import numpy as np
       from torchvision import transforms
       
       data = np.random.randint(0, 255, size=6)
       img = data.reshape(2,1,3)
       print(img)
       img_tensor = transforms.ToTensor()(img) # 转换成tensor
       print(img_tensor)
      

      在这里插入图片描述
      在这里插入图片描述

    • ndarray最外边一层是每行像素,一共两行;中间一层是每列像素值,一共两列;最里层是三通道即RGB值。

    • tensor最外层是有几个通道,三通道则表示RGB;第二层中为每列有几个像素值;第三层为每行几个像素值。

UserWarning

弄清楚原理, 那来看看我今天遇到的一个用户警告。
在这里插入图片描述

D:\Program Files\python\python3.8\lib\site-packages\torchvision\datasets\mnist.py:498: UserWarning: The given NumPy array is not writeable, and PyTorch does not support non-writeable tensors. This means you can write to the underlying (supposedly non-writeable) NumPy array using the tensor. You may want to copy the array to protect its data or make it writeable before converting it to a tensor. This type of warning will be suppressed for the rest of this program. (Triggered internally at …\torch\csrc\utils\tensor_numpy.cpp:180.)
return torch.from_numpy(parsed.astype(m[2], copy=False)).view(*s)

大致意思是
D:\\Program Files\\python\\python3.8\\lib\\site packages\\torchvision\\dataset\\mnist.py:498:UserWarning:说:
给定的NumPy数组不可写,并且PyTorch不支持不可写的张量。这意味着你可以使用张量写入底层(假定不可写)NumPy数组。在将数组转换为张量之前,可能需要复制数组以保护其数据或使其可写。
在本程序的其余部分,此类警告将被抑制。(在…\torch\csc\utils\tensor\u numpy.cpp:180处内部触发。)
返回return torch.from_numpy(parsed.astype(m[2], copy=False)).view(*s)

看了看别人的说法一般都是说这玩意儿可以直接处理numpy的数组,你不需要转换成tensor。但是怎么不让他显示这个警告呢。

找到上边那个地址下的mnist文件。找到498行,把return torch.from_numpy(parsed.astype(m[2], copy=False)).view(*s)copy的false改成true即可。
在这里插入图片描述

  • 15
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ann's Blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值