Skip to content

Instantly share code, notes, and snippets.

@kstoneriv3
Last active April 17, 2021 14:09
Show Gist options
  • Save kstoneriv3/8cc5d25656dd0bb090c22a3a603a1a3e to your computer and use it in GitHub Desktop.
Save kstoneriv3/8cc5d25656dd0bb090c22a3a603a1a3e to your computer and use it in GitHub Desktop.
(Incomplete) Reproduction of image denoising using kernel PCA in "Learning to Find Pre-Images"
import numpy
import scipy.ndimage
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn import decomposition
digits = datasets.load_digits()
images = digits.images
images = map(lambda x: scipy.ndimage.zoom(x, 2, order=1), images)
d = 2 * 8
X_by_digits = [[] for i in range(10)]
for x, i in zip(images, digits.target):
X_by_digits[i].append(x)
X_train = numpy.concatenate([X[:10] for X in X_by_digits])
X_test_without_noise = numpy.concatenate([X[10:20] for X in X_by_digits])
X_test = X_test_without_noise + 11 * numpy.random.randn(100 * d * d).reshape(100, d, d)
X_test = numpy.clip(X_test, 0, 16)
kpca = decomposition.KernelPCA(n_components=10, fit_inverse_transform=True, alpha=1)
kpca.fit(X_train.reshape(-1, d * d))
X_trans = kpca.transform(X_test.reshape(-1, d * d))
X_rec = kpca.inverse_transform(X_trans).reshape(-1, d, d)
plt.figure(figsize=(24, 8))
for i, x in enumerate(X_test_without_noise):
col, row = 1 + i % 10, 1 + i // 10
plt.subplot(10, 32, col + 32 * (row - 1))
plt.imshow(x, cmap=plt.cm.gray_r)
plt.axis('off')
for i, x in enumerate(X_test):
col, row = 12 + i % 10, 1 + i // 10
plt.subplot(10, 32, col + 32 * (row - 1))
plt.imshow(x, cmap=plt.cm.gray_r)
plt.axis('off')
for i, x in enumerate(X_rec):
col, row = 23 + i % 10, 1 + i // 10
plt.subplot(10, 32, col + 32 * (row - 1))
plt.imshow(x, cmap=plt.cm.gray_r)
plt.axis('off')
plt.suptitle("Test images before adding noise (left), test images (middle), and reconstruction of test images (right)")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment