|
| 1 | +""" |
| 2 | +Dataset utils for OTE Anomaly |
| 3 | +""" |
| 4 | + |
| 5 | +# Copyright (C) 2021 Intel Corporation |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, |
| 14 | +# software distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions |
| 17 | +# and limitations under the License. |
| 18 | + |
| 19 | +from typing import Tuple |
| 20 | + |
| 21 | +from ote_sdk.entities.annotation import AnnotationSceneEntity, AnnotationSceneKind |
| 22 | +from ote_sdk.entities.dataset_item import DatasetItemEntity |
| 23 | +from ote_sdk.entities.datasets import DatasetEntity |
| 24 | +from ote_sdk.entities.resultset import ResultSetEntity |
| 25 | +from ote_sdk.entities.shapes.rectangle import Rectangle |
| 26 | + |
| 27 | + |
| 28 | +def split_local_global_dataset(dataset) -> Tuple[DatasetEntity, DatasetEntity]: |
| 29 | + """Split a dataset into globally and locally annotated items.""" |
| 30 | + globally_annotated = [] |
| 31 | + locally_annotated = [] |
| 32 | + for gt_item in dataset: |
| 33 | + |
| 34 | + annotations = gt_item.get_annotations() |
| 35 | + global_annotations = [annotation for annotation in annotations if Rectangle.is_full_box(annotation.shape)] |
| 36 | + local_annotations = [annotation for annotation in annotations if not Rectangle.is_full_box(annotation.shape)] |
| 37 | + |
| 38 | + if not any(label.is_anomalous for label in gt_item.get_shapes_labels()): |
| 39 | + # normal images get added to both datasets |
| 40 | + globally_annotated.append(gt_item) |
| 41 | + locally_annotated.append(gt_item) |
| 42 | + else: # image is abnormal |
| 43 | + globally_annotated.append( |
| 44 | + DatasetItemEntity( |
| 45 | + media=gt_item.media, |
| 46 | + annotation_scene=AnnotationSceneEntity(global_annotations, kind=AnnotationSceneKind.ANNOTATION), |
| 47 | + metadata=gt_item.metadata, |
| 48 | + subset=gt_item.subset, |
| 49 | + ignored_labels=gt_item.ignored_labels, |
| 50 | + ) |
| 51 | + ) |
| 52 | + # add locally annotated dataset items |
| 53 | + if len(local_annotations) > 0: |
| 54 | + locally_annotated.append( |
| 55 | + DatasetItemEntity( |
| 56 | + media=gt_item.media, |
| 57 | + annotation_scene=AnnotationSceneEntity(local_annotations, kind=AnnotationSceneKind.ANNOTATION), |
| 58 | + metadata=gt_item.metadata, |
| 59 | + subset=gt_item.subset, |
| 60 | + ignored_labels=gt_item.ignored_labels, |
| 61 | + ) |
| 62 | + ) |
| 63 | + global_gt_dataset = DatasetEntity(globally_annotated, purpose=dataset.purpose) |
| 64 | + local_gt_dataset = DatasetEntity(locally_annotated, purpose=dataset.purpose) |
| 65 | + return global_gt_dataset, local_gt_dataset |
| 66 | + |
| 67 | + |
| 68 | +def split_local_global_resultset(resultset) -> Tuple[ResultSetEntity, ResultSetEntity]: |
| 69 | + """Split resultset based on the type of available annotations.""" |
| 70 | + # splits the dataset |
| 71 | + globally_annotated = [] |
| 72 | + locally_annotated = [] |
| 73 | + globally_predicted = [] |
| 74 | + locally_predicted = [] |
| 75 | + for gt_item, pred_item in zip(resultset.ground_truth_dataset, resultset.prediction_dataset): |
| 76 | + |
| 77 | + annotations = gt_item.get_annotations() |
| 78 | + global_annotations = [annotation for annotation in annotations if Rectangle.is_full_box(annotation.shape)] |
| 79 | + local_annotations = [annotation for annotation in annotations if not Rectangle.is_full_box(annotation.shape)] |
| 80 | + |
| 81 | + predictions = gt_item.get_annotations() |
| 82 | + global_predictions = [predictions for predictions in predictions if Rectangle.is_full_box(predictions.shape)] |
| 83 | + local_predictions = [predictions for predictions in predictions if not Rectangle.is_full_box(predictions.shape)] |
| 84 | + |
| 85 | + if not any(label.is_anomalous for label in gt_item.get_shapes_labels()): |
| 86 | + # normal images get added to both datasets |
| 87 | + globally_annotated.append(gt_item) |
| 88 | + locally_annotated.append(gt_item) |
| 89 | + globally_predicted.append( |
| 90 | + DatasetItemEntity( |
| 91 | + media=pred_item.media, |
| 92 | + annotation_scene=AnnotationSceneEntity(global_predictions, kind=AnnotationSceneKind.PREDICTION), |
| 93 | + metadata=pred_item.metadata, |
| 94 | + subset=pred_item.subset, |
| 95 | + ignored_labels=pred_item.ignored_labels, |
| 96 | + ) |
| 97 | + ) |
| 98 | + locally_predicted.append( |
| 99 | + DatasetItemEntity( |
| 100 | + media=pred_item.media, |
| 101 | + annotation_scene=AnnotationSceneEntity(local_predictions, kind=AnnotationSceneKind.PREDICTION), |
| 102 | + metadata=pred_item.metadata, |
| 103 | + subset=pred_item.subset, |
| 104 | + ignored_labels=pred_item.ignored_labels, |
| 105 | + ) |
| 106 | + ) |
| 107 | + else: # image is abnormal |
| 108 | + globally_annotated.append( |
| 109 | + DatasetItemEntity( |
| 110 | + media=gt_item.media, |
| 111 | + annotation_scene=AnnotationSceneEntity(global_annotations, kind=AnnotationSceneKind.ANNOTATION), |
| 112 | + metadata=gt_item.metadata, |
| 113 | + subset=gt_item.subset, |
| 114 | + ignored_labels=gt_item.ignored_labels, |
| 115 | + ) |
| 116 | + ) |
| 117 | + globally_predicted.append( |
| 118 | + DatasetItemEntity( |
| 119 | + media=pred_item.media, |
| 120 | + annotation_scene=AnnotationSceneEntity(global_predictions, kind=AnnotationSceneKind.PREDICTION), |
| 121 | + metadata=pred_item.metadata, |
| 122 | + subset=pred_item.subset, |
| 123 | + ignored_labels=pred_item.ignored_labels, |
| 124 | + ) |
| 125 | + ) |
| 126 | + # add locally annotated dataset items |
| 127 | + if len(local_annotations) > 0: |
| 128 | + locally_annotated.append( |
| 129 | + DatasetItemEntity( |
| 130 | + media=gt_item.media, |
| 131 | + annotation_scene=AnnotationSceneEntity(local_annotations, kind=AnnotationSceneKind.ANNOTATION), |
| 132 | + metadata=gt_item.metadata, |
| 133 | + subset=gt_item.subset, |
| 134 | + ignored_labels=gt_item.ignored_labels, |
| 135 | + ) |
| 136 | + ) |
| 137 | + locally_predicted.append( |
| 138 | + DatasetItemEntity( |
| 139 | + media=pred_item.media, |
| 140 | + annotation_scene=AnnotationSceneEntity(local_predictions, kind=AnnotationSceneKind.PREDICTION), |
| 141 | + metadata=pred_item.metadata, |
| 142 | + subset=pred_item.subset, |
| 143 | + ignored_labels=pred_item.ignored_labels, |
| 144 | + ) |
| 145 | + ) |
| 146 | + |
| 147 | + global_resultset = ResultSetEntity( |
| 148 | + model=resultset.model, |
| 149 | + ground_truth_dataset=DatasetEntity(globally_annotated, purpose=resultset.ground_truth_dataset.purpose), |
| 150 | + prediction_dataset=DatasetEntity(globally_predicted, purpose=resultset.prediction_dataset.purpose), |
| 151 | + purpose=resultset.purpose, |
| 152 | + ) |
| 153 | + local_resultset = ResultSetEntity( |
| 154 | + model=resultset.model, |
| 155 | + ground_truth_dataset=DatasetEntity(locally_annotated, purpose=resultset.ground_truth_dataset.purpose), |
| 156 | + prediction_dataset=DatasetEntity(locally_predicted, purpose=resultset.prediction_dataset.purpose), |
| 157 | + purpose=resultset.purpose, |
| 158 | + ) |
| 159 | + |
| 160 | + return global_resultset, local_resultset |
| 161 | + |
| 162 | + |
| 163 | +def contains_anomalous_images(dataset: DatasetEntity) -> bool: |
| 164 | + """Find the number of local annotations in a resultset.""" |
| 165 | + for item in dataset: |
| 166 | + labels = item.get_shapes_labels() |
| 167 | + if any(label.is_anomalous for label in labels): |
| 168 | + return True |
| 169 | + return False |
0 commit comments