ShuffleNet reduces computation by making even the 1×1 convolutions grouped convolutions, and compensates for the resulting lack of information flow between groups with a "channel shuffle" (a reordering of channels). It is a convolutional neural network for devices with very limited computing resources, proposed in 2017 by Zhang et al. at Megvii (Face++).
Contemporary lightweight models such as Xception, ResNeXt, and MobileNet made 3×3 convolutions lighter by turning them into depthwise convolutions or grouped convolutions. As a result, however, most of the computation remains in the 1×1 convolutions that mix channels. In ResNeXt (with 32 groups), for example, 93.4% of a block's computation was in the 1×1 convolutions. In small models with a tight compute budget, the cost of these 1×1 layers prevents increasing the number of channels and lowers accuracy. ShuffleNet aims to make these 1×1 convolutions lighter.
The main results of the paper are as follows.
On ImageNet, at about 40 MFLOPs, its top-1 error is 7.8 points lower than MobileNet (41.6% vs. 49.4%).
ShuffleNet 2× at 524 MFLOPs has 3.1 points lower error than MobileNet (569 MFLOPs) (26.3% vs. 29.4%).
On a smartphone CPU (Snapdragon 820), it runs about 13 times faster than AlexNet at almost the same accuracy (18 times in theoretical FLOPs).
The ideas of ShuffleNet were carried over to the later ShuffleNet V2.
Theory: 1×1 grouped convolution and channel shuffle
The core of ShuffleNet consists of two parts: 1×1 grouped convolution and the channel shuffle.
Making the 1×1 convolution grouped
A grouped convolution splits the channels into g groups and convolves only within each group, reducing the computation to 1/g. ShuffleNet applies this to the 1×1 convolutions as well.
However, when two grouped convolutions are stacked, each output group is computed only from the input channels that were in the same group from the start. Because no information flows between groups, representational power weakens (Figure 1(a) of the paper).
Mixing groups with the channel shuffle
The solution is to reorder the output channels of the first grouped convolution so that every group of the next layer receives channels from all groups of the previous layer. This reordering is the channel shuffle, computed as follows (Section 3.1 of the paper).
Let the output of the previous layer have g×n channels (g is the number of groups and n is the number of channels per group).
Reshape the channel dimension into a table of shape (g,n).
Transpose the table into shape (n,g).
Flatten it back into a single row and feed it to the next layer.
Since it is only a reordering, it costs almost nothing, and since it is differentiable, it can be embedded in the network and trained as is. It also works when the two layers have different numbers of groups. The lower right of the figure at the top of the article shows an example with g=3 and n=3.
It is like dealing playing cards into three piles and then rebuilding new piles by taking one card from each pile in turn. Each new pile contains cards from all of the original piles.
ShuffleNet unit
The ShuffleNet unit is based on the ResNet bottleneck block and computes as follows (Figure 2(b) of the paper).
A 1×1 grouped convolution reduces the channel count to 1/4 of the output (the bottleneck), followed by batch normalization (BN) and ReLU.
Apply the channel shuffle.
Apply a 3×3 depthwise convolution followed by BN. No ReLU is applied here (following the suggestion of Xception).
A 1×1 grouped convolution restores the channel count, followed by BN. No shuffle is applied after this one (doing so gave almost the same results).
Add the input as is and apply ReLU.
The unit that halves the spatial size (Figure 2(c) of the paper) changes two things.
A 3×3 average pooling (stride 2) is placed on the shortcut, and the depthwise convolution also uses stride 2.
Instead of addition, the outputs are concatenated along the channel dimension. This increases the channel count with little extra computation.
For an input of c×h×w, a bottleneck of m channels, and g groups, the computation (FLOPs) of one unit compares as follows (Section 3.2 of the paper).
Unit
FLOPs
ResNet bottleneck
hw(2cm+9m2)
ResNeXt
hw(2cm+9m2/g)
ShuffleNet unit
hw(2cm/g+9m)
In ShuffleNet, the 1×1 part (2cm) is also reduced to 1/g, and the 3×3 part is a depthwise convolution, so it is merely proportional to m. For the same FLOPs, the channel count can be increased accordingly.
Overall architecture
ShuffleNet starts with a 3×3 convolution and max pooling, followed by ShuffleNet units stacked in three stages. In the figure at the top of the article, the left side shows the overall flow and the right side shows the inside of the units.
An image flows through in the following order (for a 224×224 ImageNet image; Table 1 of the paper).
A 3×3 convolution (24 channels, stride 2) reduces it to 112×112.
A 1000-class fully connected layer predicts the class.
The more groups g, the lighter the 1×1 layers, so the channel count can be increased for the same FLOPs (about 140 MFLOPs) (Table 1 of the paper).
Number of groups g
1
2
3
4
8
Stage2 output channels
144
200
240
272
384
Stage3 output channels
288
400
480
544
768
Stage4 output channels
576
800
960
1088
1536
FLOPs (MFLOPs)
143
140
137
133
137
The network in this table is called "ShuffleNet 1×", and the one with s times as many channels is called "ShuffleNet s×". Its FLOPs are roughly s2 times larger.
The ImageNet training settings in the paper are as follows (based on the ResNeXt settings, with two changes).
Small networks tend to suffer from underfitting rather than overfitting, so regularization is weakened.
Why it works
The paper confirms that each of ShuffleNet's two ideas is effective as follows (ImageNet top-1 error; lower is better).
Effect of the number of groups (varying the number of groups g with FLOPs held fixed; Table 2 of the paper)
Model (FLOPs)
g=1
g=2
g=3
g=4
g=8
ShuffleNet 1× (140 MFLOPs)
33.6
32.7
32.6
32.8
32.4
ShuffleNet 0.5× (38 MFLOPs)
45.1
44.4
43.2
41.6
42.3
ShuffleNet 0.25× (13 MFLOPs)
57.1
56.8
55.0
54.2
52.7
Grouped models (g>1) outperformed the ungrouped model (g=1) at every size. The effect is larger for smaller models: the gap between the best setting and g=1 is 1.2 points for 1×, 3.5 points for 0.5×, and 4.4 points for 0.25×. The paper attributes this to grouping allowing more channels (wider feature maps). On the other hand, too many groups, as in g=8 for 0.5×, can make things worse because each filter sees fewer input channels.
Effect of the channel shuffle (Table 3 of the paper)
Model
Without shuffle
With shuffle
Difference
ShuffleNet 1× (g=3)
34.5
32.6
1.9
ShuffleNet 1× (g=8)
37.6
32.4
5.2
ShuffleNet 0.5× (g=3)
45.7
43.2
2.5
ShuffleNet 0.5× (g=8)
48.1
42.3
5.8
ShuffleNet 0.25× (g=3)
56.3
55.0
1.3
ShuffleNet 0.25× (g=8)
56.5
52.7
3.8
The shuffle lowered the error in every setting. The gap is larger with more groups (g=8), showing the importance of exchanging information between groups.
ShuffleNet with g=1 uses ordinary 1×1 convolutions and no shuffle, which is the same as an "Xception-like" unit using depthwise separable convolution. ShuffleNet can be seen as this form with the 1×1 layers made even lighter.
Experimental results
Comparison with other units (keeping the overall architecture of Table 1, replacing only the units in Stage2–4, and matching FLOPs by adjusting channel counts. Top-1 error %. Table 4 of the paper)
Comparing the Stage4 output channel counts at 38 MFLOPs, VGG-like, ResNet, ResNeXt, Xception-like, and ShuffleNet have 50, 192, 192, 288, and 576, respectively, and more channels tended to give higher accuracy.
Comparison with MobileNet (top-1 error %. Table 5 of the paper)
ShuffleNet has 50 layers and is deeper than MobileNet (28 layers), so the paper also compares a 26-layer "shallow" version with half the units in Stage2–4. Even the shallow version is 6.6 points better than MobileNet, and the paper states that the gain comes from the architecture rather than the depth.
Actual speed on hardware (Snapdragon 820, single thread, 224×224 input. Table 8 of the paper)
Object detection (COCO minival, Faster R-CNN, mAP@[.5, .95]. Table 7 of the paper)
Backbone
300 input
600 input
ShuffleNet 2× (g=3)
18.7%
25.0%
ShuffleNet 1× (g=3)
14.5%
19.8%
1.0 MobileNet-224 (value from the MobileNet paper)
16.4%
19.8%
1.0 MobileNet-224 (retrained with the same settings)
14.9%
19.3%
Practical considerations
Keep the following points in mind when using ShuffleNet.
Do not put ReLU after the depthwise convolution: following the suggestion of Xception, the paper applies only BN after the depthwise convolution.
The downsampling unit concatenates, so count channels carefully: since the output is concatenated with the shortcut (average pooling), design the last 1×1 of the main path to output "output channels − input channels".
Make channel counts divisible by the number of groups: both the input and output channel counts of a grouped convolution must be multiples of g.
Even with low FLOPs, actual speed on hardware does not improve as much as the FLOPs ratio suggests. In the paper's implementation, reducing FLOPs to 1/4 gave only about a 2.6× actual speedup, and compared with AlexNet the measured speedup was about 13× against a theoretical 18×. More groups (g=4 or 8) often improve accuracy, but they reduced efficiency in the paper's implementation, which reported g=3 as the best trade-off between speed and accuracy. Models with SE added also became 25–40% slower on smartphones despite almost no increase in FLOPs. Measure on your target device before deciding on settings.
torchvision does not include the ShuffleNet (V1) from this paper. torchvision.models.shufflenet_v2_x1_0 and related models are ShuffleNet V2, which was redesigned with emphasis on actual hardware speed and has a different unit structure.
Comparison with other models
The following compares ShuffleNet with contemporary lightweight models, focusing on how they mix channels.
Low inference memory; built from standard operations only
ShuffleNet uses depthwise convolution only on the bottleneck layers, which have few channels. This is because depthwise convolution involves many memory reads and writes relative to its computation and is hard to run efficiently on small devices.
As a rule of thumb:
When accuracy is needed under a very small compute budget (tens of MFLOPs): ShuffleNet has the advantage. The smaller the model, the larger its gap over MobileNet.
When it must run on any framework or device: some environments cannot execute grouped convolution and channel shuffle efficiently. Even the MobileNetV2 paper could not measure ShuffleNet's latency with the TF-Lite of the time. In such cases, consider MobileNetV2 or similar models.
When actual hardware speed is the top priority: ShuffleNet V2 and MobileNetV3, which were designed from the start around actual hardware speed, are also candidates.
PyTorch example
▶Show code
python
import torch
import torch.nn as nn
"""
Build the ShuffleNet (V1) unit and network exactly as in Figure 2 and Table 1 of the paper.
torchvision has no built-in V1 (shufflenet_v2_* is ShuffleNet V2, a different architecture).
"""
def channel_shuffle(x, groups):
# (N, g·n, H, W) → (N, g, n, H, W) → transpose to (N, n, g, H, W) → (N, g·n, H, W) (Sec. 3.1)
n_batch, ch, h, w = x.shape
x = x.view(n_batch, groups, ch // groups, h, w)
x = x.transpose(1, 2).contiguous()
return x.view(n_batch, ch, h, w)
class ShuffleUnit(nn.Module):
def __init__(self, in_ch, out_ch, groups, stride, first_group=True):
super().__init__()
self.stride = stride
self.groups = groups
# The downsampling unit concatenates with the input, so the main path produces (output − input) channels
main_out = out_ch - in_ch if stride == 2 else out_ch
mid = out_ch // 4 # Bottleneck is 1/4 of the output
g1 = groups if first_group else 1 # The first 1×1 in Stage2 is not grouped
self.gconv1 = nn.Sequential(
nn.Conv2d(in_ch, mid, 1, groups=g1, bias=False), nn.BatchNorm2d(mid), nn.ReLU(inplace=True)
)
self.g1 = g1
self.dwconv = nn.Sequential( # Only BN after the depthwise conv (no ReLU)
nn.Conv2d(mid, mid, 3, stride=stride, padding=1, groups=mid, bias=False), nn.BatchNorm2d(mid)
)
self.gconv2 = nn.Sequential( # No shuffle after this one
nn.Conv2d(mid, main_out, 1, groups=groups, bias=False), nn.BatchNorm2d(main_out)
)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
out = self.gconv1(x)
if self.g1 > 1:
out = channel_shuffle(out, self.g1)
out = self.gconv2(self.dwconv(out))
if self.stride == 2: # Concatenate with the average-pooling shortcut (Fig. 2(c))
short = nn.functional.avg_pool2d(x, 3, stride=2, padding=1)
return self.relu(torch.cat([short, out], dim=1))
return self.relu(x + out) # Add, then ReLU (Fig. 2(b))
class ShuffleNetV1(nn.Module):
STAGE_CH = {1: 144, 2: 200, 3: 240, 4: 272, 8: 384} # Stage2 output channels from Table 1
def __init__(self, groups=3, num_classes=1000):
super().__init__()
self.stem = nn.Sequential(
nn.Conv2d(3, 24, 3, stride=2, padding=1, bias=False), nn.BatchNorm2d(24), nn.ReLU(inplace=True),
nn.MaxPool2d(3, stride=2, padding=1),
)
layers, in_ch, out_ch = [], 24, self.STAGE_CH[groups]
for stage, repeat in enumerate([4, 8, 4]): # Number of units in Stage2–4
for i in range(repeat):
layers.append(ShuffleUnit(in_ch, out_ch, groups, stride=2 if i == 0 else 1,
first_group=not (stage == 0 and i == 0)))
in_ch = out_ch
out_ch *= 2 # Double the channels for the next stage
self.stages = nn.Sequential(*layers)
self.pool = nn.AdaptiveAvgPool2d(1)
self.fc = nn.Linear(in_ch, num_classes)
def forward(self, x):
x = self.pool(self.stages(self.stem(x)))
return self.fc(torch.flatten(x, 1))
model = ShuffleNetV1(groups=3)
model.eval() # BN uses the running mean and variance accumulated during training
with torch.no_grad():
print(model(torch.randn(1, 3, 224, 224)).shape) # torch.Size([1, 1000])
model.train() # During training, BN uses the mini-batch mean and variance
FAQ
Q. Does the channel shuffle have learnable parameters?
A.
No. It only reorders the channels according to a fixed rule: arrange the g×n channels into a (g,n) table, transpose it, and flatten it back into a single row. It costs almost nothing, and because it is a reordering, gradients can be routed straight back to their original positions.
Q. How many groups $g$ should be used?
A.
In the paper's ImageNet results, every g>1 was better than g=1, and larger g favored smaller models. On the other hand, when actual hardware speed is also taken into account, the paper's implementation reported g=3 as the best balance between accuracy and speed. Start with g=3 and tune it while measuring speed on your target device.
Q. What is the difference between ShuffleNet and ShuffleNet V2?
A.
ShuffleNet (V1) was designed mainly to reduce FLOPs and uses 1×1 grouped convolution and channel shuffle. ShuffleNet V2 is a redesign that takes memory reads and writes and other factors into account, because FLOPs alone do not determine actual hardware speed. The version built into torchvision is V2.