Exemple #1
0
 def test_rca(self):
   self.assertEqual(str(metric_learn.RCA()),
                    "RCA(num_dims=None, pca_comps=None, preprocessor=None)")
   self.assertEqual(str(metric_learn.RCA_Supervised()),
                    "RCA_Supervised(chunk_size=2, num_chunks=100, "
                    "num_dims=None, pca_comps=None,\n        "
                    "preprocessor=None)")
Exemple #2
0
 def test_rca(self):
     self.assertEqual(
         remove_spaces(str(metric_learn.RCA())),
         remove_spaces("RCA(n_components=None, "
                       "num_dims='deprecated', "
                       "pca_comps='deprecated', "
                       "preprocessor=None)"))
     self.assertEqual(
         remove_spaces(str(metric_learn.RCA_Supervised())),
         remove_spaces("RCA_Supervised(chunk_size=2, "
                       "n_components=None, num_chunks=100, "
                       "num_dims='deprecated', pca_comps='deprecated', "
                       "preprocessor=None, random_state=None)"))
Exemple #3
0
 def test_rca(self):
   def_kwargs = {'n_components': None, 'preprocessor': None}
   nndef_kwargs = {'n_components': 3}
   merged_kwargs = sk_repr_kwargs(def_kwargs, nndef_kwargs)
   self.assertEqual(remove_spaces(str(metric_learn.RCA(n_components=3))),
                    remove_spaces(f"RCA({merged_kwargs})"))
   def_kwargs = {'chunk_size': 2, 'n_components': None, 'num_chunks': 100,
                 'preprocessor': None, 'random_state': None}
   nndef_kwargs = {'num_chunks': 5}
   merged_kwargs = sk_repr_kwargs(def_kwargs, nndef_kwargs)
   self.assertEqual(
       remove_spaces(str(metric_learn.RCA_Supervised(num_chunks=5))),
       remove_spaces(f"RCA_Supervised({merged_kwargs})"))
Exemple #4
0
  def test_string_repr(self):
    # we don't test LMNN here because it could be python_LMNN

    self.assertEqual(str(metric_learn.Covariance()), "Covariance()")

    self.assertEqual(str(metric_learn.NCA()),
                     "NCA(learning_rate=0.01, max_iter=100, num_dims=None)")

    self.assertEqual(str(metric_learn.LFDA()),
                     "LFDA(dim=None, k=7, metric='weighted')")

    self.assertEqual(str(metric_learn.ITML()), """
ITML(convergence_threshold=0.001, gamma=1.0, max_iters=1000, verbose=False)
""".strip('\n'))
    self.assertEqual(str(metric_learn.ITML_Supervised()), """
ITML_Supervised(A0=None, bounds=None, convergence_threshold=0.001, gamma=1.0,
                max_iters=1000, num_constraints=None, num_labeled=inf,
                verbose=False)
""".strip('\n'))

    self.assertEqual(str(metric_learn.LSML()),
                     "LSML(max_iter=1000, tol=0.001, verbose=False)")
    self.assertEqual(str(metric_learn.LSML_Supervised()), """
LSML_Supervised(max_iter=1000, num_constraints=None, num_labeled=inf,
                prior=None, tol=0.001, verbose=False, weights=None)
""".strip('\n'))

    self.assertEqual(str(metric_learn.SDML()), """
SDML(balance_param=0.5, sparsity_param=0.01, use_cov=True, verbose=False)
""".strip('\n'))
    self.assertEqual(str(metric_learn.SDML_Supervised()), """
SDML_Supervised(balance_param=0.5, num_constraints=None, num_labeled=inf,
                sparsity_param=0.01, use_cov=True, verbose=False)
""".strip('\n'))

    self.assertEqual(str(metric_learn.RCA()), "RCA(dim=None)")
    self.assertEqual(str(metric_learn.RCA_Supervised()),
                     "RCA_Supervised(chunk_size=2, dim=None, num_chunks=100)")

    self.assertEqual(str(metric_learn.MLKR()), """
MLKR(A0=None, alpha=0.0001, epsilon=0.01, max_iter=1000, num_dims=None)
""".strip('\n'))
Exemple #5
0
def analyse_KNN_RCA_NCA(k=10):
    """
    Analyse and collect all the different results
    with respect to different kNNs tests

    Parameters
    ----------
    k: int
        How many neighbeours should we consider

    Returns
    -------
    results: list of lists
        Measured results which are going to be later analysed
    true_labels: list
        True test labels
    """

    all_data = load_data(False)
    training_data = all_data[0]

    training_labels = training_data[1]
    training_features = training_data[0]
    training_camIds = training_data[2]

    query_data = all_data[1]
    gallery_data = all_data[2]

    query_labels = query_data[1]
    gallery_labels = gallery_data[1]

    query_features = query_data[0]
    gallery_features = gallery_data[0]

    query_camIds = query_data[2]
    gallery_camIds = gallery_data[2]

    errors = [0] * k
    labels = [None] * k
    tops = [0] * k

    query_features = normalize(query_features, axis=1)
    training_features = normalize(training_features, axis=1)
    gallery_features = normalize(gallery_features, axis=1)

    rca = metric_learn.RCA(pca_comps=250)
    rca.fit(training_features, training_labels)

    query_features = rca.transform(query_features)
    training_features = rca.transform(training_features)
    gallery_features = rca.transform(gallery_features)

    S = NCA(max_iter=10, tol=None, verbose=True)

    training_features = S.fit_transform(training_features, training_labels)
    query_features = S.transform(query_features)
    gallery_features = S.transform(gallery_features)

    for i in tqdm(range(len(query_features))):
        query = query_features[i, :]
        query_label = query_labels[i]
        query_camId = query_camIds[i]

        selected_gallery_features, selected_gallery_labels = select_features(
            gallery_camIds, query_camId, gallery_labels, query_label,
            gallery_features)

        clf = neighbors.KNeighborsClassifier(k, metric="euclidean")
        clf.fit(selected_gallery_features, selected_gallery_labels)

        distances, predicted_neighbors = clf.kneighbors(query.reshape(1, -1),
                                                        return_distance=True)
        predicted_labels = np.array([
            selected_gallery_labels[l] for l in predicted_neighbors
        ]).flatten()
        weighted_distances = weight(distances).flatten()

        for j in range(len(predicted_labels)):
            rank = predicted_labels[:j + 1]
            rank_weights = weighted_distances[:j + 1]
            label = vote(rank, rank_weights)

            if labels[j] is None:
                labels[j] = [label]
            else:
                labels[j].append(label)
            if query_label not in rank:
                tops[j] += 1

            if label != query_label:
                errors[j] += 1

    for i in range(len(errors)):
        errors[i] /= len(query_features)
        tops[i] /= len(query_features)

    return labels, errors, tops, query_labels
Exemple #6
0
 def rca(self, train_X, train_y, test_X, dims):
     learner = ml.RCA(num_dims=dims)
     train_X = learner.fit_transform(train_X, train_y)
     test_X = learner.transform(test_X)
     return train_X, test_X
 def test_rca(self):
     self.assertEqual(remove_spaces(str(metric_learn.RCA(n_components=3))),
                      remove_spaces("RCA(n_components=3)"))
     self.assertEqual(
         remove_spaces(str(metric_learn.RCA_Supervised(num_chunks=5))),
         remove_spaces("RCA_Supervised(num_chunks=5)"))