加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
read_data.py 1.19 KB
一键复制 编辑 原始数据 按行查看 历史
AlbertDarren 提交于 2022-05-12 11:01 . first commit
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
@Time : 2022/4/25 08:19
@Author : Albert Darren
@Contact : 2563491540@qq.com
@File : read_data.py
@Version : Version 1.0.0
@Description : TODO 继承Dataset抽象类,实现__getitem__和__len__两个方法完成获取每一个样本的数据及标签
@Created By : PyCharm
"""
from torch.utils.data import Dataset
import os
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.path, img_name)
img = Image.open(img_item_path)
return img, self.label_dir
def __len__(self):
return len(self.img_path)
if __name__ == '__main__':
root_folder = "hymenoptera_data/train"
ants_label_folder = "ants_image"
ants_dataset = MyData(root_folder, ants_label_folder)
bees_label_folder = "bees_image"
bees_dataset = MyData(root_folder, bees_label_folder)
print(ants_dataset)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化