Bilinear interpolation optimization method of residual layer single-channel image based on simd
By calculating and storing the coordinate relationship array and weight array in advance during the bilinear interpolation process, and using MXU3's gshufvb instruction for parallel calculations, the problem of redundant calculation in single-channel image bilinear interpolation is solved, and efficiency is improved.
Patent Information
- Application Number
- CN202310815411.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-07-04
- Publication Date
- 2025-01-10
AI Technical Summary
In the prior art, there is repeated calculation of coordinates and weights in the bilinear interpolation process of single-channel images, resulting in wasted time and redundant calculations. The traditional method can only operate on a single coordinate point, which is inefficient.
The coordinate relationship array and weight array are calculated in advance outside the loop of coordinate and weight calculations. The gshufvb instruction of MXU3 is used to obtain data in batches in the loop for parallel calculations to reduce redundant calculations.
Through early calculation and parallel processing, the calculation time is significantly reduced, the efficiency of bilinear interpolation is improved, redundant calculation is saved, and time performance is optimized.
Smart Images

Figure CN119295304A_ABST
Abstract
Description
Technical Field
[0001] The invention belongs to the technical field of data processing, and in particular relates to a bilinear interpolation optimization method for a resize layer single-channel image based on SIMD. Background Art
[0002] In the prior art, in the study of medical images, the images used are generally single-channel grayscale images, and the enlargement and reduction of grayscale images are routine operations in medical image preprocessing. The traditional method is to use bilinear interpolation to enlarge and reduce single-channel grayscale images, but bilinear interpolation operates on a single coordinate point, which not only makes the code redundant but also wastes a lot of time.
[0003] Therefore, the main defects of the prior art are:
[0004] Bilinear interpolation of single-channel images: Coordinates and weights are calculated repeatedly, which wastes time.
[0005] Bilinear interpolation of single-channel images: Only a single coordinate point can be processed at a time, which results in a lot of redundant calculations and wastes time.
[0006] In addition, the commonly used terms in the prior art include:
[0007] SIMD: single instruction multiple data, single instruction stream multiple data stream, one operation instruction can execute multiple data streams, which can improve the operation speed of the program. (Each register is 512 bits wide).
[0008] Bilinear interpolation: a commonly used image interpolation method, widely used in signal processing, digital image and video processing, etc. Bilinear interpolation is an extension of linear interpolation with two variable functions. Its core idea is to perform linear interpolation in two directions respectively.
[0009] Resize: Image zoom in and zoom out operations.
[0010] Weight array: An array that stores the weights corresponding to coordinate points.
[0011] Relation array: An array that stores the correspondence between the output image and the input image. Summary of the invention
[0012] In order to solve the above problems, the purpose of this application is: the four coordinate points and weights obtained by bilinear interpolation are calculated according to the output coordinate points and the scaling ratio, and the coordinate relationship array and the weight array are calculated in advance outside the coordinate and weight calculation loop. In the coordinate and weight calculation loop, the latest MXU3 (MXU is an integrated unit of SIMD512, 3 represents version 3) gshufvb instruction is used to batch obtain data from the coordinate relationship array, and batch read the corresponding number of weights from the weight array for calculation, thereby overcoming the shortcoming of slow bilinear interpolation calculation of single-channel images.
[0013] Specifically, the present invention provides a bilinear interpolation optimization method for a resize layer single-channel image based on SIMD, and the method comprises the following steps:
[0014] S1, calculate the maximum number of output data width_cycs for a batch processing:
[0015] S1.1, since the weight is float data, one float data is 32 bits, and one register can only store 512 bits of data at most, that is, 16 float data, so the maximum number of output data that can be processed at one time width_cycs is 16;
[0016] S1.2, the register read bit format is only o / q / d / w, where o: 256bit, q: 128bit, d: 64bit, w: 32bit, so the number of output data that can be processed at one time width_cycs can only be 1, 2, 4, 8, 16;
[0017] S1.3, the input data is obtained through the relation array through the gshfvb instruction in MXU3 to participate in the calculation. The gshfvb instruction is expressed as: Gshufvb (vrd, vrs, vrp). The gshufvb instruction obtains the data of the corresponding coordinate points of the input image through the lower 6 bits of each byte. The number that 6 bits can represent is 0-63. Therefore, the interpolation of the maximum and minimum values of each batch of data in the relation array is set to be no higher than 63. The minimum value is subtracted from all the data in the data to obtain a new relation array;
[0018] Therefore, when the input data is 8 bits, the calculation formula for width_cycs is:
[0019] Width_scale=(float)src_w / (float)dst_w;
[0020] Height_scale=(float)src_h / (float)dst_h;
[0021] Width_cycs=(int)64 / width_scale;
[0022] S2, according to the size of width_cycs, store the coordinate correspondence and weight into the relationship array and weight array in advance to save calculation time;
[0023] X and Y directions:
[0024]
[0025]
[0026] Therefore, the weights are stored in the weight array in advance, and the corresponding relationship between the output and the input is stored in the relationship array;
[0027] S3, then use the relational array to obtain the corresponding input data for parallel calculation. The gshufvb instruction in MXU3 implements this operation (same as step S1.3), that is, the gshufvb instruction is used to obtain data from the relational array and input data in batches for calculation. The specific instruction implementation and formula are as follows:
[0028] _mx512_gshufvb(VR0,W_pixel_0,src_base);
[0029] W_pixel_0 is the calculated relational array, src_base is the input address, VR0 is the storage location of the number obtained from the input address according to the relational array;
[0030] The formula is: VR0[i]=src_base[W_pixel_0[i]].
[0031] In step S1.3, the width_cycs and the relational expression are as follows:
[0032]
[0033]
[0034] For ease of understanding, assume that src_w = 64, dst_w = 6, width_scale = 10.667, and then proceed as follows:
[0035] Width_scale 10.667 dst coordinate in the x direction 0 1 2 3 4 5 6 Dst is the y-coordinate 0 Src in the x direction 0 10 21 32 42 53 64 Src in the y direction 0
[0036] When width_cycs is equal to 6, the maximum and minimum values of the src coordinates stored in a batch are already greater than 63, so according to the above relationship, width_cycs is equal to 4, and 4 coordinate points are calculated at a time in a batch.
[0037] The gshufvb instruction is represented as: Gshufvb(vrd, vrs, vrp), where for each byte element in vrs, 1 byte is selected from the 64-byte elements in vrp using its lower 6-bits, and these bytes are used to update the corresponding byte element in vrd;
[0038] Furthermore, if the most significant bit of a byte element in vrs is 0, then simply write 0 to the corresponding byte element in vrd:
[0039]
[0040] Gshufvb uses the lower 6 bits of the byte to obtain data, and can only obtain numbers with coordinates 0-63, so the input data reads 64 numbers at a time, from the 0th coordinate to the 63rd coordinate. For ease of understanding, let the stored value correspond to the coordinate point value, the 0th coordinate stores 0, and the 63rd coordinate stores 63;
[0041] W_pixel_0 0 10 21 32,
[0042] The data stored in the relation array is calculated in advance in step S2;
[0043] The relational array stores the src coordinates of W_pixel_0. Use the gshvb instruction gshvb(src_js, w_pixel_0, src) to directly obtain the input data src[0], src
[10] , src
[21] , src
[32] , and store the data in src_js[0], src_js[1], src_js[2], src_js[3] of the src_js array respectively.
[0044] Src_js 0 10 21 32,
[0045] The input data participating in the calculation of the output data is calculated by the above-mentioned formula of gshufvb; thus, four output values are obtained in one parallel operation.
[0046] In step S3, when the relation array stores 16 data, the time optimization efficiency is the highest.
[0047] Furthermore, in the method:
[0048] The underlying calculation logic of the resize layer is:
[0049] 1) Get the scaling ratio in the w direction:
[0050] Width_scale=(float)src_w / (float)dst_w;
[0051] 2) Get the scaling ratio in the h direction:
[0052] Height_scale=(float)src_h / (float)dst_h;
[0053] Calculate the horizontal and vertical coordinates of the four selected points and the corresponding weights:
[0054] 1) Calculate the x-direction coordinate point:
[0055] x0 = (int)x * width_scale;
[0056] 2) The second coordinate point in the x direction is the first coordinate point in the x direction plus 1:
[0057] x1=x0+1;
[0058] 3) Determine whether it is out of bounds:
[0059] X1=x1>=dst_w? dst_w:x1;
[0060] 4) Calculate the weight w0:
[0061] W0 = x * width_scale - x0;
[0062] 5) Calculate the weight w1:
[0063] W1=1-w0;
[0064] 6) Calculate the first coordinate point in the y direction:
[0065] y0 = (int)y * height_scale;
[0066] 7) The second coordinate point in the y direction is the first coordinate point in the y direction plus 1:
[0067] y1=y0+1;
[0068] 8) Determine whether it is out of bounds:
[0069] y1=y1>=dst_h? dst_h:y1;
[0070] 9) Calculate the weight h0
[0071] H0 = y * height_scale - y0;
[0072] 10) Calculate the weight h1
[0073] h1=1-h0;
[0074] The pixel value calculation:
[0075] Src: Input data
[0076] Dst: output data
[0077] dst(x,y)=(w0*src(x0,y0)+w1*src(x1,y0))*h0+(w0*src(x0,y1)+w1*src(x1,y1))*h1.
[0078] Therefore, the advantages of this application are:
[0079] 1. Calculate the correspondence between the weights and image output images and the input images in advance outside the loop, and store them in the weight array and relationship array to reduce calculation redundancy and optimize time efficiency.
[0080] 2. Use the gshufvb instruction of MXU3 to perform parallel calculations on multiple data, greatly reducing the calculation time and optimizing time efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0081] The drawings described herein are used to provide a further understanding of the present invention, constitute a part of this application, and do not constitute a limitation of the present invention.
[0082] Figure 1 It is a flow chart of the present application method. DETAILED DESCRIPTION
[0083] In order to more clearly understand the technical content and advantages of the present invention, the present invention is now further described in detail in conjunction with the accompanying drawings.
[0084] In view of the problems of the prior art, the present invention proposes a bilinear interpolation optimization method of a resize layer single-channel image based on SIMD to solve the problems.
[0085] The underlying calculation logic of the Resize layer is:
[0086] 1) Get the scaling ratio in the w direction:
[0087] Width_scale=(float)src_w / (float)dst_w;
[0088] 2) Get the scaling ratio in the h direction:
[0089] Height_scale=(float)src_h / (float)dst_h;
[0090] Calculate the horizontal and vertical coordinates of the four selected points and the corresponding weights:
[0091] 1) Calculate the x-direction coordinate point:
[0092] x0 = (int)x * width_scale;
[0093] 2) The second coordinate point in the x direction is the first coordinate point in the x direction plus 1:
[0094] x1=x0+1;
[0095] 3) Determine whether it is out of bounds:
[0096] X1=x1>=dst_w? dst_w:x1;
[0097] 4) Calculate the weight w0:
[0098] W0 = x * width_scale - x0;
[0099] 5) Calculate the weight w1:
[0100] W1=1-w0;
[0101] 6) Calculate the first coordinate point in the y direction:
[0102] y0 = (int)y * height_scale;
[0103] 7) The second coordinate point in the y direction is the first coordinate point in the y direction plus 1:
[0104] y1=y0+1;
[0105] 8) Determine whether it is out of bounds:
[0106] y1=y1>=dst_h? dst_h:y1;
[0107] 9) Calculate the weight h0
[0108] H0 = y * height_scale - y0;
[0109] 10) Calculate the weight h1
[0110] h1=1-h0;
[0111] For each output image point, it is necessary to calculate the corresponding four bilinear interpolation coordinate point values and their corresponding weights in the original image. Too much repeated calculation wastes a lot of time.
[0112] Pixel value calculation:
[0113] Src: Input data
[0114] Dst: output data
[0115] dst(x,y)=(w0*src(x0,y0)+w1*src(x1,y0))*h0+(w0*src(x0,y1)+w1*src(x1,y1))*h1;
[0116] The optimization method is as follows:
[0117] The four coordinate points and weights obtained by bilinear interpolation are calculated based on the output coordinate points and the scaling ratio. The coordinate relationship array and weight array are calculated in advance outside the coordinate and weight calculation loop. In the coordinate and weight calculation loop, the latest MXU3 gshufvb instruction is used to batch obtain the coordinate point values from the coordinate relationship array and batch read the corresponding number of weights from the weight array for calculation, which can save a lot of redundant calculations and optimize time. Among them, MXU is the integrated unit of SIMD512, and 3 represents version 3.
[0118] Specific implementation is as follows Figure 1 As shown, including:
[0119] S1, calculate the maximum number of output data width_cycs for a batch processing:
[0120] S1.1, since the weight is float data (a float data is 32 bits), a register can only store 512 bits of data at most, that is, 16 float data, so the maximum number of output data that can be processed at one time width_cycs is 16.
[0121] S1.2, the register read bit format is only o(256bit) / q(128bit) / d(64bit) / d(32bit), so the number of output data that can be processed at one time width_cycs can only be 1, 2, 4, 8, 16.
[0122] S1.3, optimization is to obtain the input data for calculation through the relation array through the gshfvb instruction in MXU3. The gshufvb instruction obtains the data of the corresponding coordinate points of the input image through the lower 6 bits of each byte. The number that 6 bits can represent is 0-63. Therefore, the interpolation of the maximum and minimum values of each batch of data in the relation array is set to no higher than 63. The minimum value is subtracted from all the data in the data to obtain a new relation array.
[0123] Therefore, when the input data is 8 bits, the calculation formula of width_cycs is:
[0124] Width_scale=(float)src_w / (float)dst_w;
[0125] Height_scale=(float)src_h / (float)dst_h;
[0126] Width_cycs=(int)64 / width_scale;
[0127] Relational Width_cycs Greater than or equal to 2 Less than 4 2 Greater than or equal to 4 Less than 8 4 Greater than or equal to 8 Less than 16 8 Greater than 16 16
[0128] For ease of understanding, the following explanation is given using src_w = 64, dst_w = 6, width_scale = 10.667 as an example:
[0129]
[0130]
[0131] S2, according to the size of width_cycs, the coordinate correspondence and weight are stored in the relationship array and weight array in advance, which can save a lot of calculation time. Take the x and y directions as an example:
[0132]
[0133]
[0134] Therefore, the weights are stored in the weight array in advance, and the corresponding relationship between the output and the input is stored in the relationship array.
[0135] S3, then you need to use the relational array to obtain the corresponding input data for parallel calculation. The gshufvb instruction in the latest version of MXU3 can achieve this operation. The gshufvb instruction and formula are introduced as follows:
[0136] _mx512_gshufvb(VR0,W_pixel_0,src_base);
[0137] W_pixel_0 is the relational array for calculation, src_base is the input address, and VR0 is the storage location of the number obtained from the input address according to the relational array.
[0138] The formula is: VR0[i]=src_base[W_pixel_0[i]];
[0139] The specific implementation process is as follows:
[0140]
[0141]
[0142] As a result, four output values can be calculated in parallel at one time, reducing redundant calculations and optimizing time efficiency.
[0143] The above method uses the correspondence between the output image and the input image, stores the weights and relationship arrays in the array in advance, and then uses the gshufvb instruction to batch obtain data from the relationship array and input data for calculation. When the relationship array stores 16 data, the time optimization efficiency is the highest, reaching 93% (86ms->6ms).
[0144] In summary, this method is to:
[0145] 1. The four coordinate points and weights obtained by bilinear interpolation are calculated based on the output coordinate points and the scaling ratio. The weights and the corresponding relationship between the output image and the input image are calculated in advance outside the coordinate and weight calculation loop and stored in the weight array and relationship array;
[0146] 2. In the coordinate and weight calculation loop, by using the MXU3 gshufvb instruction, the coordinate point values are batch-acquired from the coordinate relationship array and the corresponding number of weights are batch-read from the weight array to perform parallel calculations on multiple data.
[0147] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, the embodiments of the present invention may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
Claims
1. A bilinear interpolation optimization method for a resize layer single-channel image based on SIMD, characterized in that: The method comprises the following steps: S1, calculate the maximum number of output data width_cycs for a batch processing: S1.1, since the weight is float data, one float data is 32 bits, and one register can only store 512 bits of data at most, that is, 16 float data, so the maximum number of output data that can be processed at one time width_cycs is 16; S1.2, the register read bit format is only o / q / d / w, where o: 256bit, q: 128bit, d: 64bit, w: 32bit, so the number of output data that can be processed at one time width_cycs can only be 1, 2, 4, 8, 16; S1.3, the input data is obtained through the relation array through the gshfvb instruction in MXU3 to participate in the calculation. The gshfvb instruction is expressed as: Gshufvb (vrd, vrs, vrp). The gshufvb instruction obtains the data of the corresponding coordinate points of the input image through the lower 6 bits of each byte. The number that 6 bits can represent is 0-63, so the interpolation of the maximum and minimum values of each batch of data in the relation array is set to be no higher than 63, and the minimum value is subtracted from all the data in the data to obtain a new relation array; Therefore, when the input data is 8 bits, the calculation formula for width_cycs is: Width_scale=(float)src_w / (float)dst_w; Height_scale=(float)src_h / (float)dst_h; Width_cycs=(int)64 / width_scale; S2, according to the size of width_cycs, store the coordinate correspondence and the weights in the w direction and the h direction into the relationship array and weight array in advance to save calculation time; X and Y directions: Therefore, the weights are stored in the weight array in advance, and the corresponding relationship between the output and the input is stored in the relationship array; S3, then use the relational array to obtain the corresponding input data for parallel calculation. The gshufvb instruction in MXU3 implements this operation, that is, the gshufvb instruction is used to obtain data from the relational array and input data in batches for calculation. The specific instruction is implemented as follows: _mx512_gshufvb(VR0,W_pixel_0,src_base); W_pixel_0 is the calculated relational array, src_base is the input address, VR0 is the storage location of the number obtained from the input address according to the relational array; The formula is: VR0[i]=src_base[W_pixel_0[i]].
2. According to a bilinear interpolation optimization method for a resize layer single-channel image based on SIMD according to claim 1, it is characterized in that: In step S1.3, the width_cycs and the relational expression are as follows: Assume src_w = 64, dst_w = 6, width_scale = 10.667, and then proceed to the following description: When width_cycs is equal to 6, the maximum and minimum values of the src coordinates stored in a batch are already greater than 63, so according to the above relationship, width_cycs is equal to 4, and 4 coordinate points are calculated at a time in a batch.
3. According to claim 2, a bilinear interpolation optimization method for a resize layer single-channel image based on SIMD, characterized in that: The gshufvb instruction is represented as: Gshufvb(vrd, vrs, vrp), where for each byte element in vrs, 1 byte is selected from the 64-byte elements in vrp using its lower 6-bits, and these bytes are used to update the corresponding byte element in vrd; Furthermore, if the most significant bit of a byte element in vrs is 0, then simply write 0 to the corresponding byte element in vrd: Gshufvb uses the lower 6 bits of the byte to obtain data, and can only obtain numbers with coordinates 0-63, so the input data reads 64 numbers at a time, from the 0th coordinate to the 63rd coordinate. For ease of understanding, let the stored value correspond to the coordinate point value, the 0th coordinate stores 0, and the 63rd coordinate stores 63; W_pixel_0 0 10 21 32 The data stored in the relation array is calculated in advance in step S2; The relational array stores the src coordinates of W_pixel_0. Use the gshvb instruction gshvb(src_js, w_pixel_0, src) to directly obtain the input data src[0], src[10], src[21], src[32], and store the data in src_js[0], src_js[1], src_js[2], src_js[3] of the src_js array respectively. Src_js 0 10 21 32, Input data: Data involved in the calculation of output data, calculated by the formula of gshufvb in step S2; Thus, four output values are obtained in one parallel operation.
4. According to a bilinear interpolation optimization method for a resize layer single-channel image based on SIMD according to claim 1, it is characterized in that: In step S3, when the relation array stores 16 data, the time optimization efficiency is the highest.
5. According to a bilinear interpolation optimization method for a resize layer single-channel image based on SIMD according to claim 1, it is characterized in that: In the method: The underlying calculation logic of the resize layer is: 1) Get the scaling ratio in the w direction: Width_scale=(float)src_w / (float)dst_w; 2) Get the scaling ratio in the h direction: Height_scale=(float)src_h / (float)dst_h; Calculate the horizontal and vertical coordinates of the four selected points and the corresponding weights: 1) Calculate the x-direction coordinate point: x0 = (int)x * width_scale; 2) The second coordinate point in the x direction is the first coordinate point in the x direction plus 1: x1=x0+1; 3) Determine whether it is out of bounds: X1=x1>=dst_w? dst_w:x1; 4) Calculate the weight w0: W0 = x * width_scale - x0; 5) Calculate the weight w1: W1=1-w0; 6) Calculate the first coordinate point in the y direction: y0 = (int)y * height_scale; 7) The second coordinate point in the y direction is the first coordinate point in the y direction plus 1: y1=y0+1; 8) Determine whether it is out of bounds: y1=y1>=dst_h? dst_h:y1; 9) Calculate the weight h0 H0 = y * height_scale - y0; 10) Calculate the weight h1 h1=1-h0; The pixel value calculation: Src: Input data Dst: output data dst(x,y)=(w0*src(x0,y0)+w1*src(x1,y0))*h0+(w0*src(x0,y1)+w1*src(x1,y1))*h1.