提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
os.path.join(dir1,dir2) #路径的拼接
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
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 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()
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])
from torch.utils.data import DataLoader
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()