AI Logic

Architectures

RegNet

Updated๏ผš

Architecture of RegNet. The left side shows the overall flow of RegNetX-3.2GF (a 3ร—3 convolution stem, four stages with 2, 6, 15, and 2 blocks, global average pooling, and a fully connected layer). The upper right shows a graph and equations for the rule that sets per-block widths with a line, rounds them into steps, and groups consecutive blocks with the same width into a stage. The lower right shows the X block (1ร—1 convolution, 3ร—3 group convolution, 1ร—1 convolution, and a shortcut) and the Y block, which adds SE.

Background and overview

RegNet is a family of networks built by narrowing down the design space (the set of candidate models) itself so that it contains many good networks, and then using the resulting rule that "the width and depth of each stage are determined by a single quantized line". It was proposed in 2020 by Radosavovic, Girshick, He, Dollรกr, and colleagues at Facebook AI Research in the paper "Designing Network Design Spaces" and presented at CVPR 2020.

Until then, networks were built in two main ways.

  • Manual design: as with VGG and ResNet, principles such as "go deeper" or "use residual connections" are found through trial and error. The principles transfer to other settings, but as the number of choices grows, people can no longer explore them by hand.
  • Neural architecture search (NAS): as with NASNet and EfficientNet, the single best model is found automatically within a fixed search space. This yields good models, but the result is one model tuned to specific conditions, and it does not reveal principles explaining why it is good.

The RegNet paper takes a middle path between the two: people progressively simplify the design space while comparing populations of models rather than single models. The paper calls this "designing design spaces".

The main results of the paper are as follows.

  • It reduced the degrees of freedom of the design space from 16 to 6, cutting the number of candidates by roughly 10 orders of magnitude while increasing the fraction of good models.
  • Under the same training settings, RegNetX outperformed ResNet and ResNeXt at every size, whether matched on computation or on activations.
  • Under the same training settings, RegNetY outperformed EfficientNet at medium and larger computation budgets and ran up to 5ร— faster on GPUs.

Theory: progressively narrowing the design space

The paper simplified the design space by repeatedly sampling and training many models from a design space and comparing design spaces by their error distributions.

Measuring the quality of a design space

Sample nn models from a design space, train them, and measure their errors eie_i. The empirical distribution function (EDF) of errors is the fraction of models whose error is below ee (Eq. 1 of the paper).

F(e)=1nโˆ‘i=1n1[ei<e]F(e) = \frac{1}{n} \sum_{i=1}^{n} \mathbf{1}[e_i < e]

The closer the curve is to the upper left, the better the design space, with more models concentrated at low error. Comparing distributions is less subject to chance than comparing the single best model found by a search. For quick comparisons, 500 models of around 400 million operations (400MF) were sampled and each trained on ImageNet for only 10 epochs.

Starting point: AnyNetX

The network is divided into "stem (3ร—33 \times 3 convolution, 32 channels, stride 2) โ†’ body (four stages) โ†’ head (global average pooling and a fully connected layer)", and only the body is varied. Each stage repeats the same block, which is the ResNeXt bottleneck (the X block in the paper). For each stage ii, four values can be chosen freely: the number of blocks did_i, width wiw_i, bottleneck ratio bib_i, and group width gig_i, giving 16 degrees of freedom over four stages.

Narrowing procedure (Sections 3.2โ€“3.3 and Table 1 of the paper)

  1. Share the bottleneck ratio across all stages (AnyNetXB_B): the error distribution barely changed.
  2. Share the group width across all stages (AnyNetXC_C): again the distribution barely changed.
  3. Make widths increase in later stages (wi+1โ‰ฅwiw_{i+1} \geq w_i, AnyNetXD_D): the distribution clearly improved. Conversely, keeping widths equal or decreasing them made it much worse.
  4. Make the number of blocks increase in later stages (di+1โ‰ฅdid_{i+1} \geq d_i, AnyNetXE_E): the distribution also improved.
  5. Set width and depth with a quantized line (RegNet): the rule described next reduced the degrees of freedom to 6.
Design spaceConstraint addedDegrees of freedomNumber of candidates (approx.)
AnyNetXA_ANone161.8ร—10181.8 \times 10^{18}
AnyNetXC_CShared bb and gg103.2ร—10143.2 \times 10^{14}
AnyNetXE_EIncreasing width and depth105.5ร—10115.5 \times 10^{11}
RegNetQuantized line63.0ร—1083.0 \times 10^{8}

The quantized linear rule

Lining up the per-block widths of the top 20 models in AnyNetXE_E showed that, overall, they roughly follow a line (wj=48โ‹…(j+1)w_j = 48 \cdot (j + 1)). Actual model widths, however, form steps that are constant within each stage. So first, a line sets the width of each block (Eq. 2 of the paper). dd is the total number of blocks, w0w_0 is the initial width, and waw_a is the slope.

uj=w0+waโ‹…j(0โ‰คj<d)u_j = w_0 + w_a \cdot j \qquad (0 \leq j < d)

Next, uju_j is expressed as "w0w_0 multiplied by wmw_m some number of times", and that exponent sjs_j is rounded to an integer (Eqs. 3 and 4 of the paper).

uj=w0โ‹…wmโ€‰sj,wj=w0โ‹…wmโŒŠsjโŒ‰u_j = w_0 \cdot w_m^{\,s_j}, \qquad w_j = w_0 \cdot w_m^{\lfloor s_j \rceil}

โŒŠsjโŒ‰\lfloor s_j \rceil denotes rounding. Blocks with the same rounded width wjw_j form one stage; the width of stage ii is w0โ‹…wmโ€‰iw_0 \cdot w_m^{\,i}, and its number of blocks is the number of blocks with that width. The upper right of the figure at the top of the article shows this line and the steps.

As a result, a network is determined by six values: d,w0,wa,wmd, w_0, w_a, w_m, the bottleneck ratio bb, and the group width gg. This design space, RegNetX, has a lower mean error than AnyNetX while keeping the error of the best model. The paper also shows that trying only about 32 random models is likely to find a good one.

The official implementation further aligns the rounded widths to multiples of 8 and of the group width. For example, in RegNetX-3.2GF (d=25d = 25, w0=88w_0 = 88, wa=26.31w_a = 26.31, wm=2.25w_m = 2.25, g=48g = 48), the stage widths given by the equations, 88, 198, 446, and 1003, become 96, 192, 432, and 1008.

What the design space revealed

Examining the RegNet design space across computation budgets revealed trends that differ from designs common at the time (Section 4 of the paper).

  • The depth of good models is nearly constant regardless of computation: the best models had about 20 blocks (about 60 layers). This differs from the common practice of making larger models deeper.
  • It is better not to use a bottleneck: the best models have bottleneck ratio b=1b = 1, meaning the width is not narrowed in the middle.
  • The width multiplier between stages is about 2.5: close to the commonly used "2ร— per stage", but slightly larger.
  • Inverted bottlenecks and depthwise convolutions do not fit: the inverted bottleneck (b<1b < 1) used in MobileNetV2 and others slightly worsened the distribution, and depthwise convolution (group width g=1g = 1) made it worse still.
  • Keeping the input resolution at 224ร—224 is best: unlike EfficientNet, not increasing the resolution was better even at large computation budgets.
  • Adding SE helps: the design space RegNetY, which adds the SE module from SENet (reduction ratio 1/4) after the 3ร—33 \times 3 group convolution of the X block, had a better distribution.

Activations determine speed on GPUs

In addition to computation and parameter count, the paper examined activations (the total size of the output tensors of all convolutions). On hardware such as GPUs and TPUs, where memory reads and writes tend to determine speed, activations correlated more strongly with runtime than computation did (Figure 12 of the paper: on a V100 GPU, a correlation coefficient of 0.643 for computation versus 0.897 for activations).

In good RegNets, activations grow in proportion to the square root of computation. In EfficientNet, which scales resolution and depth together, they grow in proportion to computation. This is why RegNet is faster on GPUs at the same computation.

Overall architecture

Every RegNet model follows the same overall flow: "stem โ†’ four stages โ†’ head". The left side of the figure at the top of the article shows the flow of RegNetX-3.2GF.

  • Stem: a 3ร—33 \times 3 convolution (32 channels, stride 2).
  • Body: four stages. The first block of each stage halves the height and width with stride 2, and the rest use stride 1.
  • Head: global average pooling and a fully connected layer.

There are two kinds of blocks (Figure 4 and the appendix of the paper).

  • X block: the ResNeXt bottleneck. A 1ร—11 \times 1 convolution โ†’ a 3ร—33 \times 3 group convolution โ†’ a 1ร—11 \times 1 convolution, with BN and ReLU after each convolution, and the input added back.
  • Y block: an X block with SE added after the 3ร—33 \times 3 group convolution.

Examples of models the paper selected for each computation budget are shown below (Figures 15 and 16 of the paper). did_i is the number of blocks per stage, and wiw_i is the width per stage.

Modeldid_iwiw_iggComputationParametersActivations
RegNetX-200MF1, 1, 4, 724, 56, 152, 36880.2B2.7M2.2M
RegNetX-3.2GF2, 6, 15, 296, 192, 432, 1008483.2B15.3M11.4M
RegNetX-16GF2, 6, 13, 1256, 512, 896, 204812815.9B54.3M25.5M
RegNetY-600MF1, 3, 7, 448, 112, 256, 608160.6B6.1M4.3M
RegNetY-4.0GF2, 6, 12, 2128, 192, 512, 1088644.0B20.6M12.3M
RegNetY-32GF2, 5, 12, 1232, 696, 1392, 371223232.3B145.0M30.3M

All models use bottleneck ratio b=1b = 1. The larger the computation, the more blocks in the third stage and the fewer in the last stage, which resembles the design of ResNet. The group width gg grows with computation, but depth plateaus even in large models.

All comparison experiments in the paper use the same simple training setup (Appendix C).

ItemValue
OptimizationSGD (momentum 0.9)
Learning rate0.1 (scaled in proportion to batch size), 5-epoch warmup, cosine decay
Epochs100
Weight Decay5e-5
Other regularizationNone (no stronger data augmentation, Dropout, etc.)

Experimental results

To compare only differences in architecture, the paper retrains the comparison models with the same training setup as RegNet. Errors in the tables are top-1 errors on the ImageNet validation set, averaged over 5 training runs.

Comparison with ResNet and ResNeXt (models with similar computation; inference time for 64 images on a V100 GPU; Table 3(b) of the paper)

ModelComputationParametersActivationsInference timeTop-1 error
ResNet-504.1B22.6M11.1M53 ms23.2%
ResNeXt-504.2B25.0M14.4M78 ms21.9%
RegNetX-4.0GF4.0B22.1M12.2M69 ms21.4%
ResNet-1017.8B44.6M16.2M90 ms21.4%
ResNeXt-1018.0B44.2M21.2M137 ms20.7%
RegNetX-8.0GF8.0B39.6M14.1M94 ms20.7%

With the same component (the X block) and the same training setup, RegNetX outperformed ResNet and ResNeXt through architecture alone. RegNetX-8.0GF matches the error of ResNeXt-101 with about 1.5ร— faster inference.

Comparison with EfficientNet (both trained for the same 100 epochs; excerpt from Table 4 of the paper)

ModelComputationParametersActivationsInference timeTop-1 error
EfficientNet-B00.4B5.3M6.7M34 ms24.9%
RegNetY-400MF0.4B4.3M3.9M19 ms25.9%
EfficientNet-B31.8B12.0M23.8M114 ms22.5%
RegNetY-1.6GF1.6B11.2M8.0M39 ms22.0%
EfficientNet-B59.9B30.0M98.9M504 ms21.5%
RegNetY-8.0GF8.0B39.2M18.0M113 ms20.1%

EfficientNet was better at low computation, but RegNetY was better at medium and larger budgets. EfficientNet has many activations and is therefore slow on GPUs; for example, RegNetX-8.0GF has lower error than EfficientNet-B5 and runs about 5ร— faster.

Comparison at mobile sizes (around 600 million operations; other models use values from their original papers; excerpt from Table 2 of the paper)

ModelComputationParametersTop-1 error
MobileNetV20.59B6.9M25.3%
ShuffleNet V20.59Bโˆ’25.1%
NASNet-A0.56B5.3M26.0%
AmoebaNet-C0.57B6.4M24.3%
DARTS0.57B4.7M26.7%
RegNetX-600MF0.60B6.2M25.9%
RegNetY-600MF0.60B6.1M24.5%

The other models' values come from long training or strong data augmentation, whereas RegNet reached similar errors with 100 epochs and no regularization other than Weight Decay. On ImageNetV2, a freshly collected test set, the ranking of models barely changed (Appendix A of the paper).

Practical considerations

Keep the following points in mind when using RegNet.

  • Choose by computation and measure speed on the target hardware: RegNet model names include their computation (200MF to 32GF), which makes it easy to choose based on available compute. They run fast on GPUs thanks to their small activations, but the paper does not compare speed on, for example, smartphone CPUs.
  • The paper's errors come from a simple training setup: the numbers in the paper's tables are from 100-epoch training without regularization. In the reproduction of EfficientNet-B0, training longer and adding regularization lowered the error from 25.6% to 23.7% (Table 7 of the paper). When comparing with numbers from other papers, align the training setups.
  • Choosing between X and Y: at the same computation, RegNetY with SE had lower error. However, SE adds slightly to the parameter count and inference time.

RegNet's rule was found under the conditions used to narrow the design space (ImageNet, 224ร—224 input, four stages, X blocks). The paper confirms that the ranking of design spaces does not change when computation, number of epochs, number of stages, or block type are varied, but the same rule is not necessarily optimal for other tasks or data.

torchvision includes regnet_x_400mf through regnet_x_32gf and regnet_y_400mf through regnet_y_128gf, and can load weights trained on ImageNet.

Comparison with other models

The following compares RegNet with models whose architectures are determined in similar ways.

ModelHow the architecture is determinedMain blockHow size is scaledMain focus
ResNet, ResNeXtManual designBottleneck (group convolution in ResNeXt)Number of layers (50, 101, 152)Accuracy
EfficientNetNAS finds one model, then compound scaling enlarges itMBConv (depthwise convolution)Depth, width, and resolution togetherParameters and computation
RegNetNarrow the design space and generate from a ruleX block (Y adds SE)Choose the six values per computation budget (fixed resolution)GPU speed and error distribution
EfficientNetV2NAS targeting training timeFused-MBConv and MBConvModified compound scalingTraining speed and parameters

Guidelines for choosing:

  • For a straightforward CNN that runs fast on GPUs: RegNet is a good fit, since it avoids depthwise convolutions and has small activations.
  • To replace ResNet at similar computation: RegNetX has lower error with the same components, and RegNetY is a candidate if more accuracy is needed.
  • To minimize parameters or computation above all: at low computation, EfficientNet and the MobileNet family can have lower error.
  • To build a model tailored to your own conditions: you can try a small number of models from the range of the six values with random search. The paper shows that trying only about 32 models from the RegNetX design space is likely to find a good one.

PyTorch example

Show code
python
import math
import torch
from torchvision.models import regnet_y_1_6gf, RegNet_Y_1_6GF_Weights

"""
Use RegNet via torchvision's built-in models.
Available: regnet_x_400mf to regnet_x_32gf (X blocks) and regnet_y_400mf to regnet_y_128gf (Y blocks with SE).
"""

weights = RegNet_Y_1_6GF_Weights.IMAGENET1K_V1
model = regnet_y_1_6gf(weights=weights)
preprocess = weights.transforms()

model.eval()                               # BN uses the accumulated mean and variance
with torch.no_grad():
    x = torch.rand(1, 3, 256, 256)         # stand-in for an image (values in 0โ€“1)
    print(model(preprocess(x)).shape)      # torch.Size([1, 1000])


# Build per-stage block counts and widths with the quantized linear rule (Eqs. 2โ€“4 of the paper)
def regnet_widths(depth, w0, wa, wm, group_w, q=8):
    u = [w0 + wa * j for j in range(depth)]                       # Eq. 2: linear per-block widths
    s = [round(math.log(uj / w0) / math.log(wm)) for uj in u]     # Eq. 3: round s_j in u_j = w0 * wm^s_j
    w = [int(round(w0 * wm ** sj / q) * q) for sj in s]           # Eq. 4: rounded widths (aligned to multiples of 8 in the implementation)
    widths = sorted(set(w))
    depths = [w.count(wi) for wi in widths]                       # blocks with the same width form one stage
    groups = [min(group_w, wi) for wi in widths]
    widths = [int(round(wi / g) * g) for wi, g in zip(widths, groups)]  # align to multiples of the group width
    return depths, widths


# RegNetX-3.2GF (config from the official pycls implementation): d=25, w0=88, wa=26.31, wm=2.25, g=48
print(regnet_widths(25, 88, 26.31, 2.25, 48))   # ([2, 6, 15, 2], [96, 192, 432, 1008]) โ€ฆ matches Figure 15 of the paper

FAQ

Q. How does RegNet differ from NAS (neural architecture search)?
A.

NAS searches a fixed search space for the single best model. The RegNet paper sampled many models, compared their error distributions, and had people progressively simplify the design space itself. This yields rules that transfer to other computation budgets, such as "stage widths and depths are determined by a quantized line" and "it is better not to use a bottleneck".

Q. What is the difference between RegNetX and RegNetY?
A.

Whether the block has SE. RegNetX uses the ResNeXt bottleneck (the X block), and RegNetY adds SE (reduction ratio 1/4) after the 3ร—33 \times 3 group convolution of the X block. In the paper, RegNetY had lower error at the same computation (for example, 25.9% for X versus 24.5% for Y at 600MF).

Q. Why is RegNet faster than EfficientNet on GPUs at the same computation?
A.

Because it has fewer activations (the total size of the convolution output tensors). On GPUs, memory reads and writes tend to determine speed, and in the paper's measurements, activations correlated more strongly with runtime than computation did. EfficientNet scales resolution and depth together, so its activations grow in proportion to computation, whereas RegNet's grow only in proportion to the square root of computation.

References

  1. Radosavovic, I., Kosaraju, R. P., Girshick, R., He, K., & Dollรกr, P. (2020). Designing Network Design Spaces. CVPR 2020. arXiv:2003.13678.
  2. facebookresearch/pycls: the official implementation and configuration files for RegNet
  3. PyTorch. torchvision.models.regnet_y_1_6gf.