[opengm] 12/40: add simple python and matlab demos
Ghislain Vaillant
ghisvail-guest at moszumanska.debian.org
Sun Feb 14 16:06:01 UTC 2016
This is an automated email from the git hooks/post-receive script.
ghisvail-guest pushed a commit to branch master
in repository opengm.
commit 34c8b90590b6f41ee17875b3cde05f00a8e0e8be
Author: joergkappes <kappes at math.uni-heidelberg.de>
Date: Tue Dec 1 14:30:09 2015 +0100
add simple python and matlab demos
---
src/tutorials/matlab/applications/binaryNDSeg.m | 57 +++++++++++++++++++++
src/tutorials/matlab/demo/demo1.m | 5 ++
src/tutorials/matlab/demo/demo2.m | 67 +++++++++++++++++++++++++
src/tutorials/matlab/demo/demo3.m | 43 ++++++++++++++++
src/tutorials/matlab/demo/demo4.m | 52 +++++++++++++++++++
src/tutorials/matlab/demo/denoise_fast.m | 41 +++++++++++++++
src/tutorials/matlab/demo/setup.m | 5 ++
src/tutorials/python/demo/demo1.py | 1 +
src/tutorials/python/demo/demo2.py | 1 +
src/tutorials/python/demo/demo3.py | 1 +
src/tutorials/python/demo/demo4.py | 1 +
src/tutorials/python/demo/demo5.py | 1 +
12 files changed, 275 insertions(+)
diff --git a/src/tutorials/matlab/applications/binaryNDSeg.m b/src/tutorials/matlab/applications/binaryNDSeg.m
new file mode 100644
index 0000000..9624b42
--- /dev/null
+++ b/src/tutorials/matlab/applications/binaryNDSeg.m
@@ -0,0 +1,57 @@
+function [ L ] = binaryNDSeg( U, lambda )
+% binary segmentation on a nD-grid with a Issing model
+% Detailed explanation goes here
+ dims = size(U);
+ numStates = dims(1);
+ shape = dims(2:end);
+ numVar = prod(shape);
+
+ opengmpath = '~/GIT/opengm-2.3/opengm/';
+ opengmbuildpath = '~/GIT/opengm-2.3/opengm/build/';
+
+ addpath( [opengmbuildpath,'src/interfaces/matlab/opengm/mex-src/'])
+ addpath(genpath([opengmpath,'src/interfaces/matlab/opengm/m_files/model/']))
+
+ gm = openGMModel;
+ gm.addVariables(ones(1,numVar)*numStates);
+
+ %% Add Unaries
+ gm.addUnaries( 0:(numVar-1),U(:,:) );
+
+
+ %% Add Regularizer
+ pottsFunction = openGMPottsFunction([numStates, numStates], 0, lambda);
+ gm.addFunction(pottsFunction);
+
+ if(numel(shape)==2)
+ % 1st dim
+ variables = 0 : (numVar - 1);
+ ind = sub2ind(shape,ones(1,shape(2))*shape(1),1:shape(2));
+ variables(ind) = [];
+ variables = cat(1, variables, variables + 1);
+ gm.addFactors(pottsFunction, variables);
+ % 2st dim
+ variables = 0 : (numVar - 1);
+ ind = sub2ind(shape,1:shape(1), ones(1,shape(1))*shape(2));
+ variables(ind) = [];
+ variables = cat(1, variables, variables + shape(1));
+ gm.addFactors(pottsFunction, variables);
+ elseif(numel(shape)==3)
+ V = reshape(0 : (numVar - 1),shape);
+ %1
+ T = V(1:end-1,:,:);
+ gm.addFactors(pottsFunction, cat(1, T(:)', T(:)'+1));
+ %2
+ T = V(:,1:end-1,:);
+ gm.addFactors(pottsFunction, cat(1, T(:)', T(:)'+shape(1)));
+ %3
+ T = V(:,:,1:end-1);
+ gm.addFactors(pottsFunction, cat(1, T(:)', T(:)'+prod(shape(1:2))));
+ else
+ disp('Other than 2 dim need to be implemented')
+
+ end
+ opengm('m',gm,'a','GRAPHCUT','o','out.h5');
+ L=reshape(h5read('out.h5','/states'),shape);
+end
+
diff --git a/src/tutorials/matlab/demo/demo1.m b/src/tutorials/matlab/demo/demo1.m
new file mode 100644
index 0000000..e210d7c
--- /dev/null
+++ b/src/tutorials/matlab/demo/demo1.m
@@ -0,0 +1,5 @@
+function L = demo1(I, color, T)
+ I = double(I);
+ d = abs(I(:,:,1)-color(1)) + abs(I(:,:,2)-color(2)) + abs(I(:,:,3)-color(3));
+ L = d<T;
+end
\ No newline at end of file
diff --git a/src/tutorials/matlab/demo/demo2.m b/src/tutorials/matlab/demo/demo2.m
new file mode 100644
index 0000000..d8891a6
--- /dev/null
+++ b/src/tutorials/matlab/demo/demo2.m
@@ -0,0 +1,67 @@
+function L = demo2(I,color, T, lambda)
+ I = double(I);
+ d = abs(I(:,:,1)-color(1)) + abs(I(:,:,2)-color(2)) + abs(I(:,:,3)-color(3));
+
+ % parameter
+ numVariablesN = size(I,1); % number of variables of first dimension
+ numVariablesM = size(I,2); % number of variables of second dimension
+ numLabels = 2; % number of labels for each variable
+
+
+ numVariables = numVariablesN * numVariablesM;
+
+ % binary function
+ pottsFunction = openGMPottsFunction([numLabels, numLabels], 0, lambda);
+
+ % create model
+ gm = openGMModel;
+
+ % add variables
+ gm.addVariables(repmat(numLabels, 1, numVariables));
+
+ % add unary functios and factor to each variable
+ fastUnaries = 1;
+ if(fastUnaries)
+ gm.addUnaries(0:numVariables-1, [d(:)';T*ones(1,numVariables)]);
+ else
+ disp('please wait for a few minutes :-)');
+ progress = 0;
+ for i=1:numVariables
+ if(floor(i/numVariables*100)>progress)
+ progress = floor(i/numVariables*100);
+ disp([num2str(progress),' %']);
+ end
+ unaryFunction = openGMExplicitFunction(numLabels, [d(i);T]);
+ gm.addFunction(unaryFunction);
+ gm.addFactor(unaryFunction, i-1);
+ end
+ end
+
+ % add potts functions
+ gm.addFunction(pottsFunction);
+
+
+ % add binary factors to create grid structure
+ % horizontal factors
+ variablesH = 0 : (numVariables - 1);
+ variablesH(numVariablesN : numVariablesN : numVariables) = [];
+ variablesH = cat(1, variablesH, variablesH + 1);
+ % vertical factors
+ variablesV = 0 : (numVariables - (numVariablesN + 1));
+ variablesV = cat(1, variablesV, variablesV + numVariablesN);
+ % concatenate horizontal and vertical factors
+ variables = cat(2, variablesH, variablesV);
+ % add factors
+ gm.addFactors(pottsFunction, variables);
+
+ % print model informations
+ disp('print model informations');
+ opengm('modelinfo', 'm', gm);
+
+ %infer
+ disp('start inference');
+ opengm('a','GRAPHCUT', 'm', gm, 'o','out.h5');
+ disp('load result');
+ x = h5read('out.h5','/states');
+ L = uint8(reshape(1-x,size(I,1),size(I,2))*255);
+end
\ No newline at end of file
diff --git a/src/tutorials/matlab/demo/demo3.m b/src/tutorials/matlab/demo/demo3.m
new file mode 100644
index 0000000..917cffb
--- /dev/null
+++ b/src/tutorials/matlab/demo/demo3.m
@@ -0,0 +1,43 @@
+function L = demo3(I,color, T, lambda)
+ I = double(I);
+ d = abs(I(:,:,1)-color(1)) + abs(I(:,:,2)-color(2)) + abs(I(:,:,3)-color(3));
+
+ % parameter
+ numVariablesN = size(I,1); % number of variables of first dimension
+ numVariablesM = size(I,2); % number of variables of second dimension
+ numLabels = 2; % number of labels for each variable
+
+
+ numVariables = numVariablesN * numVariablesM;
+
+ % binary function
+ pottsFunction = openGMPottsFunction([numLabels, numLabels], 0, lambda);
+
+ % create model
+ gm = openGMModel;
+
+ % add variables
+ gm.addVariables(repmat(numLabels, 1, numVariables));
+
+ % add unary functios and factor to each variable
+ gm.addUnaries(0:numVariables-1, [d(:)';T*ones(1,numVariables)]);
+
+ % add functions
+ gm.addFunction(pottsFunction);
+
+ % add binary factors to create grid structure
+ % horizontal factors
+ variablesH = 0 : (numVariables - 1);
+ variablesH(numVariablesN : numVariablesN : numVariables) = [];
+ variablesH = cat(1, variablesH, variablesH + 1);
+ % vertical factors
+ variablesV = 0 : (numVariables - (numVariablesN + 1));
+ variablesV = cat(1, variablesV, variablesV + numVariablesN);
+ % concatenate horizontal and vertical factors
+ variables = cat(2, variablesH, variablesV);
+ % add factors
+ gm.addFactors(pottsFunction, variables);
+
+ % store model into file 'gmfile.h5' with datasetname 'gm'
+ gm.store('gmfile.h5','gm');
+end
\ No newline at end of file
diff --git a/src/tutorials/matlab/demo/demo4.m b/src/tutorials/matlab/demo/demo4.m
new file mode 100644
index 0000000..2320c5d
--- /dev/null
+++ b/src/tutorials/matlab/demo/demo4.m
@@ -0,0 +1,52 @@
+function L = demo4(I,color1, color2, T, lambda)
+ I = double(I);
+ d1 = abs(I(:,:,1)-color1(1)) + abs(I(:,:,2)-color1(2)) + abs(I(:,:,3)-color1(3));
+ d2 = abs(I(:,:,1)-color2(1)) + abs(I(:,:,2)-color2(2)) + abs(I(:,:,3)-color2(3));
+
+ % parameter
+ numVariablesN = size(I,1); % number of variables of first dimension
+ numVariablesM = size(I,2); % number of variables of second dimension
+ numLabels = 3; % number of labels for each variable
+
+
+ numVariables = numVariablesN * numVariablesM;
+
+ % binary function
+ pottsFunction = openGMPottsFunction([numLabels, numLabels], 0, lambda);
+
+ % create model
+ gm = openGMModel;
+
+ % add variables
+ gm.addVariables(repmat(numLabels, 1, numVariables));
+
+ % add unary functios and factor to each variable
+ gm.addUnaries(0:numVariables-1, [d1(:)';d2(:)';T*ones(1,numVariables)]);
+
+ % add functions
+ gm.addFunction(pottsFunction);
+
+ % add binary factors to create grid structure
+ % horizontal factors
+ variablesH = 0 : (numVariables - 1);
+ variablesH(numVariablesN : numVariablesN : numVariables) = [];
+ variablesH = cat(1, variablesH, variablesH + 1);
+ % vertical factors
+ variablesV = 0 : (numVariables - (numVariablesN + 1));
+ variablesV = cat(1, variablesV, variablesV + numVariablesN);
+ % concatenate horizontal and vertical factors
+ variables = cat(2, variablesH, variablesV);
+ % add factors
+ gm.addFactors(pottsFunction, variables);
+
+ % print model informations
+ disp('print model informations');
+ opengm('modelinfo', 'm', gm);
+
+ %infer
+ disp('start inference');
+ opengm('a','ALPHAEXPANSION', 'm', gm, 'o','out.h5');
+ disp('load result');
+ x = h5read('out.h5','/states');
+ L = uint8(reshape(x,size(I,1),size(I,2))*120);
+end
\ No newline at end of file
diff --git a/src/tutorials/matlab/demo/denoise_fast.m b/src/tutorials/matlab/demo/denoise_fast.m
new file mode 100644
index 0000000..8b1e9b4
--- /dev/null
+++ b/src/tutorials/matlab/demo/denoise_fast.m
@@ -0,0 +1,41 @@
+
+
+
+H=80;
+W=100;
+gm = openGMModel;
+
+I=zeros(H,W);
+I(40:60,40:60)=1;
+I=I+rand(size(I))*0.8;
+
+% add variables
+gm.addVariables([ ones(1,H*W)*2 ]);
+gm.addUnaries(0:H*W-1, [I(:)';1-I(:)']);
+
+numVariablesH =W;
+numVariablesV =H;
+
+ numVariables = numVariablesH * numVariablesV;
+ % horizontal factors
+ variablesH = 0 : (numVariables - 1);
+ variablesH(numVariablesH : numVariablesH : numVariables) = [];
+ variablesH = cat(1, variablesH, variablesH + 1);
+ % vertical factors
+ variablesV = 0 : (numVariables - (numVariablesV + 1));
+ variablesV = cat(1, variablesV, variablesV + numVariablesV);
+ % concatenate horizontal and vertical factors
+ variablePairs = cat(2, variablesH, variablesV);
+
+ %gm.addPairwiseTerms(variablePairs , [0;1;1;0]*ones(1,numel(variablePairs)/2));
+
+
+
+ gm.store('gm.h5','gm')
+
+ %infer
+ disp('start inference');
+ opengm('a','GRAPHCUT', 'm', gm, 'o','out.h5');
+ disp('load result');
+ x = h5read('out.h5','/states');
+ L = uint8(reshape(1-x,H,W)*255);
\ No newline at end of file
diff --git a/src/tutorials/matlab/demo/setup.m b/src/tutorials/matlab/demo/setup.m
new file mode 100644
index 0000000..6fac654
--- /dev/null
+++ b/src/tutorials/matlab/demo/setup.m
@@ -0,0 +1,5 @@
+opengm_root = '~/GIT/opengm/';
+opengm_build = '~GIT/opengm/build/';
+
+addpath([opengm_build,'src/interfaces/matlab/opengm/mex-src/'])
+addpath(genpath([opengm_root,'src/interfaces/matlab/opengm/m_files/']))
diff --git a/src/tutorials/python/demo/demo1.py b/src/tutorials/python/demo/demo1.py
new file mode 120000
index 0000000..4910c01
--- /dev/null
+++ b/src/tutorials/python/demo/demo1.py
@@ -0,0 +1 @@
+/export/home/jkappes/ipa-svn/opengm-demo/python/demo1.py
\ No newline at end of file
diff --git a/src/tutorials/python/demo/demo2.py b/src/tutorials/python/demo/demo2.py
new file mode 120000
index 0000000..11a0cc1
--- /dev/null
+++ b/src/tutorials/python/demo/demo2.py
@@ -0,0 +1 @@
+/export/home/jkappes/ipa-svn/opengm-demo/python/demo2.py
\ No newline at end of file
diff --git a/src/tutorials/python/demo/demo3.py b/src/tutorials/python/demo/demo3.py
new file mode 120000
index 0000000..e05d613
--- /dev/null
+++ b/src/tutorials/python/demo/demo3.py
@@ -0,0 +1 @@
+/export/home/jkappes/ipa-svn/opengm-demo/python/demo3.py
\ No newline at end of file
diff --git a/src/tutorials/python/demo/demo4.py b/src/tutorials/python/demo/demo4.py
new file mode 120000
index 0000000..738200a
--- /dev/null
+++ b/src/tutorials/python/demo/demo4.py
@@ -0,0 +1 @@
+/export/home/jkappes/ipa-svn/opengm-demo/python/demo4.py
\ No newline at end of file
diff --git a/src/tutorials/python/demo/demo5.py b/src/tutorials/python/demo/demo5.py
new file mode 120000
index 0000000..b052679
--- /dev/null
+++ b/src/tutorials/python/demo/demo5.py
@@ -0,0 +1 @@
+/export/home/jkappes/ipa-svn/opengm-demo/python/demo5.py
\ No newline at end of file
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/opengm.git
More information about the debian-science-commits
mailing list