I.MX8 Deep Learning Reference Designs - Customizing the Project - Implementing Custom Policies

From RidgeRun Developer Connection
< I.MX8 Deep Learning Reference Designs‎ | Customizing the Project
Revision as of 10:01, 30 September 2022 by Esalazar (talk | contribs) (What are the policies in this project?)
Jump to: navigation, search


NXP Partner Program Registered Vertical.jpg NXP Partner Program Horizontal.jpg


Previous: Customizing the Project/Implementing Custom Inference Listener Index Next: Customizing the Project/Implementing Custom Actions





What are the policies in this project?

The policies act as filters of the information that the inference process generates. It is up to each application to recognize the values that will be returned by the inference to be processed and validated by the policy. In this case, we use a business rule to filter the inference information receives from the GstInference pipeline.

Furthermore, some applications can rely on the use of additional filters that allow the frames coming from the inference to be analyzed, after a filtering process where the received data is highly accurate. For example, we can use a "low-pass filter" for filtering the inference frames since will allow greater precision of the resulting value of the inference as can be seen in the DeepStream Reference Designs Implementing Custom Policies.

Class Diagram

A policy is a structure where the user implements their business rules. To implement a new policy you must follow the next interface:

General policy class diagram


Operations

The interface methods that must be respected in any custom implementation are:


  • evaluate: This method changes the status of asserted value depending on the conditions that you want to realize using the information from the inference info. The internal state of the is_asserted flag has to be changed to false or true, to allow an action to be performed or not later on.
  • is_asserted: This method returns the current status of evaluating results.


Code Example

The following is a simple example of how a basic policy should be implemented. For the explanation, the RZD application was taken as a base, which is explained in greater detail in the Reference Designs section. In this case, the information will be filtered by category and by threshold level.

First of all, the user should know which data the inference_info is going to receive in the dictionary, with that information you are going to create business rules. For instance, in the APLVR application the dictionary is:

{'name': 'new-inference-string', 'arguments': [
    {'type': 'GstTinyyolov2', 'value': '(GstTinyyolov2) net'
    },
    {'type': 'gchararray', 'value': '{
            "id": 13,
            "enabled": "True",
            "bbox": {
                "x": 0,
                "y": 0,
                "width": 416,
                "height": 416
            },
            "classes": [],
            "predictions": [
                {
                    "id": 14,
                    "enabled": "True",
                    "bbox": {
                        "x": 230,
                        "y": 135,
                        "width": 70,
                        "height": 271
                    },
                    "classes": [
                        {
                            "Id": 0,
                            "Class": 14,
                            "Label": "person",
                            "Probability": "15.773438",
                            "Classes": 20
                        }
                    ],
                    "predictions": []
                },
                {
                    "id": 15,
                    "enabled": "True",
                    "bbox": {
                        "x": 366,
                        "y": 212,
                        "width": 49,
                        "height": 114
                    },
                    "classes": [
                        {
                            "Id": 1,
                            "Class": 6,
                            "Label": "car",
                            "Probability": "15.023438",
                            "Classes": 20
                        }
                    ],
                    "predictions": []
                }
            ]
        }'
    }
]
}

Below is the code for a simple implementation of a policy. It should focus on changing the state of the is_asserted flag.

# -*- coding: utf-8 -*-
"""
 Copyright (C) 2022 RidgeRun, LLC (http://www.ridgerun.com)
 All Rights Reserved.

 The contents of this software are proprietary and confidential to RidgeRun,
 LLC.  No part of this program may be photocopied, reproduced or translated
 into another programming language without prior written consent of
 RidgeRun, LLC.  The user is free to modify the source code after obtaining
 a software license from RidgeRun.  All source code changes must be provided
 back to RidgeRun without any encumbrance.
"""


class BasicPolicyError(RuntimeError):
    """Class used to represent the BasicPolicy exceptions
    It extends from RuntimeError, and covers the exceptions that may arise during the filtering process
    """


class BasicPolicy:
    def __init__(self, threshold, category, points):
        """ This class receives the inference information data and filters it to execute customized business
        rules according to threshold and categories values.

        :param threshold: threshold value that the incoming inference data must exceed
        :type threshold: float
        :param category: categories that the inference data must have
        :type category: string list
        :param points: Polygon points
        :type points: float list
        """

        self._threshold = threshold
        self._category = category
        self._points = points
        self._is_asserted = False

    def evaluate(self, inference_info):
        """This method changes the status of the asserted value depending on the proof of concept of the
           implementation in the business rules.

        :param inference_info: Structure with the categories and its threshold values to evaluate
        :type inference_info: inference_info
        """
        self._is_asserted = self.business_rule_1(inference_info)

    def is_asserted(self):
        """This method returns the current status of the evaluate result

        :return: Returns true or false depending on the method 'evaluate'
        :rtype: bool
        """
        return self._is_asserted

    def business_rule_1(self):
        """This method is a proof of concept of the implementation of a business rule

        :return: Return true depending the business rule
        :rtype: boolean
        """

        return True


Business rule

This business rule is in charge of receiving the raw inference information in some format depending on the current application. The way this business rule works is that the predictions received through the inference information are filtered according to the following conditions:

  • The label detected, must be a person.
  • The threshold
  • If the person is inside the restricted zone or not

When the conditions above are accomplished, the status of the is_asserted flag, which indicates the status of all the evaluation (filtered steps) mentioned above, is changed. The flowchart presented below exemplifies the above explained.

Flowchart


Business rule flowchart





Previous: Customizing the Project/Implementing Custom Inference Listener Index Next: Customizing the Project/Implementing Custom Actions