学习pytorch 2 导入查看dataset

学习pytorch 2

2. dataset实战

B站小土堆视频

代码

py 复制代码
from torch.utils.data import Dataset
from PIL import Image
#import cv2
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_list = os.listdir(self.path)

    def __getitem__(self, idx):
        img_name = self.img_list[idx]
        img_path = os.path.join(self.root_dir, self.label_dir, img_name)
        img = Image.open(img_path)
        label = self.label_dir
        return img, label

    def __len__(self):
        return len(self.img_list)

if __name__ == '__main__':
    root_dir = "hymenoptera_data/train"
    ants_label_dir = "ants"
    bees_label_dir = "bees"
    ants_dataset = MyData(root_dir, ants_label_dir)
    bees_dataset = MyData(root_dir, bees_label_dir)
    train_dataset = ants_dataset + bees_dataset
    img, label = ants_dataset[1]
    img, label = train_dataset[1]
    img.show()

数据集

https://download.pytorch.org/tutorial/hymenoptera_data.zip

相关推荐
xian_wwq3 小时前
【学习笔记】攻击链贯穿端边云!边缘网络访问三大核心风险预警
笔记·学习·安全·边缘计算
Wnq100724 小时前
世界模型 AI:认知跃迁的可行性与本质性挑战
人工智能
穷人小水滴4 小时前
科幻 「备用肉身虫」 系列设定集 (AI 摘要)
人工智能·aigc·科幻·未来·小说·设定
老赵聊算法、大模型备案4 小时前
北京市生成式人工智能服务已备案信息公告(2025年12月11日)
人工智能·算法·安全·aigc
咬人喵喵4 小时前
上下文窗口:AI 的“大脑容量”
人工智能
workflower5 小时前
时序数据获取事件
开发语言·人工智能·python·深度学习·机器学习·结对编程
weixin_446122465 小时前
一个案例验证 LLM大模型编码能力哪家强
人工智能
老蒋新思维5 小时前
创客匠人峰会深度解析:知识变现的 “信任 - 效率” 双闭环 —— 从 “单次交易” 到 “终身复购” 的增长密码
大数据·网络·人工智能·tcp/ip·重构·数据挖掘·创客匠人
大刘讲IT6 小时前
面向中小企业的企业AI Agent未来3年构建蓝图规划
人工智能·经验分享·ai·开源·制造
yzx9910136 小时前
深度学习的进化之路:从感知机到通用智能的曙光
人工智能·深度学习