When the FeatureExtractor is initialized in bdpy.recon.torch.icnn.reconstruct, it registers forward hooks on the encoder. However, the registered forward hooks are not erased after the reconstruct is returned and feature_extractor is destroyed. This leads to a problem that the remaining forward hooks keep accumulating features and occupy memory when the same encoder instance is used for reconstruct multiple times.
Here's a snippet that illustrates the behavior of FeatureExtractor
Issue
When the FeatureExtractor is initialized in
bdpy.recon.torch.icnn.reconstruct,
it registers forward hooks on theencoder
. However, the registered forward hooks are not erased after thereconstruct
is returned andfeature_extractor
is destroyed. This leads to a problem that the remaining forward hooks keep accumulating features and occupy memory when the same encoder instance is used forreconstruct
multiple times.Here's a snippet that illustrates the behavior of
FeatureExtractor
Yes, the example in the cookbook avoids this problem by initializing
encoder
for each image, but this is not effective when using larger models.Suggestion
One possible fix is to add a destructor method that clears forward hooks from the layers: