Back to paper library
Paper notesarXiv 2016ECCV 2016

ResNet v2

Keep the Shortcut Clean

Original paper

Identity Mappings in Deep Residual Networks

Paper authors

Kaiming He · Xiangyu Zhang · Shaoqing Ren · Jian Sun

Publication
ECCV 2016Conference
First submitted to arXiv
arXiv:1603.05027
Cite this paper · BibTeX

The citation identifies the linked source version. It cites the original paper and its authors.

@misc{classiccnnsresnetv22016,
  title = {{Identity Mappings in Deep Residual Networks}},
  author = {Kaiming He and Xiangyu Zhang and Shaoqing Ren and Jian Sun},
  year = {2016},
  eprint = {1603.05027},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1603.05027}
}
On this page

ResNet made 152-layer networks trainable and won ILSVRC 2015. On CIFAR-10, though, the original paper's 1202-layer ResNet had an error rate of 7.93%, actually worse than the 110-layer network's 6.61%.

Residual connections let networks go deeper, but at a thousand layers, a new problem showed up.

In March 2016, the original team, Kaiming He, Xiangyu Zhang, Shaoqing Ren, and Jian Sun, released this paper. This time they went back and wrote out the propagation formulas of the residual unit, used them to work out how the signal could flow most smoothly, and redesigned the residual unit accordingly. After the change, a 1001-layer ResNet reached 4.62% error on CIFAR-10.

This new version later came to be commonly called ResNet v2, or the pre-activation ResNet.

Defining the Problem

First, write the original residual unit in general form. The input to the ll-th unit is xlx_l:

yl=h(xl)+F(xl,Wl),xl+1=f(yl)y_l = h(x_l) + \mathcal{F}(x_l, \mathcal{W}_l), \qquad x_{l+1} = f(y_l)

F\mathcal{F} is the residual function, for example two 3×33\times3 convolutions; hh is the operation on the shortcut; ff is the operation after the addition. In the original ResNet, h(xl)=xlh(x_l) = x_l is an identity mapping and ff is ReLU.

On the left, diagrams of two residual units. (a) original: x_l runs straight down, and the branch on the right is weight, BN, ReLU, weight, BN in order; after being added to the shortcut, the result goes through ReLU to give x_l+1. (b) proposed: the branch on the right is BN, ReLU, weight, BN, ReLU, weight in order, and the addition gives x_l+1 directly. In both, the shortcut is drawn as a thick gray arrow. On the right are the training curves of a 1001-layer ResNet on CIFAR-10: yellow is original (error 7.61%) and blue is proposed (error 4.92%); dashed lines are training loss and solid lines are test error, and the blue training loss falls much faster right from the start
Figure 1 from the paper, excerpted from page 2 of the v3 PDF. Left: (a) the original residual unit, (b) the residual unit proposed in the paper; the gray arrows are the paths along which information propagates most easily. Right: training curves of a 1001-layer ResNet on CIFAR-10; dashed lines are training loss (left axis), and solid lines are test error (right axis).

The curves on the right put the problem in plain view. At the same 1001 layers, the original design's loss falls very slowly early in training and ends at a test error of 7.61%; the modified design's loss falls quickly from the start and ends at 4.92%.

The two units on the left use exactly the same components; only the order differs. In the original, ReLU comes after the addition, sitting on the shortcut path; the new design moves both BN and ReLU in front of the weight layers and does nothing after the addition. As a result, the gray shortcut passes through no operation at all, from start to finish.

Why does this matter? The authors' argument starts by treating both hh and ff as identity mappings.

Solving the Problem

Two Identity Mappings

If ff is also an identity mapping, xl+1=ylx_{l+1} = y_l, and the equation above becomes:

xl+1=xl+F(xl,Wl)x_{l+1} = x_l + \mathcal{F}(x_l, \mathcal{W}_l)

Unrolling all the way to a deeper unit LL:

xL=xl+∑i=lL−1F(xi,Wi)x_L = x_l + \sum_{i=l}^{L-1} \mathcal{F}(x_i, \mathcal{W}_i)

By this equation, any deep feature xLx_L equals some shallower feature xlx_l plus the outputs of all the residual functions in between. Compare an ordinary network, where xLx_L is the result of a chain of matrix multiplications, roughly ∏iWi x0\prod_i W_i\, x_0 (ignoring BN and ReLU). One adds; the other multiplies.

The same goes for backpropagation. With loss E\mathcal{E}, by the chain rule:

∂E∂xl=∂E∂xL∂xL∂xl=∂E∂xL(1+∂∂xl∑i=lL−1F(xi,Wi))\frac{\partial \mathcal{E}}{\partial x_l} = \frac{\partial \mathcal{E}}{\partial x_L}\frac{\partial x_L}{\partial x_l} = \frac{\partial \mathcal{E}}{\partial x_L}\left(1 + \frac{\partial}{\partial x_l}\sum_{i=l}^{L-1}\mathcal{F}(x_i, \mathcal{W}_i)\right)

The 1 in the parentheses delivers ∂E/∂xL\partial\mathcal{E}/\partial x_L straight to the shallower xlx_l without passing through any weight layer. The other term does pass through weight layers, but it's unlikely to be exactly −1-1 for every sample in a mini-batch and cancel the whole parenthesis out. So even when the weights are very small, the gradient won't vanish.

Both of these nice properties require the shortcut hh to be an identity mapping, and the after-addition ff to be an identity mapping too. In terms of the figure, the gray arrow must carry no operation other than the addition.

The authors then check this in two parts, first changing the shortcut hh and then the after-addition ff.

What If the Shortcut Carries Something Extra?

The simplest way to break it is to multiply the shortcut by a constant λl\lambda_l, that is, h(xl)=λlxlh(x_l) = \lambda_l x_l. Unrolling the same way, the backpropagation equation becomes:

∂E∂xl=∂E∂xL(∏i=lL−1λi+∂∂xl∑i=lL−1F^(xi,Wi))\frac{\partial \mathcal{E}}{\partial x_l} = \frac{\partial \mathcal{E}}{\partial x_L}\left(\prod_{i=l}^{L-1}\lambda_i + \frac{\partial}{\partial x_l}\sum_{i=l}^{L-1}\hat{\mathcal{F}}(x_i, \mathcal{W}_i)\right)

F^\hat{\mathcal{F}} absorbs those constants. The original 1 has turned into a product of λ\lambda's. In a very deep network, if every λ\lambda is greater than 1, this term explodes exponentially; if every λ\lambda is less than 1, it vanishes exponentially, and the gradient has to go through the weight layers instead.

Take ResNet-110, which has 54 residual units. When the residual functions are small, the gradient reaching the first layer is almost exactly λ54\lambda^{54}. λ=0.9\lambda = 0.9 looks like only a 10% discount, yet after 54 units only three thousandths remain; λ=0.5\lambda = 0.5 shrinks it to 10−1710^{-17}. With more complex shortcuts such as gates or 1×11\times1 convolutions, the product becomes one of each shortcut's derivatives, which can block the signal just the same.

The authors then compare several kinds of shortcut in practice, on ResNet-110 for CIFAR-10:

Diagrams of six residual units, each with a branch on the right of 3x3 conv, ReLU, 3x3 conv; they differ in the shortcut: (a) original has no operation; (b) constant scaling multiplies both the shortcut and the branch by 0.5; (c) exclusive gating computes a gate with a 1x1 conv and a sigmoid, multiplying the branch by the gate value and the shortcut by 1 minus the gate value; (d) shortcut-only gating multiplies only the shortcut by 1 minus the gate value; (e) conv shortcut has a 1x1 conv on the shortcut; (f) dropout shortcut has dropout on the shortcut. Every unit is followed by ReLU after the addition
Figure 2 from the paper, excerpted from page 5 of the v3 PDF. The kinds of shortcut used in Table 1. For simplicity, the BN after each weight layer isn't drawn.
Table: Selected rows excerpted from Table 1 of the paper. CIFAR-10 test set, ResNet-110, median of five runs; an error above 20% is marked “fail.” These experiments still use ReLU after the addition.
ShortcutCoefficient on the shortcutCoefficient on the branch F\mathcal{F}Error
(a) Original identity shortcut116.61%
(b) Constant scaling01fail (a plain network)
(b) Constant scaling0.51fail
(b) Constant scaling0.50.512.35%
(c) Exclusive gating, bgb_g initialized to −6-61−g(x)1-g(x)g(x)g(x)8.70%
(c) Exclusive gating, bgb_g initialized to 0 through −5-51−g(x)1-g(x)g(x)g(x)fail
(d) Shortcut-only gating, bgb_g initialized to 01−g(x)1-g(x)112.86%
(d) Shortcut-only gating, bgb_g initialized to −6-61−g(x)1-g(x)16.91%
(e) 1×11\times1 convolutional shortcut1×11\times1 conv112.22%
(f) Dropout shortcutdropout 0.51fail

Let's go through these shortcuts one by one.

  • Constant scaling by 0.5: without scaling the branch, it fails to train; with the branch also multiplied by 0.5 (like a Highway network with its gates frozen), it converges, but the error is 12.35%, nearly twice the baseline.
  • Exclusive gating is the Highway Networks approach. The initial value of the gate bias is critical: the authors searched between 0 and −10-10, and even the best value, −6-6, reaches only 8.70%.
  • With shortcut-only gating, an initial bias of −6-6 makes 1−g(x)1-g(x) close to 1, so the shortcut is nearly an identity mapping, and its 6.91% is also the closest to the baseline.
  • 1×11\times1 convolutional shortcuts worked well on the original 34-layer ResNet (option C in the ResNet paper), but stacked over 54 units they give 12.22%. The authors saw something similar with ResNet-101 on ImageNet.
  • A dropout shortcut statistically amounts to multiplying the shortcut by an expected value of 0.5, so it blocks the signal just like constant scaling.

Gates and 1×11\times1 convolutions both add parameters, so in theory they should be more expressive; with shortcut-only gating or 1×11\times1 convolutions, the solution space even contains the identity shortcut, which they would only need to learn. Yet their training error is higher. So the degradation comes from optimization and has nothing to do with expressiveness.

Where Should the After-Addition ReLU Go?

In the previous experiments, the after-addition ff was still ReLU, so the derivation held only approximately. Next up is ff.

The only way to make ff an identity mapping is to rearrange where ReLU and BN go. The authors tried these arrangements:

Diagrams of five residual units with the same components in different orders. (a) original: weight, BN, ReLU, weight, BN, then ReLU after the addition. (b) BN after addition: weight, BN, ReLU, weight, then BN and ReLU after the addition. (c) ReLU before addition: weight, BN, ReLU, weight, BN, ReLU, then the addition. (d) ReLU-only pre-activation: ReLU, weight, BN, ReLU, weight, BN, then the addition. (e) full pre-activation: BN, ReLU, weight, BN, ReLU, weight, then the addition
Figure 4 from the paper, excerpted from page 8 of the v3 PDF. The activation placements used in Table 2; all units have the same components, only in different orders.
Table: Excerpted from Table 2 of the paper. CIFAR-10 test error. ResNet-164 uses the bottleneck structure (1×11\times1, 3×33\times3, 1×11\times1), with computation similar to a unit of two 3×33\times3 layers.
ArrangementResNet-110ResNet-164
(a) Original residual unit6.61%5.93%
(b) BN after addition8.17%6.50%
(c) ReLU before addition7.84%6.14%
(d) ReLU-only pre-activation6.71%5.91%
(e) Full pre-activation6.37%5.46%

(b) BN after addition is an experiment in the opposite direction: ff now includes both BN and ReLU. The result is clearly worse. BN now alters the signal on the shortcut, and the loss has a hard time coming down early in training.

(c) ReLU before the addition is the most direct way to make ff an identity mapping, and it also made things worse. The problem is that with ReLU at the end of the branch, the residual function's output can never be negative. Intuitively, a “residual” should be able to go either way; if it can only be positive, every added unit raises the signal's mean another notch. The full pre-activation's residuals are both positive and negative, so the mean stays near 0.

A Different Angle: Pre-Activation

The remaining two arrangements come from a shift in perspective.

In the original design, the after-addition activation ff affects both paths of the next unit:

yl+1=f(yl)+F(f(yl),Wl+1)y_{l+1} = f(y_l) + \mathcal{F}(f(y_l), \mathcal{W}_{l+1})

If this activation acts only on the residual branch, with the shortcut bypassing it, we get:

yl+1=yl+F(f^(yl),Wl+1)y_{l+1} = y_l + \mathcal{F}(\hat{f}(y_l), \mathcal{W}_{l+1})

Changing notation, this is xl+1=xl+F(f^(xl),Wl)x_{l+1} = x_l + \mathcal{F}(\hat{f}(x_l), \mathcal{W}_l). Nothing comes after the addition, so ff has become an identity mapping; what used to be “the previous unit's output activation” has become “the next unit's input activation.”

Three columns of diagrams. (a) The original residual unit has an act. after the addition, and this act. affects both the shortcut and the branch of the next unit. (b) The after-addition act. is placed only on the branch leading to the weights (outlined in red), and the shortcut bypasses it. (c) After redrawing the unit boundaries, this is equivalent to a pre-activation residual unit in which each unit applies act. before weight, with no operation after the addition
Figure 5 from the paper, excerpted from page 10 of the v3 PDF. Applying the after-addition activation only to the weight branch is equivalent to building a pre-activation residual unit.

Going from (b) to (c) in the figure just redraws the unit boundaries. The authors also point out that this before/after distinction only means something in branched structures with additions; an ordinary NN-layer network has N−1N-1 activations, and it makes no difference whether you call them pre-activations or post-activations.

Following this idea, (d) in Figure 4 moves only ReLU to the front, with results about the same as the original design; the authors suspect that this ReLU, not paired with BN, doesn't get BN's benefits. (e) moves both BN and ReLU in front of the weight layers, and the results clearly improve. This is the unit the paper proposes.

Discussion

The Deeper, the Wider the Gap

Table: Excerpted from Table 3 of the paper. ResNet-1001 uses the bottleneck structure, with 333 residual units in all, 111 for each feature map size.
DatasetNetworkOriginal unitPre-activation
CIFAR-10ResNet-110 (one layer per unit)9.90%8.91%
CIFAR-10ResNet-1106.61%6.37%
CIFAR-10ResNet-1645.93%5.46%
CIFAR-10ResNet-10017.61%4.92%
CIFAR-100ResNet-16425.16%24.33%
CIFAR-100ResNet-100127.82%22.71%

Pre-activation is better in every case, and the deeper the network, the bigger the gap. At 1001 layers, CIFAR-10 error falls from 7.61% to 4.92%, and CIFAR-100 error from 27.82% to 22.71%.

The authors attribute the gains to two things.

First, easier optimization. This is most visible at 1001 layers (Figure 1, right). When ff is ReLU, negative signals get truncated; with many units, the earlier formulas stop being a good approximation. The 164-layer network is affected much less: its curve suffers a little early in training and quickly recovers. Monitoring the network's responses, the authors found that after some training the weights adjust so that yly_l is mostly above 0, and ReLU rarely truncates anything; xlx_l is already non-negative after the previous ReLU, so yly_l drops below 0 only when F\mathcal{F} is strongly negative. At a thousand layers, truncation happens far more often.

Second, less overfitting. The pre-activation version converges to a slightly higher training loss but a lower test error. The authors suspect this is BN's regularizing effect: in the original unit, the BN-normalized signal is immediately added to the shortcut, so what actually enters the next weight layer is unnormalized; in the pre-activation version, the input to every weight layer goes through BN first.

Comparison with Other Methods

On CIFAR, the authors' ResNet-1001 reaches 4.92% on CIFAR-10 (median of five runs), and 4.62% with the mini-batch size changed to 64; on CIFAR-100 it reaches 22.71%. They stress that these results come without specially tailoring the width or filter sizes and without regularization such as dropout, which is very effective on small datasets; all they rely on is “going deeper.”

On ImageNet, the authors first note an evaluation detail. The original ResNet was trained with the shorter side scaled to between 256 and 480, yet tested on 224×224224\times224 crops from images with a shorter side of 256, which works against ResNet. So this time they take a single 320×320320\times320 crop from images with a shorter side of 320. ResNet is fully convolutional and can take larger inputs directly, and this size is also close to Inception-v3's 299.

Table: Excerpted from Table 5 of the paper. ILSVRC 2012 validation set, single crop; ResNets are trained at 224×224224\times224 and tested at 320×320320\times320, while Inception-v3 is trained and tested at 299×299299\times299.
MethodAugmentationTop-1 errorTop-5 error
ResNet-152, original unitscale21.3%5.5%
ResNet-152, pre-activationscale21.1%5.5%
ResNet-200, original unitscale21.8%6.0%
ResNet-200, pre-activationscale20.7%5.3%
ResNet-200, pre-activationscale + aspect ratio20.1%4.8%
Inception-v3scale + aspect ratio21.2%5.6%

Switching ResNet-152 to pre-activation improves it only from 21.3% to 21.1%, because it had no obvious generalization problem to begin with. ResNet-200 is a different story. With the original unit, its error is 21.8%, worse than ResNet-152, yet its training error is lower than ResNet-152's, which means it's overfitting. With pre-activation it gets 20.7%, better than both versions of ResNet-152.

The authors also finished a “BN after addition” version of ResNet-101 on ImageNet, with a single-crop error of 24.6%/7.5%, worse than the original's 23.6%/7.1% and consistent with the CIFAR results.

As for computational cost, model complexity grows linearly with depth, so 1001 layers cost about ten times as much as 100 layers. ResNet-1001 on CIFAR takes about 27 hours to train on two GPUs; ResNet-200 on ImageNet takes about three weeks on eight GPUs.

Conclusion

Keeping the shortcut clean is this paper's conclusion. Don't multiply the shortcut by constants, don't add gates, don't put convolutions or dropout on it; and don't follow the addition with ReLU or BN either. That way, in both forward and backward propagation, the signal can travel directly from any unit to any other.

In practice, the change is small, just moving BN and ReLU forward in the residual branch and doing nothing after the addition. If you're training a very deep ResNet, or your model has started to overfit, try the pre-activation version first; for shallower networks, the two make little difference.

The principle of “put nothing on the shortcut” came up again a few years later in Transformers; Pre-LN moves LayerNorm off the shortcut, and the warm-up stage can be dropped.