Pytorch中如何加载数据、Tensorboard、Transforms的使用

一、Pytorch中如何加载数据
在Pytorch中涉及到如何读取数据,主要是两个类一个类是Dataset、Dataloader
Dataset 提供一种方式获取数据,及其对应的label。主要包含以下两个功能:
如何获取每一个数据以及label
告诉我们总共有多少的数据

Dataloader,可以对数据进行打包,为后面的网络提供不同的数据形式。

二、Tensorboard的使用,用来观察训练结果

from torch.utils.tensorboard import SummaryWriter

writer = SummaryWriter("log")

# writer.add_image()

for i in range(100):
    writer.add_scalar("y=x", i, i)

writer.close()

在Terminal中先切换到conda activate pytorch
使用命令 tensorboard --logdir=logs
在这里插入图片描述

TensorBoard的使用
1、使用add_image()方法

from torch.utils.tensorboard import SummaryWriter
import numpy as np
from PIL import Image
# 利用openCV中的numpy库可以获得numpy型的图片

writer = SummaryWriter("log")
img_path = "../dataset/bees/26589803_5ba7000313.jpg"
img_PIL = Image.open(img_path)    # 打开图片
img_array = np.array(img_PIL)     # 图片转换
print(type(img_array))   # 打印图片类型
print(img_array.shape)  # 打印图片格式

writer.add_images("test", img_array, 2, dataformats='HWC')  # 根据img_array.shape来指定,如果不指定dataformats就会报错
# y = 2x
for i in range(100):
    writer.add_scalar("y=2x", 2*i, i)

writer.close()

在这里插入图片描述
三、Transforms的使用
transform表示对图片进行一些变换
python的用法 -> tensor数据类型
通过transform.ToTensor去解决两个问题:
transforms该如何使用(Python)
为什么我们需要Tensor的数据类型?
在这里插入图片描述

from torchvision import transforms
from PIL import Image

img_path = "../dataset/bees/10870992_eebeeb3a12.jpg"
img = Image.open(img_path)
tensor_trans = transforms.ToTensor()
tensor_img = tensor_trans(img)
print(tensor_img)

运行后的结果

D:\tools\anaconda\envs\pytorch\python.exe D:/code/captcha_ocr-main/learn/transforms.py
tensor([[[0.5725, 0.5725, 0.5725,  ..., 0.5686, 0.5725, 0.5765],
         [0.5725, 0.5725, 0.5725,  ..., 0.5686, 0.5725, 0.5765],
         [0.5686, 0.5686, 0.5725,  ..., 0.5686, 0.5725, 0.5765],
         ...,
         [0.5490, 0.5647, 0.5725,  ..., 0.6314, 0.6235, 0.6118],
         [0.5608, 0.5765, 0.5843,  ..., 0.5961, 0.5843, 0.5765],
         [0.5725, 0.5843, 0.5922,  ..., 0.5647, 0.5529, 0.5490]],

        [[0.4471, 0.4471, 0.4471,  ..., 0.4235, 0.4275, 0.4314],
         [0.4471, 0.4471, 0.4471,  ..., 0.4235, 0.4275, 0.4314],
         [0.4431, 0.4431, 0.4471,  ..., 0.4235, 0.4275, 0.4314],
         ...,
         [0.4000, 0.4157, 0.4235,  ..., 0.4706, 0.4627, 0.4510],
         [0.4118, 0.4275, 0.4353,  ..., 0.4431, 0.4314, 0.4235],
         [0.4235, 0.4353, 0.4431,  ..., 0.4118, 0.4000, 0.3961]],

        [[0.2471, 0.2471, 0.2471,  ..., 0.2588, 0.2627, 0.2667],
         [0.2471, 0.2471, 0.2471,  ..., 0.2588, 0.2627, 0.2667],
         [0.2431, 0.2431, 0.2471,  ..., 0.2588, 0.2627, 0.2667],
         ...,
         [0.2157, 0.2314, 0.2392,  ..., 0.2510, 0.2431, 0.2314],
         [0.2275, 0.2431, 0.2510,  ..., 0.2196, 0.2078, 0.2000],
         [0.2392, 0.2510, 0.2588,  ..., 0.1961, 0.1843, 0.1804]]])

Process finished with exit code 0

加载tensor类型的图片:

from torchvision import transforms
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
img_path = "../dataset/bees/10870992_eebeeb3a12.jpg"
img = Image.open(img_path)
writer = SummaryWriter("log")
tensor_trans = transforms.ToTensor()
tensor_img = tensor_trans(img)
writer.add_image("Tensor_img", tensor_img)
writer.close()

四、常见的transforms类的使用

  1. ToTensor类
    将PIL图片转换成tensor图片。
from torchvision import transforms
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
# Tensor的使用
writer = SummaryWriter("log")
img_path = "../dataset/bees/10870992_eebeeb3a12.jpg"
img = Image.open(img_path)
print(img)
tensor_trans = transforms.ToTensor()    # 创建ToTensor()对象
tensor_img = tensor_trans(img)    # 传入图片参数,将PIL图片转换成tensor图片
writer.add_image("Tensor_img", tensor_img)
writer.close()

在这里插入图片描述
2. Normalize类

对tensor类型的图片进行归一化处理。
Normalize的使用:归一化处理
公式:output[channel] = (input[channel] - mean[channel]) / std[channel]

from torchvision import transforms
from PIL import Image
from torch.utils.tensorboard import SummaryWriter

# Tensor的使用
writer = SummaryWriter("log")
img_path = "../dataset/bees/10870992_eebeeb3a12.jpg"
img = Image.open(img_path)
print(img)


tensor_trans = transforms.ToTensor()
tensor_img = tensor_trans(img)

writer.add_image("Tensor_img", tensor_img)

# Normalize的使用
print(tensor_img[0][0][0])    # 归一化处理之前的数据
trans_norm = transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
img_norm = trans_norm(tensor_img)
print(img_norm[0][0][0])     # 归一化处理后的结果
writer.add_image("Normalize", img_norm)

writer.close()

在这里插入图片描述
在这里插入图片描述
3. Resize类:
重置图片大小。

from torchvision import transforms
from PIL import Image
from torch.utils.tensorboard import SummaryWriter

# Tensor的使用
writer = SummaryWriter("log")
img_path = "../dataset/bees/10870992_eebeeb3a12.jpg"
img = Image.open(img_path)
print(img)


tensor_trans = transforms.ToTensor()
tensor_img = tensor_trans(img)

writer.add_image("Tensor_img", tensor_img)

print(tensor_img[0][0][0])    # 归一化处理之前的数据
trans_norm = transforms.Normalize([1, 3, 5], [3, 2, 1])
img_norm = trans_norm(tensor_img)
print(img_norm[0][0][0])     # 归一化处理后的结果
writer.add_image("Normalize", img_norm)

writer.close()

# Resize的使用:重置图片大小
print(img.size)    # (500, 464) 
trans_resize = transforms.Resize((512, 512))

img_resize = trans_resize(img)
print(img_resize)   # <PIL.Image.Image image mode=RGB size=512x512 at 0x2A17E774248> 
 
img_resize = tensor_trans(img_resize)
writer.add_image("Resize", img_resize, 0)
print("" + img_resize)
writer.close()

在这里插入图片描述
在这里插入图片描述
4. Compose的使用
等比例缩放。
Compose的使用:整体缩放,不改变高宽比例
Compose()中的参数需要的是一个列表,列表中的数据需要的是transforms类型。
即 Compose([transforms参数1, transforms参数2, …])

from torchvision import transforms
from PIL import Image
from torch.utils.tensorboard import SummaryWriter

# Tensor的使用
writer = SummaryWriter("log")
img_path = "../dataset/bees/10870992_eebeeb3a12.jpg"
img = Image.open(img_path)
print(img)


tensor_trans = transforms.ToTensor()
tensor_img = tensor_trans(img)

writer.add_image("Tensor_img", tensor_img)

print(tensor_img[0][0][0])    # 归一化处理之前的数据
trans_norm = transforms.Normalize([1, 3, 5], [3, 2, 1])
img_norm = trans_norm(tensor_img)
# print(img_norm[0][0][0])     # 归一化处理后的结果
writer.add_image("Normalize", img_norm)


# Resize的使用:重置图片大小
print(img.size)    # (500, 464)
trans_resize = transforms.Resize((512, 512))

img_resize = trans_resize(img)
print(img_resize)   # <PIL.Image.Image image mode=RGB size=512x512 at 0x2A17E774248>

# Compose的使用
img_resize = tensor_trans(img_resize)
writer.add_image("Resize", img_resize, 0)
# print(img_resize)
trans_resize_2 = transforms.Resize(1024)
trans_compose = transforms.Compose([trans_resize_2, tensor_trans])
img_resize_2 = trans_compose(img)
writer.add_image("Resize", img_resize_2, 1)

writer.close()

5.RandomCrop类的使用

随机裁剪。

from torchvision import transforms
from PIL import Image
from torch.utils.tensorboard import SummaryWriter

# Tensor的使用
writer = SummaryWriter("log")
img_path = "../dataset/bees/10870992_eebeeb3a12.jpg"
img = Image.open(img_path)
print(img)


tensor_trans = transforms.ToTensor()
tensor_img = tensor_trans(img)

writer.add_image("Tensor_img", tensor_img)

print(tensor_img[0][0][0])    # 归一化处理之前的数据
trans_norm = transforms.Normalize([1, 3, 5], [3, 2, 1])
img_norm = trans_norm(tensor_img)
# print(img_norm[0][0][0])     # 归一化处理后的结果
writer.add_image("Normalize", img_norm)


# Resize的使用:重置图片大小
print(img.size)    # (500, 464)
trans_resize = transforms.Resize((512, 512))

img_resize = trans_resize(img)
print(img_resize)   # <PIL.Image.Image image mode=RGB size=512x512 at 0x2A17E774248>


#  Compose的使用:
img_resize = tensor_trans(img_resize)
writer.add_image("Resize", img_resize, 0)
# print(img_resize)
trans_resize_2 = transforms.Resize(1024)
trans_compose = transforms.Compose([trans_resize_2, tensor_trans])
img_resize_2 = trans_compose(img)
writer.add_image("Resize", img_resize_2, 1)


# RendomCrop类的使用:随机裁剪
# trans_random = transforms.RandomCrop(512)
trans_random = transforms.RandomCrop(1000, 500)
trans_compose_2 = transforms.Compose([trans_random, tensor_trans])
for i in range(10):
    img_crop = trans_compose_2(img)
    # writer.add_image("RancomCrop", img_crop, i)
    writer.add_image("RancomCropHW", img_crop, i)

writer.close()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

NeilNiu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值