Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Support empty tensor in MMSyncBN #1205

Merged
merged 10 commits into from
Sep 23, 2021

Conversation

ZwwWayne
Copy link
Collaborator

@ZwwWayne ZwwWayne commented Jul 18, 2021

Thanks for your contribution and we appreciate it a lot. The following instructions would make your pull request more healthy and more easily get feedback. If you do not understand some items, don't worry, just make the pull request and seek help from maintainers.

Motivation

In detection models trained by strong augmentation, the RoIHead may be trained with empty proposals if the objects are all contained in the cropped images, this will cause failure because the current MMSyncBN does not support training with empty tensor.

Related discussion/implementations:

  1. Empty batch support for SyncBatchNorm pytorch/pytorch#36530
  2. NaiveSyncBN in Detectron2

Modification

  1. Support to skip the CUDA calculation and leave the mean/var/norm/std/ as zero if the input tensor is empty in both the forward and backward processes. It also synchronizes a batch_flag to indicate the global batch size because when the global batch is zero, we need to make the momentum to be zero to skip the update of running_mean/running_var.
  2. Support a new mode, named stats_mode to produce more accurate batch statistics when empty tensor might be given.
  3. Use flatten rather than view(-1, ...) when reshaping the tensor which better handles empty tensor.
  4. The creation method of empty tensors is also changed from empty/empty_like to zeros/zeros_like, because the torch.empty() returns uninitialized values of tensor, which might contain unexpected values other than zeros. See related discussion in stackoverflow.

BC-breaking (Optional)

The modification does not introduce changes that break the backward compatibility of the downstream repos?
It support the new strong baseline.

Use cases (Optional)

Inference with empty tensor is supported, see the added unit tests.

Checklist

  1. Pre-commit or other linting tools are used to fix the potential lint issues.
  2. The modification is covered by complete unit tests.
  3. If the modification has potential influence on downstream projects, this PR will be tested with MMDetection in Support large scale jitter mmdetection#5537
  4. The documentation has been modified accordingly, like docstring or example tutorials.

@codecov
Copy link

codecov bot commented Jul 18, 2021

Codecov Report

Merging #1205 (96d4ecd) into master (1216e5f) will decrease coverage by 0.19%.
The diff coverage is 0.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1205      +/-   ##
==========================================
- Coverage   69.14%   68.94%   -0.20%     
==========================================
  Files         162      162              
  Lines       10746    10776      +30     
  Branches     1978     1988      +10     
==========================================
  Hits         7430     7430              
- Misses       2927     2957      +30     
  Partials      389      389              
Flag Coverage Δ
unittests 68.94% <0.00%> (-0.20%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
mmcv/ops/sync_bn.py 15.49% <0.00%> (-4.15%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1216e5f...96d4ecd. Read the comment docs.

@ZwwWayne ZwwWayne requested a review from grimoire July 22, 2021 07:18
@ZwwWayne
Copy link
Collaborator Author

Please @nbei to check again as a new argument is just added and invite @grimoire to have a look.

@zhouzaida zhouzaida mentioned this pull request Jul 27, 2021
18 tasks
@zhouzaida zhouzaida mentioned this pull request Aug 13, 2021
13 tasks
@zhouzaida zhouzaida mentioned this pull request Sep 13, 2021
16 tasks
@ZwwWayne
Copy link
Collaborator Author

ZwwWayne commented Sep 19, 2021

The performance in MMDetection has been verified through open-mmlab/mmdetection#6132

@ZwwWayne ZwwWayne merged commit 4e101e0 into open-mmlab:master Sep 23, 2021
@ZwwWayne
Copy link
Collaborator Author

Merged as the accuracy of the final commit should be guaranteed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants