pytorch / pytorch/vision

Add batch-wise random erasing support?

Open
#3,206 14 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

module: transforms new feature
Dominant language
Python
Stars
17.9k
Forks
7.3k
Avg merge
1d 15h
Merged PRs (30d)
13

Description

🚀 Feature

Motivation

The random erasing supported in torchvision now is implemented as image-wise. I need to use it after data loader collecting batches. But the current design cannot be used for a large batch because it is too slow to preprocess on CPU.

I use a for loop to process per-image in a batch, as shown below, but it is too slow:

for batch_of_imgs, labels in my_data_loader:
    # I need apply random erasing here, not in transforms of data loader
    # This for-loop is too slow!!!
    for i, img in enumerate(batch_of_imgs):
        batch_of_imgs[i] = RandomErasing(img)
    # train model as usual
    preds = model(batch_of_imgs, labels)

Pitch

If a batch-wise random erasing operation that could accelerate such preprocessing is available, it would be very helpful.

Alternatives

I've tried 2 alternatives:

  1. Use multi-threading. This does not help due to python GIL.
  2. Use multi-processing. This saves around 50% preprocessing time per-iteration at the beginning of training but gradually becomes as slow as single-processing one. It provides around <10% improvements from the whole training procedure but requires more resources for processing management, and tricky hacks to modify training code.

Additional context

The erasing procedure should be image-wise, not batch-wise. In other words, the erased region cannot be shared among images of same batch.

cc @SsnL @VitalyFedyunin @ejguan @fmassa @vfdev-5

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating torchvision's existing RandomErasing implementation and its related tests. Define a batch-capable interface that keeps erased regions independent per image, then benchmark it against the reported per-image loop and add coverage for the batch behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
computer-vision, machine-learning
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.