Electronic device and method for artificial intelligence detection

By initializing multiple artificial intelligence detection instances of mutex locks in the background component and using the mutex access component to manage status, the problem of prolonged initialization and resource contention of the AI ​​algorithm in the HTTP interface is solved, improving efficiency and stability.

CN111831463BActive Publication Date: 2024-09-06PING AN TECH (SHENZHEN) CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202010687231.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-07-16
Publication Date
2024-09-06
Estimated Expiration
2040-07-16

AI Technical Summary

Technical Problem

The service time of existing AI algorithms on the HTTP interface is prolonged because each run requires initialization of the model and configuration, resulting in low efficiency and high failure rate. Especially when multiple HTTP interfaces call the same instance at the same time, resource contention is serious.

Method used

By initializing multiple artificial intelligence detection instances with mutex locks in the background component, and using the mutex access component to obtain the instance status, we ensure that each HTTP interface has an independent instance and is called in sequence to avoid resource contention.

Benefits of technology

The sequential call arrangement mechanism of artificial intelligence detection instances is implemented, which improves work efficiency, reduces the failure rate, and avoids chaotic calls.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN111831463B
    Figure CN111831463B
Patent Text Reader

Abstract

The present application discloses an electronic device and method for implementing artificial intelligence detection. The device includes: a processor for controlling the components in the device; a background component for initializing an interactive interface after startup, initializing an artificial intelligence detection model, and generating multiple artificial intelligence detection instances with mutually exclusive locks; when the interactive interface receives a call request instruction from a foreground component, in response to the call request instruction, obtains the state of the mutually exclusive lock and determines whether the artificial intelligence detection instance is currently available; if available, accesses the artificial intelligence detection instance through the interactive interface; the artificial intelligence detection instance performs calculations and feeds back the calculation results to the foreground component through the interactive interface. The device of the present application can implement a sequential call arrangement mechanism for artificial intelligence detection instances, ensure that multiple interactive interfaces call artificial intelligence detection instances in order, and solve the problem of resource contention when multiple interactive interfaces call the same artificial intelligence detection instance at the same time.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to an electronic device and method for implementing artificial intelligence detection. Background Art

[0002] At present, AI (artificial intelligence) algorithms have been widely used in image recognition, speech recognition, text analysis, search engine ranking, recommendation system ranking, etc. With the emergence and popularity of various deep learning frameworks, more and more companies will choose to gradually apply and engineer their own AI algorithms, but different deep learning frameworks have different usage methods. For example, Google's open source TensorFlow comes with TFServer components, while PyTorch, MXNET and other frameworks are more targeted at algorithm researchers for research and development. This means that if you want to engineer some AI applications, you have to consider many compatibility factors. The most common solution for applying and engineering AI functions in the industry is to package them into web application services for HTTP requests and provide HTTP interface access to the outside world.

[0003] However, AI algorithms are different from ordinary algorithms. Generally, AI algorithms rely on their own models and initialization configuration files. This means that each time the AI ​​algorithm is run, there will be a long initialization phase to load the AI ​​model and configuration onto the GPU computing device. This means that each time an HTTP request is received, the initialization work has to be re-executed, thereby increasing the service delay of the HTTP interface, reducing work efficiency, and increasing the occurrence of failures. Summary of the invention

[0004] The purpose of this application is to provide an electronic device and method for implementing artificial intelligence detection. In order to have a basic understanding of some aspects of the disclosed embodiments, a simple summary is given below. This summary is not a general review, nor is it intended to identify key / important components or describe the scope of protection of these embodiments. Its only purpose is to present some concepts in a simple form as a preface to the detailed description that follows.

[0005] According to one aspect of an embodiment of the present application, there is provided an electronic device for implementing artificial intelligence detection, including:

[0006] a processor for controlling components in the device;

[0007] The background component is used to initialize the interactive interface and the artificial intelligence detection model after startup, and generate multiple artificial intelligence detection instances with mutex locks;

[0008] The background component is also used for, when the interactive interface receives a call request instruction from the foreground component, in response to the call request instruction, obtaining the state of the mutex lock corresponding to the artificial intelligence detection instance of the interactive interface, and judging whether the artificial intelligence detection instance corresponding to the interactive interface is currently available according to the state of the mutex lock;

[0009] The background component is further used to access the artificial intelligence detection instance corresponding to the interactive interface through the interactive interface and call the artificial intelligence detection instance if the artificial intelligence detection instance is currently available;

[0010] The backend component is also used for the artificial intelligence detection instance to perform calculations according to the call request instruction, and to feed back the calculation results to the frontend component through the interactive interface;

[0011] The front-end component is used to receive the call request instruction from the external client and send it to the back-end component; and receive the feedback from the back-end component and send it to the client;

[0012] A memory, coupled to the processor, is used to store the artificial intelligence detection model and the artificial intelligence detection instance.

[0013] Furthermore, before the artificial intelligence detection instance performs operations according to the call request instruction, the background component is also used to implement:

[0014] If the artificial intelligence detection instance is currently unavailable, the mutual exclusion access component repeatedly obtains the state of the mutual exclusion lock of the artificial intelligence detection instance corresponding to the interaction interface, and determines whether the artificial intelligence detection instance corresponding to the interaction interface is currently available according to the state of the mutual exclusion lock, until the artificial intelligence detection instance corresponding to the interaction interface is currently available.

[0015] Further, the acquiring the state of the mutex lock of the artificial intelligence detection instance corresponding to the interactive interface, and judging whether the artificial intelligence detection instance corresponding to the interactive interface is currently available according to the state of the mutex lock, comprises:

[0016] The instance name of the artificial intelligence detection instance itself is used as a key value to determine the state of the mutual exclusion lock of the artificial intelligence detection instance, and whether the artificial intelligence detection instance corresponding to the interactive interface is currently available is determined according to the state of the mutual exclusion lock.

[0017] Further, the acquiring the state of the mutex lock of the artificial intelligence detection instance corresponding to the interactive interface, and judging whether the artificial intelligence detection instance corresponding to the interactive interface is currently available according to the state of the mutex lock, comprises:

[0018] Generate an identifier for the call request instruction, set a timeout for preempting the artificial intelligence detection instance, enter the loop branch of the preemption logic, continue to try to preempt by judging whether the current time exceeds the timeout, and try to perform an assignment operation with the name of the artificial intelligence detection instance as the key value and the identifier as the value. If the assignment fails, it proves that the artificial intelligence detection instance is currently unavailable; otherwise, the artificial intelligence detection instance is currently available.

[0019] Furthermore, the background component further includes a mutually exclusive access component, which is used to obtain the state of the mutual exclusion lock of the artificial intelligence detection instance corresponding to the interactive interface, and determine whether the artificial intelligence detection instance corresponding to the interactive interface is currently available according to the state of the mutual exclusion lock, and the mutually exclusive access component is a remote dictionary service.

[0020] According to another aspect of an embodiment of the present application, a method for implementing artificial intelligence detection is provided, comprising:

[0021] After the background component is started, the interactive interface is initialized, the artificial intelligence detection model is initialized, and multiple artificial intelligence detection instances with mutex locks are generated;

[0022] When the interactive interface receives a call request instruction from the foreground component, in response to the call request instruction, obtains the state of the mutex lock corresponding to the artificial intelligence detection instance of the interactive interface, and determines whether the artificial intelligence detection instance corresponding to the interactive interface is currently available according to the state of the mutex lock;

[0023] If the artificial intelligence detection instance is currently available, accessing the artificial intelligence detection instance corresponding to the interactive interface through the interactive interface, and calling the artificial intelligence detection instance;

[0024] The artificial intelligence detection instance performs calculations according to the call request instruction, and feeds back the calculation results to the foreground component through the interactive interface;

[0025] The front-end component receives feedback from the back-end component and sends it to the client.

[0026] Furthermore, before the artificial intelligence detection instance performs operation according to the call request instruction, the method further includes:

[0027] If the artificial intelligence detection instance is currently unavailable, repeatedly obtain the state of the mutex lock of the artificial intelligence detection instance corresponding to the interactive interface, and determine whether the artificial intelligence detection instance corresponding to the interactive interface is currently available according to the state of the mutex lock, until the artificial intelligence detection instance corresponding to the interactive interface is currently available.

[0028] Further, the acquiring the state of the mutex lock of the artificial intelligence detection instance corresponding to the interactive interface, and judging whether the artificial intelligence detection instance corresponding to the interactive interface is currently available according to the state of the mutex lock, comprises:

[0029] The instance name of the artificial intelligence detection instance itself is used as a key value to determine the state of the mutual exclusion lock of the artificial intelligence detection instance, and whether the artificial intelligence detection instance corresponding to the interactive interface is currently available is determined according to the state of the mutual exclusion lock.

[0030] Further, the acquiring the state of the mutex lock of the artificial intelligence detection instance corresponding to the interactive interface, and judging whether the artificial intelligence detection instance corresponding to the interactive interface is currently available according to the state of the mutex lock, comprises:

[0031] Generate an identifier for the call request instruction, set a timeout for preempting the artificial intelligence detection instance, enter the loop branch of the preemption logic, continue to try to preempt by judging whether the current time exceeds the timeout, and try to perform an assignment operation with the name of the artificial intelligence detection instance as the key value and the identifier as the value through the mutually exclusive access component. If the assignment fails, it proves that the artificial intelligence detection instance is currently unavailable; otherwise, the artificial intelligence detection instance is currently available.

[0032] According to another aspect of an embodiment of the present application, a computer-readable storage medium is provided, on which a computer program is stored, and the program is executed by a processor to implement the above-mentioned method for artificial intelligence detection.

[0033] The technical solution provided by one aspect of the embodiments of the present application may have the following beneficial effects:

[0034] The electronic device for implementing artificial intelligence detection provided by the embodiment of the present application can ensure that each HTTP interface has a corresponding, independent and exclusive artificial intelligence detection instance, and at the same time, in combination with a mutually exclusive access component to obtain information on whether the artificial intelligence detection instance is currently available, can implement a sequential calling arrangement mechanism for the artificial intelligence detection instance, and can ensure that multiple HTTP interfaces call the corresponding artificial intelligence detection instances in order, thereby solving the problem of resource contention that occurs when multiple HTTP interfaces call the same artificial intelligence detection instance at the same time, improving work efficiency, avoiding calling confusion, and reducing the occurrence of failures.

[0035] Other features and advantages of the present application will be described in the subsequent description, and part of them will become apparent from the description, or some of them can be inferred or determined without doubt from the description, or understood by practicing the embodiments of the present application. The purpose and other advantages of the present application can be realized and obtained by the structures particularly pointed out in the written description, claims, and drawings. BRIEF DESCRIPTION OF THE DRAWINGS

[0036] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments recorded in the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0037] Figure 1 A diagram showing a connection relationship between a processor and a memory of an electronic device for implementing artificial intelligence detection according to an embodiment of the present application is shown;

[0038] Figure 2 A diagram showing the connection relationship between the background components and the foreground components of an embodiment of the present application is shown;

[0039] Figure 3 A flow chart of a method for implementing artificial intelligence detection according to an embodiment of the present application is shown. DETAILED DESCRIPTION

[0040] In order to make the purpose, technical solutions and advantages of the present application clearer, the present application is further described below in conjunction with the accompanying drawings and specific embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not intended to limit the present application. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in the field without making creative work are within the scope of protection of the present application.

[0041] It will be understood by those skilled in the art that, unless otherwise defined, all terms (including technical and scientific terms) used herein have the same meaning as those generally understood by those skilled in the art to which this application belongs. It should also be understood that terms such as those defined in general dictionaries should be understood to have meanings consistent with those in the context of the prior art, and will not be interpreted with idealized or overly formal meanings unless specifically defined as here.

[0042] In the traditional background, multiple HTTP interfaces share and call the same AI detection instance, such as the client connection instance of the MySQL database and the Redis client connection instance, which are all called by the entire background in the form of shared variables to realize the functions of the corresponding services. However, since the operation mode of the AI ​​detection model is different from that of conventional programs and the carrier is on the GPU device, if a single AI detection instance is called globally according to the conventional plan, that is, due to the lack of a call arrangement mechanism for the AI ​​detection instance when multiple HTTP interfaces call the AI ​​detection instance, it will cause multiple HTTP interfaces to compete for the same AI detection instance, resulting in competition for GPU video memory resources, and then the detection operation will be unstable, stuck, or even crashed.

[0043] like Figure 1 and Figure 2 As shown, one embodiment of the present application provides an electronic device for implementing artificial intelligence detection, including a processor and a memory;

[0044] a processor for controlling components in the device;

[0045] The background component is used to initialize the interactive interface and the artificial intelligence detection model after startup, and generate multiple artificial intelligence detection instances with mutex locks; the interactive interface in this embodiment is an HTTP interface;

[0046] The backend component is also used for, when the HTTP interface receives a call request instruction from the foreground component, in response to the call request instruction, obtaining the state of the mutex lock of the artificial intelligence detection instance corresponding to the HTTP interface, and judging whether the artificial intelligence detection instance corresponding to the HTTP interface is currently available according to the state of the mutex lock;

[0047] The backend component is further used to access the artificial intelligence detection instance corresponding to the HTTP interface through the HTTP interface if the artificial intelligence detection instance is currently available, and call the artificial intelligence detection instance;

[0048] The backend component is also used for the artificial intelligence detection instance to perform calculations according to the call request instruction, and to feed back the calculation results to the frontend component through the HTTP interface;

[0049] The front-end component is used to receive the call request instruction from the external client and send it to the back-end component; and receive the feedback from the back-end component and send it to the client;

[0050] A memory is coupled to the processor, and is used to store the artificial intelligence detection model and the artificial intelligence detection instance.

[0051] The background component further includes a mutually exclusive access component, which is used to obtain the state of the mutual exclusion lock of the artificial intelligence detection instance corresponding to the HTTP interface, and determine whether the artificial intelligence detection instance corresponding to the HTTP interface is currently available according to the state of the mutual exclusion lock. The mutually exclusive access component is a remote dictionary service.

[0052] like Figure 2 As shown, the background component is connected to the foreground component, and the foreground component is connected to the HTTP interface. For example, the foreground component may include a face-scanning attendance machine camera used for clocking in and out of the company. After the camera obtains the facial image, it sends a request to the HTTP interface to call an artificial intelligence detection instance.

[0053] In some embodiments, before the artificial intelligence detection instance performs operations according to the call request instruction, the background component is further used to implement:

[0054] If the artificial intelligence detection instance is currently unavailable, the mutex access component repeatedly obtains the state of the mutex lock of the artificial intelligence detection instance corresponding to the HTTP interface, and determines whether the artificial intelligence detection instance corresponding to the HTTP interface is currently available according to the state of the mutex lock, until the artificial intelligence detection instance corresponding to the HTTP interface is currently available.

[0055] In some embodiments, the artificial intelligence detection model is initialized to generate multiple artificial intelligence detection instances with mutex locks, including:

[0056] Using Python's built-in class data structure, the functional functions of the entire workflow required by the artificial intelligence detection model are encapsulated into an AI service class.

[0057] In some embodiments, the mutually exclusive access component is implemented to obtain the state of the mutual exclusion lock of the artificial intelligence detection instance corresponding to the HTTP interface, and determine whether the artificial intelligence detection instance corresponding to the HTTP interface is currently available according to the state of the mutual exclusion lock, including:

[0058] The mutual exclusion access component uses the instance name of the artificial intelligence detection instance itself as the key value to determine the state of the mutual exclusion lock of the artificial intelligence detection instance, and determines whether the artificial intelligence detection instance corresponding to the HTTP interface is currently available based on the state of the mutual exclusion lock.

[0059] In some embodiments, the mutual exclusion access component obtains the state of the mutual exclusion lock of the artificial intelligence detection instance corresponding to the HTTP interface, and determines whether the artificial intelligence detection instance corresponding to the HTTP interface is currently available according to the state of the mutual exclusion lock, including:

[0060] Generate an identifier for the call request instruction, set the timeout for preempting the artificial intelligence detection instance, enter the loop branch of the preemption logic, and continue to try to preempt by judging whether the current time exceeds the timeout. Use the mutually exclusive access component to try to perform an assignment operation with the name of the artificial intelligence detection instance as the key value and the identifier as the value. If the assignment fails, it proves that the artificial intelligence detection instance is currently unavailable; otherwise, the artificial intelligence detection instance is currently available.

[0061] In some embodiments, before implementing the artificial intelligence detection instance to perform operations according to the call request instruction, the background implementation device is also used to implement:

[0062] If the assignment is successful, a calculation timeout is set for the key value of the mutually exclusive access component, and the reasoning calculation function of the artificial intelligence detection instance is executed. After the calculation process is completed, the key value is released and the mutex lock is released.

[0063] In some implementations, the exclusive access component is a remote dictionary service.

[0064] In certain embodiments, the artificial intelligence detection model includes a model file of an AI algorithm and an AI algorithm code file.

[0065] like Figure 3 As shown, an embodiment of the present application provides a method for implementing artificial intelligence detection, including:

[0066] S10, after the background component is started, the HTTP interface is initialized, and the artificial intelligence detection model is initialized, and multiple artificial intelligence detection instances with mutexes are generated. Optionally, according to actual needs, the HTTP interface can be replaced with other interactive interfaces.

[0067] The artificial intelligence detection model includes the model file of the AI ​​algorithm and the AI ​​algorithm code file. Each artificial intelligence detection instance has an AI instance interface; a mutex is set in each artificial intelligence detection instance; the mutex can ensure that at any time, only one HTTP interface can access the artificial intelligence detection instance, realizing the exclusive independent calling mechanism of the artificial intelligence detection instance; each artificial intelligence detection instance corresponds to a number of HTTP interfaces, and when the artificial intelligence detection instance is callable, each HTTP interface corresponding to the artificial intelligence detection instance is connected to the AI ​​instance interface of the artificial intelligence detection instance in the order of request calling to call the artificial intelligence detection instance.

[0068] In some embodiments, the artificial intelligence detection model is initialized using Python language to generate multiple artificial intelligence detection instances with mutex locks, including:

[0069] Using Python's built-in class data structure, the functional functions of the entire workflow required by the artificial intelligence detection model are encapsulated into an AI service class.

[0070] The AI ​​detection instance is a data structure stored in memory based on the instantiation of the AI ​​detection model and code. A notable feature of the AI ​​detection model during operation is that it takes a long time to initialize. During the operation, it will occupy the memory of the host and the graphics card, and will be released after the operation is completed. If the initialization process is performed once every time an application request is received, the access delay of the HTTP interface will be very large. When the HTTP interface needs to be executed multiple times, the AI ​​detection model needs to be instantiated and reside in the memory and the graphics memory separately, without having to repeat the initialization work every time a call request instruction from the HTTP interface is executed.

[0071] Each HTTP interface that calls an AI detection instance corresponds to an AI detection instance with a mutex lock. Whether the AI ​​detection instances corresponding to each HTTP interface are the same needs to be determined based on the actual application scenario and business scenario. For example, an OCR algorithm that recognizes text in printed materials will instantiate corresponding exclusive AI detection models for various application interfaces such as book materials, invoice materials, or ID card materials. Multiple HTTP interfaces can correspond to the same AI detection instance. When multiple HTTP interfaces call the AI ​​detection instance, the sequential calling arrangement mechanism of the AI ​​detection instance takes effect, and it is called and run by each HTTP interface in chronological order.

[0072] The AI ​​detection model is equivalent to a fixed "template", which is composed of the AI ​​algorithm model file and the AI ​​algorithm code file. It is a real file in the computer, and the AI ​​detection instance refers to the data structure stored in the computer memory after the AI ​​detection model is running. The same set of AI detection model code can be run to generate multiple AI detection instances, just like a "template" that can produce many products with the same attributes.

[0073] S20. When the HTTP interface receives a call request instruction from the foreground component, in response to the call request instruction, the mutex access component obtains the state of the mutex lock of the artificial intelligence detection instance corresponding to the HTTP interface, and determines whether the artificial intelligence detection instance corresponding to the HTTP interface is currently available based on the state of the mutex lock.

[0074] In some embodiments, the mutual exclusion access component obtains the state of the mutual exclusion lock of the artificial intelligence detection instance corresponding to the HTTP interface, and determines whether the artificial intelligence detection instance corresponding to the HTTP interface is currently available according to the state of the mutual exclusion lock, including:

[0075] The mutual exclusion access component uses the instance name of the artificial intelligence detection instance itself as the key value to determine the state of the mutual exclusion lock of the artificial intelligence detection instance, and determines whether the artificial intelligence detection instance corresponding to the HTTP interface is currently available based on the state of the mutual exclusion lock.

[0076] In some embodiments, the mutual exclusion access component obtains the state of the mutual exclusion lock of the artificial intelligence detection instance corresponding to the HTTP interface, and determines whether the artificial intelligence detection instance corresponding to the HTTP interface is currently available according to the state of the mutual exclusion lock, including:

[0077] Generate an identifier for the call request instruction, set the timeout for preempting the artificial intelligence detection instance, enter the loop branch of the preemption logic, and continue to try to preempt by judging whether the current time exceeds the timeout. Use the mutually exclusive access component to try to perform an assignment operation with the name of the artificial intelligence detection instance as the key value and the identifier as the value. If the assignment fails, it proves that the artificial intelligence detection instance is currently unavailable; otherwise, the artificial intelligence detection instance is currently available.

[0078] In some embodiments, if the assignment is successful, a calculation timeout is set for the key value of the mutually exclusive access component, and the reasoning calculation function of the artificial intelligence detection instance is executed. After the calculation process is completed, the key value is released and the mutex lock is released.

[0079] In some implementations, the exclusive access component is a remote dictionary service.

[0080] S30. If the artificial intelligence detection instance is currently available, access the artificial intelligence detection instance corresponding to the HTTP interface through the HTTP interface and call the artificial intelligence detection instance.

[0081] For example, the artificial intelligence detection instance is an artificial intelligence detection instance for face recognition, which is used for face recognition clocking in and out of get off work in a company. When a face recognition request is received through the HTTP interface, if the artificial intelligence detection instance is available, the artificial intelligence detection instance is called to perform face recognition.

[0082] S50. The artificial intelligence detection instance performs calculations according to the call request instruction, and feeds back the calculation results to the foreground component through the HTTP interface; the foreground component receives feedback from the background component and sends it to the client.

[0083] For example, after the artificial intelligence detection instance for face recognition recognizes the face, it sends the face recognition result to the front-end component through the HTTP interface. The front-end component receives feedback from the back-end component and sends it to the client to implement face recognition clocking in and out.

[0084] In some implementations, before step S50, the background implementation method further includes:

[0085] S40. If the artificial intelligence detection instance is currently unavailable, the mutex access component repeatedly obtains the state of the mutex lock of the artificial intelligence detection instance corresponding to the HTTP interface, and determines whether the artificial intelligence detection instance corresponding to the HTTP interface is currently available according to the state of the mutex lock, until the artificial intelligence detection instance corresponding to the HTTP interface is currently available.

[0086] Another embodiment of the present application provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to implement the above-mentioned method for implementing artificial intelligence detection.

[0087] Another embodiment of the present application provides a computer-readable storage medium having a computer program stored thereon, the program being executed by a processor to implement the above-mentioned method for implementing artificial intelligence detection.

[0088] Another embodiment of the present application provides a method for implementing artificial intelligence detection, including: when the background service is started, the artificial intelligence detection model is initialized to generate several artificial intelligence detection instances. When an AI application request comes, the HTTP interface corresponding to the request will call the artificial intelligence detection instance to perform AI detection processing operations. Before the artificial intelligence detection instance performs the processing operation, the third-party component Redis is used to communicate with the artificial intelligence detection instance to obtain the mutex lock state in order to determine whether the artificial intelligence detection instance is currently available. If it is currently available, the subsequent processing operation process is performed, and the mutex lock is released after completion. Then the HTTP interface returns the calculation result to the foreground component. If the artificial intelligence detection instance is not currently available, the artificial intelligence detection instance status will be continuously obtained until a timeout exception occurs.

[0089] like Figure 2 As shown, the background component is electrically connected to the foreground component, and the foreground component is connected to the HTTP interface of the background component. The foreground component may include a face-scanning attendance machine camera used for clocking in and out of the company. After the camera obtains the facial image, it sends a request to the HTTP interface to call an artificial intelligence detection instance.

[0090] In order to realize the exclusive independent calling mechanism of artificial intelligence detection instances, this embodiment uses the class data structure that comes with Python to encapsulate the entire workflow required for the artificial intelligence detection model, such as initialization mode configuration, loading algorithm models, performing AI calculation and reasoning, cleaning cache and other functional functions into an AI service class.

[0091] The above code declares and implements a class of artificial intelligence detection model, named AI_demo_instance. The caller can instantiate the artificial intelligence detection model through the method of ai_model = AI_model_instance(config). The main function of its initialization function __init__ is to initialize the underlying framework of the artificial intelligence detection model based on a configuration file config, and save it to the private variable self._instance of the artificial intelligence detection instance. Another variable self.instance_name is a unique code randomly generated using the uuid library. It is the key to implement the mutual exclusion lock of artificial intelligence detection instance access in the interface. The calling method section below will be expanded in detail. The function _clear_gpu_cache is to clear the current operation state after each AI calculation, so that each AI calculation is based on a completely consistent initial state. The function inference is the main calculation function of the AI ​​service algorithm, that is, it takes file_to_detect as the input parameter (which can be pictures, videos, audio and other categories of files), uses the artificial intelligence detection model to perform inference operations, obtains the operation result (result), and then calls

[0092] _clear_gpu_cache clears the cache and finally returns the detection result (result).

[0093] When the HTTP interface is initialized, the existing AI detection model and code are used to run and generate a dedicated AI detection instance, ensuring that there is no contention for resources when multiple HTTP interfaces need to access the same AI detection instance.

[0094] Exclusive calling method of artificial intelligence detection instance:

[0095] Since there are many types of Python-based web service frameworks, but the usage methods are similar, this embodiment will use Tornado as an example to describe the exclusive calling method of the artificial intelligence detection instance.

[0096] In the Tornado framework, each HTTP interface is essentially a Python class. In order to isolate the artificial intelligence detection instances called by different HTTP interfaces from each other, the solution proposed in this embodiment is to instantiate the artificial intelligence detection instance exclusively called by the HTTP interface when the HTTP interface class is initialized. Since the artificial intelligence detection model initialization process is added to the HTTP interface initialization process, the HTTP interface initialization time will be prolonged, thereby also extending the startup time of the background server. However, the benefit of this one-time loading of the artificial intelligence detection model is that there is no need to repeat the initialization work when the subsequent HTTP interface calls the artificial intelligence detection instance for calculation.

[0097] The above is a way to implement an exclusive AI detection instance. In the example, SomeAPIHander_A is an implementation class of a POST interface service. Before the interface implementation class, this solution first instantiates an AI detection model variable exclusive_ai_instance_A, and then assigns the current interface's private variable self._exclusive_ai_instance to exclusive_ai_instance_A in the interface service class initialization function (initialize). When there is a POST request to be processed, it can be directly called

[0098] self._exclusive_ai_instance.inference() performs AI calculations and returns the corresponding calculation results to the interface caller. When multiple interface implementation classes need to call the AI ​​detection model, a private exclusive_ai_instance needs to be instantiated for each interface, thereby isolating the calls to the AI ​​detection model between multiple interface services and preventing GPU contention.

[0099] When multiple requests arrive at the server at the same time, there will still be competition for the artificial intelligence detection instance. Therefore, a corresponding mutual exclusion access mechanism is also needed for the HTTP interface. This embodiment uses an access mutual exclusion lock solution based on Redis (Redis, Remote Dictionary Server, i.e., remote dictionary service). The instance name self.instance_name of the artificial intelligence detection instance itself is used as the key value key to judge the state of the mutual exclusion lock of the current artificial intelligence detection instance to determine whether the current artificial intelligence detection instance can be called. Due to the single-threaded access mechanism of Redis, when multiple threads (processes) access Redis at the same time, dirty data will not be read.

[0100] The logic of HTTP interface preempting AI detection instance is as above. After receiving the POST request from the front-end component, the back-end needs to generate an identifier for the current call request first, generate the identifier through uuid.uuid1(), and set the timeout for preempting AI detection instance. Here, 1000 milliseconds is used as an example. Then enter the while loop branch code segment of the preemption logic, and continue to try to preempt by judging whether the current time time.time() exceeds the timeout end_ts. Through the setnx command of the Redis front-end component, try to use the name of the AI ​​detection instance.

[0101] self._exclusive_ai_instance.instance_name is the key value key, and the above-mentioned session identifier is used as the value to perform the assignment operation. If it fails, it proves that another thread has set the other party's identifier, and then wait for 0.01 seconds before making the next attempt. If it succeeds, first set the calculation timeout for the Redis key value key, which is also 1 second in this example, to prevent abnormalities in the calculation process of the artificial intelligence detection model from causing abnormal release of the Redis key, and then execute the reasoning calculation function of the artificial intelligence detection instance normally. After completing the calculation process, the key value key is actively released and the mutex lock is released. The above is the solution to determine whether the artificial intelligence detection instance is currently available.

[0102] The AI ​​detection instance performs calculations based on the call request instruction and feeds back the calculation results to the front-end component through the HTTP interface. This is the entire implementation plan for the real-time AI detection background. By utilizing the syntax characteristics of Python and the single-threaded execution characteristics of Redis, the purpose of isolating the AI ​​algorithm resources based on different situations in the background is achieved.

[0103] The HTTP interface calls the detection function of the artificial intelligence detection instance, but the artificial intelligence detection instance is initialized outside the HTTP interface.

[0104] The background construction mode of this embodiment is mainly based on the traditional interface deployment, and an artificial intelligence detection instance with a sequential call arrangement mechanism for the HTTP interface is added. Therefore, the method proposed in this embodiment can ensure that each HTTP interface has a corresponding, independent and exclusive artificial intelligence detection instance, and at the same time, combined with the mutually exclusive access component to obtain information on whether the artificial intelligence detection instance is currently available, it can implement the sequential call arrangement mechanism of the artificial intelligence detection instance, and can ensure that multiple HTTP interfaces call the corresponding artificial intelligence detection instances in order, thereby overcoming the situation where multiple HTTP interfaces call the same artificial intelligence detection instance at the same time. The situation of resource competition will occur, improve work efficiency, avoid call confusion, and reduce the occurrence of failures.

[0105] It should be noted that:

[0106] The algorithm and display provided herein are not inherently related to any particular computer, virtual device or other equipment. Various general purpose devices can also be used together with the teachings based on this. According to the above description, it is obvious to construct the structure required for this type of device. In addition, the application is not directed to any specific programming language either. It should be understood that various programming languages ​​can be utilized to realize the content of the application described herein, and the description of the specific language above is for the purpose of disclosing the best mode of the application.

[0107] Similarly, it should be understood that in order to streamline the present disclosure and aid in understanding one or more of the various inventive aspects, in the above description of the exemplary embodiments of the present application, the various features of the present application are sometimes grouped together into a single embodiment, figure, or description thereof. However, this disclosed method should not be interpreted as reflecting the following intention: the claimed application requires more features than the features explicitly recited in each claim. More specifically, as reflected in the claims, the inventive aspects lie in less than all the features of the individual embodiments disclosed above. Therefore, the claims that follow the specific embodiment are hereby expressly incorporated into the specific embodiment, with each claim itself serving as a separate embodiment of the present application.

[0108] It should be understood that, although the steps in the flowchart of the accompanying drawings are displayed in sequence as indicated by the arrows, these steps are not necessarily executed in sequence in the order indicated by the arrows. Unless otherwise specified herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least a part of the steps in the flowchart of the accompanying drawings may include multiple sub-steps or multiple stages, and these sub-steps or stages are not necessarily executed at the same time, but can be executed at different times, and their execution order is not necessarily sequential, but can be executed in turn or alternately with other steps or at least a part of the sub-steps or stages of other steps.

[0109] The above-mentioned embodiments only express the implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the present application. It should be pointed out that, for a person of ordinary skill in the art, several modifications and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the attached claims.

Claims

1. An electronic device for implementing artificial intelligence detection, characterized in that: include: a processor for controlling components in the device; The background component is used to initialize the interactive interface and the artificial intelligence detection model after startup, and generate multiple artificial intelligence detection instances with mutex locks; The background component is also used for, when the interactive interface receives a call request instruction from the foreground component, in response to the call request instruction, obtaining the state of the mutex lock corresponding to the artificial intelligence detection instance of the interactive interface, and judging whether the artificial intelligence detection instance corresponding to the interactive interface is currently available according to the state of the mutex lock; The background component is further used to access the artificial intelligence detection instance corresponding to the interactive interface through the interactive interface and call the artificial intelligence detection instance if the artificial intelligence detection instance is currently available; The backend component is also used for the artificial intelligence detection instance to perform calculations according to the call request instruction, and to feed back the calculation results to the frontend component through the interactive interface; The front-end component is used to receive the call request instruction from the external client and send it to the back-end component; and receive the feedback from the back-end component and send it to the client; A memory, coupled to the processor, for storing the artificial intelligence detection model and the artificial intelligence detection instance; The acquiring the state of the mutex lock corresponding to the artificial intelligence detection instance of the interactive interface, and judging whether the artificial intelligence detection instance corresponding to the interactive interface is currently available according to the state of the mutex lock, comprises: Generate an identifier for the call request instruction, set a timeout for preempting the artificial intelligence detection instance, enter the loop branch of the preemption logic, continue to try to preempt by judging whether the current time exceeds the timeout, and try to perform an assignment operation with the name of the artificial intelligence detection instance as the key value and the identifier as the value. If the assignment fails, it proves that the artificial intelligence detection instance is currently unavailable; otherwise, the artificial intelligence detection instance is currently available.

2. The device according to claim 1, characterized in that The background component further includes a mutually exclusive access component, which is used to obtain the state of the mutual exclusion lock of the artificial intelligence detection instance corresponding to the interactive interface, and determine whether the artificial intelligence detection instance corresponding to the interactive interface is currently available according to the state of the mutual exclusion lock; before the artificial intelligence detection instance performs calculations according to the call request instruction, the background component is also used to implement: If the artificial intelligence detection instance is currently unavailable, the mutual exclusion access component repeatedly obtains the state of the mutual exclusion lock of the artificial intelligence detection instance corresponding to the interaction interface, and determines whether the artificial intelligence detection instance corresponding to the interaction interface is currently available according to the state of the mutual exclusion lock, until the artificial intelligence detection instance corresponding to the interaction interface is currently available.

3. The device according to claim 1, characterized in that The acquiring the state of the mutex lock corresponding to the artificial intelligence detection instance of the interactive interface, and judging whether the artificial intelligence detection instance corresponding to the interactive interface is currently available according to the state of the mutex lock, comprises: The instance name of the artificial intelligence detection instance itself is used as a key value to determine the state of the mutual exclusion lock of the artificial intelligence detection instance, and whether the artificial intelligence detection instance corresponding to the interactive interface is currently available is determined according to the state of the mutual exclusion lock.

4. The device according to claim 2, characterized in that The mutually exclusive access component serves the remote dictionary.

5. A method for implementing artificial intelligence detection, characterized in that: include: After the background component is started, the interactive interface is initialized, the artificial intelligence detection model is initialized, and multiple artificial intelligence detection instances with mutex locks are generated; When the interactive interface receives a call request instruction from the foreground component, in response to the call request instruction, obtains the state of the mutex lock corresponding to the artificial intelligence detection instance of the interactive interface, and determines whether the artificial intelligence detection instance corresponding to the interactive interface is currently available according to the state of the mutex lock; If the artificial intelligence detection instance is currently available, accessing the artificial intelligence detection instance corresponding to the interactive interface through the interactive interface, and calling the artificial intelligence detection instance; The artificial intelligence detection instance performs calculations according to the call request instruction, and feeds back the calculation results to the foreground component through the interactive interface; The front-end component receives feedback from the back-end component and sends it to the client; The background component further includes a mutually exclusive access component, which is used to obtain the state of the mutual exclusion lock of the artificial intelligence detection instance corresponding to the interactive interface, and determine whether the artificial intelligence detection instance corresponding to the interactive interface is currently available according to the state of the mutual exclusion lock; The acquiring the state of the mutex lock corresponding to the artificial intelligence detection instance of the interactive interface, and judging whether the artificial intelligence detection instance corresponding to the interactive interface is currently available according to the state of the mutex lock, comprises: Generate an identifier for the call request instruction, set a timeout for preempting the artificial intelligence detection instance, enter the loop branch of the preemption logic, continue to try to preempt by judging whether the current time exceeds the timeout, and try to perform an assignment operation with the name of the artificial intelligence detection instance as the key value and the identifier as the value through the mutually exclusive access component. If the assignment fails, it proves that the artificial intelligence detection instance is currently unavailable; otherwise, the artificial intelligence detection instance is currently available.

6. The method according to claim 5, characterized in that Before the artificial intelligence detection instance performs operation according to the call request instruction, the method further includes: If the artificial intelligence detection instance is currently unavailable, repeatedly obtain the state of the mutex lock of the artificial intelligence detection instance corresponding to the interactive interface, and determine whether the artificial intelligence detection instance corresponding to the interactive interface is currently available according to the state of the mutex lock, until the artificial intelligence detection instance corresponding to the interactive interface is currently available.

7. The method according to claim 5, characterized in that The acquiring the state of the mutex lock corresponding to the artificial intelligence detection instance of the interactive interface, and judging whether the artificial intelligence detection instance corresponding to the interactive interface is currently available according to the state of the mutex lock, comprises: The instance name of the artificial intelligence detection instance itself is used as a key value to determine the state of the mutual exclusion lock of the artificial intelligence detection instance, and whether the artificial intelligence detection instance corresponding to the interactive interface is currently available is determined according to the state of the mutual exclusion lock.

8. A computer-readable storage medium having a computer program stored thereon, characterized in that: The program is executed by a processor to implement the method for implementing artificial intelligence detection as described in any one of claims 5-7.