基于门控线性注意力机制的图像分类识别方法与装置
By using an image classification and recognition method based on a gated linear attention mechanism, the problem of insufficient combination of global and local information in existing technologies is solved, achieving efficient and accurate image classification and recognition, reducing computational complexity and improving accuracy.
Patent Information
- Application Number
- CN202410664140.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-27
- Publication Date
- 2026-11-24
- Estimated Expiration
- 2044-05-27
AI Technical Summary
Existing image classification and recognition methods are limited by the inherent defects of Transformer and CNN, which cannot efficiently combine global and local information, resulting in high computational complexity and insufficient accuracy when processing high-resolution images.
An image classification and recognition method based on a gated linear attention mechanism is adopted. By introducing a linear attention mechanism, a direction-sensitive bidirectional modeling, and a hybrid long and short attention mechanism, global context perception and local information capture of visual sequences are achieved. Combined with local information encoding, a direction-sensitive gated linear attention layer, and a 2D gated hybrid layer, image features are dynamically mixed.
It achieves efficient and accurate image classification and recognition with low computational load and high hardware sensitivity, and can obtain superior accuracy at various resolutions, reducing storage and transportation costs.
Smart Images

Figure CN118397375B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of computer vision, and more specifically, relates to an image classification and recognition method and apparatus based on a gated linear attention mechanism. Background Technology
[0002] In the field of computer vision, image classification and recognition methods are the core research direction. They are used as the underlying visual processors for various visual tasks, supporting a wide range of applications.
[0003] However, current fundamental visual network designs are based on Transformers and Convolutional Neural Networks (CNNs). Traditional CNNs excel at extracting local features, efficiently capturing local details of images through local convolution operations and weight sharing mechanisms. However, the receptive field of CNNs is fixed, limiting their ability to capture global information. CNN models often perform poorly when handling tasks requiring global contextual understanding. Furthermore, computational complexity and memory consumption increase significantly with network depth. Transformer models introduced self-attention mechanisms, achieving breakthroughs in natural language processing and computer vision. Self-attention mechanisms capture global contextual information by calculating the correlation between each element in the input sequence and other elements. However, Transformer's self-attention mechanism has quadratic complexity, meaning the computational cost increases quadratically with the length of the input sequence, becoming extremely expensive when processing high-resolution images. Although some improved methods (such as local attention and hierarchical attention) attempt to reduce computational complexity, they still have certain shortcomings in capturing global contextual information.
[0004] Existing image classification and recognition methods are limited by the inherent defects of Transformer and CNN, and cannot provide efficient linear global perception capabilities. How to design a basic image classification and recognition method that can overcome the defects of Transformer and CNN and combine their advantages is a crucial and exploratory proposition. Summary of the Invention
[0005] To address the shortcomings of existing technologies and the aforementioned improvement needs, this method aims to design a novel image classification and recognition method with linear complexity and a global receptive field. This invention provides an economical, efficient, linear, and global visual network based on a gated linear attention mechanism. This method primarily relies on a gated linear attention mechanism, bidirectional modeling, and a hybrid long and short attention mechanism encoding module. By introducing a linear attention mechanism, a linear processing operator independent of CNNs is achieved; by introducing direction-sensitive bidirectional modeling, global contextual awareness of visual sequences is realized; and by introducing long and short attention mechanisms, the model can perceive two-dimensional local information. This method can perform efficient and accurate image classification and recognition.
[0006] To achieve the above objectives, this invention provides an image classification and recognition method based on a gated linear attention mechanism, comprising the following steps:
[0007] (1) Image embedding processing, including the following sub-steps:
[0008] (1.1) Divide the image to be identified into blocks and flatten them into an image sequence;
[0009] (1.2) Input the image sequence into the image sequence embedding layer and process it to obtain the image feature sequence;
[0010] (2) Input the image feature sequence into the image basic processing network constructed by stacking ViG basic processing units to obtain a new image feature sequence. The processing of the ViG basic processing unit includes the following sub-steps:
[0011] (2.1) Encode local information into the image feature sequence in the local information coding layer to obtain an image feature sequence containing local information;
[0012] (2.2) Input the image feature sequence containing local information into the direction-sensitive gated linear attention layer, and obtain the image feature sequence containing 1D global information through the gated linear attention layer;
[0013] (2.3) Input the image feature sequence containing 1D global information into the 2D gated mixing layer, and obtain the dynamically mixed image feature sequence by dynamically mixing the 1D global information through the gating mechanism;
[0014] (2.4) Input the dynamically mixed image feature sequence into the gated channel mixing layer, and perform feature mixing on the input image feature sequence at the channel level to obtain a new image feature sequence;
[0015] (3) Input the new image feature sequence into the pooling layer for average pooling, and input the average pooled features into the linear layer for image category prediction.
[0016] In one embodiment of the present invention, step (1.2) specifically comprises:
[0017] The image sequence is input into the image sequence embedding layer to obtain the image feature sequence X∈R. T×d , where T is the length of the image sequence, d is the number of feature channels, and R represents the real number field.
[0018] In one embodiment of the present invention, step (2.1) specifically comprises:
[0019] The image feature sequence is input into the local information coding layer, i.e., X∈R T×d Input to a 3×3 grouped convolution DWConv 3×3 In the middle, short-term local 2D information is captured to obtain an image feature sequence X containing local information. local =DWConv 3×3 (X).
[0020] In one embodiment of the present invention, step (2.2) specifically comprises:
[0021] The image feature sequence containing local information is input into a direction-sensitive gated linear attention layer. First, the linear layer outputs a query matrix Q, a key matrix K, and a value matrix V. These matrices are calculated as follows:
[0022] Q,K,V=X local W Q ,X local W K ,X local W V
[0023] in and It is a trainable weight matrix, where Q = [q0, ..., q t ,…,q T-1 ],K=[k0,…,k t ,…,k T-1 ],V=[v0,…,v t ,…,v T-1 ],X local =[x0,…,x t ,…,x T-1 ],q t k t v t x t , representing the feature sequences Q, K, V, X local The t-th feature in the image sequence refers to the word x at the current time t. t Calculate direction-sensitive gating:
[0024]
[0025]
[0026] in, It is a forward gating system, used to control the flow of information from front to back. It is a reverse gating system used to control the information flow from back to front, thereby calculating the forward and reverse compression states for the current word:
[0027]
[0028]
[0029]
[0030] Then the current q t Interacting with the compressed state to obtain the global receptive field:
[0031]
[0032]
[0033]
[0034] The final image feature sequence containing 1D global information is obtained. t .
[0035] In one embodiment of the present invention, step (2.3) specifically comprises:
[0036] Image feature sequences containing 1D global information are input into a 2D gated mixing layer to dynamically adjust the 1D global information O and the 2D local information X. local The fusion first predicts the proportion of 2D information injected through a linear layer:
[0037] G 2D =sigmoid(X local W 2D +b 2D )
[0038] in These are trainable weights, b 2D It is a trainable bias, which yields a 2D gated G. 2D Finally, the two are dynamically blended to obtain a dynamically blended image feature sequence:
[0039] X hy =G 2D X local +(1-G 2D )⊙O.
[0040] In one embodiment of the present invention, step (2.4) specifically comprises:
[0041] Dynamically blended image feature sequences X hy The input is fed into a gated channel mixing layer to mix information from different channels. First, it is mapped to Y and Z via a linear layer:
[0042] Y, Z = Linear(X) hy )
[0043] Then, a new image feature sequence X is obtained by channel mixing and linear transformation through a gating mechanism. final :
[0044] X gate =Y*Z,
[0045] X final =Linear(X) gate )
[0046] In one embodiment of the present invention, step (3) specifically comprises:
[0047] The image feature sequence X output by the ViG basic processing unit in the last layer of the image basic processing network. final ∈R T×d Perform pooling and use a linear layer to predict the final class score:
[0048] O final =avgpool(X final )∈R 1×d
[0049] y = sigmoid(O final W cls +b cls )∈R 1×C
[0050] Among them O final y represents the pooled features, y represents the final class confidence, and C represents the number of classes.
[0051] In one embodiment of the present invention, the method further includes: kernel function fusion, which fuses the bidirectional attention scans in the basic vision processing unit into a kernel function to improve running speed and reduce memory usage.
[0052] According to another aspect of the present invention, an image classification and recognition device based on a gated linear attention mechanism is also provided, including at least one processor and a memory, wherein the at least one processor and the memory are connected via a data bus, and the memory stores instructions that can be executed by the at least one processor, wherein the instructions, after being executed by the processor, are used to complete the image classification and recognition method based on the gated linear attention mechanism.
[0053] In summary, the technical solutions conceived by this invention have the following beneficial effects compared with the prior art:
[0054] (1) High accuracy: This invention introduces multiple gating mechanisms for the design of basic image classification and recognition method models, providing robust global spatial perception capabilities;
[0055] (2) High speed: The model based on the gated linear attention mechanism proposed in this invention has low computational cost and high hardware awareness. It can be significantly accelerated by reducing the cost of storage and transportation.
[0056] (3) Robustness: The invention can achieve superior accuracy at various resolutions. Attached Figure Description
[0057] Figure 1 This is a flowchart of an image classification and recognition method based on a gated linear attention mechanism according to the present invention, wherein the solid arrows represent the model inference / training process;
[0058] Figure 2 This is a flowchart of a basic processing unit based on a linear attention mechanism according to the present invention;
[0059] Figure 3 This is a flowchart of a direction-sensitive bidirectional gated linear attention layer according to the present invention. Detailed Implementation
[0060] To make the objectives, technical solutions, and advantages of this invention clearer, the invention will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the invention. Furthermore, the technical features involved in the various embodiments of this invention described below can be combined with each other as long as they do not conflict with each other.
[0061] The technical terminology of this invention will be explained and described below:
[0062] Transformer: A sequence modeling layer that uses a self-attention mechanism and has quadratic complexity.
[0063] RMSNorm: A regularization technique used to improve the expressiveness of methods.
[0064] like Figure 1 As shown, the image classification and recognition method based on the gated linear attention mechanism of the present invention includes the following sub-steps:
[0065] (1) Image embedding processing, including the following sub-steps:
[0066] (1.1) Divide the image to be identified into blocks and flatten them into an image sequence;
[0067] (1.2) Input the image sequence into the image sequence embedding layer and process it to obtain the image feature sequence;
[0068] Specifically, the image sequence is input into the image sequence embedding layer to obtain the image feature sequence X∈R. T×d , where T is the length of the image sequence, d is the number of feature channels, and R represents the real number field.
[0069] (2) Input the image feature sequence into the image basic processing network constructed by stacking ViG basic processing units to obtain a new image feature sequence. The processing of the ViG basic processing unit includes the following sub-steps:
[0070] (2.1) Encode local information into the image feature sequence in the local information coding layer to obtain an image feature sequence containing local information;
[0071] Specifically, in the local information coding layer, local information is encoded into the image feature sequence to obtain an image feature sequence containing local information. That is, the image feature sequence is input into the local information coding layer, i.e., X∈R. T×d Input to a 3×3 grouped convolution DWConv 3×3 In the middle, short-term local 2D information is captured to obtain an image feature sequence X containing local information. local :
[0072] X local =DWConv 3×3 ((X)
[0073] (2.2) Input the image feature sequence containing local information into the direction-sensitive gated linear attention layer, and obtain the image feature sequence containing 1D global information through the gated linear attention layer;
[0074] Specifically, the image feature sequence X containing locally novel image feature sequences local The input is fed into a direction-sensitive gated linear attention layer. First, the linear layer outputs a query matrix Q, a key matrix K, and a value matrix V. These matrices are calculated as follows:
[0075] Q,K,V=Xlocal W Q ,X local W K ,X local W V
[0076] in and It is a trainable weight matrix. Where Q = [q0, ..., q t ,…,q T-1 ],K=[k0,…,k t ,…,k T-1 ],V=[v0,…,v t ,…,v T-1 ],X local =[x0,…,x t ,…,x T-1 ],q t k t v t x t , representing the feature sequences Q, K, V, X local The t-th feature in the image sequence refers to the word x at the current time t. t Calculate direction-sensitive gating:
[0077]
[0078]
[0079] in, It is a forward gating system, used to control the flow of information from front to back. It is a reverse gating system used to control the information flow from back to front, thereby calculating the forward and reverse compression states for the current word:
[0080]
[0081]
[0082]
[0083] Then the current q t Interacting with the compressed state to obtain the global receptive field:
[0084]
[0085]
[0086]
[0087] The final image feature sequence containing 1D global information is obtained.t .
[0088] (2.3) Input the image feature sequence containing 1D global information into the 2D gated mixing layer, and obtain the dynamically mixed image feature sequence by dynamically mixing the 1D global information through the gating mechanism;
[0089] Specifically, the image feature sequence O = [o0, ..., o0] containing 1D global information is... t , ..., o T-1 The input is fed into a 2D gated hybrid layer to dynamically adjust the 1D global information O and the 2D local information X. local The fusion first predicts the proportion of 2D information injected through a linear layer:
[0090] G 2D =sigmoid(X local W 2D +b 2D )
[0091] in These are trainable weights, b 2D It is a trainable bias, which yields a 2D gated G. 2D Ultimately, the two are dynamically mixed:
[0092] X hy =G 2D X local +(1-G 2D )☉O
[0093] (2.4) Input the dynamically mixed image feature sequence into the gated channel mixing layer, and perform feature mixing on the input image feature sequence at the channel level to obtain a new image feature sequence;
[0094] Specifically, the dynamically blended image feature sequence X hy The input is fed into a gated channel mixing layer to mix information from different channels. First, it is mapped to Y and Z via a linear layer:
[0095] Y, Z = Linear(X) hy )
[0096] Then, a new image feature sequence X is obtained by channel mixing and linear transformation through a gating mechanism. final :
[0097] X gate =Y*Z,
[0098] X final =Linear(X) gate )
[0099] (3) Input the new image feature sequence into the pooling layer for average pooling, and input the average pooled features into the linear layer for image category prediction;
[0100] Specifically, the image feature sequence X output by the ViG basic processing unit in the last layer of the image basic processing network is... final ∈R T×d Perform pooling and use a linear layer to predict the final class score:
[0101] O final =avgpool(X final )∈R 1×d
[0102] y = sigmoid(O final W cls +b cls )∈R 1×C
[0103] Among them O final y represents the pooled features, y represents the final class confidence, and C represents the number of classes.
[0104] Furthermore, the method also includes kernel function fusion, which merges the bidirectional attention scans in the basic vision processing unit into a single kernel function to improve running speed and reduce memory usage.
[0105] Specifically, the bidirectional attention scanning in the basic vision processing unit is integrated into a single kernel function, and all computations are performed on-chip to avoid writing the added inverse results to a slow cache, which would slow down the model.
[0106] Furthermore, the present invention also provides an image classification and recognition device based on a gated linear attention mechanism, including at least one processor and a memory, wherein the at least one processor and the memory are connected via a data bus, and the memory stores instructions that can be executed by the at least one processor. After being executed by the processor, the instructions are used to complete the image classification and recognition method based on the gated linear attention mechanism.
[0107] Those skilled in the art will readily understand that the above description is merely a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of the present invention should be included within the scope of protection of the present invention.
Claims
1. An image classification and recognition method based on a gated linear attention mechanism, characterized in that, include: (1) Image embedding processing, including the following sub-steps: (1.1) Divide the image to be identified into blocks and flatten them into an image sequence; (1.2) Input the image sequence into the image sequence embedding layer and process it to obtain the image feature sequence; Specifically, the image sequence is input into the image sequence embedding layer to obtain the image feature sequence. ,in It is the length of the image sequence. It is the number of feature channels. Represents the real number field; (2) Input the image feature sequence into the image basic processing network constructed by stacking ViG basic processing units to obtain a new image feature sequence. The processing of the ViG basic processing unit includes the following sub-steps: (2.1) Encode local information into the image feature sequence in the local information coding layer to obtain an image feature sequence containing local information; (2.2) Input the image feature sequence containing local information into a direction-sensitive gated linear attention layer to obtain an image feature sequence containing 1D global information; step (2.2) specifically involves: The image feature sequence containing local information is input into a direction-sensitive gated linear attention layer. The query is first output through the linear layer. ,key Sum Matrices, and these matrices are calculated as follows: in , and It is a trainable weight matrix, where , , , , , , , , representing the characteristic sequence , , , The Middle One feature, targeting the current time in the image sequence. Lexicon Calculate direction-sensitive gating: in, It is a forward gating system, used to control the flow of information from front to back. It is a reverse gating system used to control the information flow from back to front, thereby calculating the forward and reverse compression states for the current word: Then the current Interacting with the compressed state to obtain the global receptive field: The final image feature sequence containing 1D global information is obtained. ; (2.3) Input the image feature sequence containing 1D global information into the 2D gated mixing layer, and obtain the dynamically mixed image feature sequence by dynamically mixing the 1D global information through the gating mechanism; (2.4) Input the dynamically mixed image feature sequence into the gated channel mixing layer, and perform feature mixing on the input image feature sequence at the channel level to obtain a new image feature sequence; (3) Input the new image feature sequence into the pooling layer for average pooling, and input the average pooled features into the linear layer for image category prediction.
2. The image classification and recognition method based on gated linear attention mechanism as described in claim 1, characterized in that, In step (2.1), the specific steps are as follows: The image feature sequence is input into the local information coding layer, i.e. Input to Grouped convolution In the middle, short-term local 2D information is captured to obtain image feature sequences containing local information. .
3. The image classification and recognition method based on gated linear attention mechanism as described in claim 1, wherein step (2.3) specifically comprises: Image feature sequences containing 1D global information are input into a 2D gated mixing layer to dynamically adjust the 1D global information. and 2D local information The fusion first predicts the proportion of 2D information injected through a linear layer: in These are trainable weights. It is a trainable bias, resulting in 2D gating. Finally, the two are dynamically blended to obtain a dynamically blended image feature sequence: 。 4. The image classification and recognition method based on gated linear attention mechanism as described in claim 1, wherein step (2.4) specifically comprises: Dynamically blended image feature sequences The input is fed into a gated channel mixing layer to mix information from different channels. This is first mapped through a linear layer. and : Then, a new image feature sequence is obtained by channel mixing and linear transformation through a gating mechanism. : 。 5. The image classification and recognition method based on gated linear attention mechanism as described in claim 1, wherein step (3) specifically comprises: The image feature sequence output from the last ViG basic processing unit of the image basic processing network. Perform pooling and use a linear layer to predict the final class score: in y represents the pooled features, and y represents the final class confidence. It represents the number of categories.
6. The image classification and recognition method based on gated linear attention mechanism as described in claim 1, characterized in that, Also includes: Kernel function fusion combines the bidirectional attention scans from the basic vision processing unit into a single kernel function to improve running speed and reduce memory usage.
7. An image classification and recognition device based on a gated linear attention mechanism, characterized in that, The method includes at least one processor and a memory, which are connected via a data bus. The memory stores instructions that can be executed by the at least one processor. After being executed by the processor, the instructions are used to complete the image classification and recognition method based on the gated linear attention mechanism as described in any one of claims 1-6.
Citation Information
Patent Citations
Image-text retrieval method and system based on attention mechanism and gating mechanism
CN112966135A
Image description generation method fusing visual common sense and enhancing multilayer global features
CN113378919A
