0

I work in ComputerVision in Google Colab. I have an array of video titles. in a loop, I iterate over the given array. At each iteration, I read the video using cv2 and close the window before finishing the iteration. After processing several videos, my RAM is full in google collab. What should I do to avoid overflow my RAM?

# make dataset only for one person
def make_dataset(df, type_, seq_len=6):
  uses_keypoints = [0, 11, 12, 13, 14, 15, 16, 23, 24, 25, 26, 27, 28]
  x = tf.Variable(tf.zeros(shape=(0, seq_len, 13, 3)), dtype='float32')
  y = []
  for k in tqdm(range(df.shape[0])):
    cap = cv2.VideoCapture(df.iloc[k].file_name)
    detector = poseDetector()
    keypoints = tf.Variable(tf.zeros(shape=(0, 13, 3)), dtype='float32')
    for i in range(0, int(cap.get(cv2.CAP_PROP_FRAME_COUNT)), 6):
      cap.set(1, i)
      success, img = cap.read()
      lmList = detector.findPose(img)
      if lmList:
        lmList = np.array(lmList)
        lmList = lmList[uses_keypoints]
        lmList = tf.cast(lmList, dtype='float32')
        lmList = tf.expand_dims(lmList, 0)
        keypoints = tf.concat([keypoints, lmList], axis=0) 
    cap.release()
    cv2.destroyAllWindows()
    sequences = [ keypoints[i - seq_len : i] for i in range(seq_len, keypoints.shape[0])]
    label = df.iloc[k].person_id
    for sequence in sequences:
      x = tf.concat([x, tf.expand_dims(sequence, 0)], axis=0)
      y.append(label)
  y = to_categorical(y)
  path_to_load = f'/some_path'
  save_dataset(x, y, path_to_load)
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
  • I do not know the answer to your problem but I have a few questions. 1. You close all windows using `cv2.destroyAllWindows()`, however you never open any windows in this script. So what are you closing? Is the detector opening windows? 2. Within this loop `for k in tqdm(range(df.shape[0])):` you initialize your detector on every iteration. I think you can better initialize your detector before starting the loop. This will probably not save you any ram, but will prevent you from reloading your detector every time. – Thijs Ruigrok Oct 05 '21 at 20:36
  • Have you tried printing the size of every variable every x many iterations in the loop. You can look here(https://stackoverflow.com/questions/24455615/python-how-to-display-size-of-all-variables) to see how you print the size of the variables. This should help you find the root cause of your problem. – Thijs Ruigrok Oct 05 '21 at 20:38
  • @ThijsRuigrok your advice was very helpfull!!! Realy, as you write I determine poseDetector befor the first loop and it solve my problem. I didn't think that it can solve my issue. Thank you!!!! – Макс Байдин Oct 05 '21 at 20:45
  • Cool! I did not expect that either. I will post my comment as an answer so you can mark the question as answered :). – Thijs Ruigrok Oct 05 '21 at 20:54

1 Answers1

1

Within this loop for k in tqdm(range(df.shape[0])): you initialize your detector on every iteration. I think you can better initialize your detector before starting the loop. This will prevent you from reloading your detector every time.

Thijs Ruigrok
  • 547
  • 2
  • 12