Esempio n. 1
0
    def test_iris(self):

        # Generate full set of constraints for comparison with reference implementation
        mask = (self.iris_labels[None] == self.iris_labels[:, None])
        a, b = np.nonzero(np.triu(mask, k=1))
        c, d = np.nonzero(np.triu(~mask, k=1))

        # Full metric
        mmc = MMC(convergence_threshold=0.01)
        mmc.fit(self.iris_points, [a, b, c, d])
        expected = [[+0.00046504, +0.00083371, -0.00111959, -0.00165265],
                    [+0.00083371, +0.00149466, -0.00200719, -0.00296284],
                    [-0.00111959, -0.00200719, +0.00269546, +0.00397881],
                    [-0.00165265, -0.00296284, +0.00397881, +0.00587320]]
        assert_array_almost_equal(expected, mmc.metric(), decimal=6)

        # Diagonal metric
        mmc = MMC(diagonal=True)
        mmc.fit(self.iris_points, [a, b, c, d])
        expected = [0, 0, 1.21045968, 1.22552608]
        assert_array_almost_equal(np.diag(expected), mmc.metric(), decimal=6)

        # Supervised Full
        mmc = MMC_Supervised()
        mmc.fit(self.iris_points, self.iris_labels)
        csep = class_separation(mmc.transform(), self.iris_labels)
        self.assertLess(csep, 0.15)

        # Supervised Diagonal
        mmc = MMC_Supervised(diagonal=True)
        mmc.fit(self.iris_points, self.iris_labels)
        csep = class_separation(mmc.transform(), self.iris_labels)
        self.assertLess(csep, 0.2)
Esempio n. 2
0
    def test_iris(self):

        # Generate full set of constraints for comparison with reference implementation
        n = self.iris_points.shape[0]
        mask = (self.iris_labels[None] == self.iris_labels[:, None])
        a, b = np.nonzero(np.triu(mask, k=1))
        c, d = np.nonzero(np.triu(~mask, k=1))

        # Full metric
        mmc = MMC(convergence_threshold=0.01)
        mmc.fit(self.iris_points, [a, b, c, d])
        expected = [[0.000514, 0.000868, -0.001195, -0.001703],
                    [0.000868, 0.001468, -0.002021, -0.002879],
                    [-0.001195, -0.002021, 0.002782, 0.003964],
                    [-0.001703, -0.002879, 0.003964, 0.005648]]
        assert_array_almost_equal(expected, mmc.metric(), decimal=6)

        # Diagonal metric
        mmc = MMC(diagonal=True)
        mmc.fit(self.iris_points, [a, b, c, d])
        expected = [0, 0, 1.210220, 1.228596]

        assert_array_almost_equal(np.diag(expected), mmc.metric(), decimal=6)

        # Supervised Full
        mmc = MMC_Supervised()
        mmc.fit(self.iris_points, self.iris_labels)
        csep = class_separation(mmc.transform(), self.iris_labels)
        self.assertLess(csep, 0.15)

        # Supervised Diagonal
        mmc = MMC_Supervised(diagonal=True)
        mmc.fit(self.iris_points, self.iris_labels)
        csep = class_separation(mmc.transform(), self.iris_labels)
        self.assertLess(csep, 0.2)
  def test_mmc_supervised(self):
    seed = np.random.RandomState(1234)
    mmc = MMC_Supervised(n_constraints=200, random_state=seed)
    mmc.fit(self.X, self.y)
    res_1 = mmc.transform(self.X)

    seed = np.random.RandomState(1234)
    mmc = MMC_Supervised(n_constraints=200, random_state=seed)
    res_2 = mmc.fit_transform(self.X, self.y)

    assert_array_almost_equal(res_1, res_2)
def runMMC(X_train, X_test, y_train, y_test):
    transformer = MMC_Supervised(num_constraints=200, diagonal=True, verbose=True)
    transformer.fit(X_train, y_train)
    X_train_proj = transformer.transform(X_train)
    X_test_proj = transformer.transform(X_test)
    np.save('X_train_MMC', X_train_proj)
    np.save('X_test_MMC', X_test_proj)
    return X_train_proj, X_test_proj
def get_model(config):
    if config is None:
        return None
    if 'model_kwargs' not in config:
        model_kwargs = dict()
    else:
        model_kwargs = config['model_kwargs']
    if config['model'] == 'svm':
        model = svm.SVC(**model_kwargs)
    elif config['model'] == 'rdf':
        model = RandomForestClassifier(**model_kwargs)
    elif config['model'] == 'adaboost':
        if 'sub_model' in config:
            base_estimator = get_model(config['sub_model'])
        else:
            base_estimator = None
        model = AdaBoostClassifier(base_estimator=base_estimator, **model_kwargs)
    elif config['model'] == 'gradient_boost':
        model = GradientBoostingClassifier(**model_kwargs)
    elif config['model'] == 'gaussion_bayes':
        model = naive_bayes.GaussianNB(**model_kwargs)
    elif config['model'] == 'mlp':
        model = MLPClassifier(**model_kwargs)
    elif config['model'] == 'k_neighbors':
        model = KNeighborsClassifier(**model_kwargs)
    elif config['model'] == 'decision_tree':
        model = DecisionTreeClassifier(**model_kwargs)
    elif config['model'] == 'voting':
        sub_models = []
        for sub_model in config['sub_model']:
            sub_models.append([sub_model['model_name'], get_model(sub_model)])
        model = VotingClassifier(sub_models, **model_kwargs)
    elif config['model'] == 'stacking':
        final_estimator = get_model(config.get('final_model', None))
        sub_models = []
        for sub_model in config['sub_model']:
            sub_models.append((sub_model['model_name'], get_model(sub_model)))
        model = StackingClassifier(estimators=sub_models, final_estimator=final_estimator, **model_kwargs)
    elif config['model'] == 'bagging':
        base_estimator = get_model(config.get('sub_model', None))
        model = BaggingClassifier(base_estimator=base_estimator, **model_kwargs)
    elif config['model'] in ['lfda', 'lmnn', 'mmc']:
        if config['model'] == 'lfda':
            metric_learner = LFDA(**model_kwargs)
        elif config['model'] == 'lmnn':
            metric_learner = LMNN(**model_kwargs)
        elif config['model'] == 'mmc':
            metric_learner = MMC_Supervised(**model_kwargs)
        else:
            raise AttributeError
        if 'final_model' in config:
            final_model = get_model(config['final_model'])
        else:
            final_model = KNeighborsClassifier()
        model = Pipeline([('metric', metric_learner), ('final', final_model)])
    else:
        raise AttributeError('unrecognized model %s' % config['model'])
    return model
 def test_deprecation(self):
     # test that the right deprecation message is thrown.
     # TODO: remove in v.0.5
     X = np.array([[0, 0], [0, 1], [2, 0], [2, 1]])
     y = np.array([1, 0, 1, 0])
     mmc_supervised = MMC_Supervised(num_labeled=np.inf)
     msg = ('"num_labeled" parameter is not used.'
            ' It has been deprecated in version 0.5.0 and will be'
            'removed in 0.6.0')
     assert_warns_message(DeprecationWarning, msg, mmc_supervised.fit, X, y)
Esempio n. 7
0
    def test_iris(self):

        # Generate full set of constraints for comparison with reference
        # implementation
        mask = self.iris_labels[None] == self.iris_labels[:, None]
        a, b = np.nonzero(np.triu(mask, k=1))
        c, d = np.nonzero(np.triu(~mask, k=1))

        # Full metric
        n_features = self.iris_points.shape[1]
        mmc = MMC(convergence_threshold=0.01, init=np.eye(n_features) / 10)
        mmc.fit(*wrap_pairs(self.iris_points, [a, b, c, d]))
        expected = [[+0.000514, +0.000868, -0.001195, -0.001703],
                    [+0.000868, +0.001468, -0.002021, -0.002879],
                    [-0.001195, -0.002021, +0.002782, +0.003964],
                    [-0.001703, -0.002879, +0.003964, +0.005648]]
        assert_array_almost_equal(expected,
                                  mmc.get_mahalanobis_matrix(),
                                  decimal=6)

        # Diagonal metric
        mmc = MMC(diagonal=True)
        mmc.fit(*wrap_pairs(self.iris_points, [a, b, c, d]))
        expected = [0, 0, 1.210220, 1.228596]
        assert_array_almost_equal(np.diag(expected),
                                  mmc.get_mahalanobis_matrix(),
                                  decimal=6)

        # Supervised Full
        mmc = MMC_Supervised()
        mmc.fit(self.iris_points, self.iris_labels)
        csep = class_separation(mmc.transform(self.iris_points),
                                self.iris_labels)
        self.assertLess(csep, 0.15)

        # Supervised Diagonal
        mmc = MMC_Supervised(diagonal=True)
        mmc.fit(self.iris_points, self.iris_labels)
        csep = class_separation(mmc.transform(self.iris_points),
                                self.iris_labels)
        self.assertLess(csep, 0.2)
Esempio n. 8
0
def get_dist_func(
    data: Array[np.float64], target: Array[np.float64]
) -> Callable[[Callable[[np.float64, np.float64], np.float64], np.int, np.int],
              np.float64]:
    """
    Get function that returns distances between examples in learned space.

    Args:
        data : Array[np.float64] - training data_trans
        target : int - target variable values (classes of training examples)
    Returns:
        Callable[[Callable[[np.float64, np.float64], np.float64], np.int, np.int], np.float64] -- higher
        order function that takes a matric function and returns a function that takes two indices of examples
        and returns distance between examples in learned metric space.
    """

    # Get transformed data.
    data_trans: Array[np.float64] = MMC_Supervised().fit_transform(
        StandardScaler().fit_transform(data), target)

    # Computing distance:
    def dist_func_res(metric: Callable[[np.float64, np.float64], np.float64],
                      i1: np.int, i2: np.int) -> np.float64:
        """ 
        distance function that takes indices of examples in training set and returns distance
        in learned space using specified distance metric.

        Args:
            i1 : int - index of first training example
            i2 : int - index of second training example
        Returns:
            np.float64 - distance in learned metric space using specified metric
                    between specified training examples.
        """

        # Compute distance in learned metric space using specified metric.
        return metric(data_trans[i1, :], data_trans[i2, :])

    return dist_func_res  # Return distance function.
Esempio n. 9
0
 def test_mmc(self):
     check_estimator(MMC_Supervised())
Esempio n. 10
0
pairs_learners = [
    (ITML(), build_pairs),
    (MMC(max_iter=2), build_pairs),  # max_iter=2 for faster
    (SDML(), build_pairs),
]
ids_pairs_learners = list(
    map(lambda x: x.__class__.__name__,
        [learner for (learner, _) in pairs_learners]))

classifiers = [(Covariance(), build_classification),
               (LFDA(), build_classification), (LMNN(), build_classification),
               (NCA(), build_classification), (RCA(), build_classification),
               (ITML_Supervised(max_iter=5), build_classification),
               (LSML_Supervised(), build_classification),
               (MMC_Supervised(max_iter=5), build_classification),
               (RCA_Supervised(num_chunks=10), build_classification),
               (SDML_Supervised(), build_classification)]
ids_classifiers = list(
    map(lambda x: x.__class__.__name__,
        [learner for (learner, _) in classifiers]))

regressors = [(MLKR(), build_regression)]
ids_regressors = list(
    map(lambda x: x.__class__.__name__,
        [learner for (learner, _) in regressors]))

WeaklySupervisedClasses = (_PairsClassifierMixin, _QuadrupletsClassifierMixin)

tuples_learners = pairs_learners + quadruplets_learners
ids_tuples_learners = ids_pairs_learners + ids_quadruplets_learners
# In[16]:


from metric_learn import MMC_Supervised
from sklearn.decomposition import PCA

#Mahalanobis - learnt - reduced set


pca = PCA(n_components=150)
X_train_pca = pca.fit_transform(X_train)
X_query_pca = pca.transform(X_query)
X_gallery_pca = pca.transform(X_gallery)

mmc = MMC_Supervised(max_iter=50)
mmc.fit(X_train_pca[0:150], y_train[0:150])



M = mmc.metric()

print ('Metric learnt')


rank_accuracies, mAP = evaluate_metric(X_query_pca, camId_query, y_query,
                                       X_gallery_pca, camId_gallery, y_gallery,
                                       metric ='mahalanobis',
                                       parameters = M)

rank_accuracies_l_2.append(rank_accuracies)
# bar.finish()
# end_time = time.time()
# print("Accuracy for Simple Nearest Neighbour @rank 1 : ", "{:.4%}".format(rank_one_score / len(query_labels)))
# print("Accuracy for Simple Nearest Neighbour @rank 5 : ", "{:.4%}".format(rank_five_score / len(query_labels)))
# print("Accuracy for Simple Nearest Neighbour @rank 10 : ", "{:.4%}".format(rank_ten_score / len(query_labels)))
#
# print("Computation Time: %s seconds" % (end_time - start_time))

# PCA-MMC
print("-----PCA_MMC-----")
pca = PCA(original_train_features,
          original_train_labels,
          M=500,
          low_dimension=False)
pca.fit()
mmc = MMC_Supervised(max_iter=20, convergence_threshold=1e-5)
mmc_metric = mmc.fit(pca.train_sample_projection, original_train_labels)
transformed_features = mmc_metric.transform(features)
transformed_query_features = transformed_features[query_idxs - 1]

n = 10
start_time = time.time()
rank_one_score = 0
rank_five_score = 0
rank_ten_score = 0
bar.start()
for k in range(len(query_features)):
    bar.update(k + 1)
    feature_vector = transformed_query_features[k]
    gallery_vectors = transformed_features[gallery_data_idx[k] - 1]
    gallery_labels = labels[gallery_data_idx[k] - 1]
                       convergence_threshold=1e-5,
                       num_constraints=500,
                       verbose=True)
start_time = time.time()
itml.fit(pca.train_sample_projection, original_train_labels)
end_time = time.time()
print("Learning time: %s" % (end_time - start_time))
transformed_query_features = itml.transform(pca_query_features)
transformed_gallery_features = itml.transform(pca_gallery_features)
compute_k_mean(num_of_clusters, transformed_query_features,
               transformed_gallery_features, gallery_labels)

# Compute PCA_MMC (Mahalanobis Metric Learning for Clustering)
print("\n-----PCA_MMC-----")
mmc = MMC_Supervised(max_iter=20,
                     convergence_threshold=1e-5,
                     num_constraints=500,
                     verbose=True)
start_time = time.time()
mmc.fit(pca.train_sample_projection, original_train_labels)
end_time = time.time()
print("Learning time: %s" % (end_time - start_time))
transformed_query_features = mmc.transform(pca_query_features)
transformed_gallery_features = mmc.transform(pca_gallery_features)
compute_k_mean(num_of_clusters, transformed_query_features,
               transformed_gallery_features, gallery_labels)

print("\n-----MMC diagonal-----")
mmc = MMC_Supervised(max_iter=20,
                     convergence_threshold=1e-5,
                     num_constraints=500,
                     diagonal=True,
Esempio n. 14
0
 def test_mmc_supervised(self):
   mmc = MMC_Supervised(num_constraints=200)
   mmc.fit(self.X, self.y)
   L = mmc.transformer()
   assert_array_almost_equal(L.T.dot(L), mmc.metric())