afiliot's picture
Update scripts/data.py
c37af77
# 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)