cns/ 0000700 0162572 0000145 00000000000 11525661477 010521 5 ustar jmutch mit cns/cns_type.m 0000600 0162572 0000145 00000004346 11267523151 012521 0 ustar jmutch mit function varargout = cns_type(method, m, n, varargin)
% CNS_TYPE
% Click here for help.
%***********************************************************************************************************************
% Copyright (C) 2009 by Jim Mutch (www.jimmutch.com).
%
% This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
% License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later
% version.
%
% This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
% warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License along with this program. If not, see
% .
%***********************************************************************************************************************
if isstruct(m)
elseif ischar(m)
if ~ischar(n), error('invalid n'); end
m = struct('package', {m});
else
error('invalid m');
end
if isnumeric(n)
def = cns_def(m);
if n < 0
n = -n;
if n > def.gCount, error('invalid n'); end
d = def.layers{def.groups{n}.zs(1)};
else
d = def.layers{n};
end
type = d.type;
elseif ischar(n)
if isempty(n), error('invalid n'); end
def = cns_def(m.package);
type = n;
n = [];
d = def.type.(type);
else
error('invalid n');
end
if ~isfield(d.method, method), error('invalid method'); end
list = d.method.(method);
first = list{end};
rest = list(1 : end - 1);
global CNS_METHOD;
save_method = CNS_METHOD;
CNS_METHOD = struct;
CNS_METHOD.method = method;
CNS_METHOD.super = rest;
err = [];
try
if isempty(n)
[varargout{1 : nargout}] = feval([m.package '_cns_type_' first], method, m, varargin{:});
else
[varargout{1 : nargout}] = feval([m.package '_cns_type_' first], method, m, n, varargin{:});
end
catch
err = lasterror;
end
if isempty(save_method)
clear global CNS_METHOD;
else
CNS_METHOD = save_method;
end
if ~isempty(err), rethrow(err); end
return; cns/cns_getknownfields.m 0000600 0162572 0000145 00000004340 11267523151 014555 0 ustar jmutch mit function [k, u] = cns_getknownfields(m)
% CNS_GETKNOWNFIELDS
% Click here for help.
%***********************************************************************************************************************
% Copyright (C) 2009 by Jim Mutch (www.jimmutch.com).
%
% This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
% License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later
% version.
%
% This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
% warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License along with this program. If not, see
% .
%***********************************************************************************************************************
def = cns_def(m);
mm = m;
if isfield(m, 'layers'), mm = rmfield(mm, 'layers'); end
if isfield(m, 'groups'), mm = rmfield(mm, 'groups'); end
[k, u] = Separate(mm, cns_reservednames(true), def, []);
for z = 1 : numel(def.layers)
[k.layers{z}, u.layers{z}] = Separate(m.layers{z}, cns_reservednames(false), def.layers{z}, false);
end
for g = 1 : def.gCount
z = def.groups{g}.zs(1);
[k.groups{g}, u.groups{g}] = Separate(m.groups{g}, cns_reservednames(false), def.layers{z}, true);
end
return;
%***********************************************************************************************************************
function [k, u] = Separate(m, reserved, d, group)
if isequal(group, false) && d.auto, group = []; end
known = reserved;
for i = 1 : numel(d.syms)
name = d.syms{i};
if d.sym.(name).field && (isempty(group) || (d.sym.(name).group == group))
known{end + 1} = name;
end
end
names = fieldnames(m);
k = struct;
for i = 1 : numel(known)
if ismember(known{i}, names)
k.(known{i}) = m.(known{i});
end
end
u = struct;
for i = 1 : numel(names)
if ~ismember(names{i}, known)
u.(names{i}) = m.(names{i});
end
end
return; cns/demo/ 0000700 0162572 0000145 00000000000 11525661475 011443 5 ustar jmutch mit cns/demo/demopkg_cns_type_base.m 0000600 0162572 0000145 00000001504 11350624306 016133 0 ustar jmutch mit function varargout = demopkg_cns_type_base(method, varargin)
[varargout{1 : nargout}] = feval(['method_' method], varargin{:});
return;
%***********************************************************************************************************************
function p = method_props
p.abstract = true;
p.methods = {'initlayer'};
p.dims = {1 1 2};
p.dparts = {2 1 1};
p.dnames = {'f' 'y' 'x'};
p.dmap = [false true true];
return;
%***********************************************************************************************************************
function d = method_fields
d.pz = {'lz', 'type', 'base'};
d.val = {'cv', 'cache', 'dflt', 0};
return;
%***********************************************************************************************************************
function m = method_initlayer(m, z)
return; cns/demo/demopkg_cns_type_input.h 0000600 0162572 0000145 00000000006 11467652337 016365 0 ustar jmutch mit #NULL
cns/demo/demopkg_cns.m 0000600 0162572 0000145 00000001404 11244067310 014075 0 ustar jmutch mit function varargout = demopkg_cns(method, varargin)
[varargout{1 : nargout}] = feval(['method_' method], varargin{:});
return;
%***********************************************************************************************************************
function p = method_props
p.methods = {'init'};
return;
%***********************************************************************************************************************
function d = method_fields
d = struct;
return;
%***********************************************************************************************************************
function m = method_init(m)
m = cns_setstepnos(m, 'field', 'pz');
for z = 1 : numel(m.layers)
m = cns_type('initlayer', m, z);
end
m.independent = true;
return; cns/demo/demopkg_cns_type_input.m 0000600 0162572 0000145 00000000741 11467652337 016400 0 ustar jmutch mit function varargout = demopkg_cns_type_input(method, varargin)
[varargout{1 : nargout}] = feval(['method_' method], varargin{:});
return;
%***********************************************************************************************************************
function p = method_props
p = struct;
return;
%***********************************************************************************************************************
function d = method_fields
d = struct;
return; cns/demo/demopkg_cns_type_filter.h 0000600 0162572 0000145 00000001257 11520702703 016503 0 ustar jmutch mit #BLOCKSIZE 16 8
// Find coordinates of input cells in the previous layer.
int y1, y2, x1, x2;
FIND_LAYER_Y_NEAREST(PZ, FVALS_Y_SIZE, y1, y2);
FIND_LAYER_X_NEAREST(PZ, FVALS_X_SIZE, x1, x2);
// Iterate over input cells.
float res = 0.0f;
float len = 0.0f;
for (int j = 0, x = x1; x <= x2; j++, x++) {
for (int i = 0, y = y1; y <= y2; i++, y++) {
// Read value of input cell.
float v = READ_LAYER_VAL(PZ, 0, y, x);
// Read corresponding filter value.
float w = READ_FVALS(i, j, THIS_F);
res += w * v;
len += v * v;
}
}
res = fabsf(res);
if (len > 0.0f) res /= sqrtf(len);
// Write out value of this cell.
WRITE_VAL(res);
cns/demo/demopkg_run.m 0000600 0162572 0000145 00000004206 11435603726 014132 0 ustar jmutch mit fprintf('Defining a network model....\n');
m = struct;
m.package = 'demopkg';
m.layers{1}.type = 'input';
m.layers{1}.pz = 0;
m.layers{1}.size{1} = 1;
m = cns_mapdim(m, 1, 'y', 'pixels', 256);
m = cns_mapdim(m, 1, 'x', 'pixels', 256);
m.layers{2}.type = 'scale';
m.layers{2}.pz = 1;
m.layers{2}.size{1} = 1;
m = cns_mapdim(m, 2, 'y', 'scaledpixels', 256, 2);
m = cns_mapdim(m, 2, 'x', 'scaledpixels', 256, 2);
m.layers{3}.type = 'filter';
m.layers{3}.pz = 2;
m.layers{3}.rfCount = 11;
m.layers{3}.fParams = {'gabor', 0.3, 5.6410, 4.5128};
m.layers{3}.size{1} = 4;
m = cns_mapdim(m, 3, 'y', 'int', 2, 11, 1);
m = cns_mapdim(m, 3, 'x', 'int', 2, 11, 1);
%***********************************************************************************************************************
% uncomment the following line if you don't have a GPU:
% cns('platform', 'cpu');
fprintf('Building the model (platform: %s)....\n', upper(cns('platform')));
cns('init', m);
%***********************************************************************************************************************
fprintf('Reading a test image and loading it into the model....\n');
in = imread(fullfile(fileparts(mfilename('fullpath')), 'ketch_0010.jpg'));
in = single(in) / single(intmax(class(in)));
in = shiftdim(in, -1);
cns('set', 1, 'val', in);
%***********************************************************************************************************************
fprintf('Running the model and retrieving result....\n');
tic;
cns('run');
res = cns('get', 3, 'val');
toc;
%***********************************************************************************************************************
fprintf('Plotting result....\n');
figure;
subplot(2, 3, 1); imshow(shiftdim(in, 1));
subplot(2, 3, 2); imagesc(shiftdim(res(1, :, :), 1));
subplot(2, 3, 3); imagesc(shiftdim(res(2, :, :), 1));
subplot(2, 3, 5); imagesc(shiftdim(res(3, :, :), 1));
subplot(2, 3, 6); imagesc(shiftdim(res(4, :, :), 1));
%***********************************************************************************************************************
fprintf('Releasing model resources....\n');
cns('done'); cns/demo/demopkg_cns_type_scale.h 0000600 0162572 0000145 00000004557 11520702703 016313 0 ustar jmutch mit // Compute kernel for a "scale" cell. A port of MATLAB's imresize using bicubic interpolation.
// Note: this kernel is separable and could therefore be rewritten as two stages.
#BLOCKSIZE 16 8
// Compute scaling factors.
float pySpace = LAYER_Y_SPACE(PZ);
float pxSpace = LAYER_X_SPACE(PZ);
float yFactor = pySpace / Y_SPACE; // If less than 1 we are shrinking.
float xFactor = pxSpace / X_SPACE;
// Compute receptive field size.
int yWidth = (yFactor < 1) ? (int)ceilf(4.0f / yFactor) : 4;
int xWidth = (xFactor < 1) ? (int)ceilf(4.0f / xFactor) : 4;
// Find receptive field index ranges.
int vy1, vy2, vx1, vx2; // Clipped to fall within the image ("valid").
int y1, y2, x1, x2; // Unclipped, may go off image edges.
FIND_LAYER_Y_NEAREST(PZ, yWidth, vy1, vy2, y1, y2);
FIND_LAYER_X_NEAREST(PZ, xWidth, vx1, vx2, x1, x2);
// Determine internal filter coordinates.
float yFiltSpace = (yFactor < 1) ? yFactor : 1.0f;
float xFiltSpace = (xFactor < 1) ? xFactor : 1.0f;
float yFiltStart = (LAYER_Y_CENTER(PZ, y1) - THIS_Y_CENTER) / pySpace * yFiltSpace;
float xFiltStart = (LAYER_X_CENTER(PZ, x1) - THIS_X_CENTER) / pxSpace * xFiltSpace;
VAL_HANDLE h = GET_LAYER_VAL_HANDLE(PZ);
float num = 0.0f; // Result numerator.
float den = 0.0f; // Result denominator.
float xFilt = xFiltStart;
for (int x = x1; x <= x2; x++, xFilt += xFiltSpace) {
// Compute x component of filter.
float fx = fabsf(xFilt);
float wx;
if (fx <= 1.0f) {
wx = (1.5f * fx - 2.5f) * fx * fx + 1.0f;
} else if (fx <= 2.0f) {
wx = ((-0.5f * fx + 2.5f) * fx - 4.0f) * fx + 2.0f;
} else {
wx = 0.0f;
}
// Determine x index of image pixel to read.
int vx = (x < vx1) ? vx1 : (x > vx2) ? vx2 : x;
float yFilt = yFiltStart;
for (int y = y1; y <= y2; y++, yFilt += yFiltSpace) {
// Compute y component of filter.
float fy = fabsf(yFilt);
float wy;
if (fy <= 1.0f) {
wy = (1.5f * fy - 2.5f) * fy * fy + 1.0f;
} else if (fy <= 2.0f) {
wy = ((-0.5f * fy + 2.5f) * fy - 4.0f) * fy + 2.0f;
} else {
wy = 0.0f;
}
// Determine y index of image pixel to read.
int vy = (y < vy1) ? vy1 : (y > vy2) ? vy2 : y;
num += wy * wx * READ_VAL_HANDLE(h, 0, vy, vx);
den += wy * wx;
}
}
// Clip output to [0 1].
WRITE_VAL(fminf(fmaxf(num / den, 0.0f), 1.0f));
cns/demo/demopkg_cns_type_filter.m 0000600 0162572 0000145 00000003604 11467652337 016527 0 ustar jmutch mit function varargout = demopkg_cns_type_filter(method, varargin)
[varargout{1 : nargout}] = feval(['method_' method], varargin{:});
return;
%***********************************************************************************************************************
function p = method_props
p.methods = {'initlayer'};
return;
%***********************************************************************************************************************
function d = method_fields
d.fVals = {'ga', 'private', 'cache', 'dims', {1 2 1}, 'dparts', {1 1 2}, 'dnames', {'y' 'x' 'f'}};
return;
%***********************************************************************************************************************
function m = method_initlayer(m, z)
c = m.layers{z};
switch c.fParams{1}
case 'gabor', c.fVals = GenerateGabor(c.rfCount, c.size{1}, c.fParams{2 : end});
otherwise , error('invalid filter type');
end
for f = 1 : c.size{1}
a = c.fVals(:, :, f);
a = a - mean(a(:));
a = a / sqrt(sum(a(:) .* a(:)));
c.fVals(:, :, f) = a;
end
m.layers{z} = c;
return;
%***********************************************************************************************************************
function fVals = GenerateGabor(rfCount, fCount, aspectRatio, lambda, sigma)
fVals = zeros(rfCount, rfCount, fCount);
points = (1 : rfCount) - ((1 + rfCount) / 2);
for f = 1 : fCount
theta = (f - 1) / fCount * pi;
for j = 1 : rfCount
for i = 1 : rfCount
x = points(j) * cos(theta) - points(i) * sin(theta);
y = points(j) * sin(theta) + points(i) * cos(theta);
if sqrt(x * x + y * y) <= rfCount / 2
e = exp(-(x * x + aspectRatio * aspectRatio * y * y) / (2 * sigma * sigma));
e = e * cos(2 * pi * x / lambda);
else
e = 0;
end
fVals(i, j, f) = e;
end
end
end
return; cns/demo/demopkg_cns_type_scale.m 0000600 0162572 0000145 00000000741 11467652337 016330 0 ustar jmutch mit function varargout = demopkg_cns_type_scale(method, varargin)
[varargout{1 : nargout}] = feval(['method_' method], varargin{:});
return;
%***********************************************************************************************************************
function p = method_props
p = struct;
return;
%***********************************************************************************************************************
function d = method_fields
d = struct;
return; cns/demo/ketch_0010.jpg 0000600 0162572 0000145 00000016233 11244067310 013674 0 ustar jmutch mit JFIF C
$.' ",#(7),01444'9=82<.342
} !1AQa"q2#BR$3br
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz ? ~j})rTCOۃHWS* pԬ9֦Җ⦪f"a9H#5:dM}qMWq
>h6} ]_c_YNcXL-5 O
Xw]V\Tڝ8d U<'9?J|@w1jRS1y>.rK;y>JzT*U^iҡƧ/|fF#zm< A++ݘP1JyIr*HRЊ\fFr1ҙleS֤^O$|f43ҕPӁM jTQݧW"#4k7T2W|4ZA˸DSt^(Mgט գ 棸*q5"E&|~lj}M^do1gڳ MI#E<(M+AQl}HS'RmBOJq ,G`RZcjpکKCe2O"BX+c5AU!wFp
^^iG9'`i[,_֤^ 4qޞZzL/OY
ckS$Ib |sO4(٩1ޢGZGؘJqf$Ve̘=kOV_neeȍ^L| K ꨵH5`r?\EG68= 2`"A Jʧ stjN"]
-)QA4ҐTZf>wcښSm"-ܟ|2*ouBÂ Odν%*l `W⼉!=W*j@^>$t`cj"zݰ4E>́=Pq4~RSJ 8|TXXTuqR槞AsKڛ %i Z\Zb\qJ/#XTW-ƸbA=F3+%+pȸtt+RQ^W(#<~ m%3!9YiP$zԑ'S0w*89Ո<ԀdR?A*SOc)Awƻ#4IU~bH|UfyT&sW)g0G>|{W&oF0HFF_ּAj!F|YGAYj:%݀{R.=J
˸baw3.0MѓڜÞ<