def gaussin_embedding_ap(filename_feature, filename_list_key, filename_scaler):
    """I wanted to use the gaussian as the embedding,
    but Bhattacharyya distance may not in [0, 1], so I normalized them by dividing the maximum value """

    logger = logging.getLogger(__name__)

    list_feature_flatten_test, label_integer_test, le, scaler = \
        load_data_embedding(filename_feature=filename_feature,
                            filename_list_key=filename_list_key,
                            filename_scaler=filename_scaler)

    embeddings = []
    for ii_emb in range(len(list_feature_flatten_test)):
        logger.info('calculating..., %s, total, %s', ii_emb,
                    len(list_feature_flatten_test))

        feature = scaler.transform(list_feature_flatten_test[ii_emb])

        # print(feature.shape)
        gmm = GaussianMixture(n_components=1, covariance_type='diag')
        gmm.fit(feature)
        mu = gmm.means_[0, :]
        cov = gmm.covariances_[0, :]
        embeddings.append([mu, cov])

    dist_mat = distance_matrix_embedding_gaussian(embeddings)
    dist_mat = 1.0 - dist_mat / np.max(dist_mat)
    gt_mat = ground_truth_matrix(label_integer_test)

    ap = eval_embeddings(dist_mat=dist_mat, gt_mat=gt_mat)
    return ap
Esempio n. 2
0
def embedding_siamese_tsne(filename_feature, filename_list_key, filename_scaler):
    """calculate average precision of siamese triplet embedding"""

    list_feature_flatten_test, label_integer_test, le, scaler = \
        load_data_embedding(filename_feature=filename_feature,
                            filename_list_key=filename_list_key,
                            filename_scaler=filename_scaler)

    path_model = '/Users/gong/Documents/pycharmProjects/phoneticSimilarity/models/phoneme_embedding_siamese_triplet'
    path_eval = '/Users/gong/Documents/pycharmProjects/phoneticSimilarity/eval/phoneme_embedding_siamese_triplet'

    model_name = 'phone_embedding_RNN_triplet_margin08'

    np.save(file=os.path.join(path_eval, model_name + '_labels'), arr=label_integer_test)
    pickle.dump(le, open(os.path.join(path_eval, model_name + '_le.pkl'), 'wb'), protocol=2)

    embedding_dim = 29

    for ii in range(1):
        filename_model = os.path.join(path_model, model_name + '_' + str(ii) + '.h5')
        model = load_model(filepath=filename_model, compile=False)
        model_embedding = model.get_layer('embedding')

        embeddings = np.zeros((len(list_feature_flatten_test), embedding_dim))
        for ii_emb in range(len(list_feature_flatten_test)):
            print('calculate', ii, 'run time', ii_emb, 'embedding', len(list_feature_flatten_test), 'total')

            x_batch = np.expand_dims(scaler.transform(list_feature_flatten_test[ii_emb]), axis=0)
            embeddings[ii_emb, :] = model_embedding.predict_on_batch(x_batch)

        np.save(file=os.path.join(path_eval, model_name + '_embedding_' + str(ii)), arr=embeddings)
def embedding_siamese_ap(filename_feature, filename_list_key, filename_scaler,
                         model_name):
    """calculate average precision of siamese triplet embedding"""

    list_feature_flatten_test, label_integer_test, le, scaler = \
        load_data_embedding(filename_feature=filename_feature,
                            filename_list_key=filename_list_key,
                            filename_scaler=filename_scaler)

    path_model = '/home/gong/Documents/pycharmProjects/phoneticSimilarity/models/phoneme_embedding_siamese_triplet'
    path_eval = '/home/gong/Documents/pycharmProjects/phoneticSimilarity/eval/phoneme_embedding_siamese_triplet'

    list_ap = []
    embedding_dim = 29

    for ii in range(5):
        filename_model = os.path.join(path_model,
                                      model_name + '_' + str(ii) + '.h5')
        model = load_model(filepath=filename_model, compile=False)
        model_embedding = model.get_layer('embedding')

        embeddings = np.zeros((len(list_feature_flatten_test), embedding_dim))
        for ii_emb in range(len(list_feature_flatten_test)):
            print('calculate', ii, 'run time', ii_emb, 'embedding',
                  len(list_feature_flatten_test), 'total')

            x_batch = np.expand_dims(scaler.transform(
                list_feature_flatten_test[ii_emb]),
                                     axis=0)
            embeddings[ii_emb, :] = model_embedding.predict_on_batch(x_batch)

        # dist_mat = distance_matrix_embedding_classifier(embeddings)

        dist_mat = (2.0 - squareform(pdist(embeddings, 'cosine'))) / 2.0
        gt_mat = ground_truth_matrix(label_integer_test)

        np.save(file=os.path.join(path_eval, 'dist_mat_' + str(ii)),
                arr=dist_mat)

        ap = eval_embeddings(dist_mat=dist_mat, gt_mat=gt_mat)

        list_ap.append(ap)

    filename_eval = os.path.join(path_eval, model_name + '.csv')
    with open(filename_eval, 'w') as csvfile:
        csvwriter = csv.writer(
            csvfile,
            delimiter=',',
        )
        csvwriter.writerow([np.mean(list_ap), np.std(list_ap)])
Esempio n. 4
0
def embedding_classifier_tsne(filename_feature, filename_list_key, filename_scaler):
    """calculate average precision of classifier embedding"""

    list_feature_flatten_test, label_integer_test, le, scaler = \
        load_data_embedding(filename_feature=filename_feature,
                            filename_list_key=filename_list_key,
                            filename_scaler=filename_scaler)

    path_model = '/Users/gong/Documents/pycharmProjects/phoneticSimilarity/models/phone_embedding_classifier'
    path_eval = '/Users/gong/Documents/pycharmProjects/phoneticSimilarity/eval/phone_embedding_classifier'

    # configs = [[1, 0], [1, 1], [2, 0], [2, 1], [2, 2], [3, 0], [3, 1], [3, 2], [3, 3]]
    configs = [[1, 1]]

    for config in configs:
        model_name = config_select(config)
        pickle.dump(le, open(os.path.join(path_eval, model_name + '_le.pkl'), 'wb'), protocol=2)

        embedding_dim = 29

        for ii in range(1):
            filename_model = os.path.join(path_model, model_name + '_' + str(ii) + '.h5')
            model = load_model(filepath=filename_model)
            weights = model.get_weights()

            input_shape = [1, None, 80]
            model_1_batch = model_select(config=config, input_shape=input_shape)
            model_1_batch.compile(optimizer='adam',
                                  loss='categorical_crossentropy',
                                  metrics=['accuracy'])
            model_1_batch.set_weights(weights=weights)

            embeddings = np.zeros((len(list_feature_flatten_test), embedding_dim))
            for ii_emb in range(len(list_feature_flatten_test)):
                print('calculate', ii, 'run time', ii_emb, 'embedding', len(list_feature_flatten_test), 'total')

                x_batch = np.expand_dims(scaler.transform(list_feature_flatten_test[ii_emb]), axis=0)
                embeddings[ii_emb, :] = model_1_batch.predict_on_batch(x_batch)

            np.save(file=os.path.join(path_eval, model_name + '_embedding_' + str(ii)), arr=embeddings)
            np.save(file=os.path.join(path_eval, model_name + '_labels_'), arr=label_integer_test)
    # filename_label_encoder = '/homedtic/rgong/phoneEmbeddingModelsTraining/dataset/le_phn_embedding.pkl'
    # filename_data_splits = '/homedtic/rgong/phoneEmbeddingModelsTraining/dataset/data_splits.pkl'
    #
    # path_model = '/homedtic/rgong/phoneEmbeddingModelsTraining/out/'

    filename_feature = '/Users/gong/Documents/MTG document/dataset/phoneEmbedding/feature_phn_embedding_train.pkl'
    filename_list_key = '/Users/gong/Documents/MTG document/dataset/phoneEmbedding/list_key.pkl'
    filename_scaler = '/Users/gong/Documents/MTG document/dataset/phoneEmbedding/scaler_phn_embedding.pkl'
    filename_label_encoder = '/Users/gong/Documents/MTG document/dataset/phoneEmbedding/le_phn_embedding.pkl'
    filename_data_splits = '/Users/gong/Documents/MTG document/dataset/phoneEmbedding/data_splits.pkl'

    path_model = '/Users/gong/Documents/pycharmProjects/phoneticSimilarity/models/phone_embedding_classifier/'
    # path_model = '../temp'

    list_feature_flatten, labels_integer, le, scaler = load_data_embedding(
        filename_feature=filename_feature,
        filename_list_key=filename_list_key,
        filename_scaler=filename_scaler)

    # # split folds
    # folds5_split_indices = cv5foldsIndices(list_feature_flatten=list_feature_flatten, label_integer=labels_integer)

    # index_feature = range(len(list_feature_flatten))
    # train_index, val_index, _, _ = train_test_split(index_feature, labels_integer, test_size=0.1, stratify=labels_integer)
    #
    # pickle.dump(le ,open(filename_label_encoder, 'wb'), protocol=2)
    # pickle.dump([train_index, val_index], open(filename_data_splits, 'wb'), protocol=2)

    train_index, val_index = pickle.load(open(filename_data_splits, 'rb'))
    #
    # for train_index, val_index in folds5_split_indices:
def embedding_classifier_ap(filename_feature, filename_list_key,
                            filename_scaler):
    """calculate average precision of classifier embedding"""

    list_feature_flatten_test, label_integer_test, le, scaler = \
        load_data_embedding(filename_feature=filename_feature,
                            filename_list_key=filename_list_key,
                            filename_scaler=filename_scaler)

    path_model = '/Users/gong/Documents/pycharmProjects/phoneticSimilarity/models/phone_embedding_classifier'
    path_eval = '/Users/gong/Documents/pycharmProjects/phoneticSimilarity/eval/phone_embedding_classifier'

    # configs = [[1, 0], [1, 1], [2, 0], [2, 1], [2, 2], [3, 0], [3, 1], [3, 2], [3, 3]]
    configs = [[1, 1]]

    for config in configs:
        model_name = config_select(config)

        list_ap = []
        embedding_dim = 29

        for ii in range(5):
            filename_model = os.path.join(path_model,
                                          model_name + '_' + str(ii) + '.h5')
            model = load_model(filepath=filename_model)
            weights = model.get_weights()

            input_shape = [1, None, 80]
            model_1_batch = model_select(config=config,
                                         input_shape=input_shape)
            model_1_batch.compile(optimizer='adam',
                                  loss='categorical_crossentropy',
                                  metrics=['accuracy'])
            model_1_batch.set_weights(weights=weights)

            embeddings = np.zeros(
                (len(list_feature_flatten_test), embedding_dim))
            for ii_emb in range(len(list_feature_flatten_test)):
                print('calculate', ii, 'run time', ii_emb, 'embedding',
                      len(list_feature_flatten_test), 'total')

                x_batch = np.expand_dims(scaler.transform(
                    list_feature_flatten_test[ii_emb]),
                                         axis=0)
                embeddings[ii_emb, :] = model_1_batch.predict_on_batch(x_batch)

            # dist_mat = distance_matrix_embedding_classifier(embeddings)

            dist_mat = (2.0 - squareform(pdist(embeddings, 'cosine'))) / 2.0
            gt_mat = ground_truth_matrix(label_integer_test)

            np.save(file=os.path.join(path_eval, 'dist_mat_' + str(ii)),
                    arr=dist_mat)

            ap = eval_embeddings(dist_mat=dist_mat, gt_mat=gt_mat)

            list_ap.append(ap)

        filename_eval = os.path.join(path_eval, model_name + '.csv')
        with open(filename_eval, 'w') as csvfile:
            csvwriter = csv.writer(
                csvfile,
                delimiter=',',
            )
            csvwriter.writerow([np.mean(list_ap), np.std(list_ap)])