如何Fasten Knn算法进行实时人脸识别

How to Fasten Knn Algorithm for face recognition in real time

我正在做人脸检测和识别方面的工作,我想实时检测人脸,

但是到了训练点,训练

需要很长时间

数据是否有可能减少训练数据的时间任何人都可以提供帮助

我解决了这个问题

'''

def train(train_dir, model_save_path=None, n_neighbors=None, knn_algo='ball_tree', verbose=False):
    
    X = []
    y = []

    # Loop through each person in the training set
    for class_dir in tqdm(os.listdir(train_dir)):

        if not os.path.isdir(os.path.join(train_dir, class_dir)):
            continue

        # Loop through each training image for the current person
        for img_path in image_files_in_folder(os.path.join(train_dir, class_dir)):
            image = face_recognition.load_image_file(img_path)
            face_bounding_boxes = face_recognition.face_locations(image)

            if len(face_bounding_boxes) != 1:
                # If there are no people (or too many people) in a training image, skip the image.
                if verbose:
                    print("Image {} not suitable for training: {}".format(img_path, "Didn't find a face" if len(face_bounding_boxes) < 1 else "Found more than one face"))
            else:
                # Add face encoding for current image to the training set
                X.append(face_recognition.face_encodings(image, known_face_locations=face_bounding_boxes)[0])
                y.append(class_dir.split('_')[0])

    # Determine how many neighbors to use for weighting in the KNN classifier
    if n_neighbors is None:
        n_neighbors = int(round(math.sqrt(len(X))))
        if verbose:
            print("Chose n_neighbors automatically:", n_neighbors)

    # Create and train the KNN classifier
    knn_clf = neighbors.KNeighborsClassifier(n_neighbors=n_neighbors, algorithm=knn_algo, weights='distance')
    print(knn_clf)
    knn_clf.fit(X, y)

    # Save the trained KNN classifier
    if model_save_path is not None:
        with open(model_save_path, 'wb') as f:
            pickle.dump(knn_clf, f)

    return knn_clf

'''

这是最后的决定

'''

def trainer():
    # STEP 1: Train the KNN classifier and save it to disk
    # Once the model is trained and saved, you can skip this step next time.
    print("Training KNN classifier...")
    classifier = train("app/facerec/dataset", model_save_path="app/facerec/models/trained_model.clf", n_neighbors=3)
    print("Training complete!")

'''

还想知道是否有可能不重写 'trained_model.clf' 文件,我们可以更新文件。

k-nn算法的时间复杂度为O(n)。我建议您使用近似最近邻 (a-nn) 算法。它的时间复杂度太低了。比如Google图片搜索就是基于这个算法。

Spotify annoy、Facebook faiss、nmslib 是 a-nn 库。

训练 kNN 模型不应强加高运行时开销。毕竟,直接(“精确搜索”)模型是懒惰的。它存储向量并在查询(或分类)时执行强力搜索。

我推测嵌入计算占据了您的训练时间。

如@johncasey 所述,您可能希望使用近似 kNN 模型(或 similarity search engines). There are many open-source similarity search libraries. Yet, if you need a production-ready, robust, real-time, efficient solution, then you should check out pinecone.io。(免责声明,我在 Pinecone 工作。)