Ejemplo n.º 1
0
def learn_vocabulary(k, max_iter, single_file, vector_matrix=None, input_path=None):
    if vector_matrix is None:
        if single_file:
            vector_matrix = read_feature_vector(input_path)
        else:
            file_names = list_all_files(input_path)
            vector_matrix = read_feature_vector(input_path + file_names[0])
            for f in xrange(1, len(file_names)):
                vector = read_feature_vector(input_path + file_names[f])
                if vector is not None:
                    vector_matrix = append(vector_matrix, vector, axis=0)
    return mykmeans_plus(data=vector_matrix,k=k, max_iter=max_iter)[0]
Ejemplo n.º 2
0
def learn_vocabulary(k,
                     max_iter,
                     single_file,
                     vector_matrix=None,
                     input_path=None):
    if vector_matrix is None:
        if single_file:
            vector_matrix = read_feature_vector(input_path)
        else:
            file_names = list_all_files(input_path)
            vector_matrix = read_feature_vector(input_path + file_names[0])
            for f in xrange(1, len(file_names)):
                vector = read_feature_vector(input_path + file_names[f])
                if vector is not None:
                    vector_matrix = append(vector_matrix, vector, axis=0)
    return mykmeans_plus(data=vector_matrix, k=k, max_iter=max_iter)[0]
Ejemplo n.º 3
0
def vlad_vector(vector_path, vocabulary):
    features = read_feature_vector(vector_path)
    codes = vq(features, vocabulary)[0]
    vlad = zeros(vocabulary.shape)
    for idx in range(codes.size):
        diff = subtract(features[idx], vocabulary[codes[idx]])
        vlad[codes[idx]] = add(diff, vlad[codes[idx]])
    return ndarray.flatten(vlad)
Ejemplo n.º 4
0
def vlad_vector(vector_path, vocabulary):
    features = read_feature_vector(vector_path)
    codes_idx = vq(features, vocabulary)[
        0]  #A length M array holding the code book index for each observation
    vlad = zeros(vocabulary.shape)
    for i in range(codes_idx.size):
        code_idx = codes_idx[i]
        diff = subtract(features[i], vocabulary[code_idx])
        vlad[code_idx] = add(diff, vlad[code_idx])
    return ndarray.flatten(vlad)