A neural network model deployment method and system based on FPGA acceleration
By deploying compressed and optimized neural network models on the FPGA development board, the problem of hardware not suitable for low power consumption and low latency in embedded scenarios is solved, and fast, low power consumption, and high accuracy target detection is achieved, improving Improved the neural network deployment performance of FPGA hardware.
Patent Information
- Application Number
- CN202410927277.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-11
- Publication Date
- 2024-10-01
- Estimated Expiration
- 2044-07-11
AI Technical Summary
When the existing technology deploys neural networks in embedded situations, the hardware is not suitable for low-power, low-latency applications. The FPGA language development efficiency is low, the degree of parallelism is not high, the processor architecture is inefficient, low precision, and poor versatility, resulting in neural networks. There are shortcomings in the network model deployment method.
Propose a neural network model deployment method based on FPGA acceleration, including selecting the neural network model and performing compression processing, designing a global architecture suitable for the FPGA development board, generating a runnable Linux system, and deploying the compressed model to the FPGA development board , and optimizes the model through quantized compression and pruning to fine-tune and achieve fast detection and low power consumption.
It achieves fast detection, low power consumption, and high accuracy target detection, is suitable for low power consumption, and low latency situations, and improves the performance of FPGA hardware in neural network deployment.
Smart Images

Figure CN118468952B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of target detection technology, and in particular to a neural network model deployment method and system based on FPGA acceleration. Background Art
[0002] When deploying neural networks in embedded scenarios, most hardware is not suitable for low-power, low-latency applications due to energy constraints. FPGAs have the advantages of low power consumption, low latency, and reconfigurability, making them very suitable for the inference stage of neural networks. Compared with other hardware, FPGAs can reconfigure the hardware structure and customize hardware for specific applications through configuration, making them suitable for scenarios such as deep learning, which are rapidly evolving, iterating, and constantly updating. Therefore, FPGAs are a very promising option that can meet current development needs very well. However, due to the low efficiency of FPGA language development, low parallelism, inefficiency of various processor architectures, low precision, and poor versatility, it is necessary to design a suitable neural network model compression and deployment method. Summary of the invention
[0003] Based on the shortcomings of the above-mentioned existing methods, the present invention proposes a neural network model deployment method and system based on FPGA acceleration.
[0004] On the one hand, the present invention provides a neural network model deployment method based on FPGA acceleration, the method comprising the following steps:
[0005] S1, selecting a neural network model and training it, and compressing the trained neural network model to obtain a compressed neural network model;
[0006] S2. Select an FPGA development board, design a global architecture suitable for the neural network model based on the FPGA development board, add the description file of the global architecture to the device tree file, and generate a Linux system that can run on the FPGA development board;
[0007] S3, burning the generated Linux system into the Nand Flash on the FPGA development board and powering on, compiling the compressed neural network model into a binary code file and deploying it to the FPGA development board, obtaining the deployed FPGA development board;
[0008] S4. Collect the image to be detected and input it to the deployed FPGA development board for processing, obtain the predicted box and confidence score of the target to be detected, load the predicted box and confidence score of the target to be detected onto the image to be detected, and transmit it to the display for display.
[0009] Preferably, S1 specifically includes the following:
[0010] S11, select a neural network model, obtain the image to be detected and create a data set, and store the data set on a server with a GPU;
[0011] S12, pre-training the selected neural network model on a server with a GPU using the data set to obtain a pre-trained neural network model;
[0012] S13, performing sparse training on the pre-trained neural network model, and calculating the model loss through a preset loss function to obtain a sparse weight;
[0013] S14, pruning and fine-tuning the sparse weights to obtain a pruned and fine-tuned neural network model;
[0014] S15. Perform quantization compression on the neural network model after pruning and fine-tuning to obtain a quantized compressed neural network model.
[0015] Preferably, the loss function preset in S13 is specifically:
[0016] ;
[0017] in, is the loss function of the neural network model after adding the scaling factor, represents the loss function of the neural network model, represents the convolution calculation, is the input feature map, is the convolution calculation result of the input feature map, is the weight parameter of the neural network model, For the A scaling factor, It is for Scaling Factor The penalty caused by the sparsity of is the penalty factor, is the total number of scaling factors.
[0018] Preferably, the specific process of S14 is as follows:
[0019] S141, calculating the mean and variance of all parameters in the feature map parameter set;
[0020] S142, calculating the average value of each feature map parameter after batch normalization according to the mean and variance;
[0021] S143, training the feature map parameters and updating the scaling factor according to the batch-normalized average value of each feature map parameter;
[0022] S144, setting a pruning rate according to the scaling factor and pruning the neural network model, and fine-tuning the pruned neural network model to obtain a pruned and fine-tuned neural network model.
[0023] Preferably, in S15, the neural network model after pruning and fine-tuning training is quantized and compressed, specifically, the quantization compression is performed using a KLD quantization method, and the method includes the following steps:
[0024] S151, obtaining a computation graph of the neural network model after pruning and fine-tuning, inserting a pseudo-quantization operator into the computation graph, and obtaining a computation graph after the pseudo-quantization operator is inserted;
[0025] S152, obtaining an unlabeled data set, and inputting the unlabeled data set into a calculation graph into which a pseudo-quantization operator is inserted to generate a calculation result distribution histogram;
[0026] S153, calculating the probability distribution of the calculation result with the pseudo quantization operator inserted and the probability distribution of the calculation result without the pseudo quantization operator inserted according to the calculation result distribution histogram, and calculating the KL divergence of the two probability distributions;
[0027] S154, selecting the quantized value range corresponding to the minimum KL divergence, and quantizing the weights and activation functions of the neural network model after pruning and fine-tuning according to the quantized value range to obtain the quantized and compressed neural network model.
[0028] Preferably, the neural network model in S1 includes an image preprocessing unit, a convolution calculation unit and an image post-processing unit connected in sequence. In S2, a global architecture suitable for the neural network model is designed based on an FPGA development board. The specific design process of the global architecture is as follows:
[0029] S21. Select an FPGA development board. The FPGA development board includes an ARM end, an FPGA end, and peripheral devices that are interconnected. The ARM end and the FPGA end communicate through an AXI bus. The ARM end is responsible for image input and decoding, and the FPGA end is responsible for image preprocessing, calculation, and post-processing by a neural network model.
[0030] S22, the ARM side includes an ARM controller, a DDR controller, a DDR memory and a hardware encoder, the original image is encoded by the hardware encoder and then input and stored in the DDR memory, and the ARM controller calls the DDR memory through the DDR controller to write or read the original image;
[0031] S23, designing an image preprocessing kernel, a convolution calculation kernel, and an image postprocessing kernel corresponding to the image preprocessing unit, the convolution calculation unit, and the image postprocessing unit, respectively, on the FPGA side, connecting the image preprocessing kernel to the input end of the convolution calculation kernel through the first on-chip DDR, and connecting the image postprocessing kernel to the output end of the convolution calculation kernel through the second on-chip DDR;
[0032] S24, the image preprocessing kernel is connected to the DDR memory storing the image on the ARM side through AXI_M, reads the original image from the DDR memory and preprocesses it, and writes the preprocessed image into the first on-chip DDR on the FPGA side;
[0033] S25, the convolution calculation kernel reads the preprocessed image from the first on-chip DDR on the FPGA side, performs calculations through the on-chip BRAM, generates the target frame position and confidence score of each frame image, and writes the target frame position and confidence score of each frame image to the second on-chip DDR on the FPGA side;
[0034] S26, the image post-processing kernel reads the target frame position and confidence score of each frame image from the second on-chip DDR of the FPGA end and processes them, and selects the prediction frame and the corresponding confidence score that meet the preset conditions.
[0035] Preferably, the image preprocessing kernel in S24 reads the original image from the DDR memory and preprocesses it, which specifically includes the following:
[0036] S241, converting the format of the input image to obtain an image in a converted format;
[0037] S242, adjusting the size of the converted image according to the neural network requirements;
[0038] S243, quantizing the number of data bits of the resized image to obtain a preprocessed image.
[0039] Preferably, S26 specifically includes the following:
[0040] S261, reading the target frame position and confidence score of each frame of image from the second on-chip DDR on the FPGA end, and decoding the prediction frame of each frame of image according to the target frame position and confidence score of each frame of image;
[0041] S262, dequantizing the prediction frame of each frame of image into 32-bit data, and converting the position of the prediction frame of each frame of image into actual image coordinates;
[0042] S263. Filter out prediction boxes and corresponding confidence scores that meet preset conditions according to the confidence scores of the target boxes.
[0043] Another aspect of the present invention provides a neural network model deployment system based on FPGA acceleration, the system includes a camera, a personal computer, a server, a display, and an FPGA development board, the FPGA development board is connected to the camera, the server, and the display respectively, the personal computer is connected to the server and the FPGA development board respectively through a router, the personal computer is also connected to the display through an HDMI converter, the FPGA development board is provided with a quantized compressed neural network model and a Linux system that can be run on the FPGA development board, wherein:
[0044] The camera is used to collect the image to be detected;
[0045] The server is used to train and compress the neural network model to obtain a compressed neural network model;
[0046] A personal computer is used to perform inference on the bootable FPGA development board;
[0047] The FPGA development board is used to receive the image to be detected and process it through the compressed network model and the Linux system that can run on the FPGA development board to obtain the predicted box and confidence score of the target to be detected, load the predicted box and confidence score of the target to be detected onto the image to be detected, and transmit it to the display for display.
[0048] The above-mentioned neural network model deployment method and system based on FPGA acceleration selects a neural network model and trains it, compresses the trained neural network model to obtain a compressed neural network model, selects an FPGA development board, designs a global architecture suitable for the neural network model based on the FPGA development board, adds the description file of the global architecture to the device tree file, generates a Linux system that can run on the FPGA development board, burns the generated Linux system into the Nand Flash on the FPGA development board and powers on, compiles the compressed neural network model into a binary code file and deploys it to the FPGA development board to obtain the deployed FPGA development board, collects the image to be detected and inputs it to the deployed FPGA development board for processing, obtains the prediction box and confidence score of the target to be detected, loads the prediction box and confidence score of the target to be detected onto the image to be detected, and transmits it to the display for display. The method has fast detection speed, low power consumption, high accuracy, and can be applied to low power consumption and low latency occasions. BRIEF DESCRIPTION OF THE DRAWINGS
[0049] Figure 1 It is a flow chart of a neural network model deployment method based on FPGA acceleration in one embodiment of the present invention;
[0050] Figure 2 is a schematic diagram of pruning a neural network model in one embodiment of the present invention, wherein Figure 2 (a) is a schematic diagram before pruning. Figure 2 (b) is a schematic diagram after pruning;
[0051] Figure 3 is a schematic diagram of non-saturated quantization and saturated quantization in one embodiment of the present invention, wherein Figure 3 (a) is a schematic diagram of non-saturated quantization. Figure 3 (b) is a schematic diagram of saturation quantization;
[0052] Figure 4 is a schematic diagram of a quantization process using saturation quantization in one embodiment of the present invention;
[0053] Figure 5 is a schematic diagram of a circular convolution expansion in one embodiment of the present invention;
[0054] Figure 6 is a schematic diagram of pipeline calculation in one embodiment of the present invention;
[0055] Figure 7 is a schematic diagram of a global architecture based on FPGA acceleration in one embodiment of the present invention;
[0056] Figure 8 It is a structural diagram of a neural network model deployment system based on FPGA acceleration in one embodiment of the present invention. DETAILED DESCRIPTION
[0057] In order to enable those skilled in the art to better understand the technical solution of the present invention, the present invention is further described in detail below in conjunction with the accompanying drawings.
[0058] See also Figure 1 , Figure 1 The flowchart of a neural network model deployment method based on FPGA acceleration in one embodiment of the present invention. A neural network model deployment method based on FPGA acceleration includes the following steps:
[0059] S1. Select a neural network model and train it, and compress the trained neural network model to obtain a compressed neural network model.
[0060] In one embodiment, S1 specifically includes the following:
[0061] S11. Select a neural network model, obtain the image to be detected and create a data set, and store the data set on a server with a GPU.
[0062] S12. Pre-train the selected neural network model on a server with a GPU using the data set to obtain a pre-trained neural network model.
[0063] Specifically, the neural network model YOLOv4 is selected, and the size of the maximum pooling layer (MaxPool) of the feature map extraction part of YOLOv4, that is, the spatial pyramid pooling (SPP), is changed from 5x5, 9x9, and 13x13 to 3x3, 5x5, and 7x7, and LeakyReLU is selected as the activation function.
[0064] S13, performing sparse training on the pre-trained neural network model, and calculating the model loss through a preset loss function to obtain sparse weights.
[0065] Furthermore, the preset loss function is specifically:
[0066] The loss function preset in S13 is specifically:
[0067] ;
[0068] in, is the loss function of the neural network model after adding the scaling factor, represents the loss function of the neural network model (that is, the neural network model selected in S11), represents the convolution calculation, is the input feature map, is the convolution calculation result of the input feature map, is the weight parameter of the neural network model, is the L1 regularization constraint term of the BN layer of the neural network model, For the A scaling factor, It is for Scaling Factor The penalty caused by the sparsity of is the penalty factor, is the total number of scaling factors.
[0069] S14. Prune and fine-tune the sparse weights to obtain a pruned and fine-tuned neural network model.
[0070] In one embodiment, the specific process of S14 is as follows:
[0071] S141, calculating the mean and variance of all parameters in the feature map parameter set;
[0072] S142, calculating the average value of each feature map parameter after batch normalization according to the mean and variance;
[0073] ,
[0074] in, ;
[0075] in, represents the feature map parameter set, Represents the feature map parameter set The feature map parameters, , Represents the number of feature map parameters, Representative The average value of the feature map parameters after batch normalization, is a small positive number, represents the mean of batch normalization, Represents the variance of batch normalization.
[0076] S143, training feature map parameters according to the average value after batch normalization of each feature map parameter and updating the scaling factor ;
[0077] S144, according to the scaling factor The pruning rate is set and the neural network model is pruned, and the pruned neural network model is fine-tuned to obtain a pruned and fine-tuned neural network model.
[0078] Specifically, see Figure 2 , Figure 2 Schematic diagram of pruning a neural network model in one embodiment of the present invention, wherein Figure 2 (a) is a schematic diagram before pruning. Figure 2 (b) is the schematic diagram after pruning.
[0079] When pruning, the pruning rate needs to be set according to the scaling factor. For example, the larger the scaling factor is, the larger the pruning rate can be selected. The channels closer to 0 are more likely to be pruned. The higher the pruning rate is, the smaller the resulting model will be. However, when the pruning amount is too high, the accuracy of the model will decrease. In this case, the accuracy of the model can be improved by adding a small amount of training again.
[0080] S15. Perform quantization compression on the neural network model after pruning and fine-tuning training to obtain a quantized and compressed neural network model.
[0081] In one embodiment, in S15, the neural network model after pruning and fine-tuning is quantized and compressed, specifically, the quantization compression is performed using a KLD quantization method, and the method includes the following steps:
[0082] S151, obtaining a computation graph of the neural network model after pruning and fine-tuning, inserting a pseudo-quantization operator into the computation graph, and obtaining a computation graph after the pseudo-quantization operator is inserted;
[0083] S152, obtaining an unlabeled data set, and inputting the unlabeled data set into a calculation graph into which a pseudo-quantization operator is inserted to generate a calculation result distribution histogram;
[0084] S153, calculating the probability distribution of the calculation result with the pseudo quantization operator inserted and the probability distribution of the calculation result without the pseudo quantization operator inserted according to the calculation result distribution histogram, and calculating the KL divergence of the two probability distributions;
[0085] S154, selecting the quantized value range corresponding to the minimum KL divergence, and quantizing the weights and activation functions of the neural network model after pruning and fine-tuning according to the quantized value range to obtain the quantized and compressed neural network model.
[0086] Specifically, see Figure 3 and Figure 4 , Figure 3 FIG. 1 is a schematic diagram of non-saturated quantization and saturated quantization in one embodiment of the present invention, wherein Figure 3 (a) is a schematic diagram of non-saturated quantization. Figure 3 (b) is a schematic diagram of saturation quantization; Figure 4 FIG. 4 is a schematic diagram of a quantization process using saturation quantization in an embodiment of the present invention.
[0087] Quantization can be divided into unsaturated quantization and saturated quantization. Unsaturated quantization directly maps the weight [min, max] to [-127, 127]. Saturated quantization determines the maximum and minimum values of the quantization weight by inferring the calibration data set.
[0088] In this embodiment, the KLD saturation quantization method is specifically used. Since the value range of the input feature map is not fixed, the value range of the input feature map is selected and quantized multiple times, and a convolution operation is performed with the input feature map to obtain a calculation result, and a calculation result histogram is generated. The probability distribution of the calculation result of inserting the pseudo quantization operator and the probability distribution of the calculation result of not inserting the pseudo quantization operator are calculated based on the histogram, and the KL divergence of the two probability distributions is calculated. The value range corresponding to the most similar probability distribution (that is, the minimum KL divergence) is selected as the value range after quantization. The weights and activation functions of the neural network model after quantization pruning and fine-tuning are quantized according to the quantized value range to obtain a quantized and compressed neural network model, corresponding to Figure 4 The neural network model after int8 bit quantization compression.
[0089] Taking a layer in the above-mentioned pruning and fine-tuning neural network model as an example, KL divergence is used to measure the similarity between two distributions. The calculation formula is as follows:
[0090] ;
[0091] in, It is the sum of the KL divergences of the two distributions corresponding to the histogram of the calculation results obtained by unquantized and quantized methods for a certain layer of the neural network model after pruning and fine-tuning. is the probability distribution value of the convolution calculation result of the unquantized i-th convolution kernel, is the probability distribution value of the convolution calculation result of the i-th convolution kernel after selecting different quantization intervals, and N represents the number of convolution kernels in this layer.
[0092] S2. Select an FPGA development board, design a global architecture suitable for the neural network model based on the FPGA development board, add the description file of the global architecture to the device tree file, and generate a Linux system that can run on the FPGA development board;
[0093] In one embodiment, the neural network model in S1 includes an image preprocessing unit, a computing unit, and an image post-processing unit connected in sequence. In S2, a global architecture suitable for the neural network model is designed based on an FPGA development board. The specific design process of the global architecture is as follows:
[0094] S21. Select an FPGA development board. The FPGA development board includes an ARM side and an FPGA side that are interconnected. The ARM side and the FPGA side communicate through an AXI bus. The ARM side is responsible for image input and decoding, and the FPGA side is responsible for the preprocessing, calculation, and post-processing of the image by the neural network model.
[0095] S22, the ARM side includes an ARM controller, a DDR controller, a DDR memory and a hardware encoder. The original image is encoded by the hardware encoder and then input and stored in the DDR memory. The ARM controller calls the DDR memory through the DDR controller to write or read the original image.
[0096] Specifically, the original image may be video data, which is input frame by frame into the hardware encoder on the ARM side, encoded line by line using H.264 to obtain encoded image data, and the encoded image data is transmitted and stored in NV12 format.
[0097] S23. Design an image preprocessing kernel, a convolution calculation kernel, and an image post-processing kernel corresponding to the image preprocessing unit, the convolution calculation unit, and the image post-processing unit, respectively, on the FPGA side, connect the image preprocessing kernel to the input end of the convolution calculation kernel through the first on-chip DDR, and connect the image post-processing kernel to the output end of the convolution calculation kernel through the second on-chip DDR.
[0098] Specifically, three kernels of preprocessing, convolution calculation, and post-processing are designed on the FPGA development board. These three kernels correspond to the image preprocessing unit, calculation unit, and image post-processing unit of the neural network model respectively. These three kernels are used for image preprocessing, calculation, and image post-processing of the neural network model respectively.
[0099] S24, the image preprocessing kernel is connected to the DDR memory storing the image on the ARM side through AXI_M, reads the original image from the DDR memory and preprocesses it, and writes the preprocessed image into the first on-chip DDR on the FPGA side.
[0100] Furthermore, the image preprocessing kernel in S24 reads the original image from the DDR memory and preprocesses it, specifically including the following:
[0101] S241, converting the format of the input image to obtain an image in a converted format;
[0102] S242, adjusting the size of the converted image according to the neural network requirements;
[0103] S243, quantizing the number of data bits of the resized image to obtain a preprocessed image.
[0104] Specifically, the image preprocessing kernel converts the encoded image data stored in the NV12 format according to the format required by the neural network model after quantization and compression. Specifically, the input image format is converted into the BGR format, and then the image size in the BGR format is adjusted to the input size and bit width required by the neural network model, and the data bit number of the resized BGR format image is quantized to an 8-bit fixed-point number (int8), thereby obtaining the preprocessed image.
[0105] S25, the convolution calculation kernel reads the preprocessed image from the first on-chip DDR on the FPGA side, performs calculations through the on-chip BRAM, generates the target box position and confidence score for each frame of the image, and writes the target box position and confidence score for each frame of the image to the second on-chip DDR on the FPGA side.
[0106] Specifically, the neural network model parameters and weights are loaded from NAND Flash to the on-chip BRAM, and the convolution calculation kernel reads the pre-processed image data from the first on-chip DDR to the on-chip BRAM for calculation (the calculation kernel is composed of multiple calculation engines, each of which is composed of a group of multipliers and adders, and the calculation tasks are assigned by the FPGA driver. Each FPGA core represents a thread, so the thread parallelism can be used to implement pipeline design, without having to wait for a frame of image to be read before processing, thereby realizing loop expansion convolution ( Figure 5 ) and pipelined computing (see Figure 6), on-chip BRAM is used for input and output data cache, which can improve data reuse and reduce data access), the original image is input frame by frame, read by row, and copied and expanded for convolution calculation with each group of convolution kernels, and finally the position and confidence score of the target box corresponding to each frame image are generated, and finally the position and confidence score of the target box corresponding to each frame image are written back to the second on-chip DDR.
[0107] S26, the image post-processing kernel reads the target frame position and confidence score of each frame image from the second on-chip DDR of the FPGA end and processes them, and selects the prediction frame and the corresponding confidence score that meet the preset conditions.
[0108] Furthermore, S26 specifically includes the following:
[0109] S261, reading the target frame position and confidence score of each frame image from the second on-chip DDR on the FPGA end, and decoding the prediction frame of each frame image according to the target frame position and confidence score of each frame image;
[0110] S262, dequantizing the prediction frame of each frame of image into 32-bit data, and converting the position of the prediction frame of each frame of image into actual image coordinates;
[0111] S263. Filter out prediction boxes and corresponding confidence scores that meet preset conditions according to the confidence scores of the target boxes.
[0112] Specifically, the prediction boxes that meet the preset conditions are screened out according to the confidence scores of the target boxes, and the prediction boxes corresponding to the target boxes with higher confidence scores are screened out.
[0113] Specifically, see Figure 7 , taking the ZYNQ development board (ZYNQ development board is a general term for a series of development boards) as an example, it includes the ARM side and the FPGA side. The ARM side is responsible for image input and decoding. The three cores of preprocessing, convolution calculation, and post-processing designed on the FPGA development board are responsible for the preprocessing, calculation, and post-processing of the neural network model for images. Such a global architecture has the characteristics of high parallelism, convolution blocking, and loop unrolling, and has three dimensions of parallelism: pixel parallelism, input channel parallelism, and output channel parallelism. Add the description file of the designed global architecture to the device tree file to generate the Linux system running on the board. The specific process is as follows:
[0114] 1. Vivado platform design: Vivado design adds platform input and output parameters, adds IP cores, and IP uses AXI interface. Input and output parameters include: input feature map data stream, output feature map data stream, weight data stream and configuration data stream, where the configuration data stream includes input and output feature map dimension information and the type of the current layer. Provide the physical interface of the hardware to the Vitis compiler to create an extensible hardware platform. The main component of the hardware platform is the hardware description file generated by Vivado.
[0115] 2. VitisHLS design pre-processing IP and post-processing IP: Use high-level synthesis (HLS) to compile C / C++ code pre-processing and post-processing code into FPGA pre-processing IP and post-processing IP. The core is compiled into an xo file and used by the Vitis compiler in the next step.
[0116] 3. Cross-compile and add library functions: Use the cross-compilation tool SDK to add the packaged library functions to the hardware platform generated by Vivado and provide them to the Vitis compiler for use.
[0117] 4. Vitis generates a device image system: In Vitis, the FPGA core generated in HLS is integrated into the scalable hardware platform generated by Vivado, and finally a Linux system running on the board is generated.
[0118] S3. Burn the generated Linux system into the Nand Flash on the FPGA development board and power it on. Compile the compressed neural network model into a binary code file and deploy it to the FPGA development board to obtain the deployed FPGA development board.
[0119] Specifically, connect the camera to the FPGA development board (corresponding to Figure 8 Connect the FPGA development board to the ZYNQ development board in the image), connect the HDMI interface on the FPGA development board to the display, connect the network port on the FPGA development board to the server, power on and start, burn the generated Linux system to the Nand Flash on the FPGA development board, compile the quantized and compressed neural network model into a binary code file and transfer it to the FPGA development board, thus obtaining the deployed FPGA development board.
[0120] S4. Collect the image to be detected and input it to the deployed FPGA development board for processing, obtain the predicted box and confidence score of the target to be detected, load the predicted box and confidence score of the target to be detected onto the image to be detected, and transmit it to the display for display.
[0121] Specifically, the image to be detected is collected and input into the neural network model deployed on the FPGA development board. The FPGA development board performs real-time reasoning to obtain the prediction box and confidence score of the target to be detected, loads the prediction box and confidence score of the target to be detected onto the image to be detected, and transmits it to the display for display.
[0122] In one embodiment, see Figure 8 A neural network model deployment system based on FPGA acceleration includes a camera, a personal computer, a server, a display, and an FPGA development board. The FPGA development board is connected to the camera, the server, and the display respectively. The personal computer is connected to the server and the FPGA development board respectively through a router. The personal computer is also connected to the display through an HDMI converter. The FPGA development board is provided with a compressed neural network model and a Linux system that can run on the FPGA development board.
[0123] The camera is used to collect the image to be detected;
[0124] The server is used to train and compress the neural network model to obtain a compressed neural network model;
[0125] The personal computer is used to start the FPGA development board for inference;
[0126] The FPGA development board is used to receive the image to be detected and process it through the compressed network model and the Linux system that can run on the FPGA development board to obtain the predicted box and confidence score of the target to be detected, load the predicted box and confidence score of the target to be detected onto the image to be detected, and transmit it to the display for display.
[0127] The status of the FPGA development board can be monitored in real time through the network, and the quantized and compressed neural network model can be replaced online in real time as needed (because the trained and quantized compressed neural network model can only recognize one or several objects corresponding to the training. If you want to recognize other categories of objects, you need to retrain and compress the model before deploying it to the FPGA development board).
[0128] For the specific limitations of a neural network model deployment system based on FPGA acceleration, please refer to the limitations of a neural network model deployment method based on FPGA acceleration in the above text, which will not be repeated here.
[0129] The above-mentioned neural network model deployment method and system based on FPGA acceleration, selects a neural network model and trains it, compresses the trained neural network model to obtain a compressed neural network model, selects an FPGA development board, designs a global architecture suitable for the neural network model based on the FPGA development board, adds the description file of the global architecture to the device tree file, generates a Linux system that can run on the FPGA development board, burns the generated Linux system into the Nand Flash on the FPGA development board and powers on, compiles the compressed neural network model into a binary code file and deploys it to the FPGA development board, collects the image to be detected and inputs it to the FPGA development board, uses the compressed neural network model deployed on the FPGA development board for processing, obtains the prediction box and confidence score of the target to be detected, loads the prediction box and confidence score of the target to be detected onto the image to be detected, and transmits it to the display for display. The method has fast detection speed, low power consumption, high accuracy, and can be applied to low power consumption and low latency occasions.
[0130] The above is a detailed introduction to a neural network model deployment method and system based on FPGA acceleration provided by the present invention. This article uses specific examples to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the core idea of the present invention. It should be pointed out that for ordinary technicians in this technical field, without departing from the principles of the present invention, several improvements and modifications can be made to the present invention, and these improvements and modifications also fall within the scope of protection of the claims of the present invention.
Claims
1. A neural network model deployment method based on FPGA acceleration, characterized in that: The method comprises the following steps: S1, selecting a neural network model and training it, and compressing the trained neural network model to obtain a compressed neural network model; S1 specifically includes the following: S11, select a neural network model, obtain the image to be detected and create a data set, and store the data set on a server with a GPU; S12, pre-training the selected neural network model on a server with a GPU using the data set to obtain a pre-trained neural network model; S13, performing sparse training on the pre-trained neural network model, and calculating the model loss through a preset loss function to obtain a sparse weight; S14, pruning and fine-tuning the sparse weights to obtain a neural network model after pruning and fine-tuning; the specific process of S14 is as follows: S141, calculating the mean and variance of all parameters in the feature map parameter set; S142, calculating the average value of each feature map parameter after batch normalization according to the mean and variance; S143, training the feature map parameters and updating the scaling factor according to the batch-normalized average value of each feature map parameter; S144, setting a pruning rate according to the scaling factor and pruning the neural network model, and fine-tuning the pruned neural network model to obtain a pruned and fine-tuned neural network model; S15, quantizing and compressing the neural network model after pruning and fine-tuning to obtain a quantized and compressed neural network model; in S15, the neural network model after pruning and fine-tuning training is quantized and compressed, specifically using a KLD quantization method for quantization compression, and the method includes the following steps: S151, obtaining a computation graph of the neural network model after pruning and fine-tuning, inserting a pseudo-quantization operator into the computation graph, and obtaining a computation graph after the pseudo-quantization operator is inserted; S152, obtaining an unlabeled data set, and inputting the unlabeled data set into a calculation graph into which a pseudo-quantization operator is inserted to generate a calculation result distribution histogram; S153, calculating the probability distribution of the calculation result with the pseudo quantization operator inserted and the probability distribution of the calculation result without the pseudo quantization operator inserted according to the calculation result distribution histogram, and calculating the KL divergence of the two probability distributions; S154, selecting a quantized value range corresponding to the minimum KL divergence, and quantizing the weights and activation functions of the neural network model after pruning and fine-tuning according to the quantized value range to obtain a quantized and compressed neural network model; S2. Select an FPGA development board, design a global architecture suitable for the neural network model based on the FPGA development board, add the description file of the global architecture to the device tree file, and generate a Linux system that can run on the FPGA development board; S3, burning the generated Linux system into the Nand Flash on the FPGA development board and powering on, compiling the compressed neural network model into a binary code file and deploying it to the FPGA development board, obtaining the deployed FPGA development board; S4, collect the image to be detected and input it to the deployed FPGA development board for processing, obtain the prediction box and confidence score of the target to be detected, load the prediction box and confidence score of the target to be detected onto the image to be detected, and transmit it to the display for display; The neural network model in S1 includes an image preprocessing unit, a convolution calculation unit, and an image post-processing unit connected in sequence. In S2, a global architecture suitable for the neural network model is designed based on an FPGA development board. The specific design process of the global architecture is as follows: S21. Select an FPGA development board. The FPGA development board includes an ARM end, an FPGA end, and peripheral devices that are interconnected. The ARM end and the FPGA end communicate through an AXI bus. The ARM end is responsible for image input and decoding, and the FPGA end is responsible for image preprocessing, calculation, and post-processing by a neural network model. S22, the ARM side includes an ARM controller, a DDR controller, a DDR memory and a hardware encoder, the original image is encoded by the hardware encoder and then input and stored in the DDR memory, and the ARM controller calls the DDR memory through the DDR controller to write or read the original image; S23, designing an image preprocessing kernel, a convolution calculation kernel, and an image postprocessing kernel corresponding to the image preprocessing unit, the convolution calculation unit, and the image postprocessing unit, respectively, on the FPGA side, connecting the image preprocessing kernel to the input end of the convolution calculation kernel through the first on-chip DDR, and connecting the image postprocessing kernel to the output end of the convolution calculation kernel through the second on-chip DDR; S24, the image preprocessing kernel is connected to the DDR memory storing the image on the ARM side through AXI_M, reads the original image from the DDR memory and preprocesses it, and writes the preprocessed image into the first on-chip DDR on the FPGA side; S25, the convolution calculation kernel reads the preprocessed image from the first on-chip DDR on the FPGA side, performs calculations through the on-chip BRAM, generates the target frame position and confidence score of each frame image, and writes the target frame position and confidence score of each frame image to the second on-chip DDR on the FPGA side; S26, the image post-processing kernel reads the target frame position and confidence score of each frame image from the second on-chip DDR of the FPGA end and processes them, and selects the prediction frame and the corresponding confidence score that meet the preset conditions.
2. The method for deploying a neural network model based on FPGA acceleration as claimed in claim 1, characterized in that: The loss function preset in S13 is specifically: ; in, is the loss function of the neural network model after adding the scaling factor, represents the loss function of the neural network model, represents the convolution calculation, is the input feature map, is the convolution calculation result of the input feature map, is the weight parameter of the neural network model, For the A scaling factor, It is for Scaling Factor The penalty caused by the sparsity of is the penalty factor, is the total number of scaling factors.
3. The neural network model deployment method based on FPGA acceleration as claimed in claim 1, characterized in that: The image preprocessing kernel in S24 reads the original image from the DDR memory and preprocesses it, including the following: S241, converting the format of the input image to obtain an image in a converted format; S242, adjusting the size of the converted image according to the neural network requirements; S243, quantizing the number of data bits of the resized image to obtain a preprocessed image.
4. The method for deploying a neural network model based on FPGA acceleration as claimed in claim 3, characterized in that: S26 specifically includes the following: S261, reading the target frame position and confidence score of each frame of image from the second on-chip DDR on the FPGA end, and decoding the prediction frame of each frame of image according to the target frame position and confidence score of each frame of image; S262, dequantizing the prediction frame of each frame of image into 32-bit data, and converting the position of the prediction frame of each frame of image into actual image coordinates; S263. Filter out prediction boxes and corresponding confidence scores that meet preset conditions according to the confidence scores of the target boxes.
5. A neural network model deployment system based on FPGA acceleration, which is deployed using the neural network model deployment method based on FPGA acceleration as described in any one of claims 1 to 4, characterized in that: It includes a camera, a personal computer, a server, a display, and an FPGA development board. The FPGA development board is connected to the camera, the server, and the display respectively. The personal computer is connected to the server and the FPGA development board respectively through a router. The personal computer is also connected to the display through an HDMI converter. The FPGA development board is provided with a quantized compressed neural network model and a Linux system that can run on the FPGA development board. The camera is used to collect the image to be detected; The server is used to train and compress the neural network model to obtain a compressed neural network model; A personal computer is used to perform inference on the bootable FPGA development board; The FPGA development board is used to receive the image to be detected and process it through the compressed network model and the Linux system that can run on the FPGA development board to obtain the predicted box and confidence score of the target to be detected, load the predicted box and confidence score of the target to be detected onto the image to be detected, and transmit it to the display for display.
