Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimizations for deepface.recognition.find, Optimization and New Iterator Functionality in image_utils #1420

Merged
merged 11 commits into from
Jan 7, 2025

Conversation

PyWoody
Copy link
Contributor

@PyWoody PyWoody commented Jan 6, 2025

What has been done

With this PR, I have made optimizations and opinionated changes to deepface.modules.recognition as well as deepface.commons.image_utils. The optimizations include better use of sets, delayed object creation, and ahead of time creation for set objects used in tight loops.

The modifications proposed in this PR are highly opinionated, so please feel free to request any modifications or to refuse it outright.

deepface.commons.image_utils

Modified

list_images

  • Image and PIL file extensions are now stored in sets built ahead of walking the directory. This prevents new sets from needing to be created on each iteration.
  • exact_path is not created unless the file ext is already in image_exts

New

yield_images

  • New generator function
  • Mimics the same functionality as list_images but yields the exact_path instead of building and returning the list
  • Using an iterator will improve speed and reduce memory when created as a set in deepface.modules.recognition

 

deepface.modules.recognition

Modified

  • storage_images is now built using the new deepface.commons.image_utils.yield_images iterator
  • storage_images is created as a set instead of a list, as all operations on it were set operations
  • pickled_images is now delayed until if refresh_database is True'sh to prevent unnecessary object creation
  • pickled_images is now created as a set comprehension instead of a list comprehension, as all operations on it were set operations
  • new_images and old_images can now be created without having to convert pickled_images and storage_images into sets
  • df_cols is stored as a set. If column order will be necessary in the future, this should be restored to a list

How to test

The test in deepface.tests.test_find.test_filetype_for_find_bulk_embeddings checks the new deepface.commons.image_utils.yield_images for functionality and equivalence to the existing deepface.commons.image_utils.list_images function.

make lint && make test

PyWoody added 10 commits January 6, 2025 09:06
…f time rather than on each iteration. exact_path is not created unless the file's ext is a valid image ext.
…a given path. The functionality is equivalent to list_images, but, instead of building then return a list, it yields the image path at each iteration.
…age_utils.yield_images generator function. Previously, storage_images was created with deepface.commons.image_utils.list_images as a list, then converted to a set while never being used as purely a list.
… list comprehension as before. Like storage_images, all subsequent actions where set and not list actions, so it saves time re-creating the list as a set later on.
…ations on the object. If the order of the columns will need to be maintained in future versions, this should be restored to the original list.
… IMAGE_EXTS and PIL_EXTS to match Python naming conventions.
@serengil serengil merged commit 5bab888 into serengil:master Jan 7, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants