AI Logic

Regularization

DropBlock

Updated:

Dropout vs. DropBlock. On a 4-channel feature map, Dropout (left) zeroes out pixels independently one by one (equivalent to DropBlock with block_size = 1). DropBlock (right) picks seeds per channel from the region where blocks stay inside the map (inside the dashed line), zeroes out the 3 × 3 region centered on each seed, and scales the remaining values by count(M) / count_ones(M). Nothing is done at inference

Background and overview

DropBlock is a regularization method that zeroes out contiguous square regions (blocks) of a convolutional feature map at once. It was presented at NeurIPS 2018 by Ghiasi et al. at Google Brain.

Dropout works well in fully connected layers, but it was known to be less effective in convolutional layers. The paper explains why as follows.

  • In convolutional feature maps, the values of neighboring pixels are spatially correlated.
  • Dropping pixels randomly one at a time still lets the same information flow to the next layer through neighboring pixels.
  • As a result, the network overfits.

DropBlock closes this loophole by dropping contiguous regions all at once. For example, if the region responding to the "head" in a dog image is erased entirely, the network learns to recognize the dog from other parts as well (the body and legs). The idea comes from Cutout, a data augmentation that erases part of the input image, and the paper describes DropBlock as "a generalization of Cutout applied to every feature map in the network."

Setting the block size block_size to 1 makes it identical to Dropout, and setting it to cover the entire feature map makes it identical to Spatial Dropout, which drops whole channels. DropBlock lets one choose the size of the dropped unit between these two extremes.

Theory (constructing the mask)

The explanation follows Algorithm 1 of the paper. DropBlock has two main settings: the block side length block_size, and the keep probability keep_prob, which has the same meaning as in Dropout. The mask is generated separately for each channel (in the paper this worked better than sharing one mask across all channels).

  1. At inference, return the feature AA as is (do nothing).
  2. During training, first decide for each pixel, with probability γ\gamma, whether it becomes a block center (seed). Seeds are chosen only from the region where the block fits inside the feature map (a square with side feat_size−block_size+1\text{feat\_size} - \text{block\_size} + 1). Mi,j∼Bernoulli(γ)M_{i,j} \sim \mathrm{Bernoulli}(\gamma)
  3. Around each chosen seed, set the mask to 0 over a square of side block_size. Blocks may overlap.
  4. Apply the mask to the feature. A←A×MA \leftarrow A \times M
  5. To keep the values from shrinking because of the dropped units, multiply by the ratio of the number of mask elements to the number of ones to normalize the magnitude. A←A×count(M)count_ones(M)A \leftarrow A \times \frac{\mathrm{count}(M)}{\mathrm{count\_ones}(M)}

Choosing γ\gamma: The paper does not set γ\gamma directly but computes it from keep_prob. One seed drops block_size2\text{block\_size}^2 pixels, and seeds can only be chosen inside the valid region, so γ\gamma is reduced with the following formula so that the fraction of dropped pixels is close to 1−keep_prob1 - \text{keep\_prob} (feat_size\text{feat\_size} is the side length of the feature map).

γ=1−keep_probblock_size2⋅feat_size2(feat_size−block_size+1)2\gamma = \frac{1 - \text{keep\_prob}}{\text{block\_size}^2} \cdot \frac{\text{feat\_size}^2}{(\text{feat\_size} - \text{block\_size} + 1)^2}

For example, with feat_size=14\text{feat\_size} = 14, block_size=7\text{block\_size} = 7, and keep_prob=0.9\text{keep\_prob} = 0.9, γ=0.149⋅19664=0.00625\gamma = \frac{0.1}{49} \cdot \frac{196}{64} = 0.00625. On average 0.4 of the 8×8=648 \times 8 = 64 eligible pixels become seeds, and each seed drops 49 pixels, so on average 19.6 pixels are dropped, about 10% of the total (196 pixels). Because blocks can overlap, this formula is an approximation.

Lower keep_prob gradually during training (Scheduled DropBlock). According to the paper, dropping with a small keep_prob from the start hurts the early stage of training and does not work well. Decreasing keep_prob linearly from 1 to the target value improved accuracy for many keep_prob values and made the results less sensitive to the choice of value.

Why it works better than Dropout

Using a ResNet-50 trained on ImageNet, the paper examined whether DropBlock drops "semantic information."

  • Dropping contiguous regions removes information more effectively: When DropBlock was applied only at inference to a model trained without regularization, accuracy dropped as keep_prob decreased. Dropping contiguous regions removes semantic information (what is in the image) more effectively than dropping individual pixels.
  • Trained models become more robust: A model trained with block_size = 7 kept its accuracy whether block_size = 1 or 7 was used at inference. A model trained with block_size = 1 (equivalent to Dropout) lost accuracy quickly when 7 was used at inference.
  • The model looks at multiple parts of the image: Class activation maps (CAM), which show where the model attends in the image, revealed that the model without regularization focused on one or a few locations, while the model trained with DropBlock attended to multiple distant locations. This is likely because it learned to decide from evidence elsewhere even when one location is erased.

Experimental results

Results of ImageNet image classification (ResNet-50, validation set) from the original paper. Regularization methods are compared, and for Dropout, DropPath, and Spatial Dropout the best result over keep_prob values is shown (average of 3 runs).

MethodTop-1 accuracyTop-5 accuracy
None76.51%93.20%
Dropout (keep_prob = 0.7)76.80%93.41%
DropPath (keep_prob = 0.9)77.10%93.50%
Spatial Dropout (keep_prob = 0.9)77.41%93.74%
Cutout76.52%93.21%
Label Smoothing (0.1)77.17%93.45%
DropBlock (keep_prob = 0.9, block_size = 7)78.13%94.02%
DropBlock + Label Smoothing78.35%94.15%
  • Image classification: DropBlock improved top-1 by 1.62 points over no regularization, outperforming Dropout, Spatial Dropout, and DropPath. Combining it with Label Smoothing improved results further. It also outperformed the strong data augmentation AutoAugment (77.63%). On another architecture, it raised the top-1 of AmoebaNet-B from 82.25% to 82.52%.
  • Object detection: When training RetinaNet (ResNet-50 FPN) on COCO from random initialization, average precision (AP) rose from 36.8 to 38.4 (block_size = 5). This also exceeds fine-tuning a model pretrained on ImageNet (36.5).
  • Semantic segmentation: On PASCAL VOC 2012, mIOU when training from random initialization rose from 47.2 to 53.4 (block_size = 16).
  • Block size: For ResNet-50, applying block_size = 7 to both the 3rd and 4th groups (the later, lower-resolution layers) worked best, and sizes larger than block_size = 1 (equivalent to Dropout) generally gave better results.

Practical considerations

Do not keep keep_prob fixed. In the paper, using a small keep_prob from the start of training hurt the early phase and did not work well. Decrease keep_prob linearly with training progress from 1 to the target value (searched between 0.75 and 0.95 in the paper, mostly 0.9). torchvision's DropBlock2d has no such schedule, so update p yourself as in the code example.

  • p in PyTorch (torchvision) is the drop probability: The p in torchvision.ops.DropBlock2d(p, block_size) is the probability of dropping an element, corresponding to 1−keep_prob1 - \text{keep\_prob} in the paper. block_size must be odd, and if it is larger than the feature map it is clipped to the feature map size.
  • Apply it to the later, lower-resolution layers: For ResNet-50, the paper applied it to the 3rd and 4th groups (feature maps of 14 × 14 and 7 × 7). Applying it not only to the convolution branch but also to the residual (skip) connection improved accuracy further.
  • Choose block_size relative to the feature map size: The paper used the same block_size in every layer. If block_size equals the side of the feature map, it behaves like Spatial Dropout, dropping whole channels.
  • Consider training length as well: In the paper's ResNet-50 experiments, the number of training epochs was increased from the usual 90 to 270. Because the unregularized model overfits under longer training, the best validation accuracy during training was compared.
  • Do nothing at inference: Since the remaining values are scaled up during training, features are used as is at inference. After calling model.eval(), torchvision's DropBlock2d does nothing.

Comparison with other methods

Dropout and three of its variants, compared from the same perspectives.

MethodWhat is droppedNumber of keep/drop decisions per sampleTypical layersAt inference
DropoutUnits (output elements), one at a timeNumber of unitsFully connected layersUse all, scale weights by pp
DropConnectWeights (connections), one at a timeNumber of weights (inputs × outputs)Fully connected layersApproximate with a Gaussian and average samples
Spatial DropoutWhole feature-map channelsNumber of channelsConvolutional layersUse all, scale by pp (same as Dropout)
DropBlockSquare regions of the feature mapNumber of pixels (pick seeds, expand to blocks)Convolutional layersDo nothing (magnitude normalized during training)

(In every case pp is the keep probability. PyTorch's Dropout-family layers are implemented by scaling by 1/(1−pdrop)1/(1-p_{\text{drop}}) during training and doing nothing at inference.)

  • To maximize accuracy in a convolutional network, choose DropBlock. It does, however, require tuning block_size, keep_prob, and the schedule.
  • To minimize tuning, Spatial Dropout (nn.Dropout2d), built into PyTorch, is the easy option.
  • For residual networks, Stochastic Depth (DropPath) randomly skips entire blocks. In the paper's ResNet-50 experiments, DropBlock achieved higher accuracy.

PyTorch example

Show code
python
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision.ops import DropBlock2d

# DropBlock in torchvision. p is the probability of dropping an element (1 − keep_prob in the paper); block_size must be odd
drop_block = DropBlock2d(p=0.1, block_size=7)
x = torch.randn(2, 64, 14, 14)
drop_block.train()
y = drop_block(x)      # Zero out 7 × 7 blocks and scale up the rest to preserve the mean
drop_block.eval()
y = drop_block(x)      # Does nothing at inference (same as x)


# A minimal implementation following Algorithm 1 of the paper
def drop_block_2d(A, keep_prob, block_size, training=True):
    if not training or keep_prob == 1.0:
        return A                                            # 1. Return as is at inference
    n, c, h, w = A.shape
    # Eq. (1): choose the seed probability γ so the drop rate is close to 1 − keep_prob
    gamma = (1 - keep_prob) / block_size ** 2 * (h * w) / ((h - block_size + 1) * (w - block_size + 1))
    # 2. Pick seeds per channel only where the block fits inside the map
    seeds = torch.bernoulli(torch.full((n, c, h - block_size + 1, w - block_size + 1), gamma, device=A.device))
    seeds = F.pad(seeds, [block_size // 2] * 4)             # Pad back to the original size h × w
    # 3. Expand each seed to a block_size × block_size square (via max pooling)
    dropped = F.max_pool2d(seeds, kernel_size=block_size, stride=1, padding=block_size // 2)
    M = 1 - dropped                                         # 1 = keep, 0 = drop
    # 4–5. Apply the mask and scale by count(M) / count_ones(M)
    return A * M * M.numel() / M.sum().clamp(min=1.0)


# Scheduled DropBlock: decrease keep_prob linearly from 1 to the target (torchvision's p rises from 0)
target_keep_prob = 0.9
total_steps = 1000
for step in range(total_steps):
    keep_prob = 1.0 - (1.0 - target_keep_prob) * step / (total_steps - 1)
    drop_block.p = 1.0 - keep_prob
    # ... train one step here with model.train() ...

FAQ

Q. How should block_size and keep_prob be chosen?
A.

For ResNet-50 (ImageNet) in the paper, block_size = 7 and keep_prob = 0.9 was the best combination. Search keep_prob in the range 0.75–0.95. Choose block_size based on the feature map size of the layer where it is used. Setting it equal to the side of the feature map drops whole channels and can be too strong. In the paper's object detection experiments (RetinaNet), block_size = 5 gave the best result.

Q. What is the difference between DropBlock and Cutout?
A.

Cutout is a data augmentation that erases a square region of the input image, while DropBlock is a regularization method that erases square regions of feature maps inside the network. The DropBlock paper describes it as a generalization of Cutout applied to every feature map. In the same paper's ImageNet experiments, Cutout barely improved accuracy (76.51% → 76.52%), while DropBlock raised it to 78.13%.

Q. How do I use DropBlock in PyTorch?
A.

Use torchvision.ops.DropBlock2d from torchvision. The argument p is the probability of dropping an element (1 − keep_prob in the paper), and block_size is the side of the block, which must be odd. There is no built-in way to gradually increase the drop rate as in the paper's Scheduled DropBlock, so update p from 0 to the target value inside the training loop.

References

  1. G. Ghiasi, T.-Y. Lin, and Q. V. Le, "DropBlock: A regularization method for convolutional networks," NeurIPS, 2018.
  2. J. Tompson, R. Goroshin, A. Jain, Y. LeCun, and C. Bregler, "Efficient Object Localization Using Convolutional Networks," CVPR, 2015.
  3. N. Srivastava, G. Hinton, A. Krizhevsky, I. Sutskever, and R. Salakhutdinov, "Dropout: A Simple Way to Prevent Neural Networks from Overfitting," Journal of Machine Learning Research, 15(56), 1929–1958, 2014.
  4. torchvision Documentation, "torchvision.ops.DropBlock2d."