summaries: The work proposes a new attention-based architecture for computer vision (classification). The proposed architecture replaces the self-attention layer in ViT with computational blocks involving the Fourier transform. Due to the efficiency of FFT and IFFT the quadratic complexity of self-attention can be reduced to a log-linear computational cost. In order to boost performance distillation of taken features from a pre-trained vision transformer is used. Results are presented on ImageNet as well as on some transfer learning experiments.  This paper replaces the self-attention layer with a global filter layer:  a sequence of a 2D FFT, einsum, and then 2D inverse FFT. A benefit of the approach is that the attention computation is reduced from O(n^2) to O(nlogn) time-complexity. This demonstrates that GFNet is Pareto optimal versus other Transformer-like models on ImageNet on an accuracy-FLOPs basis. This paper proposes to replace self-attention in vision transformer by a *Global Filter Layer* consisting of a 2D fast Fourier transform (FFT), a point-wise multiplication with learned weights and an 2D inverse FFT. The proposed layer compares favourably with (a) self-attention as it replaces the quadratic dependency on compute cost and memory footprint with a log-linear rate and (b) variants of MLP-mixer as the number of weights scales linearly with the number of pixels instead of quadratically. The performances of the proposed model are convincing both for supervised classification on ImageNet and fine-tuning to downstream task on smaller datasets. This paper proposes Global Filter Networks (GFNet), which make use of discrete Fourier transform (DFT) and a global filter layer to mix the input tokens. According to the convolution theorem, the combination of the two operations can be regarded as a depthwise global circular convolution, and thus this work can be viewed as a kind of convolutional network with global convolutional layers. By taking advantage of FFT, the proposed method is faster than previous methods such as self-attention and spatial MLP with comparable performance on ImageNet.
abstractText: Recent advances in self-attention and pure multi-layer perceptrons (MLP) models for vision have shown great potential in achieving promising performance with fewer inductive biases. These models are generally based on learning interaction among spatial locations from raw data. The complexity of self-attention and MLP grows quadratically as the image size increases, which makes these models hard to scale up when high-resolution features are required. In this paper, we present the Global Filter Network (GFNet), a conceptually simple yet computationally efficient architecture, that learns long-term spatial dependencies in the frequency domain with log-linear complexity. Our architecture replaces the self-attention layer in vision transformers with three key operations: a 2D discrete Fourier transform, an element-wise multiplication between frequency-domain features and learnable global filters, and a 2D inverse Fourier transform. We exhibit favorable accuracy/complexity trade-offs of our models on both ImageNet and downstream tasks. Our results demonstrate that GFNet can be a very competitive alternative to transformer-style models and CNNs in efficiency, generalization ability and robustness. Code is available at https://github.com/raoyongming/GFNet.
authors.name: Yongming Rao
authors.name: Wenliang Zhao
authors.name: Zheng Zhu
authors.name: Jiwen Lu
authors.name: Jie Zhou
references.authors: Gregory A Baxes
references.title: Digital image processing: principles and applications
references.authors: Lucas Beyer Olivier J Hénaff Alexander Kolesnikov Xiaohua Zhai Aäron van den Oord
references.title: Are we done with imagenet
references.venue: arXiv preprint arXiv:2006.07159,
references.authors: Nicolas Carion Francisco Massa Gabriel Synnaeve Nicolas Usunier Alexander Kirillov Sergey Zagoruyko
references.title: End-to-end object detection with transformers
references.venue: In ECCV,
references.authors: Bowen Cheng Alexander G. Schwing Alexander Kirillov
references.title: Per-pixel classification is not all you need for semantic segmentation
references.venue: NeurIPS,
references.authors: James W Cooley John W Tukey
references.title: An algorithm for the machine calculation of complex fourier series
references.venue: Mathematics of computation,
references.authors: Jia Deng Wei Dong Richard Socher Li-Jia Li Kai Li Li Fei-Fei
references.title: Imagenet: A large-scale hierarchical image database
references.venue: In CVPR,
references.authors: Caiwen Ding Siyu Liao Yanzhi Wang Zhe Li Ning Liu Youwei Zhuo Chao Wang Xuehai Qian Yu Bai Geng Yuan
references.title: Circnn: accelerating and compressing deep neural networks using block-circulant weight matrices
references.venue: In MICRO,
references.authors: Alexey Dosovitskiy Lucas Beyer Alexander Kolesnikov Dirk Weissenborn Xiaohua Zhai Thomas Unterthiner Mostafa Dehghani Matthias Minderer Georg Heigold Sylvain Gelly Jakob Uszkoreit Neil Houlsby
references.title: An image is worth 16x16 words: Transformers for image recognition at scale
references.venue: arXiv preprint arXiv:2010.11929,
references.authors: Chengyue Gong Dilin Wang Meng Li Vikas Chandra Qiang Liu
references.title: Improve vision transformers training by suppressing over-smoothing
references.venue: arXiv preprint arXiv:2104.12753,
references.authors: Ian J Goodfellow Jonathon Shlens Christian Szegedy
references.title: Explaining and harnessing adversarial examples
references.venue: arXiv preprint arXiv:1412.6572,
references.authors: Kaiming He Xiangyu Zhang Shaoqing Ren Jian Sun
references.title: Deep residual learning for image recognition
references.venue: In CVPR,
references.authors: Dan Hendrycks Thomas Dietterich
references.title: Benchmarking neural network robustness to common corruptions and perturbations
references.venue: arXiv preprint arXiv:1903.12261,
references.authors: Dan Hendrycks Kevin Zhao Steven Basart Jacob Steinhardt Dawn Song
references.title: Natural adversarial examples
references.venue: In CVPR,
references.authors: Elad Hoffer Tal Ben-Nun Itay Hubara Niv Giladi Torsten Hoefler Daniel Soudry
references.title: Augment your batch: Improving generalization through instance repetition
references.venue: In CVPR,
references.authors: Zihang Jiang Qibin Hou Li Yuan Daquan Zhou Xiaojie Jin Anran Wang Jiashi Feng
references.title: Token labeling: Training a 85.5% top-1 accuracy vision transformer with 56m parameters on imagenet
references.venue: arXiv preprint arXiv:2104.10858,
references.authors: Jonathan Krause Michael Stark Jia Deng Li Fei-Fei
references.title: 3d object representations for fine-grained categorization
references.venue: In ICCVW,
references.authors: Alex Krizhevsky Geoffrey Hinton
references.title: Learning multiple layers of features from tiny images
references.authors: Alex Krizhevsky Ilya Sutskever Geoffrey E Hinton
references.title: Imagenet classification with deep convolutional neural networks
references.venue: NeurIPS, 25:1097–1105,
references.authors: Jae-Han Lee Minhyeok Heo Kyung-Rae Kim Chang-Su Kim
references.title: Single-image depth estimation based on fourier domain analysis
references.venue: In CVPR,
references.authors: James Lee-Thorp Joshua Ainslie Ilya Eckstein Santiago Ontanon
references.title: Fnet: Mixing tokens with fourier transforms
references.venue: arXiv preprint arXiv:2105.03824,
references.authors: Shaohua Li Kaiping Xue Bin Zhu Chenkai Ding Xindi Gao David Wei Tao Wan
references.title: Falcon: A fourier transform based approach for fast and secure convolutional neural network predictions
references.venue: In CVPR,
references.authors: Zongyi Li Nikola Kovachki Kamyar Azizzadenesheli Burigede Liu Kaushik Bhattacharya Andrew Stuart Anima Anandkumar
references.title: Fourier neural operator for parametric partial differential equations
references.authors: Hanxiao Liu Zihang Dai David R So Quoc V Le
references.title: Pay attention to mlps
references.venue: arXiv preprint arXiv:2105.08050,
references.authors: Ze Liu Yutong Lin Yue Cao Han Hu Yixuan Wei Zheng Zhang Stephen Lin Baining Guo
references.title: Swin transformer: Hierarchical vision transformer using shifted windows
references.venue: arXiv preprint arXiv:2103.14030,
references.authors: Aleksander Madry Aleksandar Makelov Ludwig Schmidt Dimitris Tsipras Adrian Vladu
references.title: Towards deep learning models resistant to adversarial attacks
references.venue: arXiv preprint arXiv:1706.06083,
references.authors: Xiaofeng Mao Gege Qi Yuefeng Chen Xiaodan Li Shaokai Ye Yuan He Hui Xue
references.title: Rethinking the design principles of robust vision transformer
references.venue: arXiv preprint arXiv:2105.07926,
references.authors: Maria-Elena Nilsback Andrew Zisserman
references.title: Automated flower classification over a large number of classes
references.venue: In ICVGIP,
references.authors: Adam Paszke Sam Gross Francisco Massa Adam Lerer James Bradbury Gregory Chanan Trevor Killeen Zeming Lin Natalia Gimelshein Luca Antiga
references.title: Pytorch: An imperative style, high-performance deep learning library
references.venue: NeurIPS,
references.authors: Ioannis Pitas
references.title: Digital image processing algorithms and applications
references.authors: Boris T Polyak Anatoli B Juditsky
references.title: Acceleration of stochastic approximation by averaging
references.venue: SIAM journal on control and optimization,
references.authors: Ilija Radosavovic Raj Prateek Kosaraju Ross Girshick Kaiming He Piotr Dollár
references.title: Designing network design spaces
references.venue: In CVPR, 2020
references.authors: Yongming Rao Wenliang Zhao Benlin Liu Jiwen Lu Jie Zhou Cho-Jui Hsieh
references.title: Dynamicvit: Efficient vision transformers with dynamic token sparsification
references.venue: In NeurIPS,
references.authors: Benjamin Recht Rebecca Roelofs Ludwig Schmidt Vaishaal Shankar
references.title: Do imagenet classifiers generalize to imagenet? In ICML, pages 5389–5400
references.venue: PMLR, 2019
references.authors: Mingxing Tan Quoc Le
references.title: Efficientnet: Rethinking model scaling for convolutional neural networks. In ICML, pages 6105–6114
references.venue: PMLR,
references.authors: Ilya Tolstikhin Neil Houlsby Alexander Kolesnikov Lucas Beyer Xiaohua Zhai Thomas Unterthiner Jessica Yung Daniel Keysers Jakob Uszkoreit Mario Lucic
references.title: Mlp-mixer: An all-mlp architecture for vision
references.venue: arXiv preprint arXiv:2105.01601,
references.authors: Hugo Touvron Piotr Bojanowski Mathilde Caron Matthieu Cord Alaaeldin El-Nouby Edouard Grave Armand Joulin Gabriel Synnaeve Jakob Verbeek Hervé Jégou
references.title: Resmlp: Feedforward networks for image classification with data-efficient training
references.venue: arXiv preprint arXiv:2105.03404,
references.authors: Hugo Touvron Matthieu Cord Matthijs Douze Francisco Massa Alexandre Sablayrolles Hervé Jégou
references.title: Training data-efficient image transformers & distillation through attention
references.venue: arXiv preprint arXiv:2012.12877,
references.authors: Hugo Touvron Matthieu Cord Alexandre Sablayrolles Gabriel Synnaeve Hervé Jégou
references.title: Going deeper with image transformers
references.venue: arXiv preprint arXiv:2103.17239,
references.authors: Ashish Vaswani Noam Shazeer Niki Parmar Jakob Uszkoreit Llion Jones Aidan N Gomez Lukasz Kaiser Illia Polosukhin
references.title: Attention is all you need
references.venue: In NeurIPS,
references.authors: Wenhai Wang Enze Xie Xiang Li Deng-Ping Fan Kaitao Song Ding Liang Tong Lu Ping Luo Ling Shao
references.title: Pyramid vision transformer: A versatile backbone for dense prediction without convolutions, 2021
references.authors: Haiping Wu Bin Xiao Noel Codella Mengchen Liu Xiyang Dai Lu Yuan Lei Zhang
references.title: Cvt: Introducing convolutions to vision transformers
references.venue: arXiv preprint arXiv:2103.15808,
references.authors: Saining Xie Ross Girshick Piotr Dollár Zhuowen Tu Kaiming He
references.title: Aggregated residual transformations for deep neural networks
references.venue: In CVPR,
references.authors: Yanchao Yang Stefano Soatto
references.title: Fda: Fourier domain adaptation for semantic segmentation
references.venue: In CVPR,
references.authors: Xumin Yu Yongming Rao Ziyi Wang Zuyan Liu Jiwen Lu Jie Zhou
references.title: Pointr: Diverse point cloud completion with geometry-aware transformers
references.venue: In ICCV,
references.authors: Li Yuan Yunpeng Chen Tao Wang Weihao Yu Yujun Shi Zihang Jiang Francis EH Tay Jiashi Feng Shuicheng Yan
references.title: Tokens-to-token vit: Training vision transformers from scratch on imagenet
references.venue: arXiv preprint arXiv:2101.11986,
references.authors: Sixiao Zheng Jiachen Lu Hengshuang Zhao Xiatian Zhu Zekun Luo Yabiao Wang Yanwei Fu Jianfeng Feng Tao Xiang Philip HS Torr
references.title: Rethinking semantic segmentation from a sequence-to-sequence perspective with transformers
references.venue: arXiv preprint arXiv:2012.15840,
sections.heading: 1 Introduction
sections.text: The transformer architecture, originally designed for the natural language processing (NLP) tasks [42], has shown promising performance on various vision problems recently [10, 40, 27, 49, 4, 47, 35, 5]. Different from convolutional neural networks (CNNs), vision transformer models use self-attention layers to capture long-term dependencies, which are able to learn more diverse interactions between spatial locations. The pure multi-layer perceptrons (MLP) models [38, 39] further simplify the vision transformers by replacing the self-attention layers with MLPs that are applied across spatial locations. Since fewer inductive biases are introduced, these two kinds of models have the potential to learn more generic and flexible interactions among spatial locations from raw data.
  One primary challenge of applying self-attention and pure MLP models to vision tasks is the considerable computational complexity that grows quadratically as the number of tokens increases. Therefore, typical vision transformer style models usually consider a relatively small resolution for the intermediate features (e.g. 14× 14 tokens are extracted from the input images in both ViT [10] and MLP-Mixer [38]). This design may limit the applications of downstream dense prediction tasks like detection and segmentation. A possible solution is to replace the global self-attention with several local self-attention like Swin transformer [27]. Despite the effectiveness in practice, local self-attention brings quite a few hand-made choices (e.g., window size, padding strategy, etc.) and limits the receptive field of each layer.
  ∗Equal contribution. †Corresponding author.
  35th Conference on Neural Information Processing Systems (NeurIPS 2021).
  In this paper, we present a new conceptually simple yet computationally efficient architecture called Global Filter Network (GFNet), which follows the trend of removing inductive biases from vision models while enjoying the log-linear complexity in computation. The basic idea behind our architecture is to learn the interactions among spatial locations in the frequency domain. Different from the self-attention mechanism in vision transformers and the fully connected layers in MLP models, the interactions among tokens are modeled as a set of learnable global filters that are applied to the spectrum of the input features. Since the global filters are able to cover all the frequencies, our model can capture both long-term and short-term interactions. The filters are directly learned from the raw data without introducing human priors. Our architecture is largely based on the vision transformers only with some minimal modifications. We replace the self-attention sub-layer in vision transformers with three key operations: a 2D discrete Fourier transform to convert the input spatial features to the frequency domain, an element-wise multiplication between frequency-domain features and the global filters, and a 2D inverse Fourier transform to map the features back to the spatial domain. Since the Fourier transform is used to mix the information of different tokens, the global filter is much more efficient compared to the self-attention and MLP thanks to the O(L logL) complexity of the fast Fourier transform algorithm (FFT) [7]. Benefiting from this, the proposed global filter layer is less sensitive to the token length L and thus is compatible with larger feature maps and CNN-style hierarchical architectures without modifications. The overall architecture of GFNet is illustrated in Figure 1. We also compare our global filter with prevalent operations in deep vision models in Table 1.
  Our experiments on ImageNet verify the effectiveness of GFNet. With a similar architecture, our model outperform the recent vision transformer and MLP models including DeiT [40], ResMLP [39] and gMLP [26]. When using the hierarchical architecture, GFNet can further enlarge the gap. GFNet also works well on downstream transfer learning and semantic segmentation tasks. Our results demonstrate that GFNet can be a very competitive alternative to transformer-style models and CNNs in efficiency, generalization ability and robustness.
sections.heading: 2 Related works
sections.text: Vision transformers. Since Dosovitskiy et al. [10] introduce transformers to the image classification and achieve a competitive performance compared to CNNs, transformers begin to exhibit their
  potential in various vision tasks [3, 4, 49]. Recently, there are a large number of works which aim to improve the transformers [40, 41, 27, 44, 18, 11, 48]. These works either seek for better training strategies [40, 11] or design better architectures [27, 44, 48] or both [41, 11]. However, most of the architecture modification of the transformers [44, 18, 27, 48] introduces additional inductive biases similar to CNNs. In this work, we only focus on the standard transformer architecture [10, 40] and our goal is to replace the heavy self-attention layer (O(L2)) to an more efficient operation which can still model the interactions among different spatial locations without introducing the inductive biases associated with CNNs.
  MLP-like models. More recently, there are several works that question the importance of selfattention in the vision transformers and propose to use MLP to replace the self-attention layer in the transformers [38, 39, 26]. The MLP-Mixer [38] employs MLPs to perform token mixing and channel mixing alternatively in each block. ResMLP [39] adopts a similar idea but substitutes the Layer Normalization with an Affine transformation for acceleration. The recently proposed gMLP [26] uses a spatial gating unit to re-weight tokens in the spatial dimension. However, all of the above models include MLPs to mix the tokens spatially, which brings two drawbacks: (1) like the self-attention in the transformers, the spatial MLP still requires computational complexity quadratic to the length of tokens. (2) unlike transformers, MLP models are hard to scale up to higher resolution since the weights of the spatial MLPs have fixed sizes. Our work follows this trend and successfully resolves the above issues in MLP-like models. The proposed GFNet enjoys log-linear complexity and can be easily scaled up to any resolution.
  Applications of Fourier transform in vision. Fourier transform has been an important tool in digital image processing for decades [32, 1]. With the breakthroughs of CNNs in vision [14, 13], there are a variety of works that start to incorporate Fourier transform in some deep learning method [24, 46, 9, 22, 6] for vision tasks. Some of these works employ discrete Fourier transform to convert the images to the frequency domain and leverage the frequency information to improve the performance in certain tasks [22, 46], while others utilize the convolution theorem to accelerate the CNNs via fast Fourier transform (FFT) [24, 9]. FFC [6] replaces the convolution in CNNs with an Local Fourier Unit and perform convolutions in the frequency domain. Very recent works also try to leverage Fourier transform to develop deep learning models to solve partial differential equations [25] and NLP tasks [23]. In this work, we propose to use learnable filters to interchange information globally among the tokens in the Fourier domain, inspired by the frequency filters in the digital image processing [32]. We also take advantage of some properties of FFT to reduce the computational costs and the number of parameters.
sections.heading: 3 Method
sections.heading: 3.1 Preliminaries: discrete Fourier transform
sections.text: We start by introducing the discrete Fourier transform (DFT), which plays an important role in the area of digital signal processing and is a crucial component in our GFNet. For clarity, We first consider the 1D DFT. Given a sequence of N complex numbers x[n], 0 ≤ n ≤ N − 1, the 1D DFT converts the sequence into the frequency domain by:
  X[k] = N−1∑ n=0 x[n]e−j(2π/N)kn := N−1∑ n=0 x[n]W knN (3.1)
  Algorithm 1 Pseudocode of Global Filter Layer.
  # x: the token features, B x H x W x D (where N = H * W) # K: the frequency-domain filter, H x W_hat x D (where W_hat = W // 2 + 1, see Section 3.2 for details)
  X = rfft2(x, dim=(1, 2)) X_tilde = X * K x = irfft2(X_tilde, dim=(1, 2))
  rfft2/irfft2: 2D FFT/IFFT for real signal
  where j is the imaginary unit and WN = e−j(2π/N). The formulation of DFT in Equation (3.1) can be derived from the Fourier transform for continuous signal by sampling in both the time domain and the frequency domain (see Appendix A for details). Since X[k] repeats on intervals of length N , it is suffice to take the value of X[k] at N consecutive points k = 0, 1, . . . , N − 1. Specifically, X[k] represents to the spectrum of the sequence x[n] at the frequency ωk = 2πk/N .
  It is also worth noting that DFT is a one-to-one transformation. Given the DFT X[k], we can recover the original signal x[n] by the inverse DFT (IDFT):
  x[n] = 1
  N N−1∑ k=0 X[k]ej(2π/N)kn. (3.2)
  For real input x[n], it can be proved that (see Appendix A) its DFT is conjugate symmetric, i.e., X[N − k] = X∗[k]. The reverse is true as well: if we perform IDFT to X[k] which is conjugate symmetric, a real discrete signal can be recovered. This property implies that the half of the DFT {X[k] : 0 ≤ k ≤ dN/2e} contains the full information about the frequency characteristics of x[n]. DFT is widely used in modern signal processing algorithms for mainly two reasons: (1) the input and output of DFT are both discrete thus can be easily processed by computers; (2) there exist efficient algorithms for computing the DFT. The fast Fourier transform (FFT) algorithms take advantage of the symmetry and periodicity properties of W knN and reduce the complexity to compute DFT from O(N2) to O(N logN). The inverse DFT (3.2), which has a similar form to the DFT, can also be computed efficiently using the inverse fast Fourier transform (IFFT).
  The DFT described above can be extend to 2D signals. Given the 2D signal X[m,n], 0 ≤ m ≤ M − 1, 0 ≤ n ≤ N − 1, the 2D DFT of x[m,n] is given by:
  X[u, v] = M−1∑ m=0 N−1∑ n=0 x[m,n]e−j2π( um M + vn N ). (3.3)
  The 2D DFT can be viewed as performing 1D DFT on the two dimensions alternatively. Similar to 1D DFT, 2D DFT of real input x[m,n] satisfied the conjugate symmetry property X[M − u,N − v] = X∗[u, v]. The FFT algorithms can also be applied to 2D DFT to improve computational efficiency.
sections.heading: 3.2 Global Filter Networks
sections.text: Overall architecture. Recent advances in vision transformers [10, 40] demonstrate that models based on self-attention can achieve competitive performance even without the inductive biases associated with the convolutions. Henceforth, there are several works [39, 38] that exploit approaches (e.g., MLPs) other than self-attention to mix the information among the tokens. The proposed Global Filter Networks (GFNet) follows this line of work and aims to replace the heavy self-attention layer (O(N2)) with a simpler and more efficient one. The overall architecture of our model is depicted in Figure 1. Our model takes as an input H ×W non-overlapping patches and projects the flattened patches into L = HW tokens with dimension D. The basic building block of GFNet consists of: 1) a global filter layer that can exchange spatial information efficiently (O(L logL)); 2) a feedforward network (FFN) as in [10, 40]. The output tokens of the last block are fed into a global average pooling layer followed by a linear classifier.
  Global filter layer. We propose global filter layer as an alternative to the self-attention layer which can mix tokens representing different spatial locations. Given the tokens x ∈ RH×W×D, we first
  perform 2D FFT (see Section 3.1) along the spatial dimensions to convert x to the frequency domain:
  X = F [x] ∈ CH×W×D, (3.4) where F [·] denotes the 2D FFT. Note that X is a complex tensor and represents the spectrum of x. We can then modulate the spectrum by multiplying a learnable filter K ∈ CH×W×D to the X:
  X̃ = K X, (3.5)
  where is the element-wise multiplication (also known as the Hadamard product). The filter K is called the global filter since it has the same dimension with X , which can represent an arbitrary filter in the frequency domain. Finally, we adopt the inverse FFT to transform the modulated spectrum X̃ back to the spatial domain and update the tokens:
  x← F−1[X̃]. (3.6)
  The formulation of the global filter layer is motivated by the frequency filters in the digital image processing [32], where the global filter K can be regarded as a set of learnable frequency filters for different hidden dimensions. It can be proved (see Appendix A) that the global filter layer is equivalent to a depthwise global circular convolution with the filter size H ×W . Therefore, the global filter layer is different from the standard convolutional layer which adopts a relatively small filter size to enforce the inductive biases of the locality. We also find although the proposed global filter can also be interpreted as a spatial domain operation, the filters learned in our networks exhibit more clear patterns in the frequency domain than the spatial domain, which indicates our models tend to capture relation in the frequency domain instead of spatial domain (see Figure 4). Note that the global filter implemented in the frequency domain is also much more efficient compared to the spatial domain, which enjoys a complexity of O(DL logL) while the vanilla depthwise global circular convolution in the spatial domain has O(DL2) complexity. We will also show that the global filter layer is better than its local convolution counterparts in the experiments.
  It is also worth noting that in the implementation, we make use of the property of DFT to reduce the redundant computation. Since x is a real tensor, its DFT X is conjugate symmetric, i.e. X[H − u,W − v, :] = X∗[H,W, :]. Therefore, we can take only the half of the values in the X but preserve the full information at the same time:
  Xr = X[:, 0 : Ŵ ] := Fr[x], Ŵ = dW/2e, (3.7)
  where Fr denotes the 2D FFT for real input. In this way, we can implement the global filter as Kr ∈ CH×Ŵ×D, which can reduce half the parameters. This can also ensure F−1r [Kr Xr] is a real tensor, thus it can be added directly to the input x. The global filter layer can be easily in modern deep learning frameworks (e.g., PyTorch [31]), as is shown in Algorithm 1. The FFT and IFFT are well supported by GPU and CPU thanks to the acceleration libraries like cuFFT and mkl-fft, which makes our models perform well on hardware.
  Relationship to other transformer-style models. The GFNet follows the line of research about the exploration of approaches to mix the tokens. Compared to existing architectures like vision transformers and pure MLP models, we exhibit that GFNet has several favorable properties: 1) GFNet is more efficient. The complexity of both the vision transformers [10, 40, 41] and the MLP models [38, 39] is O(L2). Different from them, global filter layer only consists an FFT (O(L logL)), an element-wise multiplication (O(L)) and an IFFT (O(L logL)), which means the total computational complexity is O(L logL). 2) Although pure MLP models are simpler compared to transformers, it is hard to fine-tune them on higher resolution (e.g., from 224 × 224 resolution to 384× 384 resolution) since they can only process a fixed number of tokens. As opposed to pure MLP models, we will show that our GFNet can be easily scaled up to higher resolution. Our model is more flexible since both the FFT and the IFFT have no learnable parameters and can process sequences with arbitrary length. We can simply interpolate the global filter K to K ′ ∈ CH′×W ′×D for different inputs, where H ′ ×W ′ is the target size. The interpolation is reasonable due to the property of DFT. Each element of the global filter K[u, v] corresponds to the spectrum of the filter at ωu = 2πu/H, ωv = 2πv/W and thus, the global filter K can be viewed as a sampling of a continuous spectrum K(ωu, ωv), where ωu, ωv ∈ [0, 2π]. Hence, changing the resolution is equivalent to changing the sampling interval of K(ωu, ωv). Therefore, we only need to perform interpolation to shift from one resolution to another.
  We also notice recently a concurrent work FNet [23] leverages Fourier transform to mix tokens. Our work is distinct from FNet in three aspects: (1) FNet performs FFT to the input and directly adds the real part of the spectrum to the input tokens, which blends the information from different domains (spatial/frequency) together. On the other hand, GFNet draws motivation from the frequency filters, which is more reasonable. (2) FNet only keeps the real part of the spectrum. Note that the spectrum of real input is conjugate symmetric, which means the real part is exactly symmetric and thus contains redundant information. Our GFNet, however, utilizes this property to simplify the computation. (3) FNet is designed for NLP tasks, while our GFNet focuses on vision tasks. In our experiments, we also implement the FNet and show that our model outperforms it.
  Architecture variants. Due to the limitation from the quadratic complexity in the self-attention, vision transformers [10, 40] are usually designed to process a relatively small feature map (e.g., 14×14). However, our GFNet, which enjoys log-linear complexity, avoids that problem. Since in our GFNet the computational costs do not grow such significantly when the feature map size increases, we can adopt a hierarchical architecture inspired by the success of CNNs [21, 14]. Generally speaking, we can start from a large feature map (e.g., 56× 56) and gradually perform downsampling after a few blocks. In this paper, we mainly investigate two kinds of variants of GFNet: transformer-style models with a fixed number of tokens in each block and CNN-style hierarchical models with gradually downsampled tokens. For transformer-style models, we begin with a 12-layer model (GFNet-XS) with a similar architecture with DeiT-S and ResMLP-12. Then, we obtain 3 variants of the model (GFNet-Ti, GFNet-S and GFNet-B) by simply adjusting the depth and embedding dimension, which have similar computational costs with ResNet-18, 50 and 101 [14]. For hierarchical models, we also design three models (GFNet-H-Ti, GFNet-H-S and GFNet-H-B) that have these three levels of complexity following the design of PVT [43]. We use 4× 4 patch embedding to form the input tokens and use a non-overlapping convolution layer to downsample tokens following [43, 27]. Unlike PVT [43] and Swin [27], we directly apply our building block on different stages without any modifications. The detailed architectures are summarized in Table 2.
sections.heading: 4 Experiments
sections.text: We conduct extensive experiments to verify the effectiveness of our GFNet. We present the main results on ImageNet [8] and compare them with various architectures. We also test our models on the downstream transfer learning datasets including CIFAR-10/100 [20], Stanford Cars [19] and Flowers-102 [30]. Lastly, we investigate the efficiency and robustness of the proposed models and provide visualization to have an intuitive understanding of our method.
sections.heading: 4.1 ImageNet Classification
sections.text: Setups. We conduct our main experiments on ImageNet [8], which is a widely used large-scale benchmark for image classification. ImageNet contains roughly 1.2M images from 1,000 categories. Following common practice [14, 40], we train our models on the training set of ImageNet and report the single-crop top-1 accuracy on 50,000 validation images. To fairly compare with previous works [40, 39], we follow the most training details for our models and do not add extra regularization methods like [18]. Different from [40], we does not use EMA model [33], RandomEarse [50] and
  repeated augmentation [17], which are important to train DeiT while sightly hurting the performance of our models. We set the gradient clipping norm to 1 for all of our models. During finetuning at the higher resolution, we use the hyper-parameters suggested by the implementation of [40] and train the model for 30 epochs. All of our models are trained on a single machine with 8 GPUs. More details can be found in Appendix B.
  Comparisons with transformer-style architectures. The results are presented in Table 3. We compare our method with different transformer-style architectures for image classification including
  vision transformers (DeiT [40]) and MLP-like models (ResMLP [39] and gMLP [26]) that have similar complexity and number of parameters. We see that our method can clearly outperform recent MLP-like models such as ResMLP [39] and gMLP [26], and show similar performance to DeiT. Specifically, GFNet-XS outperforms ResMLP-12 by 2.0% while having slightly fewer FLOPs. GFNet-S also achieves better top-1 accuracy compared to gMLP-S and DeiT-S. Our tiny model is significantly better compared to both DeiT-Ti (+2.4%) and gMLP-Ti (+2.6%) with the similar level of complexity.
  Comparisons with hierarchical architectures. We compare different kinds of hierarchical models in Figure 4. ResNet [14] is the most widely used convolutional model while RegNet [34] is a family of carefully designed CNN models. We also compare with recent hierarchical vision transformers PVT [43] and Swin [27]. Benefiting from the log-linear complexity, GFNet-H models show significantly better performance than ResNet, RegNet and PVT and achieve similar performance with Swin while having a much simpler and more generic design.
  Fine-tuning at higher resolution. One prominent problem of MLP-like models is that the feature resolution is not adjustable. On the contrary, the proposed global filter is more flexible. We demonstrate the advantage of GFNet by finetuning the model trained at 224 × 224 resolution to higher resolution following the practice in vision transformers [40]. As shown in Table 3, our model can easily adapt to higher resolution with only 30 epoch finetuning and achieve better performance.
sections.heading: 4.2 Transfer learning
sections.text: To test the generality of our architecture and the learned representation, we evaluate GFNet on a set of commonly used transfer learning benchmark datasets including CIFAR-10 [20], CIFAR-100 [20], Stanford Cars [19] and Flowers-102 [30]. We follow the setting of previous works [37, 10, 40, 39], where the model is initialized by the ImageNet pre-trained weights and finetuned on the new datasets. We evaluate the transfer learning performance of our basic model and best model. The results are presented in Table 5. The proposed models generally work well on downstream datasets. GFNet models outperform ResMLP models by a large margin and achieve very competitive performance with state-of-the-art EfficientNet-B7. Our models also show competitive performance compared to state-of-the-art CNNs and vision transformers.
sections.heading: 4.3 Analysis and visualization
sections.text: Efficiency of GFNet. We demonstrate the efficiency of our GFNet in Figure 2, where the models are compared in theoretical FLOPs, actual latency and peak memory usage on GPU. We test a single building block of each model (including one token mixing layer and one FFN) with respect to the different numbers of tokens and set the feature dimension and batch size to 384 and 32 respectively. The self-attention model quickly runs out of memory when feature resolution exceeds 562, which is also the feature resolution of our hierarchical model. The advantage of the proposed architecture becomes larger as the resolution increases, which strongly shows the potential of our model in vision tasks requiring high-resolution feature maps.
  0.0 2.5 5.0 7.5 10.0 12.5 15.0 17.5 GFLOPs
  72
  74
  76
  78
  80
  82
  Im ag
  eN et
  T op
  -1 A
  cc (%
  )
  GFNet-Ti
  GFNet-XS
  GFNet-S
  GFNet-B
  GFNet-S 384
  DeiT-Ti
  DeiT-S
  DeiT-B
  ResMLP-12
  ResMLP-24
  ResMLP-36
  gMLP-Ti
  gMLP-S
  gMLP-B
  GFNet DeiT ResMLP gMLP
  Figure 3: ImageNet acc. vs model complexity.
  Table 6: Comparisons among the GFNet and other variants based on the transformer-like architecture on ImageNet. We show that GFNet outperforms the ResMLP [39], FNet [23] and models with local depthwise convolutions. We also report the number of parameters and theoretical complexity in FLOPs.
  Model Acc Param FLOPs(%) (M) (G)
  DeiT-S [40] 79.8 22 4.6
  Local Conv (3× 3) 77.7 15 2.8 Local Conv (5× 5) 78.1 15 2.9 Local Conv (7× 7) 78.2 15 2.9 ResMLP [39] 76.6 15 3.0 FNet [23] 71.2 15 2.9
  GFNet-XS 78.6 16 2.9
  Complexity/accuracy trade-offs. We show the computational complexity and accuracy trade-offs of various transformer-style architectures in Figure 3. It is clear that GFNet achieves the best trade-off among all kinds of models.
  Ablation study on the global filter. To more clearly show the effectiveness of the proposed global filters, we compare GFNet-XS with several baseline models that are equipped with different token mixing operations. The results are presented in Table 6. All models have a similar building block ( token mixing layer + FFN ) and the same feature dimension of D = 384. We also implement the recent FNet [23] for comparison, where a 1D FFT on feature dimension and a 2D FFT on spatial dimensions are used to mix tokens. As shown in Table 6, our method outperforms all baseline methods except DeiT-S that has 64% higher FLOPs.
  Robustness & generalization ability. Inspired by the [29], we further conduct experiments to evaluate the robustness and the generalization ability of the GFNet. For robustness, we consider ImageNet-A, ImageNet-C, FGSM and PGD. ImageNet-A [16] (IN-A) is a challenging dataset that contains natural adversarial examples. ImageNet-C [15] (IN-C) is used to validate the robustness of the model under various types of corruption. We use the mean corruption error (mCE, lower is better) on ImageNet-C as the evaluation metric. FGSM [12] and PGD [28] are two widely used algorithms that are targeted to evaluate the adversarial robustness of the model by single-step attack and multistep attack, respectively. For generalization ability, we adopt two variants of ImageNet validation set: ImageNet-V2 [36] (IN-V2) and ImageNet-Real [2] (IN-Real). ImageNet-V2 is a re-collected version of ImageNet validation set following the same data collection procedure of ImageNet, while ImageNet-Real contains the same images as ImageNet validation set but has reassessed labels. We compare GFNet-S with various baselines in Table 7 including CNNs, Transformers and MLP-like architectures and find the GFNet enjoys both favorable robustness and generalization ability.
  Visualization. The core operation in GFNet is the element-wise multiplication between frequencydomain features and the global filter. Therefore, it is easy to visualize and interpret. We visualize the frequency domain filters as well as their corresponding spatial domain filters in Figure 4. The learned global filters have more clear patterns in the frequency domain, where different layers have different characteristics. Interestingly, the filters in the last layer particularly focus on the low-frequency component. The corresponding filters in the spatial domain are less interpretable for humans.
sections.heading: 5 Conclusion
sections.text: We have presented the Global Filter Network (GFNet), which is a conceptually simple yet computationally efficient architecture for image classification. Our model replaces the self-attention sub-layer in vision transformer with 2D FFT/IFFT and a set of learnable global filters in the frequency domain. Benefiting from the token mixing operation with log-linear complexity, our architecture is highly efficient. Our experimental results demonstrated that GFNet can be a very competitive alternative to vision transformers, MLP-like models and CNNs in accuracy/complexity trade-offs.
sections.heading: Acknowledgment
sections.text: This work was supported in part by the National Key Research and Development Program of China under Grant 2017YFA0700802, in part by the National Natural Science Foundation of China under Grant 62125603, Grant 61822603, Grant U1813218, Grant U1713214, in part by Beijing Academy of Artificial Intelligence (BAAI), and in part by a grant from the Institute for Guo Qiang, Tsinghua University.
title: Global Filter Networks for Image Classification