一、transform的结构和用法
from torchvision import transforms
然后contrl+鼠标左键
可以看到有很多的类,我们用的时候输入在 object 位置可以继承这个类
点击这里可以看结构
c就是class的意思
一、1 先看compose类
class Compose:
"""Composes several transforms together. This transform does not support torchscript.
Please, see the note below. //这里是说compose包含几个transorms//
Args:
transforms (list of ``Transform`` objects): list of transforms to compose.
Example:
>>> transforms.Compose([
>>> transforms.CenterCrop(10), //这里是做一个中心裁剪//
>>> transforms.PILToTensor(),
>>> transforms.ConvertImageDtype(torch.float),
>>> ])
一、2 Totensor类
lass ToTensor:
"""Convert a PIL Image or ndarray to tensor and scale the values accordingly. //PIL或者numoy列表转换成tensor的形式//
This transform does not support torchscript.
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
下面:
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)
这个方法输出的就是to tensort类型照片
通过transforms.ToTensor去解决两个问题
a.transform该如何使用(python)
b.为什么需要Tensor数据类型
第一个问题:
先导入一张图片我们看
from torchvision import transforms
from PIL import Image
img_path="D:\\PyCharm\\Py_Projects\\XiaoTuDui\\datasets_jinjieP7\\train\\ants_image\\0013035.jpg"
img=Image.open(img_path)
print(img)
输出结果为PIL 格式
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=768x512 at 0x1B0079666A0>
from torchvision import transforms
from PIL import Image
img_path="D:\\PyCharm\\Py_Projects\\XiaoTuDui\\datasets_jinjieP7\\train\\ants_image\\0013035.jpg"
img=Image.open(img_path)
print(img)
tensor_trans=transforms.ToTensor //在下方输入这个//
tensor_img=tensor_trans()
方法一、我们鼠标左键+contrl可以看到call需要pic
方法二、鼠标左键放到括号内。按住ctrl+p可以看需要的参数
小插曲!~我们继续
from torchvision import transforms
from PIL import Image
img_path="D:\\PyCharm\\Py_Projects\\XiaoTuDui\\datasets_jinjieP7\\train\\ants_image\\0013035.jpg"
img=Image.open(img_path)
print(img)
tensor_trans=transforms.ToTensor()
tensor_img=tensor_trans(img)
print(tensor_img)
输出结果为:
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=768x512 at 0x1A971B5C880>
tensor([[[0.3137, 0.3137, 0.3137, ..., 0.3176, 0.3098, 0.2980],
[0.3176, 0.3176, 0.3176, ..., 0.3176, 0.3098, 0.2980],
[0.3216, 0.3216, 0.3216, ..., 0.3137, 0.3098, 0.3020],
另外注意这里不用调入call,直接可以调用
call()是一种magic method,在类中实现这一方法可以使该类的实例(对象)像函数一样被调用。
含有__call__方法的类,调用类的名称生成实例时会自动运行该方法
tensor_img = tensor_trans(img)调用了call方法,相当于tennsor_img = tensor_trans.call(img)
python中带__方法名___的方法都是默认的运行函数时自动运行的
前面tensor_trans=transforms.ToTensor()把totensor实例化了
第二个问题
为什么需要tensor的书籍类型
我们把上述代码复制到控制台
可以看到tensor_img处有一些东西:
grad 梯度
grad fn梯度方法
device设备
以上讲解了PIL转tensor
现在我们看 numpy
复习SummarayWriter
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
from PIL import Image
img_path="D:\\PyCharm\\Py_Projects\\XiaoTuDui\\datasets_jinjieP7\\train\\ants_image\\0013035.jpg"
img=Image.open(img_path)
print(img)
SummaryWriter()
tensor_trans=transforms.ToTensor()
tensor_img=tensor_trans(img)
print(tensor_img)
ctrl+p可以看到需要写一个文件名
继续写writer.add_img()
参数有标题、图片类型。。。。。
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
from PIL import Image
img_path="D:\\PyCharm\\Py_Projects\\XiaoTuDui\\datasets_jinjieP7\\train\\ants_image\\0013035.jpg"
img=Image.open(img_path)
print(img)
writer=SummaryWriter("logs")
tensor_trans=transforms.ToTensor()
tensor_img=tensor_trans(img)
print(tensor_img)
writer.add_image("Tensor_img",tensor_img)
writer.close()
终端输入** D:\PyCharm\Py_Projects\XiaoTuDui>cd XiaoTuDui
**(DL) D:\PyCharm\Py_Projects\XiaoTuDui>tensorboard --logdir=logs