'tensorflow keras runs out of memory (OOM) when predicting images, not traning models

I've been looking at many forums about this OOM error while running machine learning projects with tensorflow and keras. I know that keras.backend.clear_session() can remove graph memory and del model removes the model being trained. But none would work for me and I wonder what other options are there.

Here are some aspects of my work and why those two don't work:

  1. I am not using jupyter notebook. I use pycharm to run .py files.
  2. I am not training models. I have models stored in local already and I call them up to use model.predict on my data. So I assume del model is not the fit.
  3. I am working on an endless dataset (due to annual new data) and I use loop to go through each task. Eventually I will hit the error below, even I put clear_session at the end of the loop. This error always occurs after successfully predicting a specific number of images, no matter if I use clear_session or not. I wonder if this means I use clear_session incorrectly or clear_session can't solve the issue. Someone discussed the incapability here and I wonder if I must use CUDA.
tensorflow.python.framework.errors_impl.ResourceExhaustedError:  OOM when allocating tensor with shape[32,128,256,256] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc
     [[node concatenate_4/concat (defined at \Users\cwang\.conda\envs\aquaculture_py36\lib\site-packages\keras\backend\tensorflow_backend.py:3009) ]]
Hint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info.
 [Op:__inference_keras_scratch_graph_287801]


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source