dateset类代码基本操作
import os.path
from torch.utils.data import Dataset
from PIL import Image
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 = "dataset/train"
ants_label_dir = "ants" #蚂蚁数据集
bees_label_dir = "bees"
ants_dataset = Mydata(root_dir,ants_label_dir)
# train_dataset = ants_label_dir + bees_label_dir 也可以把两个数据集合起来
输入ants_dataset[0]可以获取
打开图片:
img,label = ants_dataset[0]
img.show()
Tensorboard的使用
SummaryWriter类的使用
SummaryWriter(“文件夹名”) #存到这个文件下
writer.add_image()
writer.add_scalar(self,tag,scalar_value,global_step)
tag:标题
scalar_value:y轴
global_step:x轴(训练了多少次)