A deployment method for a machine learning algorithm based on sleep staging
By using C language to realize the inference and deployment of the sleep staging xgboost model on edge computing devices, the device computing power and battery power limitations are solved, and efficient and low-power sleep staging algorithm deployment is realized. It is suitable for low-computing equipment and reduces system requirements and development costs.
Patent Information
- Application Number
- CN202211715986.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-30
- Publication Date
- 2025-08-26
- Estimated Expiration
- 2042-12-30
AI Technical Summary
Existing edge computing devices have limited computing power and battery power in the deployment of machine learning algorithms for sleep installment, resulting in high demand for computing resources. Traditional deployment methods require large storage space and power consumption, making it difficult to operate effectively on low computing power and low power consumption devices.
The reasoning of machine learning algorithms is implemented using C language, and the network structure is analyzed layer by layer through the math function library, the operator is encapsulated and the model is optimized, and the pure C language sleep staged xgboost model is generated, which is directly deployed to the end-side devices to reduce dependence on the system and extension libraries.
It realizes the deployment of high-accuracy sleep installment algorithms, reduces the computing volume and power consumption of end-side devices, and is suitable for low-computing power and low-power consumption devices, promotes the miniaturization and portability of equipment, and reduces development costs.
Smart Images

Figure CN115969324B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of sleep algorithms, and in particular to a deployment method of a machine learning algorithm based on sleep staging. Background Art
[0002] Common sleep monitoring devices include wristbands, non-wearable monitoring devices, and wearable sleep monitoring devices. Non-wearable monitoring devices typically include smart mattresses, smart pillows, and smart bed belts, which can be placed directly on the headboard of the bed. Sensors on the monitoring belt record sleep EEG signals and use algorithms to classify sleep into deep sleep, light sleep, REM (rapid eye movement), and wakefulness.
[0003] Wearable devices acquire EEG signals through patches and implement sleep staging through algorithms on the cloud platform. They can effectively detect, inspect and evaluate sleep quality, provide important basis for the diagnosis of various sleep disorders, and are a key prerequisite for diagnosing sleep.
[0004] Unlike cloud computing, edge computing eliminates the need to transmit data to a distant cloud; data can be processed locally, making it more suitable for real-time data analysis and intelligent processing, while also being more efficient and secure. However, due to the limited computing power of edge devices, running deep learning models requires high-performance computing resources. Therefore, the machine learning XGBoost model was selected as the algorithm for training the sleep staging model to reduce computational complexity. For portability, the trained model needs to be deployed and embedded in the edge device.
[0005] However, the battery power of edge devices is generally small, and the deployment of machine learning algorithms and deep learning algorithms now requires system reliance (Python needs to be installed), and C++ deployment requires compiled libraries. These require high storage space and computing power for edge devices, and the power consumption is high during operation, requiring a larger battery power to meet the power requirements during operation. Summary of the Invention
[0006] The purpose of the present invention is to address the deficiencies in the prior art and provide a method for deploying a machine learning algorithm based on sleep staging.
[0007] The objective of the present invention is achieved through the following technical solution: a method for deploying a machine learning algorithm based on sleep staging, comprising the following specific steps:
[0008] Step 1): Collect EEG data, divide the EEG data into several segments and save them;
[0009] Step 2): Perform feature engineering on the segmented data to prepare the data for the xgboost model;
[0010] Step 3): Divide the EEG data into a training set and a test set, train and tune the xgboost model, save the model after the accuracy reaches the requirement, and obtain the trained original sleep staging xgboost model;
[0011] Step 4): Use the math library in C language to implement the reasoning of the original sleep stage xgboost model. The specific method is as follows:
[0012] S1. Save the network structure and parameters of the original sleep staging xgboost model;
[0013] S2. Analyze the network structure layer by layer and node by node, and use the math function library to implement the operators of each node;
[0014] S3. Verify the operator performance of each node;
[0015] S4. Encapsulate operators to facilitate the calling of the same operators;
[0016] S5. After implementing the algorithm node by node, layer by layer, and finally the entire network structure algorithm, the pure C language sleep staging xgboost model is obtained;
[0017] Step 5): Load the original sleep staging xgboost model and the pure C language sleep staging xgboost model, and calibrate the pure C language sleep staging xgboost model with the original sleep staging xgboost model;
[0018] Step 6): Deploy the calibrated pure C language sleep staging XGBoost model to the end device.
[0019] Preferably, in step 1), before the EEG data is divided into segmented data, filtering and smoothing processing is performed on the EEG data.
[0020] Preferably, in step 1), the time length of the segmented data is 6 seconds.
[0021] Preferably, in step 3), the ratio of the training set to the test set is 8:2.
[0022] Preferably, in step 5), the original sleep staging xgboost model is loaded through Python.
[0023] Preferably, in step 5), calibration of the pure C language sleep staging xgboost model includes accuracy calibration and inference time evaluation optimization.
[0024] Preferably, the specific method of the accuracy calibration is as follows:
[0025] Load the original sleep staging xgboost model and the pure C language sleep staging xgboost model at the same time and perform inference to check their inference probability scores. If the two probability scores are consistent to two decimal places, the inference accuracy of the pure C language sleep staging xgboost model is consistent with that of the original sleep staging xgboost model, meeting the accuracy requirement. If the two probability scores are inconsistent to two decimal places, optimize the operator of the pure C language sleep staging xgboost model, then load the original sleep staging xgboost model and the pure C language sleep staging xgboost model again and perform inference until the two inference probability scores are consistent to two decimal places.
[0026] Preferably, the specific method for optimizing the reasoning time consumption evaluation is as follows:
[0027] Load the original sleep staging xgboost model and the pure C language sleep staging xgboost model at the same time and perform inference to check their inference time. If the inference time of the two is consistent, the inference time of the pure C language sleep staging xgboost model meets the requirement. If the inference time of the two is inconsistent, optimize the operators of the pure C language sleep staging xgboost model, then load the original sleep staging xgboost model and the pure C language sleep staging xgboost model again and perform inference until their inference time is consistent.
[0028] The beneficial effects of the present invention are: 1. The present invention provides a sleep staging algorithm based on machine learning, which has a high accuracy rate. 2. The present invention implements pure C language reasoning based on the traditional xgboost model. When deployed on the end-side device, there is no need to install the corresponding system or platform on the end-side device in advance, which can reduce the system requirements for the end-side device, reduce the amount of calculation of the end-side device, and reduce the power consumption of the end-side device. It has a great driving effect on the implementation of the sleep staging algorithm for end-side devices with low computing power and low power consumption, and is conducive to the miniaturization and portability of end-side devices. 3. The pure C language sleep staging xgboost model of the present invention can be directly deployed to the end-side device, and does not need to rely on the system and extension library. It is implemented in pure C language and only uses the built-in function library, which plays a role in reducing system power consumption and calculation amount. It can be applied to deployments in different scenarios and reduce development costs. BRIEF DESCRIPTION OF THE DRAWINGS
[0029] Figure 1 Schematic diagram of the process of EEG data collection and model training.
[0030] Figure 2 Schematic diagram of feature engineering for segmented data.
[0031] Figure 3Schematic diagram of the network structure of the trained original sleep staging xgboost model.
[0032] Figure 4 A schematic diagram of the pure C language sleep staging XGBoost model is obtained to implement the original sleep staging XGBoost model inference using the math function library.
[0033] Figure 5 This is a flowchart for calibrating the pure C language sleep staging xgboost model.
[0034] Figure 6 This is part of the code for the pure C language sleep phase xgboost model. DETAILED DESCRIPTION
[0035] The following will clearly and completely describe the technical solutions in the embodiments of the present invention in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. All other embodiments obtained by ordinary technicians in this field based on the embodiments of the present invention are within the scope of protection of the present invention.
[0036] It should be understood by those skilled in the art that, in the disclosure of the present invention, the terms "longitudinal", "transverse", "up", "down", "front", "back", "left", "right", "vertical", "horizontal", "top", "bottom", "inside", "outside", etc., indicating the orientation or position relationship, are based on the orientation or position relationship shown in the accompanying drawings, which are only for the convenience of describing the present invention and simplifying the description, and do not indicate or imply that the device or element referred to must have a specific orientation, be constructed and operate in a specific orientation. Therefore, the above terms should not be understood as limiting the present invention.
[0037] It is to be understood that the term "one" should be understood as "at least one" or "one or more", that is, in one embodiment, the number of an element may be one, while in another embodiment, the number of the elements may be multiple, and the term "one" should not be understood as a limitation on the quantity.
[0038] A method for deploying a machine learning algorithm based on sleep staging includes the following specific steps:
[0039] Step 1): Collect EEG data, divide the EEG data into several segments and save them.
[0040] The overall process of this step is shown in the attached Figure 1 , can collect EEG data of multiple people. After obtaining the EEG data, it is first filtered and smoothed, and then each person's EEG data is divided into several segmented data with a time length of 6 seconds.
[0041] Step 2): Perform feature engineering on the segmented data to prepare the data for the xgboost model.
[0042] When doing feature engineering on segmented data, please refer to the attached Figure 2 , attached Figure 2 This is a diagram of feature engineering for segmented data. The xgboost model used in this application is a commonly used machine learning model.
[0043] Step 3): Divide the EEG data into a training set and a test set, train and tune the xgboost model, save the model after the accuracy meets the requirements, and obtain the trained original sleep staging xgboost model.
[0044] The training set and test set split ratio is 8:2. When the accuracy reaches above 80%, it meets the requirements and the model can be saved. The network structure of the original sleep stage xgboost model after training is shown in the attached figure. Figure 3 .
[0045] The prediction principle of the original sleep stage xgboost model is as follows: Assuming that K trees have been trained, the final prediction value of the i-th sample is ,in represents the sample characteristics, Represents the k-th tree pair sample The prediction results, Represents each tree algorithm model set.
[0046] Step 4): Use the math library in C language to implement the original sleep stage xgboost model reasoning; see the attached Figure 4 , the specific method is as follows:
[0047] S1. Save the network structure and parameters of the original sleep staging xgboost model. When saving the parameters, the corresponding network weight parameters need to be saved according to the network structure.
[0048] S2. Analyze the network structure layer by layer and node by node, and use the math function library to implement the operators of each node; this also includes using the math function library to implement matrices and some activation functions;
[0049] S3. Verify the performance of the operators of each node. During the verification, the accuracy and computation time of the operators are mainly verified.
[0050] S4. Encapsulate operators to facilitate the calling of the same operators;
[0051] S5. After implementing the algorithm node by node, layer by layer, and finally the entire network structure algorithm, the pure C language sleep staging xgboost model is obtained. Part of the code of the pure C language sleep staging xgboost model is as follows Figure 6 shown.
[0052] Step 5): Load the original sleep staging xgboost model and the pure C language sleep staging xgboost model, and calibrate the pure C language sleep staging xgboost model through the original sleep staging xgboost model.
[0053] In this step, the original sleep stage xgboost model is loaded using traditional python. Figure 5 ,The calibration of the pure C language sleep staging xgboost model includes accuracy calibration and inference time evaluation optimization.
[0054] The specific method of accuracy calibration is as follows:
[0055] Load the original sleep staging xgboost model and the pure C language sleep staging xgboost model at the same time and perform inference to check their inference probability scores. If the two probability scores are consistent to two decimal places, the inference accuracy of the pure C language sleep staging xgboost model is consistent with that of the original sleep staging xgboost model, meeting the accuracy requirement. If the two probability scores are inconsistent to two decimal places, optimize the operator of the pure C language sleep staging xgboost model, then load the original sleep staging xgboost model and the pure C language sleep staging xgboost model again and perform inference until the two inference probability scores are consistent to two decimal places.
[0056] The specific method for optimizing inference time evaluation is as follows:
[0057] Load the original sleep staging xgboost model and the pure C language sleep staging xgboost model at the same time and perform inference to check their inference time. If the inference time of the two is consistent, the inference time of the pure C language sleep staging xgboost model meets the requirement. If the inference time of the two is inconsistent, optimize the operators of the pure C language sleep staging xgboost model, then load the original sleep staging xgboost model and the pure C language sleep staging xgboost model again and perform inference until their inference time is consistent.
[0058] After the pure C language sleep staging xgboost model is converted, the accuracy and inference time of the pure C language sleep staging xgboost model are brought to the level of the original sleep staging xgboost model, so that the performance of the original sleep staging xgboost model is not affected after the pure C language conversion.
[0059] Step 6): Deploy the calibrated pure C language sleep staging XGBoost model to the end device.
[0060] The present invention has the following advantages: 1. The present invention provides a sleep staging algorithm based on machine learning with high accuracy. 2. The present invention implements pure C language reasoning based on the traditional xgboost model. When deployed on the end-side device, there is no need to install the corresponding system or platform on the end-side device in advance, which can reduce the system requirements for the end-side device, reduce the amount of calculation of the end-side device, and reduce the power consumption of the end-side device. It has a great driving force for the implementation of sleep staging algorithms on end-side devices with low computing power and low power consumption, and is conducive to the miniaturization and portability of end-side devices. 3. The pure C language sleep staging xgboost model of the present invention can be directly deployed to the end-side device, and does not need to rely on the system and extension library. It is implemented in pure C language and only uses the built-in function library, which plays a role in reducing system power consumption and calculation amount. It can be applied to deployments in different scenarios and reduce development costs.
[0061] The present invention is not limited to the above-mentioned optimal implementation mode. Anyone can derive other forms of products under the inspiration of the present invention. However, no matter what changes are made in the shape or structure, any technical solution that is the same or similar to that of the present application falls within the scope of protection of the present invention.
Claims
1. A method for deploying a machine learning algorithm based on sleep staging, characterized in that: The specific steps include: Step 1): Collect EEG data, divide the EEG data into several segments and save them; Step 2): Perform feature engineering on the segmented data to prepare the data for the xgboost model; Step 3): Divide the EEG data into a training set and a test set, train and tune the xgboost model, save the model after the accuracy reaches the requirement, and obtain the trained original sleep staging xgboost model; Step 4): Use the math library in C language to implement the reasoning of the original sleep stage xgboost model. The specific method is as follows: S1. Save the network structure and parameters of the original sleep staging xgboost model; S2. Analyze the network structure layer by layer and node by node, and use the math function library to implement the operators of each node; S3. Verify the operator performance of each node; S4. Encapsulate operators to facilitate the calling of the same operators; S5. After implementing the algorithm node by node, layer by layer, and finally the entire network structure algorithm, the pure C language sleep staging xgboost model is obtained; Step 5): Load the original sleep staging xgboost model and the pure C language sleep staging xgboost model, and calibrate the pure C language sleep staging xgboost model using the original sleep staging xgboost model. Calibration of the pure C language sleep staging xgboost model includes accuracy calibration and inference time evaluation optimization. The specific method of the accuracy calibration is as follows: the original sleep staging xgboost model and the pure C language sleep staging xgboost model are loaded simultaneously and reasoning is performed to check the reasoning probability scores of the two; if the probability scores of the two are consistent to two decimal places, it indicates that the reasoning accuracy of the pure C language sleep staging xgboost model is consistent with the reasoning accuracy of the original sleep staging xgboost model and meets the accuracy requirements; if the probability scores of the two are inconsistent to two decimal places, the operator of the pure C language sleep staging xgboost model is optimized, and then the original sleep staging xgboost model and the pure C language sleep staging xgboost model are loaded again and reasoning is performed until the reasoning probability scores of the two are consistent to two decimal places; The specific method for optimizing the inference time evaluation is as follows: simultaneously load the original sleep staging xgboost model and the pure C language sleep staging xgboost model and perform inference, and check the inference time of the two; if the inference time of the two is consistent, it indicates that the inference time of the pure C language sleep staging xgboost model meets the requirements; if the inference time of the two is inconsistent, optimize the operator of the pure C language sleep staging xgboost model, and then load the original sleep staging xgboost model and the pure C language sleep staging xgboost model again and perform inference until the inference time of the two is consistent; Step 6): Deploy the calibrated pure C language sleep staging XGBoost model to the end device.
2. The method for deploying a machine learning algorithm based on sleep staging according to claim 1, characterized in that: In step 1), before the EEG data is divided into segmented data, the EEG data is filtered and smoothed.
3. The method for deploying a machine learning algorithm based on sleep staging according to claim 1, characterized in that: In step 1), the time length of the segmented data is 6 seconds.
4. The method for deploying a machine learning algorithm based on sleep staging according to claim 1, characterized in that: In step 3), the split ratio of the training set and the test set is 8:
2.
5. The method for deploying a machine learning algorithm based on sleep staging according to claim 1, wherein in step 5), the original sleep staging XGBoost model is loaded using Python.
Citation Information
Patent Citations
Machine learning algorithm for sleep staging by applying prefrontal lobe single-channel electroencephalogram signals
CN113576492A