pytorch学习(一)、tensorboard和torchvision

发布时间:2024年01月17日

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


一、os模块

os.path.join(dir1,dir2) #路径的拼接

二、from torch.utils.data import Dataset

  • 加载数据的模块

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)
	# 读取列表里的图片数据并展示数据
    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)

三、from torch.utils.tensorboard import SummaryWriter

  • 数据日记的创建和查看
  • tensorboard --logdir=logs 打开学习日志
  • tensorboard --logdir=logs --port=6007 打开学习日志并设置端口号

from torch.utils.tensorboard import SummaryWriter
import numpy as np
from PIL import Image
# 输出到logs文件夹
writer = SummaryWriter("logs")
# 图片路径
image_path = "D:\深度学习\dataset\\train\\ants\\0013035.jpg"
img_PIL = Image.open(image_path)
img_array = np.array(img_PIL) # 转换成numpy形式
#创建端口展示图片
writer.add_image("test", img_array,1,dataformats="HWC")

# y=2x
for i in range(100):
    # 创建一个页面
    writer.add_scalar("y=2x",2*i,i)

writer.close()

四、from torchvision import transforms

  • 输入 *PIL Image.open()
  • 输出 *tensor ToTensor()
  • 作用 *narrays cv.imread()

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

# 加载图片路径
img_path = "D:\深度学习\dataset\\train\\ants\\5650366_e22b7e1065.jpg"
img = Image.open(img_path)#打开图片
# print(type(img))
writer = SummaryWriter("logs")

#转换成tensor类型
tensor_trans = transforms.ToTensor()
tensor_img = tensor_trans(img) #转换tensor类型
writer.add_image("Tensor_img",tensor_img)

# 归一化操作Normalize(mean,std)
trans_normal = transforms.Normalize([0.5,0.5,0.5],[0.5,0.5,0.5])
normal_img = trans_normal(tensor_img)
writer.add_image("noraml_img",normal_img)

# 调整图片大小
print(img.size)
# 调整成100*100
trans_resize = transforms.Resize((100,100))
# 传入一个PIL类型
resize_img = trans_resize(img)
print(resize_img)
# 转换成tensor类型
resize_img = tensor_trans(resize_img)
print(resize_img)

writer.close()

五、torchvision 中数据集的下载

  • torchvision.datasets
    • root:存放路径

train_set = torchvision.datasets.CIFAR10(root='./dataset/CIFAR10',train=True,download=True)
test_set = torchvision.datasets.CIFAR10(root='./dataset/CIFAR10',train=False,download=True)

下载完的图片是PIL类型,我们需要转换成tensor类型:


import torchvision

dataset_transform = torchvision.transforms.Compose([
    torchvision.transforms.ToTensor()
])

train_set = torchvision.datasets.CIFAR10(root='./dataset/CIFAR10',train=True,transform=dataset_transform,download=True)
test_set = torchvision.datasets.CIFAR10(root='./dataset/CIFAR10',train=False,transform=dataset_transform,download=True)

print(train_set[0])

六、torchvision 中的测试数据集的加载

  • from torch.utils.data import DataLoader

    • dataset:数据集

import torchvision
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
#测试数据集的下载
test_data = torchvision.datasets.CIFAR10('./dataset/CIFAR10',train=False,transform=torchvision.transforms.ToTensor())
#测试数据集的加载
test_loader = DataLoader(dataset=test_data,batch_size=64,shuffle=True,num_workers=0,drop_last=False)
# 测试数据集中第一张图片及target
img,target = test_data[0]
print(img.shape)
print(target)
# 展示图片
writer = SummaryWriter('dataloader')
step = 0
for data in test_loader:
    img,target = data
    writer.add_images('loader',img,step)
    step+=1
writer.close()

文章来源:https://blog.csdn.net/weixin_73044854/article/details/135658079
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。