SAM推理时总是提示PYTORCH_CUDA_ALLOC_CONF相关错误

发布时间 2023-07-24 16:30:53作者: 海_纳百川

相关错误如下:

RuntimeError: CUDA out of memory. Tried to allocate 6.18 GiB (GPU 0; 24.00 GiB total capacity; 11.39 GiB already allocated; 3.43 GiB free; 17.62 GiB reserved in total by PyTorch) If reserved memory is >> allocated memory try setting max_split_size_mb to avoid fragmentation. See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF

刚开始遇到这个问题,去百度搜了下,很多都是设置环境变量PYTORCH_CUDA_ALLOC_CONF=max_split_size_mb:32,但是这个方案对于我的问题没有用,后又去了sam的官方github项目的issue板块,发现在设置推理参数时,将 points_per_batch设置为2即可,即SamAutomaticMaskGenerator(sam, points_per_batch=2).

经过测试,基本当图片尺寸过大时,才会出现如上报错,也就是说图片尺寸过大,采样点变多,显存就报错了。

基于这种问题,建议可以根据图片尺寸动态调整points_per_batch