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
11.01k stars 1.93k forks source link

Facial recognition takes more time after the pickle file is created as it adds more representations #1160

Closed s-rac53 closed 3 months ago

s-rac53 commented 3 months ago

Hi Serengil,

In the below image as you can see there are 64 representations created for the database of 12 images on which the find function is run against. The issue happens when I try to run the find function again because this time it runs over 64 representations and takes an exponential time to execute. The facial recognition results are flawless but the idea of using a pickle file here is being counter productive and time consuming. Please point out the mistake I'm making/ lapse in my understanding. deepface

serengil commented 3 months ago

The idea behind pickle is to have less time once you found the embeddings of your facial database. Results seem normal.

s-rac53 commented 3 months ago

Can you see below that it's taking more time to execute when I run it again. Can you point out what's happening? It took me 4 minutes to execute the first time as opposed to 18 minutes and continuing. deepface1

serengil commented 3 months ago

Cannot see how long does find function take in 1st and 2nd run in the screenshot.

If you have gpu, you may try to disable it

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

or

os.environ['CUDA_VISIBLE_DEVICES'] = ""
s-rac53 commented 3 months ago

looking at the below screenshots and the highlighted areas it is clear that the representations are piling up every time the find function runs. The pickle file is growing tremendously. I did not have this issue last month when I ran this same database location with the same number of files. I'm running Deepface in Google Colab. P.S. I have not added extra images to the database location. I have tried your suggestion to disable GPU but to avail no joy.

WhatsApp Image 2024-04-01 at 22 03 43

WhatsApp Image 2024-04-01 at 22 04 31

serengil commented 3 months ago

There were 64 items in first screenshot, 836 in second one. Size of your db is growing. This is the problem.

s-rac53 commented 3 months ago

The number of images in the database are the same nevertheless. Do you have any suggestions or turnarounds?

serengil commented 3 months ago

I need to reproduce the problem to make a comment.

s-rac53 commented 3 months ago

Okay thanks!

AndreaLanfranchi commented 3 months ago

@s-rac53 there are some things to check for trying to dissect your problem. I'm trying to provide some hints (not sure they apply to your problem but are good starting points)

  1. I noticed in the first round the log says "Finding representations 12/12" which generates 64 representations
  2. In the second round the log says "Finding representations 64/64" : this number is surprisingly equal to the number of representations found in the first round.

A question arises : is it possible you save the detected faces as images in the same dataset directory of the original images ? If it is so then the behavior is expected. I'd personally suggest to verify the number of images in the origin directory before the first round and before the second round and compare the overall number of images.

Another thing to take into account: when find starts it inspects the contents of the provided directory and all images contained in subdirectories (i.e. the scan for images is recursive). Is it possible your application saves some additional images in subdirs ?

Third hint: the logic to detect for changed images amongst rounds relies on hashing of some properties of input image files (file size, creation date, modification date). If, in the interval amongst first round and second one, any of those properties change then the hash will change as well and the file is reprocessed.

Again: I don't know if this is the problem but I hope this gives you useful hints to investigate the issue.

s-rac53 commented 3 months ago

Hi @AndreaLanfranchi, Thank you for taking your time on my issue. You hints seem useful to understand the premise of my issue. These are my findings:

  1. The number of images remain unchanged in the original directory after round 2 of execution hence ruling this speculation out.
  2. There are no sub-directories in the database location.
  3. I have hosted these images on Google Drive and access them in Colab. Not sure if Drive modifies info on the the images that I'm aware of. Some of the images have multiple faces but this should not amount to 64 different representations at the end of execution.

Hope more light could be shed on this!

AndreaLanfranchi commented 3 months ago

Hi @s-rac53, thank you for feed-back. I suggest we proceed step.by-step. Let's consider this initial pic

image

The log line saying "Finding representations [...] 12/12" shows us the app have found 12 new images since last run (or if the pickle file was not found - hence its been created from scratch). For each image a detection phase is invoked and for every detected face on each picture a digital representation is generated. The line "There are now 64 representations [,,,]" informs us the whole pictures directory contains 64 faces and each one has been represented.

Does this makes sense for you ? How many image files do you have in the directory ? The number of 64 faces is consistent with what you expect ?

s-rac53 commented 3 months ago

Hi @AndreaLanfranchi,

I executed DeepFace's find function on my computer and found no error in the number of representations being saved in the pickle file. It seems like what you speculated on point 3 in your earlier comment must be true! There is some level of modification happening to the images in the database retrieved from my Drive.

Thanks for your time again! It seems like the issue is resolved for me at the moment. Cheers :)

AndreaLanfranchi commented 3 months ago

Nice. Feel free to get back to me should you have any further trouble. Have a great day.

s-rac53 commented 3 months ago

I sure will. Thank you.