# Dictionary Learning(字典学习、稀疏表示以及其他)

1.安装NumPy
2.安装SciPy
3.安装Python机器学习工具包sklearn

step1：首先是各种工具包的导入和测试样例的导入
1. from time import time
2. import matplotlib.pyplot as plt
3. import numpy as np
4. import scipy as sp
5. from sklearn.decomposition import MiniBatchDictionaryLearning
6. from sklearn.feature_extraction.image import extract_patches_2d
7. from sklearn.feature_extraction.image import reconstruct_from_patches_2d
8. from sklearn.utils.testing import SkipTest
9. from sklearn.utils.fixes import sp_version
10. if sp_version < (0, 12):
11.     raise SkipTest("Skipping because SciPy version earlier than 0.12.0 and "
12.                    "thus does not include the scipy.misc.face() image.")
13. try:
14.     from scipy import misc
15.     face = misc.face(gray=True)
16. except AttributeError:
17.     # Old versions of scipy have face in the top level package
18.     face = sp.face(gray=True)

step2：通过测试样例计算字典V
1. # Convert from uint8 representation with values between 0 and 255 to
2. # a floating point representation with values between 0 and 1.
3. face = face / 255.0
4. # downsample for higher speed
5. face = face[::2, ::2] + face[1::2, ::2] + face[::2, 1::2] + face[1::2, 1::2]
6. face = face / 4.0
7. height, width = face.shape
8. # Distort the right half of the image
9. print(‘Distorting image...‘)
10. distorted = face.copy()
11. distorted[:, width // 2:] += 0.075 * np.random.randn(height, width // 2)
12. # Extract all reference patches from the left half of the image
13. print(‘Extracting reference patches...‘)
14. t0 = time()
15. patch_size = (7, 7)
16. data = extract_patches_2d(distorted[:, :width // 2], patch_size)
17. data = data.reshape(data.shape[0], -1)
18. data -= np.mean(data, axis=0)
19. data /= np.std(data, axis=0)
20. print(‘done in %.2fs.‘ % (time() - t0))
21. print(‘Learning the dictionary...‘)
22. t0 = time()
23. dico = MiniBatchDictionaryLearning(n_components=100, alpha=1, n_iter=500)
24. V = dico.fit(data).components_
25. dt = time() - t0
26. print(‘done in %.2fs.‘ % dt)
27. plt.figure(figsize=(4.2, 4))
28. for i, comp in enumerate(V[:100]):
29.     plt.subplot(10, 10, i + 1)
30.     plt.imshow(comp.reshape(patch_size), cmap=plt.cm.gray_r,
31.                interpolation=‘nearest‘)
32.     plt.xticks(())
33.     plt.yticks(())
34. plt.suptitle(‘Dictionary learned from face patches\n‘ +
35.              ‘Train time %.1fs on %d patches‘ % (dt, len(data)),
36.              fontsize=16)
37. plt.subplots_adjust(0.08, 0.02, 0.92, 0.85, 0.08, 0.23)#left, right, bottom, top, wspace, hspace

step3：画出标准图像和真正的噪声，方便同之后字典学习学到的噪声相比较
1. def show_with_diff(image, reference, title):
2.     """Helper function to display denoising"""
3.     plt.figure(figsize=(5, 3.3))
4.     plt.subplot(1, 2, 1)
5.     plt.title(‘Image‘)
6.     plt.imshow(image, vmin=0, vmax=1, cmap=plt.cm.gray,
7.                interpolation=‘nearest‘)
8.     plt.xticks(())
9.     plt.yticks(())
10.     plt.subplot(1, 2, 2)
11.     difference = image - reference
12.     plt.title(‘Difference (norm: %.2f)‘ % np.sqrt(np.sum(difference ** 2)))
13.     plt.imshow(difference, vmin=-0.5, vmax=0.5, cmap=plt.cm.PuOr,
14.                interpolation=‘nearest‘)
15.     plt.xticks(())
16.     plt.yticks(())
17.     plt.suptitle(title, size=16)
18.     plt.subplots_adjust(0.02, 0.02, 0.98, 0.79, 0.02, 0.2)
19. show_with_diff(distorted, face, ‘Distorted image‘)

step4：测试不同的字典学习方法和参数对字典学习的影响
1. print(‘Extracting noisy patches... ‘)
2. t0 = time()
3. data = extract_patches_2d(distorted[:, width // 2:], patch_size)
4. data = data.reshape(data.shape[0], -1)
5. intercept = np.mean(data, axis=0)
6. data -= intercept
7. print(‘done in %.2fs.‘ % (time() - t0))
8. transform_algorithms = [
9.     (‘Orthogonal Matching Pursuit\n1 atom‘, ‘omp‘,
10.      {‘transform_n_nonzero_coefs‘: 1}),
11.     (‘Orthogonal Matching Pursuit\n2 atoms‘, ‘omp‘,
12.      {‘transform_n_nonzero_coefs‘: 2}),
13.     (‘Least-angle regression\n5 atoms‘, ‘lars‘,
14.      {‘transform_n_nonzero_coefs‘: 5}),
15.     (‘Thresholding\n alpha=0.1‘, ‘threshold‘, {‘transform_alpha‘: .1})]
16. reconstructions = {}
17. for title, transform_algorithm, kwargs in transform_algorithms:
18.     print(title + ‘...‘)
19.     reconstructions[title] = face.copy()
20.     t0 = time()
21.     dico.set_params(transform_algorithm=transform_algorithm, **kwargs)
22.     code = dico.transform(data)
23.     patches = np.dot(code, V)
24.     patches += intercept
25.     patches = patches.reshape(len(data), *patch_size)
26.     if transform_algorithm == ‘threshold‘:
27.         patches -= patches.min()
28.         patches /= patches.max()
29.     reconstructions[title][:, width // 2:] = reconstruct_from_patches_2d(
30.         patches, (height, width // 2))
31.     dt = time() - t0
32.     print(‘done in %.2fs.‘ % dt)
33.     show_with_diff(reconstructions[title], face,
34.                    title + ‘ (time: %.1fs)‘ % dt)
35. plt.show()

Dictionary Learning(字典学习、稀疏表示以及其他)

(2)
(10)

0条