ML之SVM:调用(sklearn的lfw_people函数在线下载55个外国人图片文件夹数据集)来精确实现人脸识别并提取人脸特征向量


眼睛神勇
眼睛神勇 2022-09-19 16:19:45 49994
分类专栏: 人才

ML之SVM:调用(sklearn的lfw_people函数在线下载55个外国人图片文件夹数据集)来精确实现人脸识别并提取人脸特征向量

目录

输出结果

代码设计


输出结果

代码设计

  1. from __future__ import print_function
  2. from time import time
  3. import logging
  4. import matplotlib.pyplot as plt
  5. from sklearn.cross_validation import train_test_split
  6. from sklearn.datasets import fetch_lfw_people
  7. from sklearn.grid_search import GridSearchCV
  8. from sklearn.metrics import classification_report
  9. from sklearn.metrics import confusion_matrix
  10. from sklearn.decomposition import RandomizedPCA
  11. from sklearn.svm import SVC
  12. print(__doc__)
  13. logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s')
  14. lfw_people = fetch_lfw_people(min_faces_per_person=99, resize=0.4)
  15. n_samples, h, w = lfw_people.images.shape
  16. X = lfw_people.data
  17. n_features = X.shape[1]
  18. y = lfw_people.target
  19. target_names = lfw_people.target_names
  20. n_classes = target_names.shape[0]
  21. print("Total dataset size:")
  22. print("n_samples: %d" % n_samples)
  23. print("n_features: %d" % n_features)
  24. print("n_classes: %d" % n_classes)
  25. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)
  26. n_components = 150
  27. print("Extracting the top %d eigenfaces from %d faces"
  28. % (n_components, X_train.shape[0]))
  29. t0 = time()
  30. pca = RandomizedPCA(n_components=n_components, whiten=True).fit(X_train)
  31. print("done in %0.3fs" % (time() - t0))
  32. eigenfaces = pca.components_.reshape((n_components, h, w))
  33. print("Projecting the input data on the eigenfaces orthonormal basis")
  34. t0 = time()
  35. X_train_pca = pca.transform(X_train)
  36. X_test_pca = pca.transform(X_test)
  37. print("done in %0.3fs" % (time() - t0))
  38. print("Fitting the classifier to the training set")
  39. t0 = time()
  40. param_grid = {'C': [1e3, 5e3, 1e4, 5e4, 1e5],'gamma': [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.1], }
  41. clf = GridSearchCV(SVC(kernel='rbf', class_weight='balanced'), param_grid) auto改为balanced
  42. clf = clf.fit(X_train_pca, y_train)
  43. print("done in %0.3fs" % (time() - t0))
  44. print("Best estimator found by grid search:")
  45. print(clf.best_estimator_)
  46. print("Predicting people's names on the test set")
  47. t0 = time()
  48. y_pred = clf.predict(X_test_pca)
  49. print("done in %0.3fs" % (time() - t0))
  50. print(classification_report(y_test, y_pred, target_names=target_names))
  51. print(confusion_matrix(y_test, y_pred, labels=range(n_classes)))
  52. def plot_gallery(images, titles, h, w, n_row=3, n_col=4):
  53. """Helper function to plot a gallery of portraits"""
  54. plt.figure(figsize=(1.8 * n_col, 2.4 * n_row))
  55. plt.subplots_adjust(bottom=0, left=.01, right=.99, top=.90, hspace=.35)
  56. for i in range(n_row * n_col):
  57. plt.subplot(n_row, n_col, i + 1)
  58. plt.imshow(images[i].reshape((h, w)), cmap=plt.cm.gray)
  59. plt.title(titles[i], size=12)
  60. plt.xticks(())
  61. plt.yticks(())
  62. def title(y_pred, y_test, target_names, i):
  63. pred_name = target_names[y_pred[i]].rsplit(' ', 1)[-1]
  64. true_name = target_names[y_test[i]].rsplit(' ', 1)[-1]
  65. return 'predicted: %s\ntrue: %s' % (pred_name, true_name)
  66. prediction_titles = [title(y_pred, y_test, target_names, i)
  67. for i in range(y_pred.shape[0])]
  68. plot_gallery(X_test, prediction_titles, h, w)
  69. eigenface_titles = ["eigenface %d" % i for i in range(eigenfaces.shape[0])]
  70. plot_gallery(eigenfaces, eigenface_titles, h, w)
  71. plt.show()

相关文章
ML之SVM:调用(sklearn的lfw_people函数在线下载55个外国人图片文件夹数据集)来精确实现人脸识别并提取人脸特征向量

网站声明:如果转载,请联系本站管理员。否则一切后果自行承担。

本文链接:https://www.xckfsq.com/news/show.html?id=3546
赞同 0
评论 0 条
眼睛神勇L0
粉丝 0 发表 12 + 关注 私信
上周热门
如何使用 StarRocks 管理和优化数据湖中的数据?  2941
【软件正版化】软件正版化工作要点  2860
统信UOS试玩黑神话:悟空  2819
信刻光盘安全隔离与信息交换系统  2712
镜舟科技与中启乘数科技达成战略合作,共筑数据服务新生态  1246
grub引导程序无法找到指定设备和分区  1213
华为全联接大会2024丨软通动力分论坛精彩议程抢先看!  163
点击报名 | 京东2025校招进校行程预告  162
2024海洋能源产业融合发展论坛暨博览会同期活动-海洋能源与数字化智能化论坛成功举办  160
华为纯血鸿蒙正式版9月底见!但Mate 70的内情还得接着挖...  157
本周热议
我的信创开放社区兼职赚钱历程 40
今天你签到了吗? 27
信创开放社区邀请他人注册的具体步骤如下 15
如何玩转信创开放社区—从小白进阶到专家 15
方德桌面操作系统 14
我有15积分有什么用? 13
用抖音玩法闯信创开放社区——用平台宣传企业产品服务 13
如何让你先人一步获得悬赏问题信息?(创作者必看) 12
2024中国信创产业发展大会暨中国信息科技创新与应用博览会 9
中央国家机关政府采购中心:应当将CPU、操作系统符合安全可靠测评要求纳入采购需求 8

加入交流群

请使用微信扫一扫!