pytorch的dataset

dataset至少要override2个函数,一个是__len__,另一个是__getitem__
torch.utils.data.Dataset is an abstract class representing a dataset. Your custom dataset should inherit Dataset and override the following methods:
* __len__ so that len(dataset) returns the size of the dataset.
* __getitem__ to support the indexing such that dataset[i] can be used to get iith sample

官方的例子
http://pytorch.org/tutorials/beginner/data_loading_tutorial.html
代码截取

class FaceLandmarksDataset(Dataset):
    """Face Landmarks dataset."""

    def __init__(self, csv_file, root_dir, transform=None):
        """
        Args:
            csv_file (string): Path to the csv file with annotations.
            root_dir (string): Directory with all the images.
            transform (callable, optional): Optional transform to be applied
                on a sample.
        """
        self.landmarks_frame = pd.read_csv(csv_file)
        self.root_dir = root_dir
        self.transform = transform

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

    def __getitem__(self, idx):
        img_name = os.path.join(self.root_dir,
                                self.landmarks_frame.iloc[idx, 0])
        image = io.imread(img_name)
        landmarks = self.landmarks_frame.iloc[idx, 1:].as_matrix()
        landmarks = landmarks.astype('float').reshape(-1, 2)
        sample = {'image': image, 'landmarks': landmarks}

        if self.transform:
            sample = self.transform(sample)

        return sample
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,871评论 0 10
  • 壹 小时候,我的梦想,是要做一个文字的工作。我不懂文字工作的艰辛,但我心想,如果能写出优美的文字,付出再多,也是值...
    池书木易阅读 331评论 0 2
  • 这个时间,朋友圈和空间都刷不到最新的消息。 1. 凌晨一点,第一时间想到的是史叔,因为他可能要起床去出摊了。现在我...
    羞羞的麦穗阅读 239评论 0 1
  • 相信自己有这个能力 即使不能坚持 也要在每个月的时候只做一件事。
    凉生Vero阅读 260评论 0 0

友情链接更多精彩内容