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

[PyTorch] replace no_grad with inference_mode #1323

Merged
merged 1 commit into from
Sep 26, 2023

Conversation

felixdittrich92
Copy link
Contributor

@felixdittrich92 felixdittrich92 commented Sep 19, 2023

This PR:

from doctr.io import DocumentFile
from doctr.models import ocr_predictor
import time

doc_path = "/home/felix/Desktop/1.jpg"
start = time.time()
doc = DocumentFile.from_images([doc_path] * 10)
model = ocr_predictor(det_arch="db_resnet50", reco_arch="parseq", pretrained=True, det_bs=8, reco_bs=512).cuda().half()
result = model(doc)
print(time.time() - start)
# with torch.no_grad():
# cuda: VRAM: ~4400MB time: 15.616s
# cuda half: VRAM: ~2400MB time: 9.3s

# with torch.inference_mode():
# cuda: VRAM: ~4246MB time: 15.612s
# cuda half: VRAM: ~2340MB time: 9.14s

@felixdittrich92 felixdittrich92 added type: enhancement Improvement module: models Related to doctr.models ext: references Related to references folder framework: pytorch Related to PyTorch backend topic: text detection Related to the task of text detection topic: text recognition Related to the task of text recognition labels Sep 19, 2023
@felixdittrich92 felixdittrich92 added this to the 0.7.1 milestone Sep 19, 2023
@felixdittrich92 felixdittrich92 linked an issue Sep 19, 2023 that may be closed by this pull request
@felixdittrich92 felixdittrich92 marked this pull request as ready for review September 19, 2023 11:55
@codecov
Copy link

codecov bot commented Sep 19, 2023

Codecov Report

Merging #1323 (6064f57) into main (8245706) will increase coverage by 0.01%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main    #1323      +/-   ##
==========================================
+ Coverage   95.75%   95.76%   +0.01%     
==========================================
  Files         154      154              
  Lines        6902     6902              
==========================================
+ Hits         6609     6610       +1     
+ Misses        293      292       -1     
Flag Coverage Δ
unittests 95.76% <100.00%> (+0.01%) ⬆️

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

Files Changed Coverage Δ
doctr/models/classification/predictor/pytorch.py 95.45% <100.00%> (ø)
doctr/models/detection/predictor/pytorch.py 95.23% <100.00%> (ø)
doctr/models/kie_predictor/pytorch.py 100.00% <100.00%> (ø)
doctr/models/predictor/pytorch.py 100.00% <100.00%> (ø)
doctr/models/recognition/predictor/pytorch.py 91.66% <100.00%> (ø)

... and 2 files with indirect coverage changes

Copy link
Collaborator

@odulcy-mindee odulcy-mindee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the modification be made for the evaluate function in references/classification/train_pytorch.py ? Same question for the others train_pytorch.py ?

@felixdittrich92
Copy link
Contributor Author

Should the modification be made for the evaluate function in references/classification/train_pytorch.py ? Same question for the others train_pytorch.py ?

No because in the training scripts we switch between train mode and eval mode this works with no_grad but if the model/pipe is once in inference mode we cannot switch back into train mode but that's fine for inference :)

Copy link
Collaborator

@odulcy-mindee odulcy-mindee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, thanks @felixdittrich92 !

@felixdittrich92 felixdittrich92 merged commit 56395ba into mindee:main Sep 26, 2023
58 checks passed
@felixdittrich92 felixdittrich92 deleted the infernce-mode branch September 26, 2023 08:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ext: references Related to references folder framework: pytorch Related to PyTorch backend module: models Related to doctr.models topic: text detection Related to the task of text detection topic: text recognition Related to the task of text recognition type: enhancement Improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use torch.inference_mode vs torch.no_grad for predictions
2 participants