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: ① 现实场景中,给所有视频进行标记是一项繁琐和高成本的工作,而且随着监控相机的记录,视频信息会快速增多,因此需要采用半监督学习的方式,只对一部分 ...
随机推荐
- JavaScript 日期选择器 Pikaday
找一些插件的过程实在太痛苦了...好容易找到一个,赶紧记录下.免得以后重复浪费时间在这上面. 插件名:Pikaday github地址:https://github.com/dbushell/Pika ...
- Android Duplicate files copied in APK
今天调试 Android 应用遇到这么个问题: Duplicate files copied in APK META-INF/DEPENDENCIES File 1: httpmime-4.3.2.j ...
- Redis Cluster 介绍与使用
Redis Cluster 功能特性 Redis 集群是分布式的redis 实现,具有以下特性: 1. 高可用性与可线性扩张到1000个节点 2. 数据自动路由到多个节点 3. 节点间数据共享 4. ...
- sql跨库查询
---------------------------------------------------------------------------------- --1. 创建链接服务器 --1. ...
- jquery 双向select控件bootstrap Dual listbox
http://www.cnblogs.com/hangwei/p/5040866.html -->jquery 双向select控件bootstrap Dual listboxhtt ...
- Delphi中DBChart的数据库应用
一:属性相关:Series选项: (1)Format页(数据柱的风格) 在Color Each中打勾,就可使用多种颜色显示,color按钮用于设置颜色,Style用于设置图表的风格(Rectangle ...
- centos7.0 下安装git(http方式)
之前弄了个ssh方式访问的git服务器,但是那种方式不太方便,而且网页也访问不了,所以这里又弄个http方式访问的git服务器. 安装过程还和之前一样,这里我再复制一遍,省的再去找了. 1. 安装依赖 ...
- mvc file控件无刷新异步上传操作
前言 上传文件应该是很常见必不可少的一个操作,网上也有很多提供的上传控件.今天遇到一个问题:input控件file无法进行异步无刷新上传.真真的感到别扭.所以就尝试这去处理了一下.主要分三个部分:上传 ...
- Redis
1. sds类型 sds为一种抽象数据结构 typedef char *sds;struct sdshdr { // buf 已占用长度int len; // buf 剩余可用长度int free; ...
- Web Js 按键事件……Enter提交事件 Enter Js事件
$(document).ready(function(){ document.onkeydown = function (event){ if (event.keyCode==13) //回车键的键值 ...