PyTorch在做一般的深度学习图像处理任务时,先使用dataset类和dataloader类读入图片,在读入的时候需要做transform变换,其中transform一般都需要ToTensor()操作,将dataset类中__getitem__()方法内读入的PIL或CV的图像数据转换为torch.FloatTensor。详细过程如下:


class ToTensor(object):"""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.uint8In the other cases, tensors are returned without scaling."""def __call__(self, pic):"""Args:pic (PIL Image or numpy.ndarray): Image to be converted to tensor.Returns:Tensor: Converted image."""return F.to_tensor(pic)def __repr__(self):return self.__class__.__name__ + '()'class ToPILImage(object):"""Convert a tensor or an ndarray to PIL Image.Converts a torch.*Tensor of shape C x H x W or a numpy ndarray of shapeH x W x C to a PIL Image while preserving the value range.Args:mode (`PIL.Image mode`_): color space and pixel depth of input data (optional).If ``mode`` is ``None`` (default) there are some assumptions made about the input data:- If the input has 4 channels, the ``mode`` is assumed to be ``RGBA``.- If the input has 3 channels, the ``mode`` is assumed to be ``RGB``.- If the input has 2 channels, the ``mode`` is assumed to be ``LA``.- If the input has 1 channel, the ``mode`` is determined by the data type (i.e ``int``, ``float``,``short``)... _PIL.Image mode: https://pillow.readthedocs.io/en/latest/handbook/concepts.html#concept-modes"""def __init__(self, mode=None):self.mode = modedef __call__(self, pic):"""Args:pic (Tensor or numpy.ndarray): Image to be converted to PIL Image.Returns:PIL Image: Image converted to PIL Image."""return F.to_pil_image(pic, self.mode)def __repr__(self):format_string = self.__class__.__name__ + '('if self.mode is not None:format_string += 'mode={0}'.format(self.mode)format_string += ')'return format_string

可以从to_tensor()函数看到,函数接受PIL Image或numpy.ndarray,将其先由HWC转置为CHW格式,再转为float后每个像素除以255.

transforms.ToTensor()
(1) transforms.ToTensor() 将numpy的ndarray或PIL.Image读的图片转换成形状为(C,H, W)的Tensor格式,且/255归一化到[0, 1.0]之间
(2)通道的具体顺序与cv2读的还是PIL.Image读的图片有关系
cv2:(B,G,R)
PIL.Image:(R, G, B)

代码例子:

import torch
import cv2
from PIL import Image
from torchvision import transforms
image = cv2.imread('myimage.jpg')  # numpy数组格式(H,W,C=3),通道顺序(B,G,R)
image2 = Image.open('myimage.jpg')  # PIL的JpegImageFile格式(size=(W,H))
print(image.shape)  # (H,W,3)
print(image2.size)  # (W,H)
tran = transforms.ToTensor()  # 将numpy数组或PIL.Image读的图片转换成(C,H, W)的Tensor格式且/255归一化到[0,1.0]之间
img_tensor = tran(image)
img2_tensor = tran(image2)
print(img_tensor.size())  # (C,H, W), 通道顺序(B,G,R)
print(img2_tensor.size())  # (C,H, W), 通道顺序(R,G,B)

orchvision.transforms.ToTensor

对于一个图片img,调用ToTensor转化成张量的形式,发生的不是将图片的RGB三维信道矩阵变成tensor图片在内存中以bytes的形式存储,转化过程的步骤是:

  1. img.tobytes()  将图片转化成内存中的存储格式
  2. torch.BytesStorage.frombuffer(img.tobytes() )  将字节以流的形式输入,转化成一维的张量
  3. 对张量进行reshape
  4. 对张量进行permute(2,0,1)
  5. 将当前张量的每个元素除以255
  6. 输出张量

torchvision.transforms.ToPILImage

对于一个Tensor的转化过程是:

  1. 将张量的每个元素乘上255
  2. 将张量的数据类型有FloatTensor转化成Uint8
  3. 将张量转化成numpy的ndarray类型
  4. 对ndarray对象做permute (1, 2, 0)的操作
  5. 利用Image下的fromarray函数,将ndarray对象转化成PILImage形式
  6. 输出PILImage

Pytorch:ToTensor(object)类相关推荐

  1. 《pytorch车型细分类网络》的源码

    说明:<pytorch车型细分类网络>.这篇文章代码有错误.我稍微调整了一下,可以正常跑了. 标题:pytorch动手实践:pytorch车型细分类网络 1)讲解,代码,主要参考知乎文章& ...

  2. Pytorch实现102类鲜花分类(102 Category Flower Dataset)

    Pytorch实现102类鲜花分类(VGG19和ResNet152模型) 本文主要讲解该算法的实现过程,原理部分需读者自行研究,可以找一些论文之类的. 实验环境 python3.6+pytorch1. ...

  3. JAVA-初步认识-第十一章-object类-equals方法覆盖

    一. 现在要谈论equals方法另一个方面.如果不写equals方法,直接用==来比较也是可以的,貌似equals方法有点多余. 现在不比较对象是否相等,而是比较对象中的特定内容,比如说对象的年龄,之 ...

  4. 【宋红康学习日记11】Object类与equals方法

    1 == (1)当对象是基本数据类型时,比较值: (2)当对象是引用型时,比较的是地址值!!1 2 equals():只处理引用型数据:Object类中的equals方法依然比较的是地址值! 但在St ...

  5. java异常类 Object类

    1.Object类 Object类是所有类的父类,是最顶层的父类. 当一个类没有直接继承其它父类时,这个类的直接父类就是Object类. class Person { ... } 等价于 class ...

  6. Java核心技术第五章——2.Object类

    Object类:所有类的超类 Object类是Java中所有类的始祖,在Java中每个类都是由它扩展而来的.但是并不需要这样写: public class Emloyee extends Object ...

  7. c++ 实现一个object类_说说Object类下面有几种方法呢?

    欢迎关注头条号:Java小野猫 今天说一道基础题型,不过很多人会忽略或者至少说不完整,但是面试时被问到的几率还是很大的. 面试题 Object有几种方法呢? Java语言是一种单继承结构语言,Java ...

  8. python中的object是什么意思_Python object类中的特殊方法代码讲解

    python版本:3.8class object: """ The most base type """ # del obj.xxx或del ...

  9. Thread和Object类中关于线程的相关方法

    Thread和Object类中线程的7个重要方法概览 类 方法名称 简介 Thread sleep相关 相关表示sleep所有的重载方法,参数不同,实际作用大同小异 Thread join() 等待其 ...

最新文章

  1. php网站怎么伪静态,php怎么实现网页伪静态
  2. PyCharm缺少cv2模块怎么办?怎样在PyCharm中安装自己需要的package?
  3. python里pickle模块
  4. Nginx多域名多Server反向代理配置
  5. Android各层推荐开发书籍及参考资料
  6. java防止库存超买_java初探(1)之防止库存为负以及防超买
  7. ubus c语言例子,openwrt之ubus例子
  8. thymeleaf(th:each th:selected) 从后台动态获取下拉框数据回显及选中
  9. hadoop2.7.3用mapreduce计算pi值
  10. 大数据开发笔记(八):Spark综合笔记总结
  11. 几个极速版自动阅读项目的autojs脚本
  12. 优思学院|品质圈QCC是什么?如何有效实施?
  13. 前端研习录(02)——CSS内联样式、内部样式及外部样式
  14. 测试基本理论-看这篇就够了
  15. 华为性格测评注意事项
  16. Opencv实现颜色检测
  17. maps-api-v3_利用Google Maps API发挥创意
  18. C# / VB 获取PDF文档的数字签名信息
  19. 2018-2019赛季多校联合新生训练赛第四场
  20. kurento项目介绍

热门文章

  1. flutter: 建树流程
  2. wpf,后台触发按钮点击以及拖动
  3. SPS中计算值公式函数简介
  4. 【MySQL】MySQL中的查询语句的详解----等值连接、左连接、右连接、全连接
  5. 软件测试—软件测试基础知识—用例模板
  6. mysql2 0.3.16.gem_安装mysql2时出错:无法构建gem原生扩展
  7. selenium 隐藏窗口_anaconda下安装selenium包
  8. 提交信息html模板,提交留言HTML模板代码
  9. macos php开发环境,macOS 10.13 High Sierra PHP开发环境配置
  10. import java.util_importjava.util.*;classKeyMaster{publi..._考试资料网