How to load a list of numpy arrays to pytorch dataset loader ...
newbedev.com › how-to-load-a-list-of-numpy-arraysimport torch from torch.utils.data import Dataset, DataLoader import numpy as np from PIL import Image class MyDataset(Dataset): def __init__(self, data, targets, transform=None): self.data = data self.targets = torch.LongTensor(targets) self.transform = transform def __getitem__(self, index): x = self.data[index] y = self.targets[index] if self.transform: x = Image.fromarray(self.data[index].astype(np.uint8).transpose(1,2,0)) x = self.transform(x) return x, y def __len__(self): return len ...
Datasets & DataLoaders — PyTorch Tutorials 1.10.1+cu102 ...
pytorch.org › tutorials › beginnerDatasets & DataLoaders. Code for processing data samples can get messy and hard to maintain; we ideally want our dataset code to be decoupled from our model training code for better readability and modularity. PyTorch provides two data primitives: torch.utils.data.DataLoader and torch.utils.data.Dataset that allow you to use pre-loaded datasets as well as your own data.
A detailed example of data loaders with PyTorch
stanford.edu › ~shervine › blogDataset): ' Characterizes a dataset for PyTorch ' def __init__ (self, list_IDs, labels): ' Initialization ' self.labels = labels self.list_IDs = list_IDs def __len__ (self): ' Denotes the total number of samples ' return len (self.list_IDs) def __getitem__ (self, index): ' Generates one sample of data ' # Select sample ID = self.list_IDs[index] # Load data and get label X = torch.load(' data/ ' + ID + '.pt ') y = self.labels[ID] return X, y