r"""ArrayDataset is a dataset for numpy array data. One or more numpy arrays are needed to initiate the dataset. And the dimensions represented sample number are expected to be the same. Args: Arrays(dataset and labels): the datas and labels to be returned iteratively. Ret
| 107 | |
| 108 | |
| 109 | class ArrayDataset(Dataset): |
| 110 | r"""ArrayDataset is a dataset for numpy array data. |
| 111 | |
| 112 | One or more numpy arrays are needed to initiate the dataset. |
| 113 | And the dimensions represented sample number are expected to be the same. |
| 114 | |
| 115 | Args: |
| 116 | Arrays(dataset and labels): the datas and labels to be returned iteratively. |
| 117 | |
| 118 | Returns: |
| 119 | Tuple: A set of raw data and corresponding label. |
| 120 | |
| 121 | |
| 122 | Examples: |
| 123 | |
| 124 | .. code-block:: python |
| 125 | |
| 126 | from megengine.data.dataset import ArrayDataset |
| 127 | from megengine.data.dataloader import DataLoader |
| 128 | from megengine.data.sampler import SequentialSampler |
| 129 | |
| 130 | rand_data = np.random.randint(0, 255, size=(sample_num, 1, 32, 32), dtype=np.uint8) |
| 131 | label = np.random.randint(0, 10, size=(sample_num,), dtype=int) |
| 132 | dataset = ArrayDataset(rand_data, label) |
| 133 | seque_sampler = SequentialSampler(dataset, batch_size=2) |
| 134 | |
| 135 | dataloader = DataLoader( |
| 136 | dataset, |
| 137 | sampler = seque_sampler, |
| 138 | num_workers=3, |
| 139 | ) |
| 140 | |
| 141 | for step, data in enumerate(dataloader): |
| 142 | print(data) |
| 143 | |
| 144 | """ |
| 145 | |
| 146 | def __init__(self, *arrays): |
| 147 | super().__init__() |
| 148 | if not all(len(arrays[0]) == len(array) for array in arrays): |
| 149 | raise ValueError("lengths of input arrays are inconsistent") |
| 150 | self.arrays = arrays |
| 151 | |
| 152 | def __getitem__(self, index: int) -> Tuple: |
| 153 | return tuple(array[index] for array in self.arrays) |
| 154 | |
| 155 | def __len__(self) -> int: |
| 156 | return len(self.arrays[0]) |
| 157 | |
| 158 | |
| 159 | class ConcatDataset(Dataset): |
no outgoing calls