title: How to reduce memory usage?
body: <p>I am trying to generate pickle file of the predictions on my dataset. But after executing the code for 6 hours PC is going out of memory again and again. I wonder if anyone can help me with this?</p>
  
  <pre><code>from keras.models import load_model
  import sys
  sys.setrecursionlimit(10000)
  
  import pickle
  import os
  import cv2
  import glob
  
  dirlist = []
  
  imgdirs = os.listdir('/chars/')
  imgdirs.sort(key=float)
  for imgdir in imgdirs:
      imglist = []
      for imgfile in glob.glob(os.path.join('/chars/', imgdir, '*.png')):
          img = cv2.imread(imgfile)
          img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
          model = load_model('mymodel.h5')
          predictions=model.predict(img)
          print('predicted model:', predictions)
          imglist.append(predictions)
  
        dirlist.append(imglist)
  q = open("predict.pkl","wb")
  pickle.dump(dirlist,q)
  q.close()
  </code></pre>