| import numpy as np | |
| def read_fbin(filename, start_idx=0, chunk_size=None): | |
| """ Read *.fbin file that contains float32 vectors | |
| Args: | |
| :param filename (str): path to *.fbin file | |
| :param start_idx (int): start reading vectors from this index | |
| :param chunk_size (int): number of vectors to read. | |
| If None, read all vectors | |
| Returns: | |
| Array of float32 vectors (numpy.ndarray) | |
| """ | |
| with open(filename, "rb") as f: | |
| nvecs, dim = np.fromfile(f, count=2, dtype=np.int32) | |
| nvecs = (nvecs - start_idx) if chunk_size is None else chunk_size | |
| arr = np.fromfile(f, count=nvecs * dim, dtype=np.float32, | |
| offset=start_idx * 4 * dim) | |
| return arr.reshape(nvecs, dim) | |
| # read data | |
| # chunk_size = 50000 | |
| # dataset = read_fbin('deep1B/base.10M.fbin', chunk_size=chunk_size) | |