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


.png)


.png)
- from time import time
- import matplotlib.pyplot as plt
- import numpy as np
- import scipy as sp
- from sklearn.decomposition import MiniBatchDictionaryLearning
- from sklearn.feature_extraction.image import extract_patches_2d
- from sklearn.feature_extraction.image import reconstruct_from_patches_2d
- from sklearn.utils.testing import SkipTest
- from sklearn.utils.fixes import sp_version
- if sp_version < (0, 12):
- raise SkipTest("Skipping because SciPy version earlier than 0.12.0 and "
- "thus does not include the scipy.misc.face() image.")
- try:
- from scipy import misc
- face = misc.face(gray=True)
- except AttributeError:
- # Old versions of scipy have face in the top level package
- face = sp.face(gray=True)

- # Convert from uint8 representation with values between 0 and 255 to
- # a floating point representation with values between 0 and 1.
- face = face / 255.0
- # downsample for higher speed
- face = face[::2, ::2] + face[1::2, ::2] + face[::2, 1::2] + face[1::2, 1::2]
- face = face / 4.0
- height, width = face.shape
- # Distort the right half of the image
- print('Distorting image...')
- distorted = face.copy()
- distorted[:, width // 2:] += 0.075 * np.random.randn(height, width // 2)
- # Extract all reference patches from the left half of the image
- print('Extracting reference patches...')
- t0 = time()
- patch_size = (7, 7)
- data = extract_patches_2d(distorted[:, :width // 2], patch_size)
- data = data.reshape(data.shape[0], -1)
- data -= np.mean(data, axis=0)
- data /= np.std(data, axis=0)
- print('done in %.2fs.' % (time() - t0))
- print('Learning the dictionary...')
- t0 = time()
- dico = MiniBatchDictionaryLearning(n_components=100, alpha=1, n_iter=500)
- V = dico.fit(data).components_
- dt = time() - t0
- print('done in %.2fs.' % dt)
- plt.figure(figsize=(4.2, 4))
- for i, comp in enumerate(V[:100]):
- plt.subplot(10, 10, i + 1)
- plt.imshow(comp.reshape(patch_size), cmap=plt.cm.gray_r,
- interpolation='nearest')
- plt.xticks(())
- plt.yticks(())
- plt.suptitle('Dictionary learned from face patches\n' +
- 'Train time %.1fs on %d patches' % (dt, len(data)),
- fontsize=16)
- plt.subplots_adjust(0.08, 0.02, 0.92, 0.85, 0.08, 0.23)#left, right, bottom, top, wspace, hspace

.png)
- def show_with_diff(image, reference, title):
- """Helper function to display denoising"""
- plt.figure(figsize=(5, 3.3))
- plt.subplot(1, 2, 1)
- plt.title('Image')
- plt.imshow(image, vmin=0, vmax=1, cmap=plt.cm.gray,
- interpolation='nearest')
- plt.xticks(())
- plt.yticks(())
- plt.subplot(1, 2, 2)
- difference = image - reference
- plt.title('Difference (norm: %.2f)' % np.sqrt(np.sum(difference ** 2)))
- plt.imshow(difference, vmin=-0.5, vmax=0.5, cmap=plt.cm.PuOr,
- interpolation='nearest')
- plt.xticks(())
- plt.yticks(())
- plt.suptitle(title, size=16)
- plt.subplots_adjust(0.02, 0.02, 0.98, 0.79, 0.02, 0.2)
- show_with_diff(distorted, face, 'Distorted image')

.png)
- print('Extracting noisy patches... ')
- t0 = time()
- data = extract_patches_2d(distorted[:, width // 2:], patch_size)
- data = data.reshape(data.shape[0], -1)
- intercept = np.mean(data, axis=0)
- data -= intercept
- print('done in %.2fs.' % (time() - t0))
- transform_algorithms = [
- ('Orthogonal Matching Pursuit\n1 atom', 'omp',
- {'transform_n_nonzero_coefs': 1}),
- ('Orthogonal Matching Pursuit\n2 atoms', 'omp',
- {'transform_n_nonzero_coefs': 2}),
- ('Least-angle regression\n5 atoms', 'lars',
- {'transform_n_nonzero_coefs': 5}),
- ('Thresholding\n alpha=0.1', 'threshold', {'transform_alpha': .1})]
- reconstructions = {}
- for title, transform_algorithm, kwargs in transform_algorithms:
- print(title + '...')
- reconstructions[title] = face.copy()
- t0 = time()
- dico.set_params(transform_algorithm=transform_algorithm, **kwargs)
- code = dico.transform(data)
- patches = np.dot(code, V)
- patches += intercept
- patches = patches.reshape(len(data), *patch_size)
- if transform_algorithm == 'threshold':
- patches -= patches.min()
- patches /= patches.max()
- reconstructions[title][:, width // 2:] = reconstruct_from_patches_2d(
- patches, (height, width // 2))
- dt = time() - t0
- print('done in %.2fs.' % dt)
- show_with_diff(reconstructions[title], face,
- title + ' (time: %.1fs)' % dt)
- plt.show()
.png)
.png)
.png)
.png)


Dictionary Learning(字典学习、稀疏表示以及其他)的更多相关文章
- 稀疏编码(sparse code)与字典学习(dictionary learning)
Dictionary Learning Tools for Matlab. 1. 简介 字典 D∈RN×K(其中 K>N),共有 k 个原子,x∈RN×1 在字典 D 下的表示为 w,则获取较为 ...
- 学习人工智能的第五个月[字典学习[Dictionary Learning,DL]]
摘要: 大白话解释字典学习,分享第五个月的学习过程,人生感悟,最后是自问自答. 目录: 1.字典学习(Dictionary Learning,DL) 2.学习过程 3.自问自答 内容: 1.字典学习( ...
- 字典学习(Dictionary Learning, KSVD)详解
注:字典学习也是一种数据降维的方法,这里我用到SVD的知识,对SVD不太理解的地方,可以看看这篇博客:<SVD(奇异值分解)小结 >. 1.字典学习思想 字典学习的思想应该源来实际生活中的 ...
- 字典学习(Dictionary Learning)
0 - 背景 0.0 - 为什么需要字典学习? 这里引用这个博客的一段话,我觉得可以很好的解释这个问题. 回答这个问题实际上就是要回答“稀疏字典学习 ”中的字典是怎么来的.做一个比喻,句子是人类社会最 ...
- 机器学习(Machine Learning)&深度学习(Deep Learning)资料【转】
转自:机器学习(Machine Learning)&深度学习(Deep Learning)资料 <Brief History of Machine Learning> 介绍:这是一 ...
- 机器学习(Machine Learning)&深度学习(Deep Learning)资料汇总 (上)
转载:http://dataunion.org/8463.html?utm_source=tuicool&utm_medium=referral <Brief History of Ma ...
- 联合CRF和字典学习的自顶向下的视觉显著性-全文解读
top-down visual saliency via joint CRF anddictionary learning 自顶向下的视觉显著性是使用目标对象的可判别表示和一个降低搜索空间的概率图来进 ...
- 论文阅读笔记(十九)【ITIP2017】:Super-Resolution Person Re-Identification With Semi-Coupled Low-Rank Discriminant Dictionary Learning
Introduction (1)问题描述: super resolution(SP)问题:Gallery是 high resolution(HR),Probe是 low resolution(LR). ...
- 论文阅读笔记(六)【TCSVT2018】:Semi-Supervised Cross-View Projection-Based Dictionary Learning for Video-Based Person Re-Identification
Introduction (1)Motivation: ① 现实场景中,给所有视频进行标记是一项繁琐和高成本的工作,而且随着监控相机的记录,视频信息会快速增多,因此需要采用半监督学习的方式,只对一部分 ...
随机推荐
- 手机设计尺寸 - iPhone界面尺寸
参考网址: http://www.qijishow.com/down/app-index.htm iPhone界面尺寸 设备 分辨率 PPI 状态栏高度 导航栏高度 标签栏高度 iPhone6 plu ...
- The specified module could not be found
打开IIS 信息服务,在左侧找到自己的计算机,点右键,选择属性,在主属性中选编辑,打开“目录安全性”选项卡,单击“匿名访问和验证控制”里的“编辑”按钮,在弹出的对话框中确保只选中了“匿名访问”和“集成 ...
- kali python pip3 的安装和卸载
今天很高兴安装完成调整了kali 然后看见kali已经帮助我安装了python2.7和python3.5可把我开心坏了,可是2.7有pip,而且包很全,但2.7与3.0切换使用我的就尴尬了 最后在su ...
- python3 -pip
https://docs.python.org/3/installing/ ===== pip is the preferred installer program. Starting with Py ...
- Markdown基本语法
Markdown 基本语法记录 # 欢迎使用 Cmd Markdown 编辑阅读器 ------ 我们理解您需要更便捷更高效的工具记录思想,整理笔记.知识,并将其中承载的价值传播给他人,**Cmd M ...
- IDEA 配置 tomcat的数据源
1.F4打开module setting面板,找到facets 配置项,这个配置项非常重要,里面可配置tomcat加载的web.xml和context.xml文件所在的路径,部署的时候IDEA会自动读 ...
- iOS delegate
有两个scene,分别为Scene A和Scene B.Scene A上有一个UIButton(Button A)和一个UILable(Lable A):Scene B上有一个UITextFiled( ...
- curses.h的安装和使用
gcc test.c -o test 用以上命令编译包含curses.h头文件的程序时会出现各种引用未定义的错误,并且已经安装了 kernel-devel ncurese-devel ncurese- ...
- java文件下载和导出文件名乱码浏览器兼容性问题
实例: String poorName= dataMap.get("NAME").toString(); String villageName = dataMap.get(&quo ...
- 使用#锚点时,jsp中不能有basePath
今天遇到一个前端问题,使用dtree点击父节点时能够点开,但是之后又left这块frame又回到了登录页面, 难道调用了history(-1)吗,鼠标放上去显示javascript:#,??,回到页面 ...