afiliot's picture
Update scripts/data.py
5c64879
raw
history blame
1.28 kB
# 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.unsqueeze(0).float())
def __len__(self) -> int:
return len(self.hf_dataset)
@property
def labels(self):
return self.hf_dataset["label"]