Skip to content

Commit

Permalink
support for conversion of draft HH models into new format
Browse files Browse the repository at this point in the history
  • Loading branch information
rmtfleming committed Sep 12, 2019
1 parent 69b73c0 commit 9ab777e
Show file tree
Hide file tree
Showing 26 changed files with 1,102 additions and 210 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,6 @@
[submodule "external/base/samplers/looplessFluxSampler"]
path = external/base/samplers/looplessFluxSampler
url = https://github.com/rmtfleming/looplessFluxSampler
[submodule "external/base/samplers/constrained-logconcave-sampler"]
path = external/base/samplers/constrained-logconcave-sampler
url = https://github.com/Bounciness/constrained-logconcave-sampler
1 change: 1 addition & 0 deletions external/base/samplers/constrained-logconcave-sampler
11 changes: 11 additions & 0 deletions external/base/utilities/histogram_distance/build.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
% Build the C/C++ files provided in the package
%
% @author: B. Schauerte
% @date: 2009
% @url: http://cvhci.anthropomatik.kit.edu/~bschauer/

cpp_files=dir('*.cpp');
for i=1:length(cpp_files)
fprintf('Building %d of %d: %s\n',i,length(cpp_files),cpp_files(i).name);
mex(cpp_files(i).name);
end
55 changes: 55 additions & 0 deletions external/base/utilities/histogram_distance/chi_square_statistics.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
function d=chi_square_statistics(XI,XJ)
% Implementation of the Chi^2 distance to use with pdist
% (cf. "The Earth Movers' Distance as a Metric for Image Retrieval",
% Y. Rubner, C. Tomasi, L.J. Guibas, 2000)
%
% @author: B. Schauerte
% @date: 2009
% @url: http://cvhci.anthropomatik.kit.edu/~bschauer/

% Copyright 2009 B. Schauerte. All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are
% met:
%
% 1. Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
%
% 2. Redistributions in binary form must reproduce the above copyright
% notice, this list of conditions and the following disclaimer in
% the documentation and/or other materials provided with the
% distribution.
%
% THIS SOFTWARE IS PROVIDED BY B. SCHAUERTE ''AS IS'' AND ANY EXPRESS OR
% IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
% WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
% DISCLAIMED. IN NO EVENT SHALL B. SCHAUERTE OR CONTRIBUTORS BE LIABLE
% FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
% BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
% WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
% OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
% ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
%
% The views and conclusions contained in the software and documentation
% are those of the authors and should not be interpreted as representing
% official policies, either expressed or implied, of B. Schauerte.

m=size(XJ,1); % number of samples of p
p=size(XI,2); % dimension of samples

assert(p == size(XJ,2)); % equal dimensions
assert(size(XI,1) == 1); % pdist requires XI to be a single sample

d=zeros(m,1); % initialize output array

for i=1:m
for j=1:p
m=(XI(1,j) + XJ(i,j)) / 2;
if m ~= 0 % if m == 0, then xi and xj are both 0 ... this way we avoid the problem with (xj - m)^2 / m = (0 - 0)^2 / 0 = 0 / 0 = ?
d(i,1) = d(i,1) + ((XI(1,j) - m)^2 / m); % XJ is the model! makes it possible to determine each "likelihood" that XI was drawn from each of the models in XJ
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* Copyright 2009 B. Schauerte. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY B. SCHAUERTE ''AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL B. SCHAUERTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation
* are those of the authors and should not be interpreted as representing
* official policies, either expressed or implied, of B. Schauerte.
*/

/**
* chi_square_statistics_fast
*
* Fast C/C++ calculation of the chi-square statistics (compatible with pdist).
* (cf. "The Earth Movers' Distance as a Metric for Image Retrieval",
* Y. Rubner, C. Tomasi, L.J. Guibas, 2000)
*
* @author: B. Schauerte
* @date: 2009
* @url: http://cvhci.anthropomatik.kit.edu/~bschauer/
*/
#include "mex.h"

#define SQR(x) ((x)*(x))

void
mexFunction (int nlhs, mxArray* plhs[],
int nrhs, const mxArray* prhs[])
{
mwSize i = 0, j = 0; /* variables for for-loops */

/* Check number of input parameters */
if (nrhs != 2)
{
mexErrMsgTxt("Two inputs required.");
}
else
if (nlhs > 1)
{
mexErrMsgTxt("Wrong number of output arguments.");
}

/* Check type of input parameters */
if (!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1]))
mexErrMsgTxt("Input should be double.\n");

/* Input data */
const mxArray* XI = prhs[0];
const mxArray* XJ = prhs[1];
const double* XI_data = mxGetPr(XI);
const double* XJ_data = mxGetPr(XJ);
/* some helper variables */
const mwSize m = mxGetM(XJ); /* number of samples of p */
const mwSize p = mxGetN(XI); /* dimension of samples */
if (p != mxGetN(XJ))
mexErrMsgTxt("Dimension mismatch (1).\n");
if (1 != mxGetM(XI))
mexErrMsgTxt("Dimension mismatch. XI has to be an (1,n) vector.\n");
/* Output data */
mxArray* OUT = mxCreateNumericMatrix (m, 1, mxDOUBLE_CLASS, mxREAL);
plhs[0] = OUT;
double* out_data = mxGetPr(OUT);

for (i = 0; i < m; i++) /* initialize output array */
out_data[i] = 0;

for (j = 0; j < p; j++)
{
const double xi = XI_data[j];
for (i = 0; i < m; i++)
{
const double mean = (xi + *XJ_data++) / 2.0;
if (mean != 0)
out_data[i] += SQR(xi - mean) / mean;
}
}

return;
}
143 changes: 143 additions & 0 deletions external/base/utilities/histogram_distance/hist_dist_example.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
% An example of how to use the histogram distance functions for image
% matching.
%
% Please note that this is a demo to show case the usage of the histogram
% functions. But, in general, matching images solely based on their color
% histograms ist - imho - not the best idea, unless you have a really large
% image database.
%
% Some of the histogram distance functions have been used for outlier
% reduction when learning color term/name models from web images, see:
%
% [1] B. Schauerte, G. A. Fink, "Web-based Learning of Naturalized Color
% Models for Human-Machine Interaction". In Proceedings of the 12th
% International Conference on Digital Image Computing: Techniques and
% Applications (DICTA), IEEE, Sydney, Australia, December 1-3, 2010.
% [2] B. Schauerte, R. Stiefelhagen, "Learning Robust Color Name Models
% from Web Images". In Proceedings of the 21st International Conference
% on Pattern Recognition (ICPR), Tsukuba, Japan, November 11-15, 2012
%
% If you use and like this code, you are kindly requested to cite some of
% the work above.
%
% Anyway, I hope it saves you some work. Have fun with it ;)
%
% @author: B. Schauerte
% @date: 2012,2013
% @url: http://cvhci.anthropomatik.kit.edu/~bschauer/

%%
% Build the .cpp files, if necessary
if ~exist('chi_square_statistics_fast','file') && exist('./build.m')
build;
end

% Download some random sample images from the Google-512 dataset. For
% information about the dataset see:
%
% [1] B. Schauerte, G. A. Fink, "Web-based Learning of Naturalized Color
% Models for Human-Machine Interaction". In Proceedings of the 12th
% International Conference on Digital Image Computing: Techniques and
% Applications (DICTA), IEEE, Sydney, Australia, December 1-3, 2010.
% [2] B. Schauerte, R. Stiefelhagen, "Learning Robust Color Name Models
% from Web Images". In Proceedings of the 21st International Conference
% on Pattern Recognition (ICPR), Tsukuba, Japan, November 11-15, 2012

%colornames={'red','green','blue','yellow', ...
% 'pink','purple','brown','orange', ...
% 'black','grey','white'};
colornames={'red','green','blue','yellow'};
fendings={'jpeg','png','gif'};
tmp_foldername='google-512-samples';
n_samples = 100;

% download the images in a temporary folder
if ~exist(tmp_foldername,'dir'), mkdir(tmp_foldername); end % create temporary directory
filenames=cell(n_samples,1);
for i=1:n_samples
colorname=colornames{randi(numel(colornames))};
%colorname=colornames{1};
for j=1:numel(fendings)
url=sprintf('https://cvhci.anthropomatik.kit.edu/~bschauer/datasets/google-512/images-resized-128/%s+color/%d.%s',colorname,i,fendings{j});
filename=sprintf('%s_%d.%s',colorname,i,fendings{j});
[~,status] = urlwrite(url,fullfile(tmp_foldername,filename));
if status
filenames{i} = filename;
break;
end
end
end

%%
% We simply use all files that have already been downloaded
filenames=dir(fullfile(tmp_foldername,'*_*.*'));
filenames={filenames.name};
n_samples=numel(filenames);

%%
% calculate color image histograms
n_bins=4;
edges=(0:(n_bins-1))/n_bins;
histograms=zeros(n_samples,n_bins*n_bins*n_bins);
for i=1:n_samples
I=imread(fullfile(tmp_foldername,filenames{i}));
IR=imresize(I,[64 64]);
IR=im2double(IR);

[~,r_bins] = histc(reshape(IR(:,:,1),1,[]),edges); r_bins = r_bins + 1;
[~,g_bins] = histc(reshape(IR(:,:,1),1,[]),edges); g_bins = g_bins + 1;
[~,b_bins] = histc(reshape(IR(:,:,1),1,[]),edges); b_bins = b_bins + 1;

histogram=zeros(n_bins,n_bins,n_bins);
for j=1:numel(r_bins)
histogram(r_bins(j),g_bins(j),b_bins(j)) = histogram(r_bins(j),g_bins(j),b_bins(j)) + 1;
end
histograms(i,:) = reshape(histogram,1,[]) / sum(histogram(:)); % normalize, better for all probabilistic methods
end

%%
% match histograms and show best matching pairs
dist_func=@chi_square_statistics_fast;
% 1. You can use pdist to calculate the distances, iff the distance measure
% is symmetric
%D=squareform(pdist(histograms,dist_func)); % use pdist to calculate the distance for all image pairs
% 2. Use the following loop to calculate the distances, iff the measure is
% not symmetric
% D=zeros(size(histograms,1),size(histograms,1));
% for i=1:size(histograms,1)
% for j=1:size(histograms,1)
% D(i,j) = dist_func(histograms(i,:),histograms(j,:));
% end
% end
% 2. ... alternatively, use pdist2
D=pdist2(histograms,histograms,dist_func);

D(D == 0) = NaN;
n_show_samples=5; % number of samples for the illustration
figure('name','Random images (left) with their best (middle) and worst (right) match');
c = 1;
rand_indices=randperm(numel(filenames));
for i=1:n_show_samples
% image we want to match
I=imread(fullfile(tmp_foldername,filenames{rand_indices(i)}));
if numel(size(I)) > 3, I=I(:,:,1:3); end
subplot(n_show_samples,3,c); imshow(I); c = c + 1;

% best match
%[d,j]=min(D(rand_indices(i),:)); % if distances are not symmetric, then
% it might be useful to try the other order, see below, depending on the
% definition of the metric
[d,j]=min(D(:,rand_indices(i)));
I=imread(fullfile(tmp_foldername,filenames{j}));
if numel(size(I)) > 3, I=I(:,:,1:3); end
subplot(n_show_samples,3,c); imshow(I); title(sprintf('Dist: %.3f',d*100)); c = c + 1;

% worst match
%[d,j]=max(D(rand_indices(i),:)); % if distances are not symmetric, then
% it might be useful to try the other order, see below, depending on the
% definition of the metric
[d,j]=max(D(:,rand_indices(i)));
I=imread(fullfile(tmp_foldername,filenames{j}));
if numel(size(I)) > 3, I=I(:,:,1:3); end
subplot(n_show_samples,3,c); imshow(I); title(sprintf('Dist: %.3f',d*100)); c = c + 1;
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
function d=histogram_intersection(XI,XJ)
% Implementation of the histogram intersection distance to use with pdist
% (cf. "The Earth Movers' Distance as a Metric for Image Retrieval",
% Y. Rubner, C. Tomasi, L.J. Guibas, 2000)
%
% @author: B. Schauerte
% @date: 2009
% @url: http://cvhci.anthropomatik.kit.edu/~bschauer/

% Copyright 2009 B. Schauerte. All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are
% met:
%
% 1. Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
%
% 2. Redistributions in binary form must reproduce the above copyright
% notice, this list of conditions and the following disclaimer in
% the documentation and/or other materials provided with the
% distribution.
%
% THIS SOFTWARE IS PROVIDED BY B. SCHAUERTE ''AS IS'' AND ANY EXPRESS OR
% IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
% WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
% DISCLAIMED. IN NO EVENT SHALL B. SCHAUERTE OR CONTRIBUTORS BE LIABLE
% FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
% BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
% WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
% OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
% ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
%
% The views and conclusions contained in the software and documentation
% are those of the authors and should not be interpreted as representing
% official policies, either expressed or implied, of B. Schauerte.

m=size(XJ,1); % number of samples of p
p=size(XI,2); % dimension of samples

assert(p == size(XJ,2)); % equal dimensions
assert(size(XI,1) == 1); % pdist requires XI to be a single sample

d=zeros(m,1); % initialize output array

sxi=sum(XI);
for i=1:m
d(i,1) = 1 - (sum(min(XI, XJ(i,:))) / sxi);
end
Loading

0 comments on commit 9ab777e

Please sign in to comment.