创建项目的方式
DataSet类代码实战
read_data.py
from torch.utils.data import Dataset
from PIL import Image
import os
class MyData(Dataset):
def __init__(self, root_dir, label_dir):
self.root_dir = root_dir
self.label_dir = label_dir
# 将两个路径进行拼接
self.path = os.path.join(self.root_dir, self.label_dir)
# 将文件夹下的内容变成列表
self.img_path = os.listdir(self.path)
# 在python中__getitem__(self, key):方法被称为魔法方法,这个方法返回所给键对应的值。
# 1 当对象是序列时,键是整数。当对象是映射时(字典),键是任意值
# 2 在定义类时,如果希望能按照键取类的值,则需要定义__getitem__方法
# 3 如果给类定义了__getitem__方法,则当按照键取值时,可以直接返回__getitem__方法执行的结果
def __getitem__(self, idx):
img_name = self.img_path[idx]
# 相对于这个程序的相对路径
img_item_path = os.path.join(self.root_dir, self.label_dir, img_name)
img = Image.open(img_item_path)
label = self.label_dir
return img, label
def __len__(self):
# 返回列表长度
return len(self.img_path)
root_dir = "dataset/train"
ants_label_dir = "ants"
bees_label_dir = "bees"
ants_dataset = MyData(root_dir, ants_label_dir)
bees_dataset = MyData(root_dir, bees_label_dir)
train_dataset = ants_dataset + bees_dataset
rename_dataset.py
import os
root_dir = "dataset/train"
target_dir = "ants_image"
img_path = os.listdir("D:\\workspace\\learn_pytorch\\" + os.path.join(root_dir, target_dir))
print(img_path)
label = target_dir.split('_')[0]
print(label)
out_dir = "ants_label"
for i in img_path:
file_name = i.split('.jpg')[0]
# with open("D:\\workspace\\learn_pytorch\\" + os.path.join(root_dir, out_dir, "{}.txt").format(file_name), 'w') as f:
with open("D:\\workspace\\learn_pytorch\\" + os.path.join(root_dir, out_dir+f'{file_name}.txt'), 'w') as f:
f.write(label)
TensorBoard的使用
tensorboard的用途
tensorboard可以用来查看loss是否能够按照我们预想的变化,或者查看训练到某一步输出的图像时什么样子的。
tensorboard写日志
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter("logs") # 创建一个logs文件夹,writer写的文件都在该文件夹下
#writer.add_image()
for i in range(100):
writer.add_scalar("y=2x",2*i,i)
writer.close()
tensorboard读日志
① 在 Anaconda 终端里面,激活py环境(conda activate pytorch),再输入 tensorboard --logdir=D:\workspace\pytorch\transform\logs 命令,将网址赋值浏览器的网址栏,回车,即可查看tensorboard显示日志情况。
② 为避免多人使用端口导致冲突,也可以在后面加上后缀,使得端口独立,tensorboard --logdir=D:\workspace\pytorch\transform\logs
③ 输入网址可得Tensorboard界面。
tensorboard读取图片
from torch.utils.tensorboard import SummaryWriter
from PIL import Image
import numpy as np
img_path1 = "Data/FirstTypeData/train/ants/0013035.jpg"
img_PIL1 = Image.open(img_path1)
img_array1 = np.array(img_PIL1)
img_path2 = "Data/SecondTypeData/train/bees_image/17209602_fe5a5a746f.jpg"
img_PIL2 = Image.open(img_path2)
img_array2 = np.array(img_PIL2)
writer = SummaryWriter("logs")
writer.add_image("test",img_array1,1,dataformats="HWC") # 1 表示该图片在第1步
writer.add_image("test",img_array2,2,dataformats="HWC") # 2 表示该图片在第2步
writer.close()
在terminal中输入tensorboard --logdir=D:\workspace\pytorch\transboard\logs --port=6007后运行效果如下:
torchvision中的transforms
transforms结构及用法
from PIL import Image
from torchvision import transforms
# from torch.utils.tensorboard import SummaryWriter
from tensorboardX import SummaryWriter
# python的用法->tensor数据类型
img_path = "D:/workspace/pytorch-learn/dataset/train/bees_image/16838648_415acd9e3f.jpg"
img = Image.open(img_path)
writer = SummaryWriter("logs")
# 1、transforms该如何使用(python)
# 2、为什么我们需要tensor数据类型
tensor_trans = transforms.ToTensor() # 创建transforms.ToTensor类的实例化对象
tensor_img = tensor_trans(img) # 调用transforms.ToTensor类的__call__的魔术方法
print(tensor_img)
writer.add_image("Tensor_img", tensor_img)
writer.close()
transform的图示
扩展:python中_call_()的使用
class Person:
def __call__(self, name):
print("__call_" + "hello" + name)
def hello(self, name):
print("hello" + name)
person = Person()
person("张三") # 内置call不用.,可直接通过对象进行调用
person.hello("李四") # 通过对象.方法进行调用
运行效果如下:
Tensor的使用
from PIL import Image
from tensorboardX import SummaryWriter
from torchvision import transforms
writer = SummaryWriter("logs")
# 读取图片
img = Image.open("../images/pytorch.jpg")
print(img)
# ToTensor的使用
# """Convert a ``PIL Image`` or ``numpy.ndarray`` to tensor.
trans_totensor = transforms.ToTensor()
img_tensor = trans_totensor(img)
writer.add_image("ToTensor", img_tensor)
writer.close()
测试环境如下,在terminal下输入tensorboard --logdir=D:\workspace\pytorch\transform\logs,即可在TensorBoard中显示图片的形状,运行效果如下
Normalize的使用(归一化)
# Normalize的使用(归一化)
# """Normalize a tensor image with mean and standard deviation.
print(img_tensor[0][0][0])
trans_norm = transforms.Normalize([5, 2, 6], [6, 3, 4])
img_norm = trans_norm(img_tensor)
print(img_norm[0][0][0])
writer.add_image("Normalize", img_norm, 2)
writer.close()
Resize()的使用
# Resize
print(img.size)
trans_resize = transforms.Resize((512, 512))
# img PTL -> resize -> img_resize PIL
img_resize = trans_resize(img)
# img_resize PIL -> totensor ->img_resize tensor
img_resize = trans_totensor(img_resize)
writer.add_image("Resize", img_resize, 0)
print(img_resize) # <PIL.Image.Image image mode=RGB size=512x512 at 0x226457A8708>
测试环境如下,在terminal下输入tensorboard --logdir=D:\workspace\pytorch\transform\logs,即可在TensorBoard中显示图片的形状,运行效果如下
Compose()的使用
# compose - resize - 2 等比缩放
trans_resize_2 = transforms.Resize(512)
# PIL->PIL->tensor
trans_compose = transforms.Compose([trans_resize_2, trans_totensor])
img_resize_2 = trans_compose(img)
writer.add_image("Resize", img_resize_2, 1)
测试环境如下,在terminal下输入tensorboard --logdir=D:\workspace\pytorch\transform\logs,即可在TensorBoard中显示图片的形状,运行效果如下
# RandomCrop
trans_random = transforms.RandomCrop(120)
print(type(trans_random))
trans_compose_2 = transforms.Compose([trans_random, trans_totensor])
for i in range(10):
img_crop = trans_compose_2(img)
writer.add_image("RandomCrop", img_crop, i)
测试环境如下,在terminal下输入tensorboard --logdir=D:\workspace\pytorch\transform\logs,即可在TensorBoard中显示图片的形状,运行效果如下
总结使用方法
- 关注输入和输出类型
- 多看官网文档
- 关注方法需要什么参数
不知道返回值的时候
- print(type)
- debug