added image sort tool to faceswap
I added image sort tool to faceswap, which very useful to extract one face from various faces
Example original aligned folder:

Sort it by similarity:
python.exe faceswap\sorttool.py -i %WORKSPACE%\data_src\aligned -by similarity
result:

easy delete faces which you dont need:

Sort by blur:
python.exe faceswap\sorttool.py -i %WORKSPACE%\data_src\aligned -by blur
most sharp 00000.png:

most blurred 00140.png:

If I understood correctly, it sorts them, then orders them by renaming them?
yes
Ideally, a tool for making a face set would make a folder for each "type" of face and keep a single one in the original directory. Up to you afterward to move some interesting faces back to the training folder if you wish to use them.
When extracting a video, event at 1fps, you will end up with a few hundreds frames with some of them nearly identical, whereas the training process would ultimately not need them and just waste time learning them "again".
My B face folder is neatly prepared, with different poses and lighting. But the A folder is very tedious to prune each time I want to use a different target video.
But thanks for the work. It's a step in the right direction to make it easier to have good face sets.
Works great, really does, thought it was broken it was so fast. -by blur, chuck out the trash, then -by similarity to get rid of obvious dupes. Epic time saver.
Kind of like your lossA/lossB balancing, sorting and getting rid of very similar faces probably saves time so your model isn't training the same angle to perfection, while other poses are only sampled rarely.
Basically, the ultimate version would be to bin faces by similarity (which takes into account the pose primarily, I think), calculate the loss on each subset, and then optimize by training on the worst subsets.
A simple example would be you notice that your side profiles are terrible while your full front face is very good. Out of 10,000 images, you probably have 5 side profiles and 5000 full front faces. Those side profiles are sampled rarely, so you would prioritize training the side profiles after the front view is good.
Some of us already do this at an unsophisticated level manually with data curation, but it could be done more systematically and automatically.
The pose bins would be based on the target replacement video. i.e. if there are no side poses in video A, there's no point in optimizing it.
@deepfakesclub cannot understand what you mean. So you agree or disagree with optional functions loss_balancing and sorting faces ?
@iperov can I ask for feature request lol? -by cull, its sort of like sort -by blur, but deletes all pics that fall below a set threshold for blur, its hard to make decision of whats blurry or not when dealing with multiple sets. Id rather know Ive culled to the same standard of pics mathematically, from each different set rather than what my mind thinks is blurry.
i dont need this feature for myself, but you can implement it and create new PR after my PR will be merged
Ill try lol, I have no idea what Im doing, I need to maybe get the blur score and do something with it hehe.
hi iperov, would be nice to have a possibility to remove or move all visually similar images, including blurred ones without renaming the rest.
@iperov Your loss balancing and sorting are both good.
Sorry, I was just speculating on a different kind of extreme sorting. Your loss balancing got me thinking about what other optimizations are possible.
Hi,
I've extended the sorttool to now be able to sort images into folders, both by blur and similarity. It uses the same similarity and blur sorting approaches as the the current version. I have also added the option of specifying an output directory that defaults to the input directory if none is given (which preserves the original functionality of the renaming approach).
The folder grouping by similarity takes an optional float 'minimum score threshold' parameter, which acts as the minimum similarity score for an image to be considered part of the group (right now the default is 0.5, but I haven't tested it extensively).
The folder grouping by blur takes an optional integer 'bins' to sort the images based on how blurry they are into bins. If the number of images doesn't fit equally among the bins, the exess get added to the last folder (which is the bluriest).
@kcimit and @i5L4NDOF5T4BiLiTY when sorted into folders you could just delete the unecessary folder(s).
Here is a gist : https://gist.github.com/AbysmalBiscuit/03f34da39a6cf96d84312d9d309d9c8d
If it's good enough, can I receive a pull request? (note: the code styling is for PEP8 and not the repo styling, I will reformat it to match the repo style)
@AbysmalBiscuit already told you , your version is outdated.
@iperov my bad, didn't see.
also cannot understand why need work with additional folders
The main advantages as I see it are:
-
It's more convenient in terms of having clear separations between how the images are grouped together. Especially when working with very large datasets like when you extract faces from several minutes of video.
-
It would also allow you to preserve the original filenames which would make it possible to use them for the aligned folder when converting.
-
It would also make it easier to delete the images that you don't want (since you can look inside the folder and see right away if it's not the face/angle/bluriness you want), which was something people in the thread were asking for.
The usual process, at least for me, is usually the following:
1 - Have neat B facesets ready. Sorted, various poses, etc... 2 - Extract all frames from target video 3 - Extract all the faces from all frames, in order to have an aligments.json ready 4 - Prune from the extracted faces the "false positives" (wasn't a face, wasn't the desired face, was a too small face...) 5 - Start training with all the remaining faces in A, because sorting them and creating manually a good training set is a PITA. 6 - Once the loss and preview look ok, merge a small framerange to see how it looks 7 - ???? 8 - Profit
Now, point 5 is what @iperov attempted to address with his tool, which is a huge improvement, because the trainer wastes a lot of time learning very similar faces. I think that's one of the problems the IAE model has too. The GAN model seems to be affected also.
Except that, by renaming the files, it defeats the functionality that allowed to not merge the faces deleted from the aligned directory, but present in the alignments file.
So, putting similar faces in different folders would allow to quickly build a set for training by choosing various poses/faces, while allowing to build also an aligned directory ready for conversion (just have to prune the false positives and wrong faces).
Except that, by renaming the files, it defeats the functionality that allowed to not merge the faces deleted from the aligned directory, but present in the alignments file.
I dont use sorttool for dest data. Only for source data, where alignments absolutely unused.
My work process is more or less the same as @Kirin-kun.
@iperov I think there has been a misundurstanding about what each person needs the image sorting for. What I need it for is sorting the extracted 'faces', be it for training data or conversion data. If I'm not mistaken the people in this thread who want to be able to sort the faces into folders are in the same use case as I am.
Here's a more detailed example to demonstrate my use case: Let's say I extract 2000 'faces' (this includes false positives) from a 5 min video, the renaming approach wouldn't help me much since I'd still have to scroll through everything looking for when the groups change/unwanted images appear. However, if you sort the images into folders, and if the threshold used to determine similarity is discriminating enough, you will have folders that you can delete (if it's a folder of non-faces), or just pick a few images out of to get the lighting conditions/face angle/other criteria that you're looking for.
@Kirin-kun I have updated my gist to have the latest functionality from the repo, with the addition of my folder sorting: https://gist.github.com/AbysmalBiscuit/03f34da39a6cf96d84312d9d309d9c8d Could you try downloading and testing it to see how it works for you? The 'face' method also uses the threshold parameter. With the face method, folder 0 is always the non-face folder. Also it may not display a progress bar for everything if you're grouping into folders. Also backup your data before you test it just in case. :p
@iperov why not both?