os.listdir() 用于返回指定的文件夹包含的文件或文件夹的名字的列表
os.path.join() 用于连接两个或更多的路径名组件
加载数据集
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) root_dir = r'dataset/train' ants_label_dir = r'ants' bees_label_dir = r'bees' ants_datasets =MyData(root_dir,ants_label_dir) bees_datasets =MyData(root_dir,bees_label_dir)
TensorBoard的使用
from torch.utils.tensorboard import SummaryWriter #模块导入
writer = SummaryWriter('文件夹名')
writer.add_scalar('图的名称',y轴数据,x轴数据) #添加标量
输出图片 -->在终端输入tensorboard - - logdir='文件所在的绝对路径'