File size: 1,204 Bytes
377f9d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c64879
377f9d4
 
9792640
5c64879
 
377f9d4
 
 
 
 
 
 
 
 
 
 
5c64879
377f9d4
c37af77
377f9d4
 
 
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
39
40
41
42
# Copyright (c) Owkin, Inc.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.

# Copyright (c) Owkin, Inc.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.

from typing import Tuple
import numpy as np
import torch

import datasets

class SlideFeaturesDataset(torch.utils.data.Dataset):
    """Slide features dataset."""
    def __init__(self, *args, **kwargs):
        self.hf_dataset = datasets.load_dataset(*args, **kwargs).with_format("torch")
        self.features = self.hf_dataset["features"]
        self.labels = self.hf_dataset["label"]

    def __getitem__(self, item: np.int64) -> Tuple[torch.Tensor, torch.Tensor]:
        """
        Parameters
        ----------
        item: np.int64
            Index of item, will be converted to int.

        Returns
        -------
        Tuple[torch.Tensor, torch.Tensor]
            (1000, 768), (1)
        """
        return (self.features[item], self.labels[item].unsqueeze(0).float())

    def __len__(self) -> int:
        return len(self.hf_dataset)