Repository navigation
fix(inference): run classifier inference under torch.no_grad - #11
Open
Zhongqi Miao (zhmiao) wants to merge 1 commit into
Open
Zhongqi Miao (zhmiao) wants to merge 1 commit into
Zhongqi Miao (zhmiao) wants to merge 1 commit into
Conversation
Single-image and batch inference in the ResNet and TIMM classifier wrappers ran without torch.no_grad(), so every forward pass kept its autograd graph. The batch path also held all logits on the GPU until the end of the loop. On large folders this grows GPU memory without bound. Wrap both inference paths in torch.no_grad() and move each batch's logits to the CPU as they are produced. Training code (src/models/plain_resnet.py) does not call these methods and is unchanged. Ported from microsoft/Biodiversity#662 by @StepFPV, which fixed the same code in the retired PytorchWildlife package. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: cf871bd7-3efa-4dad-8dd6-c61e46669a42
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Run single-image and batch inference in the ResNet and TIMM classifier wrappers under
torch.no_grad(), and move each batch's logits to the CPU as it is produced.Files:
src/models/classification/resnet_base/base_classifier.pysrc/models/classification/timm_base/base_classifier.pyWhy
Without
no_grad, every forward pass keeps its autograd graph, and the batch loop held every batch's logits on the GPU until the end. GPU memory grows with the number of images.This is the same bug fixed in the retired PytorchWildlife package by -Step- (@StepFPV) in microsoft/Biodiversity#662. PytorchWildlife is retired, so the fix is ported here, where the classifier code now lives.
Training (
src/models/plain_resnet.py) does not call these methods and is unchanged.Verification
A CPU check with a tiny stand-in network calls
single_image_classificationandbatch_image_classificationon both wrappers and readsrequires_gradon the logits passed toresults_generation:requires_grad=TrueFalserequires_grad=TrueFalserequires_grad=TrueFalserequires_grad=TrueFalseGPU check on the real
PlainResNetInference(ResNet-50, 5 classes, random weights, torch 2.11.0+cu128, RTX 6000 Ada).batch_image_classificationover 256 random 224x224 JPEGs, default batch size 32, peak memory fromtorch.cuda.max_memory_allocated():requires_grad(single / batch)True/TrueFalse/FalseOn main, peak memory grows with the number of images, because each batch keeps its autograd graph until
torch.cat. Not run with released weights; the change does not touch weight loading orresults_generation.