• 【pytorch笔记】第二篇 Pytorch加载数据


    1. Python两大法宝

    ① Python3.6.3相当于一个package,package里面有不同的区域,不同的区域有不同的工具。

    Python语法有两大法宝:dir()、help() 函数。

    • dir():打开,看见里面有多少分区、多少工具。
    • help():说明书。
    import torch
    print(torch.cuda.is_available())
    help(torch.cuda.is_available) # 查看 torch.cuda.is_available 的用法
    dir(torch)  # 查看torch包中有哪些区、有哪些工具
    
    • 1
    • 2
    • 3
    • 4
    True
    Help on function is_available in module torch.cuda:
    
    is_available() -> bool
        Returns a bool indicating if CUDA is currently available.
    
    Output exceeds the size limit. Open the full output data in a text editor
    ['AVG',
     'AggregationType',
     'AliasDb',
     'AnyType',
     'Argument',
     'ArgumentSpec',
     'BFloat16Storage',
     'BFloat16Tensor',
     'BenchmarkConfig',
     'BenchmarkExecutionStats',
     'Block',
     'BoolStorage',
     'BoolTensor',
     'BoolType',
     'BufferDict',
     'ByteStorage',
     'ByteTensor',
     'CONV_BN_FUSION',
     'CallStack',
     'Capsule',
     'CharStorage',
     'CharTensor',
     'ClassType',
     'Code',
     'CompilationUnit',
    ...
     'rrelu',
     'rrelu_',
     'rsqrt',
     'rsqrt_',
     ...]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    1. Pytorch加载数据

    ① Pytorch中加载数据需要Dataset、Dataloader。

    • Dataset提供一种方式去获取每个数据及其对应的label,告诉我们总共有多少个数据。
    • Dataloader为后面的网络提供不同的数据形式,它将一批一批数据进行一个打包。

    2. 常用数据集两种形式

    ① 常用的第一种数据形式,文件夹的名称是它的label。

    ② 常用的第二种形式,lebel为文本格式,文本名称为图片名称,文本中的内容为对应的label。

    from torch.utils.data import Dataset
    help(Dataset)
    
    • 1
    • 2

    3. 路径直接加载数据

    from PIL import Image
    
    img_path = "Data/FirstTypeData/train/ants/0013035.jpg"        
    img = Image.open(img_path)
    img.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5

    4. Dataset加载数据

    主要是封装成类,可以单独分离为文件,主要是集成Dataset类,注意__inint__(),getitem()和__len__()三个方法的实现,可作为自定义加载数据集的模板。

    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.root_dir 相当于类中的全局变量
            self.label_dir = label_dir     
            self.path = os.path.join(self.root_dir,self.label_dir) # 字符串拼接,根据是Windows或Lixus系统情况进行拼接               
            self.img_path = os.listdir(self.path) # 获得路径下所有图片的地址
            
        def __getitem__(self,idx):
            img_name = self.img_path[idx]
            img_item_path = os.path.join(self.root_dir,self.label_dir,img_name)            
            img = Image.open(img_item_path)
            label = self.label_dir
            return img, label
        
        def __len__(self):
            return len(self.img_path)
        
    root_dir = "Data/FirstTypeData/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)
    print(len(ants_dataset))
    print(len(bees_dataset))
    train_dataset = ants_dataset + bees_dataset # train_dataset 就是两个数据集的集合了     
    print(len(train_dataset))
    
    img,label = train_dataset[200]
    print("label:",label)
    img.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
  • 相关阅读:
    图像预处理工具_CogPolarUnwrapTool
    契约测试理论篇
    UVM field automation机制
    设计模式——中介者模式、迭代器模式、访问者模式(双分派)(行为型模式)
    基于R语言机器学习方法与案例分析
    LeetCode 792. 匹配子序列的单词数 二分查找
    第8天:可变与不可变类型、字典、元组与集合的内置方法
    chatgpt赋能python:【Python实例教程】如何使用Python计算长方形面积
    【分享】“抖店“在集简云平台集成应用的常见问题与解决方案
    Linux使用netstat查看网络状态
  • 原文地址:https://blog.csdn.net/qq_35793394/article/details/127650952