serengil / deepface

A Lightweight Face Recognition and Facial Attribute Analysis (Age, Gender, Emotion and Race) Library for Python
https://www.youtube.com/watch?v=WnUVYQP4h44&list=PLsS_1RYmYQQFdWqxQggXHynP1rqaYXv_E&index=1
MIT License
14.34k stars 2.2k forks source link

GPU memory and batch size #371

Closed gambit4348 closed 2 years ago

gambit4348 commented 2 years ago

Hello, I'm experimenting DeepFace for emotion recognition, but when I try to analyze an image I get the following exception:

ResourceExhaustedError: failed to allocate memory [Op:AddV2]

Many posts on TensorFlow state this problem is related to the batchsize used during model training and recommend reducing it. However, how to do it? The code is as follows:

import cv2
import matplotlib.pyplot as plt
from deepface import DeepFace
boy_happy = cv2.imread('image/boy_happy_1.jpg')
plt.imshow(cv2.cvtColor(boy_happy, cv2.COLOR_BGR2RGB))
predictions = DeepFace.analyze(boy_happy)

The file is a 64Kb version of the following: https://www.dreamstime.com/stock-images-portrait-happy-boy-showing-thumbs-up-gesture-isolated-over-white-background-image34772834.

Thanks in advance!

serengil commented 2 years ago

your gpu memory seems not be enough. try disable it.

impost os
os.setenv['CUDA_VISIBLE_DEVICES'] = ""
#or os.setenv['CUDA_VISIBLE_DEVICES'] = "-1"
gambit4348 commented 2 years ago

Thank you! Indeed, my GPU has only 4Gb. Following your suggestion, in the very beggining of my Jupyter notebook I wrote:

import os
os.environ['CUDA_VISIBLE_DEVICES'] = ""
import cv2
import matplotlib.pyplot as plt
from deepface import DeepFace

However, the result was precisely the same. Any other idea?

Thanks in advance!

gambit4348 commented 2 years ago

It works if we do like this:

import os
os.environ['CUDA_VISIBLE_DEVICES'] = "-1"