Ejemplo n.º 1
0
def minimax(board, y, x, depth, isMax, alpha, beta):
    if not isMax:
        board[y][x] = 'b'
    else:
        board[y][x] = 'r' 
    if depth==0:
        if isMax:
            return evaluation(board, 'r', 'b', y, x)
        else:
            return evaluation(board, 'b', 'r', y, x)
    if isMax:
        best_score = -math.inf
        moves = possible_moves(board)
        for i, j in moves:
            score = minimax(board, i, j, depth-1, not isMax, alpha, beta)
            best_score = max(score, best_score)
            alpha = max(alpha, best_score)
            if alpha>=beta:
                break
        board[y][x] = ' '
        return best_score
    else:
        best_score = math.inf
        moves = possible_moves(board)
        for i, j in moves:
            score = minimax(board, i, j, depth-1, not isMax, alpha, beta)
            best_score = min(-score, best_score)
            beta = min(beta, best_score)
            if alpha>=beta:
                break
        board[y][x] = ' '
        return best_score
Ejemplo n.º 2
0
def itml_cmc(features, train_idxs, query_idxs, camId, gallery_idxs, labels):
    N, m = features[train_idxs].shape
    # features[train_idxs][:1000, ].shape
    eigvals, eigvecs = calc_eig_pca_small(features[train_idxs].T, m, N)
    m = 50
    m_eigvecs = eigvecs[:, :m]
    avg_face = compute_avg_face(features[train_idxs].T)
    phi = features - avg_face
    m_features = np.dot(phi, m_eigvecs)


    itml = ITML_Supervised(verbose=True, num_constraints=5000, gamma=0.2)
    X = m_features[train_idxs]
    Y = labels[train_idxs]
    X_itml = itml.fit_transform(X, Y)
    M = itml.metric()
    nn_idx_mat = evaluation(
                knn, 
                features=m_features,
                gallery_idxs=gallery_idxs,
                query_idxs=query_idxs,
                camId=camId, 
                labels=labels,
                metric='mahalanobis',
                metric_params={'VI': M}
            )
    return plot_CMC(nn_idx_mat, query_idxs, labels)
Ejemplo n.º 3
0
def lda_cmc(features, train_idxs, query_idxs, camId, gallery_idxs, labels):
    N, m = features[train_idxs].shape
    # features[train_idxs][:1000, ].shape
    print("calculating PCA eigenvectors...")
    eigvals, eigvecs = calc_eig_pca_small(features[train_idxs].T, m, N)
    m = 1000
    m_eigvecs = eigvecs[:, :m]
    avg_face = compute_avg_face(features[train_idxs].T)
    phi = features - avg_face
    m_features = np.dot(phi, m_eigvecs)
    
    print("calculating LDA eigenvectors...")
    eigvals_lda, eigvecs_lda = calc_eig_lda(m_features[train_idxs].T, train_label=labels[train_idxs])
    m_eigvecs_lda = eigvecs_lda[:,:len(set(labels[train_idxs]))-1].real
    avg_face = compute_avg_face(m_features[train_idxs].T)
    phi = m_features - avg_face
    m_features_lda = np.dot(phi, m_eigvecs_lda)
    
    print("evaluating LDA performance...")
    nn_idx_mat = evaluation(
                    knn, 
                    features=m_features_lda,
                    gallery_idxs=gallery_idxs,
                    query_idxs=query_idxs,
                    camId=camId, 
                    labels=labels,
                    metric='euclidean',
                    metric_params=None
                )
    return plot_CMC(nn_idx_mat, query_idxs, labels)
Ejemplo n.º 4
0
def main():
    print("importing data...")
    data = loadmat('assets/cuhk03_new_protocol_config_labeled.mat')
    with open('assets/feature_data.json') as f:
        features = ujson.load(f)

    print("data imported")
    features = np.array(features)

    train_idxs = data['train_idx'].flatten() - 1
    query_idxs = data['query_idx'].flatten() - 1
    camId = data['camId'].flatten()
    gallery_idxs = data['gallery_idx'].flatten() - 1
    labels = data['labels'].flatten()

    N, m = features[train_idxs].shape
    # features[train_idxs][:1000, ].shape
    print("calculating PCA eigenvectors...")
    eigvals, eigvecs = calc_eig_pca_small(features[train_idxs].T, m, N)
    m = 1000
    m_eigvecs = eigvecs[:, :m]
    avg_face = compute_avg_face(features[train_idxs].T)
    phi = features - avg_face
    m_features = np.dot(phi, m_eigvecs)

    print("calculating LDA eigenvectors...")
    eigvals_lda, eigvecs_lda = calc_eig_lda(m_features[train_idxs].T,
                                            train_label=labels[train_idxs])
    m_eigvecs_lda = eigvecs_lda[:, :len(set(labels[train_idxs])) - 1].real
    avg_face = compute_avg_face(m_features[train_idxs].T)
    phi = m_features - avg_face
    m_features_lda = np.dot(phi, m_eigvecs_lda)

    print("evaluating LDA performance...")
    nn_idx_mat = evaluation(knn,
                            features=m_features_lda,
                            gallery_idxs=gallery_idxs,
                            query_idxs=query_idxs,
                            camId=camId,
                            labels=labels,
                            metric='euclidean',
                            metric_params=None)

    acc = get_all_rank_acc(nn_idx_mat, query_idxs, labels)
    print("Accuracy:")
    print(acc)

    X = m_features_lda[train_idxs]
    Y = labels[train_idxs]
    plot_3d(X, Y)

    test_set_idxs = np.append(gallery_idxs, query_idxs)
    X_test = m_features_lda[test_set_idxs]
    Y_test = labels[test_set_idxs]
    n_cluster = np.unique(Y_test).size
    nmi_kmean, acc_kmean = evaluation_k_means(X_test, n_cluster, Y_test)
    print("PCA k-means accuracy (test set):")
    print(acc_kmean)
Ejemplo n.º 5
0
def main():
    print("importing data...")
    data = loadmat('assets/cuhk03_new_protocol_config_labeled.mat')
    with open('assets/feature_data.json') as f:
        features = ujson.load(f)

    print("data imported")
    features = np.array(features)

    train_idxs = data['train_idx'].flatten() - 1
    query_idxs = data['query_idx'].flatten() - 1
    camId = data['camId'].flatten()
    gallery_idxs = data['gallery_idx'].flatten() - 1
    labels = data['labels'].flatten()

    print("evaluating baseline performance...")
    nn_idx_mat = evaluation(knn,
                            features=features,
                            gallery_idxs=gallery_idxs,
                            query_idxs=query_idxs,
                            camId=camId,
                            labels=labels,
                            metric='euclidean',
                            metric_params=None)
    acc = get_all_rank_acc(nn_idx_mat, query_idxs, labels)
    print("Accuracy:")
    print(acc)

    X_ori = features[train_idxs]
    Y_ori = labels[train_idxs]
    plot_3d(X_ori, Y_ori)
    n_cluster = np.unique(Y_ori).size
    nmi_kmean, acc_kmean = evaluation_k_means(X_ori, n_cluster, Y_ori)
    print("baseline k-means accuracy (train set):")
    print(acc_kmean)

    X_gallery = features[gallery_idxs]
    Y_gallery = labels[gallery_idxs]
    plot_3d(X_gallery, Y_gallery)

    X_query = features[query_idxs]
    Y_query = labels[query_idxs]
    plot_3d(X_query, Y_query)

    test_set_idxs = np.append(gallery_idxs, query_idxs)
    X_test = features[test_set_idxs]
    Y_test = labels[test_set_idxs]
    n_cluster = np.unique(Y_test).size
    nmi_kmean, acc_kmean = evaluation_k_means(X_test, n_cluster, Y_test)
    print("baseline k-means accuracy (test set):")
    print(acc_kmean)
Ejemplo n.º 6
0
def siamese_cmc(features, train_idxs, query_idxs, camId, gallery_idxs, labels):
    siamese = Siamese()
    siamese.load_model('3400')    
    siamese_out = siamese.test_model(input_1 = features)
    nn_idx_mat = evaluation(
                knn, 
                features=siamese_out,
                gallery_idxs=gallery_idxs,
                query_idxs=query_idxs,
                camId=camId, 
                labels=labels,
                metric='euclidean',
                metric_params=None
            )
    return plot_CMC(nn_idx_mat, query_idxs, labels)
Ejemplo n.º 7
0
def pca_cmc(features, train_idxs, query_idxs, camId, gallery_idxs, labels):
    N, m = features[train_idxs].shape
    # features[train_idxs][:1000, ].shape
    eigvals, eigvecs = calc_eig_pca_small(features[train_idxs].T, m, N)
    m = 50
    m_eigvecs = eigvecs[:, :m]
    avg_face = compute_avg_face(features[train_idxs].T)
    phi = features - avg_face
    
    m_features = np.dot(phi, m_eigvecs)
    print(m_features.shape)
    
    print("evaluating PCA performance...")
    nn_idx_mat = evaluation(
                    knn, 
                    features=m_features,
                    gallery_idxs=gallery_idxs,
                    query_idxs=query_idxs,
                    camId=camId, 
                    labels=labels,
                    metric='euclidean',
                    metric_params=None
                )
    return plot_CMC(nn_idx_mat, query_idxs, labels)
def main():
    print("importing data...")
    data = loadmat('assets/cuhk03_new_protocol_config_labeled.mat')
    with open('assets/feature_data.json') as f:
        features = ujson.load(f)

    print("data imported")
    features = np.array(features)

    train_idxs = data['train_idx'].flatten() - 1
    query_idxs = data['query_idx'].flatten() - 1
    camId = data['camId'].flatten()
    gallery_idxs = data['gallery_idx'].flatten() - 1
    labels = data['labels'].flatten()

    N, m = features[train_idxs].shape
    # features[train_idxs][:1000, ].shape
    eigvals, eigvecs = calc_eig_pca_small(features[train_idxs].T, m, N)
    m = 50
    m_eigvecs = eigvecs[:, :m]
    avg_face = compute_avg_face(features[train_idxs].T)
    phi = features - avg_face
    m_features = np.dot(phi, m_eigvecs)

    itml = ITML_Supervised(verbose=True, num_constraints=5000, gamma=0.1)
    X = m_features[train_idxs]
    Y = labels[train_idxs]
    X_itml = itml.fit_transform(X, Y)
    M = itml.metric()
    plot_3d(X_itml, Y)
    nn_idx_mat = evaluation(knn,
                            features=m_features,
                            gallery_idxs=gallery_idxs,
                            query_idxs=query_idxs,
                            camId=camId,
                            labels=labels,
                            metric='mahalanobis',
                            metric_params={'VI': M})

    acc = get_all_rank_acc(nn_idx_mat, query_idxs, labels)
    print("Accuracy:")
    print(acc)

    test_set_idxs = np.append(gallery_idxs, query_idxs)
    features_ITML = itml.transform(m_features)
    X_test = features_ITML[test_set_idxs]
    Y_test = labels[test_set_idxs]
    n_cluster = np.unique(Y_test).size
    nmi_kmean, acc_kmean = evaluation_k_means(X_test, n_cluster, Y_test)
    print("ITML k-means accuracy (test set):")
    print(acc_kmean)

    gamma = [i / 10 for i in range(1, 11)]
    X_itmls = []
    all_rank_acc_g = []
    for g in gamma:
        itml = ITML_Supervised(verbose=True, num_constraints=5000, gamma=0.2)
        X = m_features[train_idxs]
        X_itml = itml.fit_transform(X, Y)
        X_itmls.append(X_itml)
        M = itml.metric()
        nn_idx_mat = evaluation(knn,
                                features=m_features,
                                gallery_idxs=gallery_idxs,
                                query_idxs=query_idxs,
                                camId=camId,
                                labels=labels,
                                metric='mahalanobis',
                                metric_params={'VI': M})
        acc_g = get_all_rank_acc(nn_idx_mat, query_idxs, labels)
        all_rank_acc_g.append(acc_g)
    plt.plot(gamma, all_rank_acc_g)
    plt.legend(('Rank 1', 'Rank 5', 'Rank10'))
    plt.ylabel('Accuracy')
    plt.xlabel('gamma')
    print(all_rank_acc_g)
    plt.show()