绘制字典人脸图像块

Machine LearningMachine LearningBeginner
立即练习

This tutorial is from open-source community. Access the source code

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

本实验展示了如何使用scikit-learn API来处理一个大型人脸数据集,并学习一组代表人脸的20 x 20图像块。本实验的关键在于使用在线学习,即我们一次加载并处理一张图像,并从每张图像中提取50个随机图像块。我们累积500个图像块(来自10张图像),然后运行在线KMeans对象MiniBatchKMeans的partial_fit方法。

虚拟机使用提示

虚拟机启动完成后,点击左上角切换到“笔记本”标签页,以访问Jupyter Notebook进行练习。

有时,你可能需要等待几秒钟让Jupyter Notebook完成加载。由于Jupyter Notebook的限制,操作验证无法自动化。

如果你在学习过程中遇到问题,随时向Labby提问。课程结束后提供反馈,我们会立即为你解决问题。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL sklearn(("Sklearn")) -.-> sklearn/CoreModelsandAlgorithmsGroup(["Core Models and Algorithms"]) sklearn(("Sklearn")) -.-> sklearn/DataPreprocessingandFeatureEngineeringGroup(["Data Preprocessing and Feature Engineering"]) ml(("Machine Learning")) -.-> ml/FrameworkandSoftwareGroup(["Framework and Software"]) sklearn/CoreModelsandAlgorithmsGroup -.-> sklearn/cluster("Clustering") sklearn/DataPreprocessingandFeatureEngineeringGroup -.-> sklearn/feature_extraction("Feature Extraction") ml/FrameworkandSoftwareGroup -.-> ml/sklearn("scikit-learn") subgraph Lab Skills sklearn/cluster -.-> lab-49104{{"绘制字典人脸图像块"}} sklearn/feature_extraction -.-> lab-49104{{"绘制字典人脸图像块"}} ml/sklearn -.-> lab-49104{{"绘制字典人脸图像块"}} end

加载数据

我们首先从scikit-learn中加载 Olivetti 人脸数据集。

from sklearn import datasets

faces = datasets.fetch_olivetti_faces()

学习图像字典

我们使用MiniBatchKMeans来学习图像字典。我们将聚类数设置为81,设置随机状态,并启用详细模式。然后,我们创建一个缓冲区来存储图像块,并遍历数据集中的每一张图像。我们从每张图像中提取50个图像块并重塑数据。然后,我们将数据追加到缓冲区并增加索引。如果索引是10的倍数,我们将连接缓冲区并对数据运行partial_fit。如果索引是100的倍数,我们将打印一条消息,指示到目前为止已经拟合的图像块数量。

import time
import numpy as np
from sklearn.cluster import MiniBatchKMeans
from sklearn.feature_extraction.image import extract_patches_2d

print("Learning the dictionary... ")
rng = np.random.RandomState(0)
kmeans = MiniBatchKMeans(n_clusters=81, random_state=rng, verbose=True, n_init=3)
patch_size = (20, 20)

buffer = []
t0 = time.time()

## The online learning part: cycle over the whole dataset 6 times
index = 0
for _ in range(6):
    for img in faces.images:
        data = extract_patches_2d(img, patch_size, max_patches=50, random_state=rng)
        data = np.reshape(data, (len(data), -1))
        buffer.append(data)
        index += 1
        if index % 10 == 0:
            data = np.concatenate(buffer, axis=0)
            data -= np.mean(data, axis=0)
            data /= np.std(data, axis=0)
            kmeans.partial_fit(data)
            buffer = []
        if index % 100 == 0:
            print("Partial fit of %4i out of %i" % (index, 6 * len(faces.images)))

dt = time.time() - t0
print("done in %.2fs." % dt)

绘制结果

最后,我们绘制人脸图像块并显示训练时间。

import matplotlib.pyplot as plt

plt.figure(figsize=(4.2, 4))
for i, patch in enumerate(kmeans.cluster_centers_):
    plt.subplot(9, 9, i + 1)
    plt.imshow(patch.reshape(patch_size), cmap=plt.cm.gray, interpolation="nearest")
    plt.xticks(())
    plt.yticks(())


plt.suptitle(
    "Patches of faces\nTrain time %.1fs on %d patches" % (dt, 8 * len(faces.images)),
    fontsize=16,
)
plt.subplots_adjust(0.08, 0.02, 0.92, 0.85, 0.08, 0.23)

plt.show()

总结

在本实验中,我们展示了如何使用在线学习来处理一个大型人脸数据集,并学习一组代表人脸的图像块。我们使用MiniBatchKMeans来学习图像字典并绘制了结果。