| import numpy as np | |
| def load_deep1b(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) | |
| def load_glove(filename): | |
| from gensim.models import KeyedVectors | |
| return KeyedVectors.load_word2vec_format(filename).vectors | |
| def load_sift1m(fname): | |
| data = np.fromfile(fname, dtype=np.float32) | |
| dim = data[0].view(np.int32) | |
| data = data.reshape(-1, dim + 1) | |
| data = np.ascontiguousarray(data[:, 1:]) | |
| ndata, dim = data.shape | |
| return data, ndata, dim |