Add generator function for face_enhancer to save RAM #303
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR implements a generator function version of face_enhancer to save RAM by allowing for enhancing and saving images one at a time instead of storing them all in memory. This works because
imageio.mimsave
can work with any iterable as long as the iterable has a__len__
method. The originalenhancer
function works as before for backwards compatibility with the other places it is used in the codebase. Ideally other parts of the codebase would be improved with generator functions as well, since the amount of RAM saved means that SadTalker will be able to work with longer videos and run much faster; this PR is just a first step.Some background on why this was implemented:
The linux kernel was terminating the process when running the demo notebook on a 2.5min audio file with a 800x1199px image. I found that this was because >25GB of RAM were being allocated during the face_enhancer function call because of all of the images being stored in one list (unencoded videos are huge). By using the generator instead, less than 0.5GB of RAM were needed for face_enhancer and the notebook finished successfully.