Transform image classification method based on fast Fourier transform
By using fast Fourier transform and adaptive frequency domain filtering technology in the Transformer framework, the problem of difficult global feature extraction in image classification methods is solved, and image classification effects with high precision and low parameter volume are achieved.
Patent Information
- Application Number
- CN202411030392.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-30
- Publication Date
- 2024-10-18
AI Technical Summary
Existing image classification methods based on the Transformer framework use sparse self-attention learning, resulting in the model being unable to effectively extract the global features of the image, affecting accuracy.
The Transformer method based on fast Fourier transform is adopted. In the first two stages, the operations of fast Fourier transform, adaptive frequency domain filtering and spectrogram element-wise multiplication and inverse fast Fourier transform are used to replace sparse self-attention. Expand the receptive field of the model.
It significantly improves the image classification accuracy, has a low number of parameters and reaches a Top-1 accuracy of 91.8%, with a parameter value of 17.3M, which is better than traditional methods.
Smart Images

Figure CN118799643A_ABST
Abstract
Description
Technical Field
[0001] The invention belongs to the technical field of image processing, and in particular relates to an image classification method of a Transformer based on Fast Fourier Transform (FFTT). Background Art
[0002] Image classification is based on the feature differences between different categories. It is a basic problem in the field of computer vision and an important foundation for high-level visual tasks such as image detection and behavior analysis.
[0003] At present, one of the main directions of image classification methods is the research on image classification based on the natural language processing Transformer framework (Vision Transformers, ViTs). This type of method divides the image into image blocks of different scales, and then applies Transformer Block to extract features at each scale. However, in order to reduce computational complexity, this type of method uses sparse self-attention to learn image features in the first two stages, which is bound to reduce the receptive field of the model. For example, Pyramid Vision Transformer uses spatial reduced self-attention and Swin Transformer uses windowed self-attention, which makes the model unable to effectively extract the global features of the image, thereby affecting the accuracy of image classification methods based on the Transformer framework. Summary of the invention
[0004] The purpose of the present invention is to provide an image classification method based on a fast Fourier transform (FT) transformer, which solves the problem of low precision of existing image classification methods.
[0005] The technical solution adopted by the present invention is an image classification method based on a Transformer of a fast Fourier transform, comprising the following steps:
[0006] Step 1: Select a training data set P = {p1, p2, ..., p i , ..., p n}, i = {1, 2, ..., n};
[0007] Step 2, obtain the feature map out_feat1 of the image after learning by the FFTT network stage 1;
[0008] Step 3, obtain the output feature map out_feat2 of the image after learning by the FFTT network stage 2;
[0009] Step 4, obtain the output feature map out_feat3 of the image after learning by the FFTT network stage 3;
[0010] Step 5, obtain the feature map out_feat4 of the image after learning by the FFTT network stage 4;
[0011] Step 6: Perform global average pooling on the feature map obtained in step 5 to obtain a feature vector, and classify the image according to the feature vector.
[0012] The present invention is also characterized in that:
[0013] Step 1 is as follows: select n images of size H×W×3 as the training data set P={p1, p2, …, p i ,…,p n}, i = {1, 2,…, n}.
[0014] Step 2 is as follows:
[0015] Step 2.1: The i-th image p in P = {1, 2, ..., n} i Input into Patch Embedding, divide the image into blocks, each block size is 4×4, then the image is divided into blocks;
[0016] Step 2.2: Linearly project each block obtained in step 2.1 into a semantic sequence of length C, and then embed the positional encoding to obtain a size of Input feature map in_feat1;
[0017] Step 2.3, construct FFBlock. The FFBlock structure is similar to the standard Transformer Block structure, except that the MSA in the standard Transformer Block structure is replaced by FFToken Mixer. FFBlock consists of four parts: LN, FFToken Mixer, LN, and MLP. The input feature map in_feat1 obtained in step 2.2 is processed by LN, FFToken Mixer, LN, and MLP in turn. It is iterated 3 times to obtain the output feature map out_feat1 of the image after learning the FFTT network stage 1.
[0018] In step 2.3, FFToken Mixer includes Split, Adaptive Frequency Filters, Dynamic Convolution, and Concat.
[0019] The process of FFToken Mixer processing feature maps is as follows: First, the feature map processed by LN is split along the channel dimension and evenly divided into two sub-feature maps in_feat1_f and in_feat1_c, both of size Then, the sub-feature map in_feat1_f is adaptively filtered in the frequency domain to extract the global features of the image, and the sub-feature map in_feat1_c is dynamically convolved to extract the local features of the image; then, the global features and local features are concat in the channel dimension to obtain the feature map processed by FFToken Mixer, which is of size
[0020] The process of Adaptive Frequency Filters extracting global features of an image is as follows: First, perform FFT on the sub-feature map in_feat1_f to obtain a spectrum map of size Then, the spectrum graph is processed in sequence using a 1×1 convolution layer, a RELU activation function, and a 1×1 convolution layer to obtain Adaptive Frequency Filters with a size of Then, Adaptive Frequency Filters is used to filter the spectrum graph. Then, IFFT is performed on the filtered spectrum graph. According to the convolution theorem, i.e., formula (1), frequency domain multiplication is equal to spatial domain convolution. Using an adaptive filter of the same size as the spectrum graph to perform element-by-element multiplication on the spectrum graph in the frequency domain is equivalent to using a convolution kernel of the same size as the sub-feature graph to perform convolution on the sub-feature graph in the spatial domain. The convolution theorem formula is as follows:
[0021]
[0022] In formula (1), X represents the feature map, represents the fast Fourier transform, represents the inverse fast Fourier transform, Indicates that the spectrum graph is processed in sequence using a 1×1 convolution layer, a RELU activation function, and a 1×1 convolution layer. ⊙ indicates frequency domain multiplication, and * indicates spatial domain convolution.
[0023] The process of Dynamic Convolution extracting local features of an image is as follows: First, AdaptiveAverge Pooling is performed on the sub-feature map in_feat1_c to reduce the sub-feature map to The same size as the dynamic convolution kernel; then use a 1×1 convolution layer, a RELU activation function, and a 1×1 convolution layer to process the reduced sub-feature map in turn to obtain the eigenvalue of the image, which is Then the image feature value is multiplied element by element by the weight of the dynamic convolution kernel to obtain the final weight of the dynamic convolution kernel; finally, Dynamic Convolution is used to perform a convolution operation on the sub-feat1_c feature map.
[0024] The specific process of step 3 is:
[0025] Step 3.1, construct Downsample, which includes an MBConv and a convolutional layer of size 3×3;
[0026] Step 3.2: Input the output feature map out_feat1 obtained in step 2 into Downsample, downsample the feature map, halve its height and width, double its depth, and then linearly project it to obtain a size of Input feature map in_feat2;
[0027] The specific process of step 3.2 is:
[0028] Step 3.2.1, use MBConv to perform convolution operation on the feature map out_feat1, adjust the feature weight of the feature map in the channel dimension, and enhance the features of important channels;
[0029] Step 3.2.2, use a 3×3 convolutional layer to perform a convolution operation on the feature map of the adjusted channel weights to obtain a size of Input feature map in_feat2;
[0030] Step 3.3, input the input feature map in_feat2 obtained in step 3.2 into an FFBlock, iterate 4 times, and obtain the output feature map out_feat2 of the image after learning by the FFTT network stage 2.
[0031] Step 4 is as follows:
[0032] Step 4.1: Input the output feature map out_feat2 obtained in step 3 into Downsample, downsample the feature map, halve its height and width, double its depth, and then linearly project it to obtain a size of Input feature map in_feat3;
[0033] Step 4.2: Input the input feature map obtained in step 4.1 into a Transformer Block and iterate 6 times to obtain the output feature map out_feat3 of the image after learning the FFTT network stage 3.
[0034] Step 5 is as follows:
[0035] Step 5.1: Input the output feature map out_feat3 obtained in step 4 into Downsample, downsample the feature map, halve its height and width, double its depth, and then linearly project it to obtain a size of Input feature map in_feat4;
[0036] Step 5.2: Input the input feature map obtained in step 5.1 into a Transformer Block and iterate 3 times to obtain the feature map out_feat4 of the image learned by the FFTT network stage 4.
[0037] Step 6 is as follows:
[0038] Step 6.1, perform global average pooling on the output feature map out_feat4 obtained in step 5 to obtain a feature vector;
[0039] Step 6.2: Input the feature vector obtained in step 6.1 into the classifier MLPHead to calculate the image p i The category probability of the image p is the category with the highest probability. i The classification results;
[0040] Step 6.3, repeat steps 1.2 to 6.2 to obtain image p i+1 The classification results are calculated until i+1>n.
[0041] The beneficial effects of the present invention are:
[0042] (1) Compared with the existing Transformer-based image classification method, in the first two stages, the present invention uses three consecutive operations of fast Fourier transform, adaptive frequency domain filtering and spectrum map element-by-element multiplication, and inverse fast Fourier transform to replace sparse self-attention to extract global features of the image. Since the adaptive filter of the same size as the spectrum map is used in the frequency domain to multiply the spectrum map element-by-element, it is equivalent to using the convolution kernel of the same size as the feature map in the spatial domain to convolve the feature map, so the model has a global receptive field;
[0043] (2) The image classification method based on the Transformer of the fast Fourier transform of the present invention has a lower parameter amount and significantly improved classification accuracy compared with the existing image classification methods, with an accuracy value of 91.8 and a parameter value of 17.3M. BRIEF DESCRIPTION OF THE DRAWINGS
[0044] Figure 1 It is a framework diagram of the FFTT network in the method of the present invention;
[0045] Figure 2 It is the FFBlock network structure diagram of FFTT in the method of the present invention;
[0046] Figure 3 This is a network structure diagram of FFToken Mixer of FFTT in the method of the present invention;
[0047] Figure 4 It is the Adaptive Frequency Filters network structure diagram of FFTT in the method of the present invention;
[0048] Figure 5 The figure is a process diagram of generating the Dynamic Convolution kernel weight of FFTT in the method of the present invention;
[0049] Figure 6 It is the Downsample network structure diagram of FFTT in the method of the present invention;
[0050] Figure 7 It is the Transformer Block network structure diagram of FFTT in the method of the present invention;
[0051] Figure 8 It is the heat map of the focus area of the FFTT and PVT and Swin Transformer of the present invention method;
[0052] Fig. 9 This is a performance comparison chart of the FFTT method of the present invention and existing methods on the CIFAR 10 dataset. DETAILED DESCRIPTION
[0053] The present invention is described in detail below with reference to the accompanying drawings and specific embodiments.
[0054] The present invention provides an image classification method based on a fast Fourier transform (FFTT) transformer, such as Figure 1 As shown, the specific implementation steps are as follows:
[0055] Step 1: Select n images of size H×W×3 as the training data set P={p1, p2, …, p i ,…,p n}, i = {1, 2, ..., n};
[0056] Step 2, obtain the feature map out_feat1 of the image after the FFTT network stage 1 learning, specifically: transform the image p in P iDivide the image into 4×4 blocks, then perform linear projection and position embedding on the image blocks. The processed image blocks are input into a FFBlock (Feature Fusion Block) to obtain the feature map out_feat1 of the image learned by the FFTT network stage 1.
[0057] The specific process of step 2 is:
[0058] Step 2.1: The i-th image p in P = {1, 2, ..., n} i Input into Patch Embedding, divide the image into blocks, each block size is 4×4, then the image is divided into Blocks;
[0059] Step 2.2: Linearly project each block obtained in step 2.1 into a semantic sequence (tokens) of length C, and then embed the positional encoding to obtain a size of Input feature map in_feat1;
[0060] Step 2.3, construct FFBlock. The FFBlock structure is similar to the standard Transformer Block structure. The difference is that the MSA (multi-head self-attention) in the standard Transformer Block structure is replaced by FFTokenMixer (Feature Fusion Token Mixer). FFBlock consists of four parts: LN, FFToken Mixer, LN, and MLP. Figure 2 As shown, the input feature map in_feat1 obtained in step 2.2 is processed by LN, FFToken Mixer, LN, and MLP in turn, iterated 3 times, and the output feature map out_feat1 of the image after learning by FFTT network stage 1 is obtained.
[0061] In step 2.3, first design FFToken Mixer, such as Figure 3 As shown, the FFToken Mixer includes Split, Adaptive Frequency Filters (adaptive frequency domain filter layer), Dynamic Convolution (dynamic convolution layer) and Concat.
[0062] The process of FFToken Mixer processing feature maps is as follows: First, the feature map processed by LN is split along the channel dimension and evenly divided into two sub-feature maps in_feat1_f and in_feat1_c, both of size Then, the sub-feature map in_feat1_f is adaptively filtered in the frequency domain to extract the global features of the image, and the sub-feature map in_feat1_c is dynamically convolved to extract the local features of the image; then, the global features and local features are concat in the channel dimension to obtain the feature map processed by FFToken Mixer, which is of size
[0063] Among them, Adaptive Frequency Filters extracts the global features of the image, such as Figure 4 First, perform FFT (Fast Fourier Transform) on the sub-feature graph in_feat1_f to obtain a spectrum graph with a size of Then, the spectrum graph is processed in sequence using a 1×1 convolution layer, a RELU activation function, and a 1×1 convolution layer to obtain AdaptiveFrequency Filters with a size of Then use Adaptive Frequency Filters to filter the spectrum (adaptive filtering and spectrum are multiplied element by element); then perform IFFT (inverse fast Fourier transform) on the filtered spectrum. According to the convolution theorem (Formula 1), frequency domain multiplication is equal to spatial domain convolution. Using an adaptive filter of the same size as the spectrum to perform element-by-element multiplication on the spectrum in the frequency domain is equivalent to using a convolution kernel of the same size as the sub-feature map to perform convolution on the sub-feature map in the spatial domain. Because the convolution operation equivalent to the filtering operation has the global receptive field of the sub-feature map, the adaptive filter can extract the global features of the image. The convolution theorem formula is as follows;
[0064]
[0065] In formula (1), X represents the feature map, represents the fast Fourier transform, represents the inverse fast Fourier transform, Indicates that the spectrum graph is processed in sequence using a 1×1 convolution layer, a RELU activation function, and a 1×1 convolution layer. ⊙ indicates frequency domain multiplication, and * indicates spatial domain convolution.
[0066] Among them, Dynamic Convolution extracts the local features of the image, such as Figure 5As shown in, first, AdaptiveAverge Pooling is performed on the sub-feature map in_feat1_c to reduce the sub-feature map to The same size as the dynamic convolution kernel; then use a 1×1 convolution layer, a RELU activation function, and a 1×1 convolution layer to process the reduced sub-feature map in turn to obtain the eigenvalue of the image, which is Then, the image feature value is multiplied by the weight of the dynamic convolution kernel element by element to obtain the final weight of the dynamic convolution kernel. Finally, Dynamic Convolution is used to perform convolution operation on the sub-feature map in_feat1_c. Because the weight of the dynamic convolution kernel contains the characteristics of the image, using dynamic convolution to perform convolution operation on the sub-feature map can better extract the local features of the image;
[0067] Step 3, obtaining the output feature map out_feat2 of the image after learning the FFTT network stage 2, specifically: dividing the output feature map out_feat1 obtained in step 2 into image blocks of size 8×8, and then performing linear projection processing on the image blocks, and inputting the processed image blocks into an FFBlock to obtain the output feature map out_feat2 of the image after learning the FFTT network stage 2;
[0068] The specific process of step 3 is:
[0069] Step 3.1, construct Downsample. Figure 6 As shown, the Downsample includes an MBConv (mobile bottleneck convolution layer) and a convolution layer of size 3×3.
[0070] Step 3.2: Input the output feature map out_feat1 obtained in step 2 into Downsample, downsample the feature map, halve its height and width, double its depth, and then linearly project it to obtain a size of Input feature map in_feat2;
[0071] The specific process of step 3.2 is:
[0072] Step 3.2.1, use MBConv to perform convolution operation on the feature map out_feat1, adjust the feature weight of the feature map in the channel dimension, and enhance the features of important channels;
[0073] Step 3.2.2, use a 3×3 convolutional layer to perform a convolution operation on the feature map of the adjusted channel weights to obtain a size of Input feature map in_feat2;
[0074] Step 3.3, input the input feature map in_feat2 obtained in step 3.2 into an FFBlock, iterate 4 times, and obtain the output feature map out_feat2 of the image after learning by the FFTT network stage 2;
[0075] Step 4, obtaining the output feature map out_feat3 of the image after learning the FFTT network stage 3, specifically: dividing the output feature map out_feat2 obtained in step 3 into image blocks of size 16×16, and then performing linear projection processing on the image blocks, and inputting the processed image blocks into a standard Transformer Block to obtain the output feature map out_feat3 of the image after learning the FFTT network stage 3;
[0076] The specific process of step 4:
[0077] Step 4.1: Input the output feature map out_feat2 obtained in step 3 into Downsample, downsample the feature map, halve its height and width, double its depth, and then linearly project it to obtain a size of Input feature map in_feat3;
[0078] Step 4.2: Input the input feature map obtained in step 4.1 into a Transformer Block and iterate 6 times to obtain the output feature map out_feat3 of the image after learning the FFTT network stage 3.
[0079] Transformer Block structure is as follows Figure 7 As shown, it consists of four parts: LN (Layer Norm), MSA, LN, and MLP;
[0080] Step 5, obtaining the feature map out_feat4 of the image after learning by the FFTT network stage 4, specifically: dividing the output feature map out_feat3 obtained in step 4 into image blocks of size 32×32, and then performing linear projection processing on the image blocks, and inputting the processed image blocks into a standard Transformer Block to obtain the feature map out_feat4 of the image after learning by the FFTT network stage 4;
[0081] The specific process of step 5:
[0082] Step 5.1: Input the output feature map out_feat3 obtained in step 4 into Downsample, downsample the feature map, halve its height and width, double its depth, and then linearly project it to obtain a size of Input feature map infeat4;
[0083] Step 5.2: Input the input feature map obtained in step 5.1 into a Transformer Block and iterate 3 times to obtain the feature map out_feat4 of the image learned by the FFTT network stage 4.
[0084] Step 6: Perform global average pooling on the feature map obtained in step 5 to obtain a feature vector, and classify the image according to the feature vector.
[0085] The specific process of step 6 is:
[0086] Step 6.1, perform global average pooling on the output feature map out_feat4 obtained in step 5 to obtain a feature vector;
[0087] Step 6.2: Input the feature vector obtained in step 6.1 into the classifier MLP Head to calculate the image p i The category probability of the image p is the category with the highest probability. i The classification results;
[0088] Step 6.3, repeat steps 1.2 to 6.2 to obtain image p i+1 The classification results are calculated until i+1>n.
[0089] Example 1
[0090] The present invention is based on the image classification method of the Transformer of Fast Fourier Transform (FFTT), such as Figure 1 As shown, the specific implementation steps are as follows:
[0091] Step 1: Select 10,000 images of size 224×224×3 as the training data set P = {p1, p2, …, p i ,…,p 10000};
[0092] Step 2: transform image p in P i Divide the image into 4×4 blocks, then perform linear projection and position embedding on the image blocks. The processed image blocks are input into a FFBlock (Feature Fusion Block) to obtain the feature map out_feat1 of the image learned by the FFTT network stage1.
[0093] The specific process of step 2 is:
[0094] Step 2.1, the i-th image p in P = {1, 2, ..., 10000} i Input into Patch Embedding, divide the image into blocks, each block size is 4×4, so the image is divided into 56×56 blocks;
[0095] Step 2.2: Linearly project each block obtained in step 2.1 into a semantic sequence (tokens) of length 96, and then embed the positional encoding to obtain an input feature map in_feat1 of size 56×56×96;
[0096] Step 2.3, construct FFBlock. The FFBlock structure is similar to the standard Transformer Block structure. The difference is that the MSA (multi-head self-attention) in the standard Transformer Block structure is replaced by FFTokenMixer (Feature Fusion Token Mixer). FFBlock consists of four parts: LN, FFToken Mixer, LN, and MLP. Figure 2 As shown, the input feature map in_feat1 obtained in step 2.2 is processed by LN, FFToken Mixer, LN, and MLP in turn, iterated 3 times, and the output feature map out_feat1 of the image after learning by FFTT network stage 1 is obtained.
[0097] In step 2.3, first design FFToken Mixer, such as Figure 3 As shown, the FFToken Mixer includes Split, Adaptive Frequency Filters (adaptive frequency domain filter layer), Dynamic Convolution (dynamic convolution layer) and Concat.
[0098] The process of FFToken Mixer processing feature maps is as follows: First, the feature map processed by LN is split along the channel dimension and evenly divided into two sub-feature maps in_feat1_f and in_feat1_c, both of size Then, the sub-feature map in_feat1_f is adaptively filtered in the frequency domain to extract the global features of the image, and the sub-feature map in_feat1_c is dynamically convolved to extract the local features of the image; then, the global features and local features are concat in the channel dimension to obtain the feature map processed by FFToken Mixer, which is of size
[0099] Among them, Adaptive Frequency Filters extracts the global features of the image, such as Figure 4 As shown. First, perform FFT (Fast Fourier Transform) on the sub-feature map in_feat1_f to obtain a spectrum map with a size of 56×28×48; then, use a 1×1 convolution layer, a RELU activation function, and a 1×1 convolution layer to process the spectrum map in turn to obtain AdaptiveFrequency Filters with a size of 56×28×48; then use Adaptive Frequency Filters to filter the spectrum map (adaptive filtering is element-by-element multiplication of the spectrum map); then perform IFFT (Inverse Fast Fourier Transform) on the filtered spectrum map. According to the convolution theorem (Formula 1), frequency domain multiplication is equal to spatial domain convolution. Using an adaptive filter of the same size as the spectrum map to perform element-by-element multiplication on the spectrum map in the frequency domain is equivalent to using a convolution kernel of the same size as the sub-feature map to convolve the sub-feature map in the spatial domain. Because the convolution operation equivalent to the filtering operation has the global receptive field of the sub-feature map, the adaptive filtering can extract the global features of the image;
[0100]
[0101] In formula (1), X represents the feature map, represents the fast Fourier transform, represents the inverse fast Fourier transform, Indicates that the spectrum graph is processed in sequence using a 1×1 convolution layer, a RELU activation function, and a 1×1 convolution layer. ⊙ indicates frequency domain multiplication, and * indicates spatial domain convolution.
[0102] Among them, Dynamic Convolution extracts the local features of the image, such as Figure 5 As shown in the figure, first, AdaptiveAverge Pooling is performed on the sub-feature map in_feat1_c to reduce the sub-feature map to 7×7×48, the same size as the dynamic convolution kernel; then the 1×1 convolution layer, RELU activation function, and 1×1 convolution layer are used to process the reduced sub-feature map in turn to obtain the image feature value, which is 7×7×48 in size; then the image feature value is multiplied element by element by the weight of the dynamic convolution kernel to obtain the final weight of the dynamic convolution kernel. Finally, DynamicConvolution is used to perform a convolution operation on the sub-feature map in_feat1_c. Because the weight of the dynamic convolution kernel contains the characteristics of the image, using dynamic convolution to perform a convolution operation on the sub-feature map can better extract the local features of the image;
[0103] Step 3, divide the output feature map out_feat1 obtained in step 2 into image blocks of size 8×8, and then perform linear projection processing on the image blocks. The processed image blocks are input into an FFBlock to obtain the output feature map out_feat2 of the image after learning by the FFTT network stage 2;
[0104] The specific process of step 3 is:
[0105] Step 3.1, construct Downsample. Figure 6 As shown, the Downsample includes an MBConv (mobile bottleneck convolution layer) and a convolution layer of size 3×3.
[0106] Step 3.2: Input the output feature map out_feat1 obtained in step 2 into Downsample, downsample the feature map, halve its height and width, double its depth, and then linearly project it to obtain an input feature map in_feat2 of size 28×28×192.
[0107] Step 3.3, input the input feature map obtained in step 3.2 into an FFBlock, iterate 4 times, and obtain the output feature map out_feat2 of the image after learning by the FFTT network stage 2;
[0108] The specific process of step 3.2 is:
[0109] Step 3.2.1, use MBConv to perform convolution operation on the feature map out_feat1, adjust the feature weight of the feature map in the channel dimension, and enhance the features of important channels;
[0110] In step 3.2.2, a 3×3 convolutional layer is used to perform a convolution operation on the feature map with adjusted channel weights to obtain an input feature map in_feat2 of size 28×28×192.
[0111] Step 4, divide the output feature map out_feat2 obtained in step 3 into image blocks of size 16×16, and then perform linear projection processing on the image blocks. The processed image blocks are input into a standard Transformer Block to obtain the output feature map out_feat3 of the image after learning by the FFTT network stage 3;
[0112] The specific process of step 4:
[0113] Step 4.1, input the output feature map out_feat2 obtained in step 3 into Downsample, downsample the feature map, halve the height and width of the feature map, double the depth, and then linearly project it to obtain the input feature map in_feat3 with a size of 14×14×384;
[0114] Step 4.2: Input the input feature map obtained in step 4.1 into a Transformer Block and iterate 6 times to obtain the output feature map out_feat3 of the image after learning the FFTT network stage 3.
[0115] Transformer Block structure is as follows Figure 7 As shown, it consists of four parts: LN (Layer Norm), MSA, LN, and MLP;
[0116] Step 5, divide the output feature map out_feat3 obtained in step 4 into image blocks of size 32×32, and then perform linear projection on the image blocks. The processed image blocks are input into a standard Transformer Block to obtain the feature map out_feat4 of the image learned by the FFTT network stage 4;
[0117] The specific process of step 5:
[0118] Step 5.1, input the output feature map out_feat3 obtained in step 4 into Downsample, downsample the feature map, halve the height and width of the feature map, double the depth, and then linearly project it to obtain the input feature map in_feat4 of size 7×7×768;
[0119] Step 5.2: Input the input feature map obtained in step 5.1 into a Transformer Block and iterate 3 times to obtain the feature map out_feat4 of the image learned by the FFTT network stage 4.
[0120] Step 6: Perform global average pooling on the feature map obtained in step 5 to obtain a feature vector, and classify the image according to the feature vector.
[0121] The specific process of step 6 is:
[0122] Step 6.1, perform global average pooling on the output feature map out_feat4 obtained in step 5 to obtain a feature vector;
[0123] Step 6.2: Input the feature vector obtained in step 6.1 into the classifier MLPHead to calculate the image p i The category probability of the image p is the category with the highest probability.i The classification results;
[0124] Step 6.3, repeat steps 1.2 to 6.2 to obtain image p i+1 The classification results are calculated until i+1>n.
[0125] The experimental results of Example 1 are as follows: The data sets used in this example are CIFAR 10 and CIFAR 100. CIFAR10 has 10 common categories, including 50,000 training images and 10,000 test images. CIFAR 100 has 100 common categories, including 50,000 training images and 10,000 test images. The image size in CIFAR 10 and CIFAR 100 is 32×32. During the training process, the image size is adjusted to 224×224. In order to effectively learn the image features under various affine transformations, the training images are appropriately enhanced. The data enhancement methods include: random cropping, random horizontal flipping, and normalization.
[0126] The experiment uses a 3090 graphics card for training, and the batch size is set to 128. Software environment configuration: Pytorch library and Pytorch visual model library (timm); the initial learning rate is 0.01, the minimum learning rate is set to 2e-4, and the weight decay is set to 5e-4. The SGD optimizer is used with a momentum of 0.9. The learning rate is adjusted using cosine annealing, and Tmax is set to 60. A total of 300 epochs are trained.
[0127] Heat map analysis:
[0128] like Figure 8 As shown, the method FFTT of the present invention (corresponding to Figure 8 The first row) focuses on the area in the first two stages more than SwinTransformer (corresponding to Figure 8 The second row) and PVT (corresponding Figure 8 The experiment proves that the present invention uses three consecutive operations of fast Fourier transform, adaptive frequency domain filtering and spectrum graph element-by-element multiplication, and inverse fast Fourier transform instead of sparse self-attention to expand the receptive field of the model, so that the model can better extract the global features of the image.
[0129] Parameter complexity:
[0130] like Fig. 9As shown, the FFTT method of the present invention can achieve 91.8% Top-1 accuracy on CIFAR 10 with 17.3M parameters and 2.4G MAC, which is lower than the parameters of 24.5M and 3.8G MAC of Pyramid Vision Transformer (Top-1: 88.1%) and the parameters of 29M and 4.5G MAC of Swin Transformer (Top-1: 89.7%).
[0131] Performance comparison:
[0132] In order to verify the feasibility of the FFTT method of the present invention, ViT, CrossViT, PVT and SwinTransformer image classification methods are selected for comparison. The accuracy of the top 1 of the CIFAR 10 validation set, the top 1 and top 5 accuracy of the CIFAR 100 validation set of these methods are sorted out, and the parameter indicators include: parameter quantity (Params (M)), computational complexity (FLOPs (G)) and evaluation accuracy (Top-1 Acc. (%), Top-5 Acc. (%)).
[0133] It can be seen from Table 1 that when the parameter amount of FFTT of the method of the present invention is 17.3M (lower than the existing image classification methods, such as PVT, whose parameter amount is 24.5M, and Swin Transformer, whose parameter amount is 29M), the method of the present invention achieves higher classification accuracy. The Top-1 accuracy value on CIFAR 10 is 91.8, which is higher than the PVT method (88.1) and the Swin Transformer method (89.7). The Top-1 accuracy value on CIFAR 100 is 72.9, and the Top-5 accuracy value is 92.2, which are higher than the PVT method (61.6, 84.7) and the Swin Transformer method (66.7, 88.3).
[0134] Table 1 Experimental results of various methods on CIFAR 10 and CIFAR 100 datasets
[0135]
[0136] Example 2
[0137] The image classification method of Transformer based on fast Fourier transform includes the following steps:
[0138] Step 1: Select a training data set P = {p1, p2, ..., p i ,…,p n}, i = {1, 2, ..., n};
[0139] Step 2, obtain the feature map out_feat1 of the image after learning by the FFTT network stage 1;
[0140] Step 3, obtain the output feature map out_feat2 of the image after learning by the FFTT network stage 2;
[0141] Step 4, obtain the output feature map out_feat3 of the image after learning by the FFTT network stage 3;
[0142] Step 5, obtain the feature map out_feat4 of the image after learning by the FFTT network stage 4;
[0143] Step 6: Perform global average pooling on the feature map obtained in step 5 to obtain a feature vector, and classify the image according to the feature vector.
[0144] Example 3
[0145] The image classification method of Transformer based on fast Fourier transform includes the following steps:
[0146] Step 1: Select a training data set P = {p1, p2, ..., p i ,…,p n}, i = {1, 2, ..., n};
[0147] Step 1 is as follows: select n images of size H×W×3 as the training data set P={p1, p2, …, p i ,…,p n}, i = {1, 2,…, n}.
[0148] Step 2, obtain the feature map out_feat1 of the image after learning by the FFTT network stage 1;
[0149] Step 3, obtain the output feature map out_feat2 of the image after learning by the FFTT network stage 2;
[0150] Step 4, obtain the output feature map out_feat3 of the image after learning by the FFTT network stage 3;
[0151] Step 5, obtain the feature map out_feat4 of the image after learning by the FFTT network stage 4;
[0152] Step 6: Perform global average pooling on the feature map obtained in step 5 to obtain a feature vector, and classify the image according to the feature vector.
Claims
1. The image classification method based on the Transformer of fast Fourier transform is characterized by: The steps include: Step 1: Select the training data set P = {p1, p2, ..., p i ,...,p n }, i = {1, 2, ..., n}; Step 2, obtain the feature map out_feat1 of the image after learning by the FFTT network stage 1; Step 3, obtain the output feature map out_feat2 of the image after learning by the FFTT network stage 2; Step 4, obtain the output feature map out_feat3 of the image after learning by the FFTT network stage 3; Step 5, obtain the feature map out_feat4 of the image after learning by the FFTT network stage 4; Step 6: Perform global average pooling on the feature map obtained in step 5 to obtain a feature vector, and classify the image according to the feature vector.
2. The image classification method based on the Transformer of fast Fourier transform according to claim 1, characterized in that: Step 1 is as follows: select n images of size H×W×3 as the training data set P={p1,p2,...,p i ,...,p n }, i={1,2,...,n}.
3. The image classification method based on the Transformer of Fast Fourier Transform according to claim 1, characterized in that: Step 2 is as follows: Step 2.1, the i-th image p in P = {1, 2, ..., n} i Input into Patch Embedding, divide the image into blocks, each block size is 4×4, then the image is divided into Blocks; Step 2.2: Linearly project each block obtained in step 2.1 into a semantic sequence of length C, and then embed the positional encoding to obtain a size of Input feature map in_feat1; Step 2.3, construct FFBlock. The FFBlock structure is similar to the standard Transformer Block structure, except that the MSA in the standard Transformer Block structure is replaced by FFToken Mixer. FFBlock consists of four parts: LN, FFToken Mixer, LN, and MLP. The input feature map in_feat1 obtained in step 2.2 is processed by LN, FFToken Mixer, LN, and MLP in turn. It is iterated 3 times to obtain the output feature map out_feat1 of the image after learning the FFTT network stage 1.
4. The image classification method based on the Transformer of fast Fourier transform according to claim 3 is characterized in that: In step 2.3, FFToken Mixer includes Split, Adaptive Frequency Filters, Dynamic Convolution, and Concat; The process of FFToken Mixer processing feature maps is as follows: First, the feature map processed by LN is split along the channel dimension and evenly divided into two sub-feature maps in_feat1_f and in_feat1_c, both of size Then, the sub-feature map in_feat1_f is adaptively filtered in the frequency domain to extract the global features of the image, and the sub-feature map in_feat1_c is dynamically convolved to extract the local features of the image; then, the global features and local features are concat in the channel dimension to obtain the feature map processed by FFToken Mixer, which is of size The process of Adaptive Frequency Filters extracting global features of an image is as follows: First, perform FFT on the sub-feature map in_feat1_f to obtain a spectrum map of size Then, the spectrum graph is processed in sequence using a 1×1 convolution layer, a RELU activation function, and a 1×1 convolution layer to obtain Adaptive Frequency Filters with a size of Then, Adaptive Frequency Filters is used to filter the spectrum graph. Then, IFFT is performed on the filtered spectrum graph. According to the convolution theorem, i.e., formula (1), frequency domain multiplication is equal to spatial domain convolution. Using an adaptive filter of the same size as the spectrum graph to perform element-by-element multiplication on the spectrum graph in the frequency domain is equivalent to using a convolution kernel of the same size as the sub-feature graph to perform convolution on the sub-feature graph in the spatial domain. The convolution theorem formula is as follows: In formula (1), X represents the feature map, represents the fast Fourier transform, represents the inverse fast Fourier transform, Indicates that the spectrum graph is processed in sequence using a 1×1 convolution layer, a RELU activation function, and a 1×1 convolution layer. ⊙ indicates frequency domain multiplication, and * indicates spatial domain convolution. The process of Dynamic Convolution extracting local features of an image is as follows: First, AdaptiveAverge Pooling is performed on the sub-feature map in_feat1_c to reduce the sub-feature map to The same size as the dynamic convolution kernel; then use a 1×1 convolution layer, a RELU activation function, and a 1×1 convolution layer to process the reduced sub-feature map in turn to obtain the eigenvalue of the image, which is Then the image feature value is multiplied element by element by the weight of the dynamic convolution kernel to obtain the final weight of the dynamic convolution kernel; finally, Dynamic Convolution is used to perform a convolution operation on the sub-feat1_c feature map.
5. The image classification method based on the Transformer of fast Fourier transform according to claim 4 is characterized in that: The specific process of step 3 is: Step 3.1, construct Downsample, which includes an MBConv and a convolutional layer of size 3×3; Step 3.2: Input the output feature map out_feat1 obtained in step 2 into Downsample, downsample the feature map, halve its height and width, double its depth, and then linearly project it to obtain a size of Input feature map in_feat2; The specific process of step 3.2 is: Step 3.2.1, use MBConv to perform convolution operation on the feature map out_feat1, adjust the feature weight of the feature map in the channel dimension, and enhance the features of important channels; Step 3.2.2, use a 3×3 convolutional layer to perform a convolution operation on the feature map of the adjusted channel weights to obtain a size of Input feature map in_feat2; Step 3.3, input the input feature map in_feat2 obtained in step 3.2 into an FFBlock, iterate 4 times, and obtain the output feature map out_feat2 of the image after learning by the FFTT network stage 2.
6. The image classification method based on the Transformer of fast Fourier transform according to claim 5, characterized in that: Step 4 is as follows: Step 4.1: Input the output feature map out_feat2 obtained in step 3 into Downsample, downsample the feature map, halve its height and width, double its depth, and then linearly project it to obtain a size of Input feature map in_feat3; Step 4.2: Input the input feature map obtained in step 4.1 into a Transformer Block and iterate 6 times to obtain the output feature map out_feat3 of the image after learning the FFTT network stage 3.
7. The image classification method based on the Transformer of fast Fourier transform according to claim 6, characterized in that: Step 5 is as follows: Step 5.1: Input the output feature map out_feat3 obtained in step 4 into Downsample, downsample the feature map, halve its height and width, double its depth, and then linearly project it to obtain a size of Input feature map in_feat4; Step 5.2: Input the input feature map obtained in step 5.1 into a Transformer Block and iterate 3 times to obtain the feature map out_feat4 of the image learned by the FFTT network stage 4.
8. The image classification method based on the Transformer of Fast Fourier Transform according to claim 7, characterized in that: Step 6 is as follows: Step 6.1, perform global average pooling on the output feature map out_feat4 obtained in step 5 to obtain a feature vector; Step 6.2: Input the feature vector obtained in step 6.1 into the classifier MLP Head to calculate the image p i The category probability of the image p is the category with the highest probability. i The classification results; Step 6.3, repeat steps 1.2 to 6.2 to obtain image p i+1 The classification results are counted until i+1>n.
