简介
本实验展示了如何使用 Scikit-learn 库中的 KBinsDiscretizer 对浣熊脸的样本图像执行向量量化。向量量化是一种减少用于表示图像的灰度级数量的技术。我们将使用 KBinsDiscretizer 对浣熊脸图像执行向量量化。我们将使用 8 个灰度级来表示图像,这样可以压缩到每个像素仅使用 3 位。我们将比较均匀和 k 均值聚类策略,以将像素值映射到 8 个灰度级。
虚拟机使用提示
虚拟机启动完成后,点击左上角切换到“笔记本”标签,以访问 Jupyter Notebook 进行练习。
有时,你可能需要等待几秒钟让 Jupyter Notebook 完成加载。由于 Jupyter Notebook 的限制,操作验证无法自动化。
如果你在学习过程中遇到问题,随时向 Labby 提问。课程结束后提供反馈,我们会及时为你解决问题。
加载并显示原始图像
我们将从 Scipy 中加载浣熊脸图像开始。我们将显示该图像,并检查其形状、数据类型和内存使用情况。
from scipy.misc import face
import matplotlib.pyplot as plt
raccoon_face = face(gray=True)
print(f"The dimension of the image is {raccoon_face.shape}")
print(f"The data used to encode the image is of type {raccoon_face.dtype}")
print(f"The number of bytes taken in RAM is {raccoon_face.nbytes}")
fig, ax = plt.subplots(ncols=2, figsize=(12, 4))
ax[0].imshow(raccoon_face, cmap=plt.cm.gray)
ax[0].axis("off")
ax[0].set_title("Original Image")
ax[1].hist(raccoon_face.ravel(), bins=256)
ax[1].set_xlabel("Pixel value")
ax[1].set_ylabel("Count of pixels")
ax[1].set_title("Distribution of the pixel values")
_ = fig.suptitle("Original Image of a Raccoon Face")
使用 KBinsDiscretizer 进行向量量化
现在我们将使用 KBinsDiscretizer 对浣熊脸图像进行向量量化。我们将使用 8 个灰度级来表示图像,这样可以压缩到每个像素仅使用 3 位。我们将使用均匀和 k 均值聚类策略将像素值映射到 8 个灰度级。
均匀采样策略
我们首先使用均匀采样策略将像素值映射到 8 个灰度级。
from sklearn.preprocessing import KBinsDiscretizer
n_bins = 8
encoder = KBinsDiscretizer(
n_bins=n_bins, encode="ordinal", strategy="uniform", random_state=0
)
compressed_raccoon_uniform = encoder.fit_transform(raccoon_face.reshape(-1, 1)).reshape(
raccoon_face.shape
)
fig, ax = plt.subplots(ncols=2, figsize=(12, 4))
ax[0].imshow(compressed_raccoon_uniform, cmap=plt.cm.gray)
ax[0].axis("off")
ax[0].set_title("Uniform Sampling")
ax[1].hist(compressed_raccoon_uniform.ravel(), bins=256)
ax[1].set_xlabel("Pixel value")
ax[1].set_ylabel("Count of pixels")
ax[1].set_title("Distribution of the pixel values")
_ = fig.suptitle("Raccoon face compressed using 3 bits and a uniform strategy")
k 均值聚类策略
现在我们将使用 k 均值聚类策略将像素值映射到 8 个灰度级。
encoder = KBinsDiscretizer(
n_bins=n_bins, encode="ordinal", strategy="kmeans", random_state=0
)
compressed_raccoon_kmeans = encoder.fit_transform(raccoon_face.reshape(-1, 1)).reshape(
raccoon_face.shape
)
fig, ax = plt.subplots(ncols=2, figsize=(12, 4))
ax[0].imshow(compressed_raccoon_kmeans, cmap=plt.cm.gray)
ax[0].axis("off")
ax[0].set_title("K-Means Clustering")
ax[1].hist(compressed_raccoon_kmeans.ravel(), bins=256)
ax[1].set_xlabel("Pixel value")
ax[1].set_ylabel("Count of pixels")
ax[1].set_title("Distribution of the pixel values")
_ = fig.suptitle("Raccoon face compressed using 3 bits and a K-means strategy")
内存占用
现在我们将检查压缩图像的内存使用情况。我们预计压缩后的图像占用的内存比原始图像少 8 倍。
print(f"The number of bytes taken in RAM is {compressed_raccoon_kmeans.nbytes}")
print(f"Compression ratio: {compressed_raccoon_kmeans.nbytes / raccoon_face.nbytes}")
总结
在本实验中,我们使用了 Scikit-learn 中的 KBinsDiscretizer 对浣熊脸的样本图像执行向量量化。我们使用 8 个灰度级来表示图像,这样可以压缩到每个像素仅使用 3 位。我们比较了均匀和 k 均值聚类策略,以将像素值映射到 8 个灰度级。我们发现 k 均值聚类策略提供了更平衡的像素值分布。我们还检查了压缩图像的内存使用情况,发现由于对压缩图像使用了 64 位浮点数表示,压缩后的图像占用的内存是原始图像的 8 倍。
总结
恭喜你!你已经完成了“使用 KBinsDiscretizer 进行向量量化”实验。你可以在 LabEx 中练习更多实验来提升你的技能。