Python 3.x 如何实时加速Knn人脸识别算法
我在做人脸检测和识别的工作,我想实时检测人脸 但是到了训练的时候,要花很长时间来训练运动员 数据是否有可能减少训练的时间?数据是否有任何帮助 把这个问题告诉我 ''' ''' 还想知道是否有可能不重写“trained_model.clf”文件,而是更新该文件。k-nn算法的时间复杂度为O(n)。我建议您使用近似最近邻(a-nn)算法。它的时间复杂度太低。例如,谷歌图像搜索就是基于这种算法Python 3.x 如何实时加速Knn人脸识别算法,python-3.x,scikit-learn,pickle,face-recognition,Python 3.x,Scikit Learn,Pickle,Face Recognition,我在做人脸检测和识别的工作,我想实时检测人脸 但是到了训练的时候,要花很长时间来训练运动员 数据是否有可能减少训练的时间?数据是否有任何帮助 把这个问题告诉我 ''' ''' 还想知道是否有可能不重写“trained_model.clf”文件,而是更新该文件。k-nn算法的时间复杂度为O(n)。我建议您使用近似最近邻(a-nn)算法。它的时间复杂度太低。例如,谷歌图像搜索就是基于这种算法 Spotify Havert、Facebook faiss、nmslib都是a-nn库。训练kNN模型不应带
Spotify Havert、Facebook faiss、nmslib都是a-nn库。训练kNN模型不应带来高运行时开销。毕竟,简单的(“精确搜索”)模型是懒惰的。它存储向量并在查询(或分类)时执行暴力搜索 我推测嵌入计算支配着你的训练时间
正如@johncasey所提到的,您可能希望使用近似的kNN模型(或引擎)。有许多开源的相似性搜索库。然而,如果您需要一个生产就绪、健壮、实时、高效的解决方案,那么您应该去看看。(免责声明,我为Pinecone工作。)您是否使用Scikit learn for KNN?是@KnowledgeGainer培训时间通常取决于数据集的大小,而且KNN不使用任何GPU,因此与基于GPU的框架相比,通常需要时间。我可以知道你用来训练的数据集的大小吗?实际上,数据集包含特定文件夹的图像至少15个图像,就像当用户数量增加时,文件夹数量也会增加一样。如果我得到了正确的结果,那么你的意思是,它会随着用户的增加而不断增加,每个用户都会有15张自己的图片,对吗?
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!")