其他分享
首页 > 其他分享> > 【PyTorch】dataset数据读取

【PyTorch】dataset数据读取

作者:互联网

两个例子
Example 1
DTU训练集数据读取

Dataset-|
        |-TrainDataset-|
        |              |-train  [数据增强]
        |              |-validate
        |-TestDataset

TrainDataset要兼容train和validate两个子集
TrainDataset和TestDataset两个数据集分开写
(不确定两者是不是只有标签不同)
类名首字母大写/实例化的变量小写,并用下划线分开

from torch.utils.data import Dataset
import os
import cv2
import numpy as np
from PIL import Image

class MyDataset(Dataset):
  def __init__(self, data_path, mode)
    super(MyDataset, self).__init__()
    self.data_path = data_path
    self.mode = mode

    assert self.mode in ["train", "validate"]

    self.metas = xxx  #只保存路径,不读图片
    
  def __len__(self):
    return len(self.metes)

  def __getitem__(self, idx):
    # 训练集:返回图片和标签
    pass

if __name__ == "__main__":
  # test code HERE
  data_path = ""
  train_dataset = TrainDataset(data_path, mode="train")


标签:__,读取,self,dataset,PyTorch,train,path,import,data
来源: https://www.cnblogs.com/Todd-Qi/p/16473493.html