Open nku-zhichengzhang opened 2 months ago
The memory is not released after processing single images either.
The inference_state
object is used to cache tracking data within add_new_points_or_box()
and propagate_in_video()
. It looks like the code snippet in the OP deletes the predictor, but does not perform any cleanup for inference_state
after it is used.
reset_state(inference_state)
should clear tracking data from the object which should release some GPU memory. The object will still contain frame data that was loaded during the call to init_state()
though so if you want to release the associated GPU memory for that too you could call del inference_state
. And in both cases, the following would release the GPU memory after.
import gc
gc.collect()
torch.cuda.empty_cache()
I believe that building the predictor in every loop creates the issue, as in the build_sam2_video_predictor
it sends the model to the device, for me, it helped to create a global model instead of doing it in every iteration loop, however using the empty_cache
and gc.collect()
methods did not
Description
When running SAM2 (Segment Anything Model 2) for multiple videos sequentially, I've noticed that the GPU memory remains occupied even after processing a video. This prevents the efficient use of GPU resources for subsequent video processing tasks, despite attempts to clear the memory.
Steps to Reproduce
Code Snippet
Expected Behavior
The GPU memory should be largely released after processing each video, allowing for efficient processing of subsequent videos without memory accumulation.
Actual Behavior
Despite using
del predictor
andtorch.cuda.empty_cache()
, the GPU memory is not fully released between video processing tasks. This leads to accumulating memory usage and potential out-of-memory errors for long-running processes or large datasets.Request for Assistance
I would appreciate any insights into why the GPU memory is not being fully released and how to resolve this issue. Are there any known memory management best practices for SAM2 or similar deep learning models that I should be implementing?