jacobgil / pytorch-grad-cam

Advanced AI Explainability for computer vision. Support for CNNs, Vision Transformers, Classification, Object detection, Segmentation, Image similarity and more.
https://jacobgil.github.io/pytorch-gradcam-book
MIT License
9.79k stars 1.52k forks source link

Memory leakage during multiple loss calculations #483

Open IzumiKDl opened 4 months ago

IzumiKDl commented 4 months ago

Thank you for publishing this software package, it is very useful for the community. However, when I perform multiple loss calculations, the memory will continue to grow and will not be released in the end, resulting in the memory continuously increasing during the training process. When I restart a CAM object in a loop, even if the object is deleted at the end of the loop, memory accumulates. Have you ever thought about why this situation happened and how can I avoid it? image

jacobgil commented 4 months ago

Hi, We support a "with" clause that should solve this. Can you please try, for example with:

    with GradCAM(model=model,
                       target_layers=target_layers) as cam: