首页 > 其他分享 >Dataset & DataLoader

Dataset & DataLoader

时间:2023-06-12 18:33:29浏览次数:42  
标签:__ img self DataLoader transform Dataset image

from torch.utils.data import Dataset, DataLoader

1. Dataset

There are 2 different types of datasets:

1.1 map-style datasets (most commonly used)

Represents a map from indices/keys to data samples.

  • For example, such a dataset, when accessed with dataset[idx], could read the idx-th image and its corresponding label.

1.2 iterable-style datasets

An iterable-style dataset represents an iterable over data samples.

  • This type of datasets is particularly suitable for cases where random reads are expensive or even improbable, and where the batch size depends on the fetched data.

  • For example, such a dataset, when called iter(dataset), could return a stream of data reading from a database, a remote server, or even logs generated in real time.

1.3 Demo (map-style datasets)

# A custom Dataset class must implement three functions: __init__, __len__, and __getitem__. 

from torch.utils.data import Dataset

class CustomImageDataset(Dataset):
    def __init__(self, annotations_file, img_dir, transform=None, target_transform=None):
        self.img_labels = pd.read_csv(annotations_file)
        self.img_dir = img_dir
        self.transform = transform  # transform images
        self.target_transform = target_transform  # transform labels

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

    def __getitem__(self, idx):
        img_path = os.path.join(self.img_dir, self.img_labels.iloc[idx, 0])
        image = read_image(img_path)
        label = self.img_labels.iloc[idx, 1]
        if self.transform:
            image = self.transform(image)
        if self.target_transform:
            label = self.target_transform(label)
        return image, label

2. DataLoader

标签:__,img,self,DataLoader,transform,Dataset,image
From: https://www.cnblogs.com/shendaw/p/17475823.html

相关文章

  • Delphi 原生支持DataSet导出Json
    Xalion发现data.DBJson单元,不知什么时候加的,官方帮助中也没有写。procedureTForm18.Button2Click(Sender:TObject);varcV:TDatasetToJSONBridge;jo:Tjsonobject;begincV:=TDatasetToJSONBridge.Create;trycV.Dataset:=mt;jo:=Tjsonobject(cV.......
  • dataset.py代码阅读笔记
    #dataset.pyimportloggingimportos#处理c语言类型和二进制的数据结构importstruct#regularexpressionmoduleimportreimporttimeimportnumpyasnp#这个模块pip的时候有问题,但是按照提示出现换了pip文件名就好使了,叫做scikit-image#skimage的文档,主要是一些图像读取,显......
  • pytorch 的 torchvision.datasets.ImageFolder 来自定义数据集
    importtorchvisionclassClassificationDataset(torchvision.datasets.ImageFolder): """ YOLOv5ClassificationDataset. Arguments root:Datasetpath """ def__init__(self,root): super().__init__(root=root)#调用了父类的......
  • 【数据集】 Bosch Small Traffic Lights Dataset(BSTLD)
    前言 参考1. GitHub-berktepebag/Traffic-light-detection-with-YOLOv3-BOSCH-traffic-light-dataset;2. BoschSmallTrafficLightsDataset;3. GitHub-bosch-ros-pkg/bstld:SamplescriptsfortheBoschSmallTrafficLightsDataset;4. ADeepLearningApproachtoT......
  • Flink核心API之DataSet
    DataSetAPIDataSetAPI主要可以分为3块来分析:DataSource、Transformation、Sink。DataSource是程序的数据源输入。Transformation是具体的操作,它对一个或多个输入数据源进行计算处理,例如map、flatMap、filter等操作。DataSink是程序的输出,它可以把Transformation处理之后的数......
  • 【数据集】 Bosch Small Traffic Lights Dataset(BSTLD)
    前言 参考1. GitHub-berktepebag/Traffic-light-detection-with-YOLOv3-BOSCH-traffic-light-dataset;2. BoschSmallTrafficLightsDataset;3. GitHub-bosch-ros-pkg/bstld:SamplescriptsfortheBoschSmallTrafficLightsDataset;4. ADeepLearningApproa......
  • [记录一下]lazarus memdataset的filter问题
    在lazarus使用memdataset时,如果filter按下面的方法是得不到想要结果:MEMDataSet1.Filtered:=false;MEMDataSet1.Filter:='tasknameLike'+quotedstr('%'+Edit4.Text+'%');MEMDataSet1.Filtered:=true;最后在官网找到解决办法:unitUnit1;{$modeobjfpc}{$......
  • RUL预测常用数据集--C-MAPSS Dataset介绍
    C-MAPSS是针对航空发动机剩余寿命预测的数据集。该数据集由NASA(美国国家航空航天局)发布,包含了四个不同类型的航空发动机的传感器数据,以及相应的故障模式和剩余寿命数据,如表1所示。表1InformationoftheC-MAPSSdataset.DatasetFD001FD002FD003FD004Engineunit......
  • 利用hf datasets库包构建自己的数据集
    !wgethttps://raw.githubusercontent.com/zhangbo2008/data_ner/main/aomanyupianjian-Oaaaa!pipinstalldatasets#=======加载自己数据集withopen('aaaa')asf:t=f.readlines()save1=[]save2=[]a=''b=''foriint:ifi=......
  • This dataset does not have valid histogram required for classification method, r
     此数据集没有分类方法所需的有效直方图,请运行“计算统计信息”工具生成直方图。参考1:https://blog.csdn.net/soderayer/article/details/125409022参考2:https://blog.csdn.net/aGang_Gg/article/details/86690749 计算栅格统计信息......