一、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类的使用
-
ToTensor类
将PIL图片转换成tensor图片。from torchvision import transforms
from PIL import Image
from torch.utils.tensorboard import SummaryWriterTensor的使用
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()
- 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()
- 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()
- 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()