| 49 | |
| 50 | class BinaryDataset(Dataset): |
| 51 | def __init__(self, path, process_fn, length_per_sample=64+1024+4096, dtype='int32', preload=False, **kwargs): |
| 52 | assert length_per_sample is not None |
| 53 | self.length_per_sample = length_per_sample |
| 54 | self.dtype = np.dtype(dtype) |
| 55 | self.process_fn = process_fn |
| 56 | if preload: |
| 57 | self.bin = np.fromfile(path, dtype=self.dtype).reshape(-1, length_per_sample) |
| 58 | else: |
| 59 | with open(path, 'r') as fid: |
| 60 | nbytes = fid.seek(0, 2) |
| 61 | flen = fid.tell() // self.dtype.itemsize |
| 62 | self.bin = np.memmap(path, dtype=self.dtype, shape=(flen // length_per_sample, length_per_sample)) |
| 63 | |
| 64 | def __len__(self): |
| 65 | return self.bin.shape[0] |