当前位置:网站首页>Use of dataset of pytorch

Use of dataset of pytorch

2022-07-06 10:24:00 How about a song without trace

from torch.utils.data import Dataset
from PIL import Image
import os
class MyData():
    def __init__(self, root_dir, label_dir):
        # Use self It is equivalent to creating a global variable for the following ones, which can also be used 
        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)
        # Get every picture 
    def __getitem__(self, index):
        # adopt index Get image address 
        img_name = self.img_path[index] # Path of each picture 
        img_item_path = os.path.join(self.root_dir, self.label_dir, img_name)
        print(img_item_path)
        # Read the picture 
        img = Image.open(img_item_path)
        img.show
        label = self.label_dir
        return img, label
    def __len__(self): # How long is the dataset 
        return len(self.img_path)

root_dir = "D:\\ Data sets \\hymenoptera_data\\train"
ant_lable_dair = "ants"
bees_label_dir = "bees"
ant_dataset = MyData(root_dir, ant_lable_dair)
bees_dataset = MyData(root_dir,bees_label_dir)
ant_dataset.__getitem__(0)
for i in range(1,100):
    bees_dataset.__getitem__(i)
img, label = ant_dataset[0]
img.show()

 

原网站

版权声明
本文为[How about a song without trace]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060910588423.html

随机推荐