davidsandberg / facenet

Face recognition using Tensorflow
MIT License
13.85k stars 4.81k forks source link

[Suggestion] Incremental training for Class svm With facenet #793

Closed mohammed-Emad closed 6 years ago

mohammed-Emad commented 6 years ago

A suggestion to avoid training on a full dataset each time we add a new person!

While building a project based on facenet We had a problem adding new people after the full training For this reason, he suggested saving the extracted features and the class names and lables as they are without any change

And exploit them again during the training of new persons We will shorten the training time for new images only

See how variables look

class_name = ['Hossam','biden' ,'rose_leslie' ,'kit_harington']

paths = ['data_algin/Hossam/Hossam-0.jpg', 'data_algin/biden/biden-0.jpg', 'data_algin/biden/biden-1.jpg', 'data_algin/rose_leslie/rose_leslie-0.jpg', 'data_algin/rose_leslie/rose_leslie-1.jpg', 'data_algin/rose_leslie/rose_leslie-2.jpg', 'data_algin/rose_leslie/rose_leslie-3.jpg', 'data_algin/rose_leslie/rose_leslie-4.jpg', 'data_algin/kit_harington/kit_harington-0.jpg', 'data_algin/kit_harington/kit_harington-1.jpg']

labels = [0, 1, 1, 2, 2, 2, 2, 2, 3, 3, 4, 4]
emb_array = [[ 0.07976267 0.09515638 0.05864169 ... -0.05250152 -0.04106444 -0.04610822]]

You can take a look at the SVM model

SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape='ovr', degree=3, gamma='auto', kernel='linear',
  max_iter=-1, probability=True, random_state=None, shrinking=True,
  tol=0.001, verbose=False)

Nothing new can be added to it

The suggestion is to save the output of the pre-conversion training to the SVM model save :class_name save :labels save :emb_array

with open(features_filename, 'wb') as outfile:
         pickle.dump((emb_array ,labels ,class_name), outfile, pickle.HIGHEST_PROTOCOL)

In one file and then use it at any time to add new people to it, or build a svm model On the items in it   The training takes a very long time and also according to our persons base That's why you do not have to start Full training again when you want to add one person or several new persons.

Only two functions have been added to the file clasifiy.py

Incremental_training()
Remove_duplicate_names()

The full subject is here to add_faces_to_svm_class

Add two parameters to the operating commands

--training_type incremental
--features_filename ~/my_model/all_features.pkl 

To become so

!python my_clasifiy.py \
TRAIN  \
--training_type incremental \
~/datasets/lfw/lfw_mtcnnpy_160 \
~/datasets/20180402-114759/20180402-114759.pb  \
~/my_model/lfw_classifier.pkl \
--features_filename ~/my_model/all_features.pkl \
--batch_size 1000 \
--min_nrof_images_per_class 40 \
--nrof_train_images_per_class 35 \
--use_split_dataset

Number of classes: 19 Number of images: 665 Loading feature extraction model Model filename: /content/datasets/20180402-114759/20180402-114759.pb Calculating features for images Training classifier not found file!:/content/my_model/all_features.pkl and start not Incremental_training Saved a features as model to file "/content/my_model/all_features.pkl" Saved classifier model to file "/content/my_model/lfw_classifier.pkl"

see notebook add_clasifir_class.ipynb Any remarks I wait for thank you

harshpal-singh commented 6 years ago

Hi Emad, Can you pls share the file clasifiy.py. I believe the purpose of this file is to add new faces to the pre-trained model to be recognised.

mohammed-Emad commented 6 years ago

Hi The file you mentioned is in facenet files as you know. classifier.py But I very sorry, but I did not have the right to act on it because it is not mine. I made a mistake when I shared these amendments without referring to the owner. I no longer had a means of communication with the owner of those additions, I do not know anything about his approval or not. But he remains the only person who has the right to participate.

- What I can say is that it's about saving features and labels in the pre-SVM class. emb_array, labels class_names or Save emb_array, labels,class_names try:

        with open('your feat file.pkl', 'wb') as file:
                    pickle.dump((emb_array, labels,class_names), file)

As raw data can be overwritten, as stated in the proposal. I am sorry again and I also apologize to the owner of that addition, Thank you all.

sourangshupal commented 5 years ago

https://github.com/mohammed-Emad/add_faces_to_svm_class ...this link is not working...Need some help on how to stop training the model again and again and use incremental learning...Because I want to add and train in real-time.

mohammed-Emad commented 5 years ago

@sourangshupal @harshpal-singh I'm sorry I was late in restoring the files and description, I didn't meet with the owner of the files so I rebuilt them a second time

Now available for review https://github.com/mohammed-Emad/add_faces_to_svm_class Good luck