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):
imag_name = self.img_path[idx]
img_item_path = os.path.join(self.root_dir, self.label_dir, imag_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/NEW/train"
ants_label_dir = "ants_image"
bees_label_dir = "bees_image"
ant_dataset = Mydata(root_dir, ants_label_dir)
bees_dataset = Mydata(root_dir, bees_label_dir)
train_dataset=ant_dataset+bees_dataset
imag, label = train_dataset[0]
print(len(train_dataset),len(ant_dataset),len(bees_dataset))
imag.save("a.jpg")
pytorch数据集
最新推荐文章于 2024-01-06 15:55:21 发布