Ejemplo n.º 1
0
    def get_numpy_scores_and_labels_filtered_by_labels(self, pai_labels=None):
        if not pai_labels:
            return self.get_numpy_scores(), self.get_numpy_labels()

        ids = self.scores.keys()
        annotations_from_ids = annotations.get_annotations_from_ids(ids)
        # filtered_ids = self._get_filtered_ids(
        #     annotations_from_ids, Filter(spai=Spai.GENUINE)
        # )
        scores = []
        labels = []
        for id in ids:
            annotation = [
                annotation for annotation in annotations_from_ids if annotation.id == id
            ][0]
            if annotation.categorization.get("fine_grained_pai") == 0:
                scores.append(self.scores[id])
                labels.append(0)
            elif annotation.categorization.get("fine_grained_pai") in pai_labels:
                scores.append(self.scores[id])
                labels.append(1)

        return (
            np.asarray(list(scores), dtype=np.float32),
            np.asarray(labels, dtype=np.int),
        )
Ejemplo n.º 2
0
    def filtered_by(self, filter: Filter):
        ids = self.scores.keys()
        annotations_from_ids = annotations.get_annotations_from_ids(ids)

        filtered_ids = self._get_filtered_ids(annotations_from_ids, filter)

        return {key: value for key, value in self.scores.items() if key in filtered_ids}
Ejemplo n.º 3
0
 def get_numpy_labels(self):
     ids = self.scores.keys()
     annotations_from_ids = annotations.get_annotations_from_ids(ids)
     filtered_ids = self._get_filtered_ids(
         annotations_from_ids, Filter(scenario=Scenario.GENUINE)
     )
     labels = [0 if id in filtered_ids else 1 for id in ids]
     return np.asarray(labels, dtype=np.int)
Ejemplo n.º 4
0
 def get_attacks(self):
     ids = self.scores.keys()
     annotations_from_ids = annotations.get_annotations_from_ids(ids)
     filtered_ids = self._get_filtered_ids(
         annotations_from_ids, Filter(scenario=Scenario.GENUINE)
     )
     scores = [score for id, score in self.scores.items() if id not in filtered_ids]
     return scores
Ejemplo n.º 5
0
 def get_numpy_fine_grained_pai_labels(self):
     ids = self.scores.keys()
     annotations_from_ids = annotations.get_annotations_from_ids(ids)
     fine_grained_pai_labels = [
         annotation.categorization.get("fine_grained_pai")
         for annotation in annotations_from_ids
     ]
     return np.asarray(fine_grained_pai_labels, dtype=np.int)
Ejemplo n.º 6
0
    def _get_numpy_labels_filter_by_filter(
        self,
        options: List,
        filter_provider: Callable,
        unknown_label_value: int = None,
        encode_label: bool = False,
    ):
        ids = self.scores.keys()
        annotations_from_ids = annotations.get_annotations_from_ids(ids)

        filtered_ids_by_subdivision = {}
        filtered_ids_order = {}

        for order, option in enumerate(options):
            filtered_ids_by_subdivision[option] = self._get_filtered_ids(
                annotations_from_ids, filter_provider(option)
            )
            filtered_ids_order[option] = order

        labels = []

        for id in ids:
            value = None
            for subdivision, filtered_ids in filtered_ids_by_subdivision.items():
                if id in filtered_ids:
                    value = subdivision.value
                    break
            if value is None and unknown_label_value is not None:
                value = unknown_label_value

            labels.append(value)

        if encode_label:
            le = LabelEncoder()
            labels = le.fit_transform(labels)
            le_name_mapping = dict(zip(le.classes_, le.transform(le.classes_)))
            if le_name_mapping.get(str(unknown_label_value)) is not None:
                labels = [label - 1 for label in labels]

        return np.asarray(labels, dtype=np.int)