SPAMS是一种稀疏优化工具箱,可以进行字典学习和矩阵稀疏分解等。提供了Matlab、R语言和python三种接口,下边说一下,在windows系统下的配置。
基本配置环境:matlab 2010a,VS2010和XP系统
1、SPAMS下载:http://spams-devel.gforge.inria.fr/downloads.html
2.、将其添加进matlab目录中。
3、配置mex编译器,详见:http://blog.csdn.net/huanleu/article/details/38735797
4、修改SPAMS配置文件compile.m
编译器配置,gcc修改为mex
%%%%%%%%%%%%% COMPILER CONFIGURATION %%%%%%%%%%%%%%%%
compiler='mex';
多线程配置,因为兼容性问题,改为false
%%%%%%%%%%%% MULTITHREADING CONFIGURATION %%%%%%%%%%%%%%
use_multithread=false;
考虑速度问题,将use_64bits_integers设为alse,大矩阵的时候用true。
其他的保持不变。
5、进入matlab命令窗口,输入compile,进行编译,会报出一堆warning,作为一名程序员,直接无视。
下边贴一段测试代码,代码放在test_release文件夹下,详见https://chunqiu.blog.ustc.edu.cn/?p=570。
% test_TrainDL_GJ.m:测试mexTrainDL函数
clc
clear
close all
I = double(imread('..\data\lena.png'))/255;
I = imresize(I,0.25);%scale
X = im2col(I,[8,8],'sliding');% extract 8*8 patches
X = X-repmat(mean(X),[size(X,1) 1]);% substract the mean
X = X./repmat(sqrt(sum(X.^2)),[size(X,1) 1]);% normination
param.K = 256; % learn a dictionary with 100 elements
param.lambda = 0.15;
param.numThreads = -1; % number of threads
param.batchsize = 400;
param.verbose = false;
param.iter = 100; %iteration times
tic
D = mexTrainDL(X,param);
t = toc;
fprintf('time of comptation for Dictionary Learning: %f\n',t);
fprintf('Evaluating cost functions ...\n');
alpha = mexLasso(X,D,param);
R = mean(0.5*sum((X-D*alpha).^2)+param.lambda*sum(abs(alpha)));
ImD = displayPatches(D);
imagesc(ImD);
colormap('gray');
fprintf('objective function: %f\n',R);
最后的测试结果如下:
指标如下
time of comptation for Dictionary Learning: 13.248159
Evaluating cost functions ...
objective function: 0.207007