Pytorch 中 Dataset 类的使用
导入包
import torch
import os
from torch.utils.data import Dataset
from PIL import Image
创建 Dataset 继承类
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(root_dir, label_dir)
self.tot_img_name = os.listdir(self.path)
def __getitem__(self, idx):
img_path = os.path.join(self.path, self.tot_img_name[idx])
img = Image.open(img_path)
label = self.label_dir
return img, label
def __len__(self):
return len(self.tot_img_name)
创建数据集以及数据集的合并
ants_dataset = MyData('../input/beas-ants-demo/train','ants_image')
bees_dataset = MyData('../input/beas-ants-demo/train','bees_image')
train_dataset = ants_dataset + bees_dataset
---- suffer now and live the rest of your life as a champion ----