PageRenderTime 61ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/matlab_tools/Converted/klrftrain.m

http://github.com/aludnam/MATLAB
Objective C | 388 lines | 385 code | 3 blank | 0 comment | 66 complexity | 8e585895643791cb3d5b7f86f76342e0 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. %klrftrain 'Calculate Weights for Localized Receptive Field Classifier (K1)'
  2. % This MatLab function was automatically generated by a converter (KhorosToMatLab) from the Khoros lrftrain.pane file
  3. %
  4. % Parameters:
  5. % InputFile: i1 'Input Image', required: 'input image'
  6. % InputFile: i2 'Cluster Center Image ', required: 'cluster center image'
  7. % InputFile: i3 'Cluster Variance Image', required: 'cluster variance image'
  8. % InputFile: i4 'Cluster Number Image ', required: 'cluster number image'
  9. % Integer: b 'Input Image Border Width', default: 0: 'Border Width'
  10. % Double: cv 'Convergence Value', default: 0.1: 'convergence value'
  11. % Double: meu 'Wt. Update Value ', default: 0.5: 'weight update value'
  12. % Integer: n 'Max Iterations ', default: 10000000: 'Maximum number of iterations'
  13. % Double: delta 'MIN Delta MSE ', default: 1e-09: 'Min delta MSE value'
  14. % Integer: d 'MSE Display Interval', default: 0: 'MSE display interval'
  15. % OutputFile: o 'Weight Image', required: 'weight image'
  16. % OutputFile: f 'Ascii Stats ', required: 'output file for training statistics'
  17. %
  18. % Example: [o, f] = klrftrain({i1, i2, i3, i4}, {'i1','';'i2','';'i3','';'i4','';'b',0;'cv',0.1;'meu',0.5;'n',10000000;'delta',1e-09;'d',0;'o','';'f',''})
  19. %
  20. % Khoros helpfile follows below:
  21. %
  22. % PROGRAM
  23. % lrftrain - Calculate Weights for Localized Receptive Field Classifier (K1)
  24. %
  25. % DESCRIPTION
  26. % .I lrftrain
  27. % trains on an image for the weights used with the Localized Receptive Field
  28. % classifier (see lrfclass). The Localized Receptive Field (LRF) is based on
  29. % a single layer of self-organizing, "localized receptive field" units,
  30. % followed by a single layer perceptron. The single layer of perceptron
  31. % units use the LMS or Adaline learning rule to adjust the weights.
  32. %
  33. % In contrast, multi-layer network models, such as the multi-layer
  34. % perceptron (MLP), use the back propagation learning model. The
  35. % back propagation learning
  36. % model is based on a gradient descent procedure, which tends to converge
  37. % at a very slow rate. Back propagation is also hampered by the fact that all
  38. % layers of weights in the network are computed by minimizing the error,
  39. % which is a function of the output. This tends to slow the learning,
  40. % since all weights in the network must be determined with each iteration.
  41. %
  42. % The LRF tries to overcome some of these problems by using a localized
  43. % representation of the input space to limit the number of units that
  44. % respond to a given input. This allows the LRF to converge at a faster
  45. % rate than similar types of neural network models, since only those
  46. % receptive fields which respond to an input need to be updated. Another
  47. % factor that allows the LRF to reduce the learning time, is that it
  48. % makes use of self-organized learning techniques, such as K-means, to train
  49. % the receptive field centers.
  50. % .SH "LRF network theory"
  51. %
  52. % The basic network model of the LRF consists of a two layer topology.
  53. % The first layer of "receptive field" nodes are trained using a clustering
  54. % algorithm, such as K-means, or some other algorithm which can determine
  55. % the receptive field centers. Each node in the first layer computes a
  56. % receptive field response function, which should approach zero as the
  57. % distance from the center of the receptive field is increased. The second
  58. % layer of the LRF model sums the weighted outputs of the first layer,
  59. % which produces the output or response of the network. A supervised
  60. % LMS rule is used to train the weights of the second layer nodes.
  61. %
  62. % The response function of the LRF network is formulated as follows:
  63. % .DS
  64. %
  65. % f(x) = SUM(Ti * Ri(x))
  66. %
  67. % where,
  68. %
  69. % Ri(x) = Q( ||x - xi|| / Wi )
  70. %
  71. % x - is a real valued vector in the input space,
  72. % Ri - is the ith receptive field response function,
  73. % Q - is a radially symmetric function with a single
  74. % maximum at the origin, decreasing to zero at
  75. % large radii,
  76. % xi - is the center of the ith receptive field,
  77. % Wi - is the width of the ith receptive field,
  78. % Ti - is the weight associated with each receptive field.
  79. %
  80. % .DE
  81. %
  82. % The receptive field response functions ( Ri(x) ), should be formulated
  83. % such that they decrease rapidly with increasing radii. This ensures that
  84. % the response functions provide highly localized representations of the
  85. % input space. The response function used here is modeled after the
  86. % Gaussian, and uses the trace of the covariance matrix to set the widths
  87. % of the receptive field centers.
  88. %
  89. % The weights for the output layer are found using the LMS learning rule.
  90. % The weights are adjusted at each iteration to minimize the total error,
  91. % which is based on the difference between the network output and the
  92. % desired result.
  93. %
  94. % The key element to the success of the LRF is the self-organizing
  95. % receptive fields. As noted above, the receptive field centers can be
  96. % determined from a statistical clustering algorithm such as K-means.
  97. % The inputs to the training phase of the LRF (ie. "lrftrain"), are the
  98. % outputs from "vkmeans" and the original image. Specifically, these are
  99. % the original input image, which may be a multi-band image containing all
  100. % of the feature bands used in the classification, and the "cluster number"
  101. % image, the "cluster center" image, and the "cluster variance" image.
  102. % The "cluster number" image specifies which vector belongs to what cluster,
  103. % the "cluster center" image specifies the cluster center locations in the
  104. % feature space, and the "cluster variance" image specifies the variances
  105. % of the data associated with each cluster center.
  106. %
  107. % Prior to using the LRF algorithm, it is necessary to run "vkmeans" on the
  108. % input training image to fix the cluster centers, followed by a supervised
  109. % classification of the clustered image, which assigns a desired class to
  110. % each cluster center. NOTE that the image resulting from the supervised
  111. % classification MUST be appended to the "cluster center" image before
  112. % running the LRF. This is necessary since it makes the appropriate
  113. % desired class assignments to the cluster centers for the training phase
  114. % of the LRF.
  115. %
  116. % .SH "Input Options"
  117. %
  118. %
  119. % "-d" 8
  120. % is an integer specifying the iteration interval used to print the
  121. % mean squared error (MSE) to the output statistics file. If this
  122. % value is left at zero (the default), only the MSE of the first iteration
  123. % is written to the file. Any other integer will cause the value of the
  124. % MSE to be written to the statistics file at the iteration interval
  125. % specified.
  126. %
  127. % "-cv" 8
  128. % is a float value that specifies the convergence value for the algorithm.
  129. % When the current MSE value reaches the specified convergence value, the
  130. % algorithm will terminate.
  131. %
  132. % "-meu" 8
  133. % is a float value that specifies the weight update parameter for the
  134. % learning algorithm. This value can be adjusted from 0 to 1. NOTE: this
  135. % parameter may have a significant affect on the rate of learning, and
  136. % it may have to be adjusted several times to get a feel for the optimum
  137. % learning rate.
  138. %
  139. % "-n" 8
  140. % is an integer that specifies the maximum number of iterations that the
  141. % algorithm will run before terminating. It is initially set to an
  142. % arbitrarily large number to allow the algorithm to complete the learning
  143. % phase.
  144. %
  145. % "-delta" 8
  146. % is a float value that specifies the minimum change in the MSE value from
  147. % one iteration to the next. This parameter may be used to terminate the
  148. % algorithm when the change in the MSE is zero or very small, but the MSE
  149. % has not yet reached the specified convergence value (-cv). This may
  150. % occur when the learning has reached a "plateau" or "bench" and is no
  151. % longer learning.
  152. %
  153. % "-b" 8
  154. % is an integer that specifies the border width, in pixels, encompassing
  155. % the desired region of the image to be classified. This region is ignored
  156. % during the classification process.
  157. %
  158. % Of the four input images to this routine, all but the "cluster number"
  159. % image must be of data storage type FLOAT. The "cluster number" image
  160. % should be of data storage type INTEGER. The output "weight" image is
  161. % written out as data storage type FLOAT. The output statistics file is
  162. % stored as an ASCII file.
  163. %
  164. % The statistics output file (-f) contains the following information:
  165. % .DS
  166. %
  167. % MSE at the first iteration
  168. % MSE at each specified interval (optional)
  169. % Total Number of Iterations
  170. % Final MSE at termination of the algorithm
  171. % Convergence Parameter used (-cv)
  172. % Weight Update Parameter used (-meu)
  173. % Minimum Delta MSE value (-delta)
  174. % Border Width (-b)
  175. % Number of Response Nodes in the network
  176. % Number of Output Classes in the network
  177. %
  178. % .DE
  179. %
  180. % The number of receptive field response nodes in the first layer of the
  181. % LRF is determined by the number of cluster centers in the "cluster center"
  182. % image. The number of output classes, and hence the number of output
  183. % nodes in the second (ie. last) layer, is determined by the number of
  184. % desired classes that was specified in the "supervised" classification
  185. % phase of the clustering. This information is contained in the last
  186. % band of the cluster center image. The number of weights in the network
  187. % is determined by the number of receptive field response nodes and the
  188. % number of output nodes. That is,
  189. % .DS
  190. %
  191. % #Wts = (#rf_response_nodes * #output_nodes) + #output_nodes
  192. %
  193. % .DE
  194. % .SH Advice
  195. %
  196. % As an initial step, try running the algorithm with a small number of
  197. % iterations (ex. -n = 500) to get a feel for how the MSE is behaving
  198. % (ie. decreasing rapidly, slowly, or increasing). Make sure you have the
  199. % MSE display parameter set to a reasonable interval (ex. -d = 10) so that
  200. % you can see how the MSE is behaving. These values will be written to
  201. % the statistics file (-f).
  202. %
  203. % After you get an idea of how the MSE is behaving, set the convergence
  204. % value (-cv) to a reasonable value. You may also try decreasing the
  205. % weight update parameter (-meu) to learn at a slower rate. Often times
  206. % a large weight update parameter will cause the learning to "oscillate"
  207. % and never reach a small MSE. You may also want to set the minimum
  208. % delta MSE parameter (-delta) to a small value, to ensure that the
  209. % algorithm terminates if the MSE levels off.
  210. %
  211. % This routine was written with the help of and ideas from
  212. % Dr. Don Hush, University of New Mexico, Dept. of EECE.
  213. %
  214. %
  215. %
  216. % EXAMPLES
  217. % lrftrain -i1 feature_image.xv -i2 cluster_centers -i3 variances -i4 cluster_numbers -o weight_image -f stats -d 10 -n 500
  218. %
  219. % This example illustrates a good initial step at training on an image.
  220. % The display interval is set to write out the MSE every 10 iterations.
  221. % The number of iterations is set to a small value, 500, to ensure that
  222. % the algorithm will stop in a reasonable amount of time to get a feel
  223. % for how the MSE is behaving.
  224. %
  225. % "SEE ALSO"
  226. % lrfclass(1)
  227. %
  228. % RESTRICTIONS
  229. % All input images except the "cluster number" image (-i4) MUST be of
  230. % data storage type FLOAT. The "cluster number" image (-i4) MUST be
  231. % of data storage type INTEGER. The output "weight" image (-o) is of
  232. % data storage type FLOAT.
  233. %
  234. % REFERENCES
  235. %
  236. % COPYRIGHT
  237. % Copyright (C) 1993 - 1997, Khoral Research, Inc. ("KRI") All rights reserved.
  238. %
  239. function varargout = klrftrain(varargin)
  240. if nargin ==0
  241. Inputs={};arglist={'',''};
  242. elseif nargin ==1
  243. Inputs=varargin{1};arglist={'',''};
  244. elseif nargin ==2
  245. Inputs=varargin{1}; arglist=varargin{2};
  246. else error('Usage: [out1,..] = klrftrain(Inputs,arglist).');
  247. end
  248. if size(arglist,2)~=2
  249. error('arglist must be of form {''ParameterTag1'',value1;''ParameterTag2'',value2}')
  250. end
  251. narglist={'i1', '__input';'i2', '__input';'i3', '__input';'i4', '__input';'b', 0;'cv', 0.1;'meu', 0.5;'n', 10000000;'delta', 1e-09;'d', 0;'o', '__output';'f', '__output'};
  252. maxval={0,0,0,0,100,2,1,2,2,1,0,0};
  253. minval={0,0,0,0,0,2,0,2,2,1,0,0};
  254. istoggle=[0,0,0,0,1,1,1,1,1,1,0,0];
  255. was_set=istoggle * 0;
  256. paramtype={'InputFile','InputFile','InputFile','InputFile','Integer','Double','Double','Integer','Double','Integer','OutputFile','OutputFile'};
  257. % identify the input arrays and assign them to the arguments as stated by the user
  258. if ~iscell(Inputs)
  259. Inputs = {Inputs};
  260. end
  261. NumReqOutputs=2; nextinput=1; nextoutput=1;
  262. for ii=1:size(arglist,1)
  263. wasmatched=0;
  264. for jj=1:size(narglist,1)
  265. if strcmp(arglist{ii,1},narglist{jj,1}) % a given argument was matched to the possible arguments
  266. wasmatched = 1;
  267. was_set(jj) = 1;
  268. if strcmp(narglist{jj,2}, '__input')
  269. if (nextinput > length(Inputs))
  270. error(['Input ' narglist{jj,1} ' has no corresponding input!']);
  271. end
  272. narglist{jj,2} = 'OK_in';
  273. nextinput = nextinput + 1;
  274. elseif strcmp(narglist{jj,2}, '__output')
  275. if (nextoutput > nargout)
  276. error(['Output nr. ' narglist{jj,1} ' is not present in the assignment list of outputs !']);
  277. end
  278. if (isempty(arglist{ii,2}))
  279. narglist{jj,2} = 'OK_out';
  280. else
  281. narglist{jj,2} = arglist{ii,2};
  282. end
  283. nextoutput = nextoutput + 1;
  284. if (minval{jj} == 0)
  285. NumReqOutputs = NumReqOutputs - 1;
  286. end
  287. elseif isstr(arglist{ii,2})
  288. narglist{jj,2} = arglist{ii,2};
  289. else
  290. if strcmp(paramtype{jj}, 'Integer') & (round(arglist{ii,2}) ~= arglist{ii,2})
  291. error(['Argument ' arglist{ii,1} ' is of integer type but non-integer number ' arglist{ii,2} ' was supplied']);
  292. end
  293. if (minval{jj} ~= 0 | maxval{jj} ~= 0)
  294. if (minval{jj} == 1 & maxval{jj} == 1 & arglist{ii,2} < 0)
  295. error(['Argument ' arglist{ii,1} ' must be bigger or equal to zero!']);
  296. elseif (minval{jj} == -1 & maxval{jj} == -1 & arglist{ii,2} > 0)
  297. error(['Argument ' arglist{ii,1} ' must be smaller or equal to zero!']);
  298. elseif (minval{jj} == 2 & maxval{jj} == 2 & arglist{ii,2} <= 0)
  299. error(['Argument ' arglist{ii,1} ' must be bigger than zero!']);
  300. elseif (minval{jj} == -2 & maxval{jj} == -2 & arglist{ii,2} >= 0)
  301. error(['Argument ' arglist{ii,1} ' must be smaller than zero!']);
  302. elseif (minval{jj} ~= maxval{jj} & arglist{ii,2} < minval{jj})
  303. error(['Argument ' arglist{ii,1} ' must be bigger than ' num2str(minval{jj})]);
  304. elseif (minval{jj} ~= maxval{jj} & arglist{ii,2} > maxval{jj})
  305. error(['Argument ' arglist{ii,1} ' must be smaller than ' num2str(maxval{jj})]);
  306. end
  307. end
  308. end
  309. if ~strcmp(narglist{jj,2},'OK_out') & ~strcmp(narglist{jj,2},'OK_in')
  310. narglist{jj,2} = arglist{ii,2};
  311. end
  312. end
  313. end
  314. if (wasmatched == 0 & ~strcmp(arglist{ii,1},''))
  315. error(['Argument ' arglist{ii,1} ' is not a valid argument for this function']);
  316. end
  317. end
  318. % match the remaining inputs/outputs to the unused arguments and test for missing required inputs
  319. for jj=1:size(narglist,1)
  320. if strcmp(paramtype{jj}, 'Toggle')
  321. if (narglist{jj,2} ==0)
  322. narglist{jj,1} = '';
  323. end;
  324. narglist{jj,2} = '';
  325. end;
  326. if ~strcmp(narglist{jj,2},'__input') && ~strcmp(narglist{jj,2},'__output') && istoggle(jj) && ~ was_set(jj)
  327. narglist{jj,1} = '';
  328. narglist{jj,2} = '';
  329. end;
  330. if strcmp(narglist{jj,2}, '__input')
  331. if (minval{jj} == 0) % meaning this input is required
  332. if (nextinput > size(Inputs))
  333. error(['Required input ' narglist{jj,1} ' has no corresponding input in the list!']);
  334. else
  335. narglist{jj,2} = 'OK_in';
  336. nextinput = nextinput + 1;
  337. end
  338. else % this is an optional input
  339. if (nextinput <= length(Inputs))
  340. narglist{jj,2} = 'OK_in';
  341. nextinput = nextinput + 1;
  342. else
  343. narglist{jj,1} = '';
  344. narglist{jj,2} = '';
  345. end;
  346. end;
  347. else
  348. if strcmp(narglist{jj,2}, '__output')
  349. if (minval{jj} == 0) % this is a required output
  350. if (nextoutput > nargout & nargout > 1)
  351. error(['Required output ' narglist{jj,1} ' is not stated in the assignment list!']);
  352. else
  353. narglist{jj,2} = 'OK_out';
  354. nextoutput = nextoutput + 1;
  355. NumReqOutputs = NumReqOutputs-1;
  356. end
  357. else % this is an optional output
  358. if (nargout - nextoutput >= NumReqOutputs)
  359. narglist{jj,2} = 'OK_out';
  360. nextoutput = nextoutput + 1;
  361. else
  362. narglist{jj,1} = '';
  363. narglist{jj,2} = '';
  364. end;
  365. end
  366. end
  367. end
  368. end
  369. if nargout
  370. varargout = cell(1,nargout);
  371. else
  372. varargout = cell(1,1);
  373. end
  374. global KhorosRoot
  375. if exist('KhorosRoot') && ~isempty(KhorosRoot)
  376. w=['"' KhorosRoot];
  377. else
  378. if ispc
  379. w='"C:\Program Files\dip\khorosBin\';
  380. else
  381. [s,w] = system('which cantata');
  382. w=['"' w(1:end-8)];
  383. end
  384. end
  385. [varargout{:}]=callKhoros([w 'lrftrain" '],Inputs,narglist);