Esempio n. 1
0
    def _fit(self, classifier: BaseClassifier,
             X_train: np.ndarray,
             y_train: np.ndarray) -> BaseClassifier:
        X_train = np.reshape(X_train, (X_train.shape[0], -1))

        classifier.fit(X_train, y_train)
        return classifier
Esempio n. 2
0
    def _fit_and_predict(self, classifier: BaseClassifier, X_train: np.ndarray,
                         y_train: np.ndarray,
                         X_test: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
        X_train = np.reshape(X_train, (X_train.shape[0], -1))
        X_test = np.reshape(X_test, (X_test.shape[0], -1))

        classifier.fit(X_train, y_train)

        y_pred = classifier.predict(X_test)
        y_pred_proba = classifier.predict_proba(X_test)

        return y_pred, y_pred_proba
    def _classify_inter_dataset(self, dataset_origin: str, dataset_target: str,
                                classifier: BaseClassifier, model: CnnModel,
                                prop: PropertyExtractor):

        origin_path = join(self.features_root_path,
                           dataset_origin, self.target_all,
                           prop.get_property_alias(), model.alias)

        target_path = join(self.features_root_path,
                           dataset_target, self.target_all,
                           prop.get_property_alias(), model.alias)

        X_train = features_utils.concatenate_features(origin_path)
        X_test = features_utils.concatenate_features(target_path)

        y_train = features_utils.concatenate_labels(origin_path)
        y_test = features_utils.concatenate_labels(target_path)

        names_train = features_utils.concatenate_names(origin_path)
        names_test = features_utils.concatenate_names(target_path)

        y_pred, y_proba = self._fit_and_predict(classifier, X_train, y_train,
                                                X_test)
        results = self._evaluate_results(y_pred, y_test, names_test)

        print('HTER: %f\nAPCER: %f\nBPCER: %f' %
              (results[0], results[1], results[2]))

        output_dir = join(self.inter_dataset_output, dataset_origin,
                          dataset_target, self.target_all,
                          prop.get_property_alias(), model.alias,
                          classifier.get_alias())

        self._save_artifacts(classifier, output_dir, y_pred, y_proba, results)
    def _classify_intra_dataset(self, dataset: str, classifier: BaseClassifier,
                                model: CnnModel, prop: PropertyExtractor):

        path_features = join(self.features_root_path, dataset, self.target_all,
                             prop.get_property_alias(), model.alias)

        output_dir = os.path.join(self.intra_dataset_output,
                                  dataset, self.target_all,
                                  prop.get_property_alias(), model.alias,
                                  classifier.get_alias())

        print('features: ', path_features)
        print('output: ', output_dir)

        if os.path.exists(output_dir):
            print('Already processed, skipping.')
            return

        X_train, y_train, X_test, y_test, names_test = self._load_features_and_targets(
            path_features)

        y_pred, y_pred_proba = self._fit_and_predict(classifier, X_train,
                                                     y_train, X_test)
        results = self._evaluate_results(y_pred, y_test, names_test)
        print('results:', results)
        self._save_artifacts(classifier, output_dir, y_test, y_pred,
                             y_pred_proba, results)
Esempio n. 5
0
    def __load_train_labels(self, dataset_name: str, property_alias: str, model: CnnModel,
                            classifier: BaseClassifier) -> np.ndarray:

        path_labels = join(self.meta_dataset_output, self.INTER_NAME, "train", "features", dataset_name, property_alias,
                           model.alias,
                           classifier.get_alias(), 'labels.npy')

        return np.load(path_labels)
    def __load_test_labels(self, dataset_name: str, property_alias: str,
                           model: CnnModel, classifier: BaseClassifier,
                           protocol: str) -> np.ndarray:

        path_labels = join(self.meta_dataset_output, protocol, "test",
                           "features", dataset_name, property_alias,
                           model.alias, classifier.get_alias(), 'labels.npy')

        return np.load(path_labels)
    def __load_test_labels_intra(self, dataset_origin: str,
                                 property_alias: str, model: CnnModel,
                                 classifier: BaseClassifier) -> np.ndarray:

        path_labels = join(self.meta_dataset_output, self.INTRA_NAME, "test",
                           "features", dataset_origin, property_alias,
                           model.alias, classifier.get_alias(), 'labels.npy')

        return np.load(path_labels)
    def __load_test_probas(self, dataset_name: str, property_alias: str,
                           model: CnnModel, classifier: BaseClassifier,
                           protocol: str) -> np.ndarray:
        """
        Used to load the probabilities predictions previously generated from the first classifier
        :param dataset_name: the dataset we're looking to load the probabilities
        :param property_alias: the property (depth, illum, saliency, etc) we are loading
        :param model: the model (ResNet50, VGG, etc)
        :param classifier: used classifier (SVC, XGB, SVM, CNN, etc)
        :param protocol: protocol used (inter or intra)
        :return:
        """
        path_probas = join(self.meta_dataset_output, protocol, "test",
                           "features",
                           dataset_name, property_alias, model.alias,
                           classifier.get_alias(), 'y_pred_proba.npy')

        probas = np.load(path_probas)
        probas = np.reshape(probas[:, 0], (probas.shape[0], -1))
        return probas
Esempio n. 9
0
 def _predict(self, classifier: BaseClassifier,
              X_train: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
     y_pred = classifier.predict(X_train)
     y_pred_proba = classifier.predict_proba(X_train)
     return y_pred, y_pred_proba