def test_rectangle_generate_full_box(self):
        """
        <b>Description:</b>
        Check Rectangle generate_full_box method

        <b>Input data:</b>
        Labels specified for full_box instance of Rectangle class

        <b>Expected results:</b>
        Test passes if generate_full_box method returns instance of Rectangle class with coordinates
        (x1=0.0, y1=0.0, x2=1.0, y2=1.0)

        <b>Steps</b>
        1. Check generate_full_box method for Rectangle instance with no labels specified
        2. Check generate_full_box method for Rectangle instance with labels specified
        """
        detection_label = ScoredLabel(
            LabelEntity(name="detection", domain=Domain.DETECTION)
        )
        for label_actual, label_expected in [
            (None, []),
            ([detection_label], [detection_label]),
        ]:
            full_box = Rectangle.generate_full_box(label_actual)
            assert full_box.type == ShapeType.RECTANGLE
            assert full_box.x1 == full_box.y1 == 0.0
            assert full_box.x2 == full_box.y2 == 1.0
            assert full_box._labels == label_expected
 def roi(self) -> Annotation:
     """Region Of Interest."""
     with self.__roi_lock:
         if self.__roi is None:
             requested_roi = Annotation(Rectangle.generate_full_box(),
                                        labels=[])
             self.__roi = requested_roi
         else:
             requested_roi = self.__roi
         return requested_roi
    def test_result_media_eq(self):
        """
        <b>Description:</b>
        Check ResultMediaEntity class object __eq__ method

        <b>Input data:</b>
        ResultMediaEntity class objects with specified "name", "type", "annotation_scene", "numpy", "roi"
        and "label" parameters

        <b>Expected results:</b>
        Test passes if value returned by __eq__ method is equal to expected

        <b>Steps</b>
        1. Check value returned by __eq__ method for comparing equal ResultMediaEntity objects
        2. Check value returned by __eq__ method for comparing ResultMediaEntity objects with unequal
        "name", "type", "label" and "numpy" parameters - expected equality
        3. Check value returned by __eq__ method for comparing ResultMediaEntity objects with unequal
        "annotation_scene" and "roi" parameters - expected inequality
        4. Check value returned by __eq__ method for comparing ResultMediaEntity with different type object
        """
        initialization_params = self.optional_result_media_parameters()
        result_media = ResultMediaEntity(**initialization_params)
        # Comparing equal ResultMediaEntity objects
        equal_result_media = ResultMediaEntity(**initialization_params)
        assert result_media == equal_result_media
        # Comparing ResultMediaEntity objects with unequal "name", "type", "label" and "numpy" parameters,
        # expected equality
        unequal_values = {
            "name": "Unequal name",
            "type": "Unequal type",
            "label": LabelEntity("Unequal label", Domain.CLASSIFICATION),
            "numpy": np.random.uniform(low=0.0, high=255.0, size=(1, 2, 3)),
        }
        for key in unequal_values:
            unequal_params = dict(initialization_params)
            unequal_params[key] = unequal_values.get(key)
            equal_result_media = ResultMediaEntity(**unequal_params)
            assert result_media == equal_result_media
        # Comparing ResultMediaEntity objects with unequal "annotation_scene" and "roi" parameters, expected inequality
        unequal_values = {
            "annotation_scene":
            AnnotationSceneEntity(annotations=[],
                                  kind=AnnotationSceneKind.NONE),
            "roi":
            Rectangle.generate_full_box(),
        }
        for key in unequal_values:
            unequal_params = dict(initialization_params)
            unequal_params[key] = unequal_values.get(key)
            unequal_result_media = ResultMediaEntity(**unequal_params)
            assert result_media != unequal_result_media
        # Comparing ResultMediaEntity with different type object
        assert result_media != str
def init_environment(params, model_template, number_of_images=10):
    resolution = (224, 224)
    colors = [(0, 255, 0), (0, 0, 255)]
    cls_names = ['b', 'g']
    texts = ['Blue', 'Green']
    env_labels = [
        LabelEntity(name=name,
                    domain=Domain.CLASSIFICATION,
                    is_empty=False,
                    id=ID(i)) for i, name in enumerate(cls_names)
    ]

    items = []

    for _ in range(0, number_of_images):
        for j, lbl in enumerate(env_labels):
            class_img = np.zeros((*resolution, 3), dtype=np.uint8)
            class_img[:] = colors[j]
            class_img = cv.putText(class_img, texts[j], (50, 50),
                                   cv.FONT_HERSHEY_SIMPLEX, .8 + j * .2,
                                   colors[j - 1], 2, cv.LINE_AA)

            image = Image(data=class_img)
            labels = [ScoredLabel(label=lbl, probability=1.0)]
            shapes = [Annotation(Rectangle.generate_full_box(), labels)]
            annotation_scene = AnnotationSceneEntity(
                kind=AnnotationSceneKind.ANNOTATION, annotations=shapes)
            items.append(
                DatasetItemEntity(media=image,
                                  annotation_scene=annotation_scene))

    rng = random.Random()
    rng.seed(100)
    rng.shuffle(items)
    for i, _ in enumerate(items):
        subset_region = i / number_of_images
        if subset_region >= 0.9:
            subset = Subset.TESTING
        elif subset_region >= 0.6:
            subset = Subset.VALIDATION
        else:
            subset = Subset.TRAINING
        items[i].subset = subset

    dataset = DatasetEntity(items)
    labels_schema = generate_label_schema(dataset.get_labels(),
                                          multilabel=False)
    environment = TaskEnvironment(model=None,
                                  hyper_parameters=params,
                                  label_schema=labels_schema,
                                  model_template=model_template)
    return environment, dataset
Esempio n. 5
0
 def __init__(
     self,
     name: str,
     type: str,
     annotation_scene: AnnotationSceneEntity,
     numpy: np.ndarray,
     roi: Optional[Annotation] = None,
     label: Optional[LabelEntity] = None,
 ):
     self.name = name
     self.type = type
     self.annotation_scene = annotation_scene
     self.roi = (Annotation(Rectangle.generate_full_box(), labels=[])
                 if roi is None else roi)
     self.label = label
     self.numpy = numpy
Esempio n. 6
0
    def test_dataset_item_roi(self):
        """
        <b>Description:</b>
        Check DatasetItemEntity class "roi" property

        <b>Input data:</b>
        DatasetItemEntity class object with specified "media", "annotation_scene", "roi", "metadata" and "subset"
        parameters

        <b>Expected results:</b>
        Test passes if value returned by "roi" property is equal to expected

        <b>Steps</b>
        1. Check value returned by "roi" property for DatasetItemEntity with specified "roi" parameter
        2. Check value returned by "roi" property for DatasetItemEntity with not specified "roi" parameter
        3. Check value returned by "roi" property for DatasetItemEntity with not specified "roi" parameter but one
        of annotation objects in annotation_scene is equal to full Rectangle
        """
        media = DatasetItemParameters.generate_random_image()
        annotations = DatasetItemParameters().annotations()
        annotation_scene = DatasetItemParameters().annotations_entity()
        roi = DatasetItemParameters().roi()
        metadata = DatasetItemParameters.metadata()
        # Checking "roi" property for DatasetItemEntity with specified "roi" parameter
        specified_roi_dataset_item = DatasetItemParameters().dataset_item()
        assert specified_roi_dataset_item.roi == roi
        # Checking that "roi" property is equal to full_box for DatasetItemEntity with not specified "roi" parameter
        non_specified_roi_dataset_item = DatasetItemEntity(media,
                                                           annotation_scene,
                                                           metadata=metadata)
        default_roi = non_specified_roi_dataset_item.roi.shape
        assert isinstance(default_roi, Rectangle)
        assert Rectangle.is_full_box(default_roi)
        # Checking that "roi" property will be equal to full_box for DatasetItemEntity with not specified "roi" but one
        # of Annotation objects in annotation_scene is equal to full Rectangle
        full_box_label = LabelEntity("Full-box label",
                                     Domain.DETECTION,
                                     id=ID("full_box_label"))
        full_box_annotation = Annotation(Rectangle.generate_full_box(),
                                         [ScoredLabel(full_box_label)])
        annotations.append(full_box_annotation)
        annotation_scene.annotations.append(full_box_annotation)
        full_box_label_dataset_item = DatasetItemEntity(media,
                                                        annotation_scene,
                                                        metadata=metadata)
        assert full_box_label_dataset_item.roi is full_box_annotation
    def convert_to_annotation(
            self, predictions: np.ndarray,
            metadata: Dict[str, Any]) -> AnnotationSceneEntity:
        pred_score = predictions.reshape(-1).max()
        pred_label = pred_score >= metadata.get("threshold", 20)
        assigned_label = self.anomalous_label if pred_label else self.normal_label

        annotations = [
            Annotation(
                Rectangle.generate_full_box(),
                labels=[
                    ScoredLabel(assigned_label, probability=float(pred_score))
                ],
            )
        ]
        return AnnotationSceneEntity(kind=AnnotationSceneKind.PREDICTION,
                                     annotations=annotations)
    def convert_to_annotation(
            self,
            predictions: List[Tuple[int, float]],
            metadata: Optional[Dict] = None) -> AnnotationSceneEntity:
        labels = []
        for index, score in predictions:
            labels.append(ScoredLabel(self.labels[index], float(score)))

        if not labels and self.empty_label:
            labels = [ScoredLabel(self.empty_label, probability=1.0)]
        elif self.hierarchical:
            labels.extend(
                get_ancestors_by_prediction(self.label_schema, labels[0]))

        annotations = [
            Annotation(Rectangle.generate_full_box(), labels=labels)
        ]
        return AnnotationSceneEntity(kind=AnnotationSceneKind.PREDICTION,
                                     annotations=annotations)
Esempio n. 9
0
 def fully_covering_rectangle() -> Rectangle:
     return Rectangle.generate_full_box()
Esempio n. 10
0
    def __init__(self,
                 train_ann_file=None,
                 train_data_root=None,
                 val_ann_file=None,
                 val_data_root=None,
                 test_ann_file=None,
                 test_data_root=None,
                 **kwargs):
        self.data_roots = {}
        self.ann_files = {}
        self.multilabel = False
        if train_data_root:
            self.data_roots[Subset.TRAINING] = train_data_root
            self.ann_files[Subset.TRAINING] = train_ann_file
        if val_data_root:
            self.data_roots[Subset.VALIDATION] = val_data_root
            self.ann_files[Subset.VALIDATION] = val_ann_file
        if test_data_root:
            self.data_roots[Subset.TESTING] = test_data_root
            self.ann_files[Subset.TESTING] = test_ann_file
        self.annotations = {}
        for k, v in self.data_roots.items():
            if v:
                self.data_roots[k] = osp.abspath(v)
                if self.ann_files[k] and '.json' in self.ann_files[
                        k] and osp.isfile(self.ann_files[k]):
                    self.data_roots[k] = osp.dirname(self.ann_files[k])
                    self.multilabel = True
                    self.annotations[k] = self._load_annotation_multilabel(
                        self.ann_files[k], self.data_roots[k])
                else:
                    self.annotations[k] = self._load_annotation(
                        self.data_roots[k])
                    assert not self.multilabel

        self.label_map = None
        self.labels = None
        self._set_labels_obtained_from_annotation()
        self.project_labels = [
            LabelEntity(name=name,
                        domain=Domain.CLASSIFICATION,
                        is_empty=False,
                        id=ID(i)) for i, name in enumerate(self.labels)
        ]

        dataset_items = []
        for subset, subset_data in self.annotations.items():
            for data_info in subset_data[0]:
                image = Image(file_path=data_info[0])
                labels = [
                    ScoredLabel(
                        label=self._label_name_to_project_label(label_name),
                        probability=1.0) for label_name in data_info[1]
                ]
                shapes = [Annotation(Rectangle.generate_full_box(), labels)]
                annotation_scene = AnnotationSceneEntity(
                    kind=AnnotationSceneKind.ANNOTATION, annotations=shapes)
                dataset_item = DatasetItemEntity(image,
                                                 annotation_scene,
                                                 subset=subset)
                dataset_items.append(dataset_item)

        super().__init__(items=dataset_items, **kwargs)