AI Logic

Architectures

GhostNet

Updated:

Structure of GhostNet. Left: the overall flow (3×3 convolution, 16 Ghost bottlenecks, 1×1 convolution, global average pooling, 1×1 convolution, fully connected layer). Right: inside the Ghost module (a 1×1 primary convolution produces half of the channels, a depthwise convolution produces the other half, and the two are concatenated), and the Ghost bottlenecks with stride 1 and stride 2.

Background and Overview

GhostNet is a lightweight convolutional neural network for devices such as smartphones, built by stacking "Ghost modules," which produce only half of a convolution's output with an ordinary convolution and generate the other half by cheaply transforming it with depthwise convolutions. Han et al. at Huawei Noah's Ark Lab proposed it in 2019, and it was presented at CVPR 2020.

The intermediate feature maps of a well-trained CNN contain many maps that closely resemble one another. The paper lays out the feature maps from the first stage of ResNet-50 and shows many pairs in which one map could be obtained from the other by a simple transformation (the paper calls them "ghosts"; Figure 1 of the paper). Such redundant feature maps may help the network understand the input thoroughly. So rather than removing the redundancy, GhostNet generates the redundant feature maps with cheap operations.

Earlier lightweight models (the versions of MobileNet, ShuffleNet, and ShuffleNet V2) made 3×33 \times 3 convolutions lighter by turning them into depthwise convolutions, but the 1×11 \times 1 convolutions that mix channels still required much computation and memory. The Ghost module halves the output of this 1×11 \times 1 convolution and fills in the rest with cheap operations.

The paper's main results are as follows.

  • On ImageNet, GhostNet 1.3× reached 75.7% top-1 accuracy at 226 MFLOPs, surpassing MobileNetV3-Large (219 MFLOPs, 75.2%).
  • Comparing latency on a smartphone CPU, it was about 0.5 points more accurate than MobileNetV3 at the same latency.
  • The Ghost module can directly replace convolutions in existing networks. Replacing every convolution in ResNet-50 roughly halved the computation (4.1B → 2.2B FLOPs) with almost the same accuracy (75.3% vs. 75.0%).

Theory: The Ghost Module

The Ghost module is a component that produces a few "intrinsic" feature maps with an ordinary convolution, applies cheap linear transformations to each to produce "ghost" feature maps, and concatenates both as its output (Figure 2(b) of the paper). The upper right of the figure at the top of the article shows its internals.

When an ordinary convolution produces an nn-channel output YY from a cc-channel input XX (kernel size k×kk \times k, output size h′×w′h' \times w'), the cost is n⋅h′⋅w′⋅c⋅k⋅kn \cdot h' \cdot w' \cdot c \cdot k \cdot k. Since nn and cc are often in the hundreds, this cost is large.

The Ghost module produces the same nn-channel output as follows.

  1. Primary convolution: An ordinary convolution produces m=n/sm = n/s intrinsic feature maps Y′Y'. The kernel size, stride, and padding are the same as in the original convolution.
  2. Cheap linear transformations: Each intrinsic feature map yi′y'_i is passed through s−1s - 1 cheap linear transformations Φi,j\Phi_{i,j} to produce ghost feature maps yijy_{ij}.
  3. Identity mapping: The intrinsic feature maps themselves are kept in the output as is (the ss-th transformation Φi,s\Phi_{i,s} is the identity).
  4. Everything is concatenated into an output with n=m⋅sn = m \cdot s channels.

In equations, the primary convolution and the cheap linear transformations are as follows (f′f' is the primary convolution's filter).

Y′=X∗f′,yij=Φi,j(yi′)(i=1,…,m, j=1,…,s)Y' = X * f', \qquad y_{ij} = \Phi_{i,j}(y'_i) \quad (i = 1, \dots, m,\ j = 1, \dots, s)

For the cheap linear transformation Φ\Phi, a d×dd \times d depthwise convolution applied per channel is used. The paper notes that affine or wavelet transforms could also be considered, but chose depthwise convolution because convolution is well supported by hardware and can express a wide range of linear transformations, such as blurring and smoothing. Using the same dd within a module also allows efficient execution on CPUs and GPUs.

How much lighter it gets

When d×dd \times d is similar in size to k×kk \times k and s≪cs \ll c, the cost ratio relative to an ordinary convolution (the speed-up) is approximately ss (Eq. 4 of the paper).

rs=n⋅h′⋅w′⋅c⋅k⋅kns⋅h′⋅w′⋅c⋅k⋅k+(s−1)⋅ns⋅h′⋅w′⋅d⋅d≈s⋅cs+c−1≈sr_s = \frac{n \cdot h' \cdot w' \cdot c \cdot k \cdot k}{\frac{n}{s} \cdot h' \cdot w' \cdot c \cdot k \cdot k + (s-1) \cdot \frac{n}{s} \cdot h' \cdot w' \cdot d \cdot d} \approx \frac{s \cdot c}{s + c - 1} \approx s

The parameter ratio is likewise approximately ss (Eq. 5 of the paper). GhostNet uses s=2s = 2, d=3d = 3, and a 1×11 \times 1 primary convolution (k=1k = 1).

It is like copying a stack of documents: instead of handwriting every page, you write only half and produce the rest by running what you wrote through a copier with slight modifications. Since much of the content is similar, the modified copies serve well enough.

Depthwise separable convolutions such as MobileNet's go "1×11 \times 1 to mix channels → depthwise to process space," and every output channel is produced by the 1×11 \times 1 convolution. The Ghost module goes "produce a few maps with an ordinary convolution → add channels with a depthwise convolution," and differs in that part of what it produces is kept as is.

Overall Architecture

GhostNet is based on the MobileNetV3-Large architecture, with its bottleneck blocks replaced by Ghost bottlenecks. The left of the figure at the top of the article shows the overall flow, and the lower right shows the block internals.

The Ghost bottleneck (Figure 3 of the paper) resembles a ResNet residual block and stacks two Ghost modules.

  1. The first Ghost module expands the number of channels (the ratio of output to input channels is called the "expansion ratio"). BN and ReLU are applied.
  2. Only in blocks that halve the spatial size (stride 2), a stride-2 depthwise convolution (with BN) is inserted here.
  3. The second Ghost module reduces the number of channels to match the shortcut. Only BN is applied, without ReLU (following the suggestion of MobileNetV2).
  4. The input is added through the shortcut. In stride-2 blocks, the shortcut passes through a downsampling layer.

Some blocks also include the SE module from SENet (per-channel weighting). In the paper's reference implementation, SE sits between the two Ghost modules, and the shortcut of a stride-2 block is "depthwise convolution (stride 2) → 1×11 \times 1 convolution."

An image flows through the network in this order (for a 224×224 ImageNet image; Table 1 of the paper).

  1. A 3×33 \times 3 convolution (16 channels, stride 2) reduces it to 112×112.
  2. Sixteen Ghost bottlenecks are stacked. Output channels grow 16 → 24 → 40 → 80 → 112 → 160, and the spatial size shrinks 112 → 56 → 28 → 14 → 7.
  3. A 1×11 \times 1 convolution (960 channels), 7×7 global average pooling, and a 1×11 \times 1 convolution (1280 channels) follow, and a 1000-class fully connected layer predicts the class.

Besides the block internals, GhostNet differs from MobileNetV3 in that it does not use h-swish, which increases latency, and uses ReLU for every activation function. Multiplying the channel count of every layer by a width multiplier α\alpha gives GhostNet-α\alpha×, whose computation scales by roughly α2\alpha^2.

ImageNet training mostly follows the ShuffleNet settings, with an initial learning rate of 0.4 (8 GPUs, batch size 1024).

Why It Works

The paper studied the Ghost module's two hyperparameters (the size dd of the cheap linear transformation and the factor ss by which the output is multiplied) by replacing every convolution in VGG-16 with a Ghost module on CIFAR-10 (accuracy in %; Tables 3 and 4 of the paper).

SettingParametersComputationAccuracy
Original VGG-1615.0M313M93.6
s=2s = 2, d=1d = 17.6M157M93.5
s=2s = 2, d=3d = 37.7M158M93.7
s=2s = 2, d=5d = 57.7M160M93.4
s=2s = 2, d=7d = 77.7M163M93.1
s=3s = 3, d=3d = 35.2M107M93.4
s=4s = 4, d=3d = 34.0M80M93.0
s=5s = 5, d=3d = 33.3M65M92.9
  • d=3d = 3 works best: The paper explains that 1×11 \times 1 (d=1d = 1) cannot capture spatial information, while d=5d = 5 or 77 is more prone to overfitting and costs more.
  • Larger ss is lighter, with accuracy dropping gradually: With s=2s = 2, accuracy matched the original VGG-16 even with computation roughly halved.

The paper also checks whether ghost feature maps can really be produced by cheap transformations. Choosing three similar pairs from the feature maps of the first residual block of ResNet-50 and predicting one map from the other with a small depthwise convolution, the mean squared error was small for every pair (3.2–25.0 in units of 10−310^{-3}; Table 2 of the paper).

Effect of plugging it into an existing network (ImageNet; excerpt from Table 6 of the paper)

ModelParametersComputationTop-1 accuracy
ResNet-5025.6M4.1B75.3%
Ghost-ResNet-50 (s=2s = 2)13.0M2.2B75.0%
Ghost-ResNet-50 (s=4s = 4)6.5M1.2B74.1%

Simply replacing ResNet-50's convolutions roughly halved computation and parameters with almost no drop in accuracy. At similar computation, it was more accurate than the compared compression methods such as pruning (ThiNet, NISP, and others).

Experimental Results

Comparison on ImageNet (excerpt from Table 7 of the paper, grouped by computation level)

Computation levelModelParametersComputationTop-1 accuracy
About 50MShuffleNet V2 0.5×1.4M41M61.1%
MobileNetV3-Small 0.75×2.4M44M65.4%
GhostNet 0.5×2.6M42M66.2%
About 150MMobileNet (V1) 0.5×1.3M150M63.3%
ShuffleNet V2 1.0×2.3M146M69.4%
MobileNetV3-Large 0.75×4.0M155M73.3%
GhostNet 1.0×5.2M141M73.9%
200–300MMobileNetV2 1.0×3.5M300M71.8%
ProxylessNAS4.1M320M74.6%
MnasNet-A13.9M312M75.2%
MobileNetV3-Large 1.0×5.4M219M75.2%
GhostNet 1.3×7.3M226M75.7%

At every level, GhostNet had the highest accuracy. The paper attributes this to using computational resources efficiently to generate feature maps.

Latency on smartphones: Measured on an ARM smartphone with TFLite, a single thread, and batch size 1, GhostNet was about 0.5 points more accurate than MobileNetV3 at the same latency. For example, a GhostNet with 75.0% accuracy took about 40 ms per image, while a MobileNetV3 of similar accuracy took about 45 ms (Figure 7 of the paper).

Object detection (COCO minival, shorter input side 800; computation is that of the backbone on a 224×224 input; Table 8 of the paper)

BackboneComputationRetinaNet mAPFaster R-CNN (with FPN) mAP
MobileNetV2 1.0×300M26.7%27.5%
MobileNetV3 1.0×219M26.4%26.9%
GhostNet 1.1×164M26.6%26.9%

In detection as well, GhostNet reached about the same mAP as MobileNetV2 and MobileNetV3 with less computation.

Practical Considerations

Keep the following points in mind when using GhostNet.

  • Start with the paper's values for ss and dd: GhostNet uses s=2s = 2 and d=3d = 3 in every Ghost module. Increasing ss makes the model lighter but lowers accuracy.
  • It also works as a component for slimming existing models: The Ghost module has the same input and output shapes as an ordinary convolution, so it can replace convolutions in ResNet and other networks. Retraining is required.
  • When the output channel count is not divisible by ss: The reference implementation produces ⌈n/s⌉\lceil n/s \rceil intrinsic feature maps and trims the concatenated result to nn channels.

GhostNet has more parameters than other models with similar computation. For example, GhostNet 1.0× has 5.2M at 141 MFLOPs, MobileNetV3-Large 0.75× has 4.0M at 155 MFLOPs, and ShuffleNet V2 1.0× has 2.3M at 146 MFLOPs. On devices with limited storage or memory, compare parameter counts as well as computation. Also, the paper's latency comparison was measured on an ARM smartphone (TFLite, single thread), and the same gap may not hold on other hardware.

torchvision does not include GhostNet. You can use the authors' official implementation (huawei-noah/ghostnet) or timm's ghostnet_100, among others.

Comparison with Other Models

The table compares GhostNet with lightweight models from the same period, focusing on how output channels are produced. Accuracy is ImageNet top-1, and computation is as reported in each paper.

ModelHow output channels are producedBlock formActivationExample (computation, accuracy)
MobileNetV2All by 1×1 convolutionInverted residualReLU61.0× (300M, 71.8%)
ShuffleNet V2Half passed through, half convolvedSplit → concat → shuffleReLU1.5× (299M, 72.6%)
MobileNetV3-LargeAll by 1×1 convolutionInverted residual + SEReLU and h-swish1.0× (219M, 75.2%)
GhostNetHalf convolved, the rest by depthwise convolutionGhost bottleneck + SEReLU1.3× (226M, 75.7%)

Rough guidelines for choosing:

  • Highest possible accuracy at a given computation: GhostNet is a strong choice, though its parameter count is on the high side.
  • Especially small parameter count or memory: Also compare models with fewer parameters at the same computation, such as ShuffleNet V2.
  • Models searched for on-device latency: Compare MobileNetV3 and MnasNet.
  • Slimming an existing model: Instead of switching models, you can replace its convolutions with Ghost modules.

PyTorch example

Show code
python
import math
import torch
import torch.nn as nn

"""
Build the Ghost module and Ghost bottleneck following Eqs. 2-3 and Figures 2(b) and 3 of the paper.
The SE position and the stride-2 shortcut follow the paper's reference implementations (huawei-noah/ghostnet, timm).
torchvision has no built-in GhostNet.
"""


class GhostModule(nn.Module):
    def __init__(self, c_in, c_out, k=1, s=2, d=3, relu=True):
        super().__init__()
        self.c_out = c_out
        m = math.ceil(c_out / s)                       # number of intrinsic feature maps m = n / s
        act = nn.ReLU(inplace=True) if relu else nn.Identity()
        self.primary = nn.Sequential(                  # primary convolution (1×1 in GhostNet)
            nn.Conv2d(c_in, m, k, 1, k // 2, bias=False), nn.BatchNorm2d(m), act
        )
        self.cheap = nn.Sequential(                    # cheap linear transformation: d×d depthwise convolution (s − 1 per map)
            nn.Conv2d(m, m * (s - 1), d, 1, d // 2, groups=m, bias=False), nn.BatchNorm2d(m * (s - 1)), act
        )

    def forward(self, x):
        y1 = self.primary(x)                           # intrinsic feature maps (kept as is by the identity mapping)
        y2 = self.cheap(y1)                            # ghost feature maps
        return torch.cat([y1, y2], dim=1)[:, : self.c_out]


class SE(nn.Module):
    def __init__(self, ch, ratio=0.25):
        super().__init__()
        mid = max(8, int(ch * ratio))
        self.fc = nn.Sequential(
            nn.AdaptiveAvgPool2d(1), nn.Conv2d(ch, mid, 1), nn.ReLU(inplace=True), nn.Conv2d(mid, ch, 1), nn.Hardsigmoid()
        )

    def forward(self, x):
        return x * self.fc(x)


class GhostBottleneck(nn.Module):
    def __init__(self, c_in, c_exp, c_out, stride=1, use_se=False):
        super().__init__()
        self.ghost1 = GhostModule(c_in, c_exp, relu=True)          # expand (BN, ReLU)
        self.dw = (nn.Sequential(                                   # depthwise convolution only when stride is 2
            nn.Conv2d(c_exp, c_exp, 3, stride, 1, groups=c_exp, bias=False), nn.BatchNorm2d(c_exp)
        ) if stride == 2 else nn.Identity())
        self.se = SE(c_exp) if use_se else nn.Identity()
        self.ghost2 = GhostModule(c_exp, c_out, relu=False)        # reduce (BN only, no ReLU)
        if stride == 1 and c_in == c_out:
            self.shortcut = nn.Identity()
        else:                                                       # downsampling layer (as in the reference implementation)
            self.shortcut = nn.Sequential(
                nn.Conv2d(c_in, c_in, 3, stride, 1, groups=c_in, bias=False), nn.BatchNorm2d(c_in),
                nn.Conv2d(c_in, c_out, 1, bias=False), nn.BatchNorm2d(c_out),
            )

    def forward(self, x):
        out = self.ghost2(self.se(self.dw(self.ghost1(x))))
        return out + self.shortcut(x)


# One row of Table 1 in the paper (input 28×28×40, expansion 120, output 40, with SE, stride 1)
block = GhostBottleneck(40, 120, 40, stride=1, use_se=True)
block.eval()                                           # BN uses the mean and variance accumulated during training
with torch.no_grad():
    print(block(torch.randn(1, 40, 28, 28)).shape)    # torch.Size([1, 40, 28, 28])
down = GhostBottleneck(40, 240, 80, stride=2)          # a downsampling block from Table 1
print(down(torch.randn(2, 40, 28, 28)).shape)          # torch.Size([2, 80, 14, 14])

FAQ

Q. What are "ghost" feature maps?
A.

They are feature maps produced by applying cheap linear transformations, such as depthwise convolutions, to a few "intrinsic" feature maps generated by an ordinary convolution. The feature maps of a trained CNN contain many pairs that closely resemble each other, and one can be produced from the other by a simple transformation. The paper likens these to "ghosts" and produces them with cheap computation instead of computing everything expensively.

Q. How much does the Ghost module reduce computation?
A.

For a factor ss by which the output is multiplied, computation and parameters drop to roughly 1/s1/s. With the s=2s = 2 used in GhostNet, that is about half. Replacing every convolution in ResNet-50 with Ghost modules (s=2s = 2) reduced computation from 4.1B to 2.2B FLOPs, while ImageNet accuracy stayed nearly the same, going from 75.3% to 75.0%.

Q. How should I choose between GhostNet and MobileNetV3?
A.

If accuracy per unit of computation matters most, GhostNet has the edge. In the paper, GhostNet 1.3× (226 MFLOPs) was 0.5 points more accurate than MobileNetV3-Large (219 MFLOPs). On the other hand, GhostNet has more parameters (7.3M vs. 5.4M) and is not built into torchvision. Choose while also considering storage constraints and how easy it is to implement in your framework.

References

  1. Han, K., Wang, Y., Tian, Q., Guo, J., Xu, C., & Xu, C. (2020). GhostNet: More Features from Cheap Operations. Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR), 1580–1589. arXiv:1911.11907.
  2. Howard, A., et al. (2019). Searching for MobileNetV3. ICCV 2019. arXiv:1905.02244.
  3. huawei-noah. GhostNet (official implementation by the authors).
  4. Wightman, R. PyTorch Image Models (timm). timm/models/ghostnet.py.