00001 /*!@file VFAT/NPclassify2.H Test the nonparametric classifier 00002 */ 00003 00004 // //////////////////////////////////////////////////////////////////// // 00005 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00006 // University of Southern California (USC) and the iLab at USC. // 00007 // See http://iLab.usc.edu for information about this project. // 00008 // //////////////////////////////////////////////////////////////////// // 00009 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00010 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00011 // in Visual Environments, and Applications'' by Christof Koch and // 00012 // Laurent Itti, California Institute of Technology, 2001 (patent // 00013 // pending; application number 09/912,225 filed July 23, 2001; see // 00014 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00015 // //////////////////////////////////////////////////////////////////// // 00016 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00017 // // 00018 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00019 // redistribute it and/or modify it under the terms of the GNU General // 00020 // Public License as published by the Free Software Foundation; either // 00021 // version 2 of the License, or (at your option) any later version. // 00022 // // 00023 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00024 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00025 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00026 // PURPOSE. See the GNU General Public License for more details. // 00027 // // 00028 // You should have received a copy of the GNU General Public License // 00029 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00030 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00031 // Boston, MA 02111-1307 USA. // 00032 // //////////////////////////////////////////////////////////////////// // 00033 // 00034 // Primary maintainer for this file: T Nathan Mundhenk <mundhenk@usc.edu> 00035 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/VFAT/NPclassify2.H $ 00036 // $Id: NPclassify2.H 6182 2006-01-31 18:41:41Z rjpeters $ 00037 // 00038 00039 // ############################################################ 00040 // ############################################################ 00041 // ##### --- VFAT --- 00042 // ##### Vision Feature Analysis Tool: 00043 // ##### T. Nathan Mundhenk nathan@mundhenk.com 00044 // ##### Laurent Itt itti@pollux.usc.edu 00045 // ##### 00046 // ############################################################ 00047 // ############################################################ 00048 00049 #ifndef NPCLASSIFY2_H_DEFINED 00050 #define NPCLASSIFY2_H_DEFINED 00051 00052 #include <vector> 00053 #include "Util/readConfig.H" 00054 #include "Image/All.H" 00055 #include "Image/Pixels.H" 00056 #include "Image/Point2D.H" 00057 #include "VFAT/covEstimate.H" 00058 00059 //! Non parametric classifier version 2.0 00060 /*! When called this class will attempt to classify all data provided 00061 into an unknown number of classed. Thus, NPclassify is ment to 00062 work the same as other classifiers such as EM and Kmeans 00063 but without strong priors about class number or size. 00064 Soft parameters are used and are either provied at command line 00065 or in a configuration file 00066 */ 00067 00068 template <class FLOAT> class NPclassify2 00069 { 00070 private: 00071 // Adjustable parameters from config file 00072 // see config file for details 00073 FLOAT NP_Con1, NP_Con2, NP_Con3; 00074 FLOAT NP_DWeight1, NP_DWeight2, NP_CWeight1, NP_CWeight2; 00075 FLOAT NP_IDWeight1, NP_IDWeight2, NP_ICWeight1, NP_ICWeight2; 00076 FLOAT NP_DenWeight1, NP_DenWeight2; 00077 FLOAT NP_preDenWeight1, NP_preDenWeight2; 00078 FLOAT NP_entDWeight1, NP_entDWeight2, NP_entCWeight1, NP_entCWeight2; 00079 FLOAT NP_entIDWeight1, NP_entIDWeight2, NP_entICWeight1, NP_entICWeight2; 00080 FLOAT NP_entDenWeight1, NP_entDenWeight2; 00081 FLOAT NP_entpreDenWeight1, NP_entpreDenWeight2; 00082 FLOAT NP_trainChildWeight; 00083 int NP_defaultSize; 00084 int NP_hardClassSize, NP_hardLinkSize; 00085 FLOAT NP_enthardClassSize, NP_enthardLinkSize; 00086 bool NP_CLS; 00087 bool NP_useIter; 00088 bool NP_useBias; 00089 00090 FLOAT NP_polyDensObjectCut1,NP_polyDensObjectCut2,NP_polyDensObjectCut3; 00091 FLOAT NP_polySpaceChildCut1,NP_polySpaceChildCut2,NP_polySpaceChildCut3; 00092 FLOAT NP_fZERO; 00093 int NP_ZERO; 00094 00095 // Internal Variables used by NPclassify 00096 //! Holds space of samples, is the input in essense 00097 typename std::vector<std::vector<FLOAT> > *NP_Space; 00098 //! The distance between any two nodes in space 00099 typename std::vector<std::vector<FLOAT> > NP_Dis; 00100 //! Used with k-means 00101 typename std::vector<std::vector<FLOAT> > NP_Kmean; 00102 //! Pointer reverse map to NP_Dis for use with iterators 00103 typename std::vector<std::vector<FLOAT*> > NP_reverseDisMap; 00104 00105 //! holds the measure from metaclassify for a train 00106 std::vector<FLOAT> NP_trainMeasure; 00107 //! Holds the distance from a node to its parent node 00108 std::vector<FLOAT> NP_distance; 00109 //! the density of this node 00110 std::vector<FLOAT> NP_density; 00111 //! general measure of entropy for samples 00112 std::vector<FLOAT> NP_entropy; 00113 //! this is the mean distance within a class 00114 std::vector<FLOAT> NP_meanInterDistance; 00115 //! The standard deviation for meanInterDistance 00116 std::vector<FLOAT> NP_stdInterDistance; 00117 //! Mean number of decendants per node inter class 00118 std::vector<FLOAT> NP_meanInterChild; 00119 //! the standard deviation for meanInterClass 00120 std::vector<FLOAT> NP_stdInterChild; 00121 //! Mean density for nodes inter class 00122 std::vector<FLOAT> NP_meanInterDensity; 00123 //! Sum density for nodes inter class (used in entropy computation) 00124 std::vector<FLOAT> NP_sumInterDensity; 00125 //! Standard deviation for meanInterDensity 00126 std::vector<FLOAT> NP_stdInterDensity; 00127 //! Entropy measure for intercalss 00128 std::vector<FLOAT> NP_interEntropy; 00129 //! Final mean density per class 00130 std::vector<FLOAT> NP_meanClassDensity; 00131 //! Final Standard deviation per class 00132 std::vector<FLOAT> NP_stdClassDensity; 00133 00134 00135 00136 // POINTER MAPS to KEY NODE reference 00137 00138 //! Holds class membership for nodes 00139 std::vector<std::vector<int*> > NP_Class; 00140 //! Holds a list of all children for a node 00141 std::vector<std::vector<int*> > NP_childMap; 00142 //! Holds a list of all of nodes decendants 00143 std::vector<std::vector<int*> > NP_decend; 00144 //! Holds a list of all my predecessors 00145 std::vector<std::vector<int*> > NP_pred; 00146 //! List of all nodes I am master of 00147 std::vector<std::vector<int*> > NP_masterIndex; 00148 //! list of all nodes which have a lower density 00149 std::vector<std::vector<int*> > NP_lessDensity; 00150 00151 //! the list of covariance features for biasing densities 00152 typename std::vector<covHolder<double> > *NP_covHolder; 00153 00154 //! holds the node ID of each stem for each class 00155 std::vector<int*> NP_stem; 00156 //! holds parent for a sample in cluster space 00157 std::vector<int*> NP_parent; 00158 //! lists who a nodes master is 00159 std::vector<int*> NP_master; 00160 //! lists the index to the class I am in (this is the count value for stem) 00161 std::vector<int*> NP_classIndex; 00162 //! reverse index to the stem for which I am a master 00163 std::vector<int*> NP_revStemVal; 00164 00165 //! lists the key number for any given node 00166 std::vector<int> NP_keyVal; 00167 //! Returns the number of samples in each class 00168 std::vector<int> NP_classSize; 00169 //! Holds the total number of decendants for a node 00170 std::vector<int> NP_decCount; 00171 //! Tells which links would have been ideal in the meta-classifier 00172 std::vector<int> NP_idealLinks; 00173 //! number of children for a given node 00174 std::vector<int> NP_children; 00175 //! tells how many predecessors I have 00176 std::vector<int> NP_predCount; 00177 //! counts how many nodes I am master of 00178 std::vector<int> NP_masterIndexCount; 00179 //! Holds the total number of decendants for a node in this class 00180 std::vector<int> NP_subDecCount; 00181 //! Tracks how many nodes have a lower density than me 00182 std::vector<int> NP_lessDensityCount; 00183 00184 //! Returns if the sample has a low density in the map 00185 std::vector<bool> NP_lowDensity; 00186 //! tells if this node has a stem attached to it 00187 std::vector<bool> NP_revStem; 00188 //! Tells if this node has been selected or not 00189 std::vector<bool> NP_selected; 00190 00191 //! returns the max density value from metaclassify 00192 FLOAT NP_maxDensity; 00193 //! the total entropy in this system as measured against density 00194 FLOAT NP_totalEntropy; 00195 //! The distance at which to cut a link between two nodes 00196 FLOAT NP_distanceCut; 00197 //! the cut off point for children 00198 FLOAT NP_childCut; 00199 //! the total mean distance between nodes in the set 00200 FLOAT NP_meanDistance; 00201 //! the total standard deviation between all nodes in the set 00202 FLOAT NP_stdDistance; 00203 //! total mean density of space 00204 FLOAT NP_meanDensity; 00205 //! total sum density of space (used in entropy computation) 00206 FLOAT NP_sumDensity; 00207 //! total standard deviation of all node density 00208 FLOAT NP_stdDensity; 00209 //! this is the mean number of children for nodes 00210 FLOAT NP_meanChild; 00211 //! this is the standard deviation for children 00212 FLOAT NP_stdChildren; 00213 //! The mean number of decendents per node 00214 FLOAT NP_meanDecCount; 00215 //! the standard deviation for meanDecCount 00216 FLOAT NP_stdDecCount; 00217 //! the weight to give to biasing if used 00218 FLOAT NP_biasWeight; 00219 00220 //! Simple holder for space size, for faster access 00221 int NP_spaceSize; 00222 //! dimensions in this space 00223 int NP_dimSize; 00224 //! Simple holder for space size, for faster access (unsigned) 00225 unsigned int NP_uspaceSize; 00226 //! dimensions in this space (unsigned) 00227 unsigned int NP_udimSize; 00228 //! holds the number of root nodes (stems) for each class 00229 int NP_stems; 00230 //! Holds the number of roots 00231 int NP_roots; 00232 //! minumum distance between two nodes 00233 int NP_minDist; 00234 //! minimum number of decentants for a node 00235 int NP_minChild; 00236 //! counts how many nodes have been removed due to low density 00237 int NP_lowDensCount; 00238 //! new sample size minus the low density guys 00239 int NP_newSampleSize; 00240 //! size of the covHolder in NP_covHolder 00241 int NP_covHolderSize; 00242 00243 // ***************************************** 00244 // PRIVATE METHODS 00245 // ***************************************** 00246 //! convolve your space (first method) void NPconvolveSpace(); 00247 //! convolve your space, use Euclidian distance (revision 1) 00248 void NPconvolveSpace(); 00249 //! convolve your space (revision 1) when adding a single point to space 00250 void NPconvolveSpace(long item); 00251 //! bias the densities of clustering with prior densities 00252 void NPbiasConvolveSpace(); 00253 //! find the basic mean for density of nodes 00254 void NPbasicMeanDensity(); 00255 //! compute entropy using density as a rough estimate as P 00256 void NPcomputeEntropy(); 00257 //! link your points together 00258 void NPlinkSpace(); 00259 //! link your points together incrementally 00260 void NPlinkSpaceIncr(); 00261 //! link your points together called from NPlinkSpace, used for single runs 00262 void NPdoLinkSpace(); 00263 //! link your points together called from NPlinkSpace, used for incremental 00264 void NPdoLinkSpace(long item); 00265 //! map space. Who is below me 00266 void NPmapSpace(); 00267 //! run your analisys on the space group based upon group weights, on length 00268 void NPanalizeSpace(); 00269 //! cut lengths, link groups, traverse tree 00270 void NPevolveSpace(); 00271 //! find interclass variance 00272 void NPanalizeInterSpace(); 00273 //! evolve space again using interclass variance 00274 void NPevolveInterSpace(); 00275 //! computer the final class densities 00276 void NPanalizeClassDensity(); 00277 //! computes the master lists of all points in classes 00278 void NPcomputeMasters(); 00279 //! resets space size parameters 00280 void NPparamSpace(int samples, int dim); 00281 //! create a reverse mapping for NP_Dis for use with iterators 00282 void NPreverseMap(); 00283 00284 public: 00285 int NP_doLinkMap,NP_doDensityMap,NP_doClassMap,NP_usePolySet; 00286 // ***************************************** 00287 // PUBLIC METHODS 00288 // ***************************************** 00289 //! create object with readConfig as defaults 00290 NPclassify2(readConfig &settings, readConfig &polySet, 00291 bool commandLineSettings = false); 00292 NPclassify2(); 00293 ~NPclassify2(); 00294 //! input command line settings for some variables insted of using .conf 00295 void NPsetup(readConfig &settings, readConfig &polySet, 00296 bool commandLineSettings = false); 00297 00298 void NPinputCommandLineSettings(FLOAT inVals[31]); 00299 //! resizes all vectors if more space is added 00300 void NPresizeSpace(int samples, int dim); 00301 00302 //! add a point to the space 00303 void NPaddPoint(std::vector<int> point); 00304 //! add whole space (concatinates if data is there) 00305 /*! 00306 @param space This is a set of feature vectors as a vector of vectors 00307 @param sSize This is how many elements you are adding leave blank if \ 00308 the number of elements equals the vector size 00309 */ 00310 void NPaddSpace(typename std::vector<std::vector<FLOAT> > &space); 00311 //! echo the space you are working in 00312 void NPechoSpace(); 00313 //! reset the dataset to empty (does not resize vectors) 00314 void NPresetSpace(int features, int dims); 00315 //! reset the dataset to empty (does not resize vectors) 00316 void NPresetSpace(); 00317 //! bias the convolution of space with prior densities 00318 void NPsetConvolveBias(std::vector<covHolder<double> > *ch, 00319 int listSize, 00320 FLOAT bias); 00321 //! classify space based upon rules given, for the first time 00322 void NPclassifySpaceNew(bool doBias); 00323 //! ulternativly use Kmeans to classify space, return error 00324 /*! 00325 @param K the number of classes to find 00326 @param minDiff the minimum difference between errors on which to stop 00327 @param stopIter the maximum number of iterations to run 00328 */ 00329 FLOAT NPclassifySpaceKmeans(unsigned int *K, FLOAT *minDiff, 00330 unsigned int *stopIter); 00331 00332 //! classify space in subsequent iterations 00333 void NPclassifySpacePartial(); 00334 //! return how many stems there are (how many classes) 00335 int NPgetStemNumber(); 00336 //! return how many stems there are (how many classes) 00337 /*! account for classes that are too small */ 00338 int NPgetStemNumberEdit(); 00339 //! return the max density value 00340 FLOAT NPgetMaxDensity(); 00341 //! return the mean density 00342 FLOAT NPgetMeanDensity(); 00343 //! get the Standard Deviation of density 00344 FLOAT NPgetStdDensity(); 00345 //! return the Mean Density per inter group 00346 typename std::vector<FLOAT>* NPgetMeanClassDensity(); 00347 //! return the Standard Deviation per inter group 00348 typename std::vector<FLOAT>* NPgetStdClassDensity(); 00349 //! get a measure of entropy as measured with density as P 00350 FLOAT NPgetEntropy(); 00351 //! return if this point is a low density point 00352 bool NPisLowDensity(int item); 00353 //! returns if this item is a stem 00354 bool NPisStem(int item); 00355 //! return density weight map (linear) 00356 typename std::vector<FLOAT> NPgetDensity(); 00357 //! return density weight map (linear) 00358 typename std::vector<FLOAT>* NPgetDensityPtr(); 00359 //! Returns a vector of all class parents 00360 std::vector<int*> NPgetStems(); 00361 //! Return how many points/vectors are in class n (1... n) 00362 int NPgetClassSize(int _Class); 00363 //! return what is set as the minimum class size 00364 int NPgetMinClassSize(); 00365 //! Return the who map for the class list as a FLOAT nested vector 00366 std::vector<std::vector<int*> >* NPgetClass(); 00367 //! Return class n as a vector (Set of all points/vectors in this class) 00368 std::vector<int*> NPgetClass(int _Class); 00369 //! Return item n from class m 00370 int NPgetClass(int _Class, int item); 00371 //! Return feature m of vector n 00372 FLOAT NPgetFeature(int m_feature_index, int n_vector_index); 00373 //! returns parents 00374 std::vector<int*> NPgetParents(); 00375 //! Returns a vector of all decendants (children, grandchildren) or all nodes 00376 std::vector<std::vector<int*> > NPgetChildren(); 00377 //! calculate and return simple bounding boxes for classes 00378 std::vector<std::vector<int> > NPgetBoundingBoxes(bool ignore); 00379 //! draw the bounding boxes to an image for you from NPgetBoundingBoxes 00380 void NPdrawBoundingBoxes(std::vector<std::vector<int> > *BB, 00381 Image<PixRGB<FLOAT> > *ibox, 00382 int offsetX, int offsetY, 00383 PixRGB<FLOAT> pix, bool ignorMin); 00384 //! analyze classification stratagy. create meta classifier 00385 void NPmetaClassify(int objects); 00386 //! dump stats to files about the links for analysis purposes 00387 void NPdumpLinkInfo(std::string fileName); 00388 //! gaussian kernel 00389 inline FLOAT NPgaussKernel(FLOAT x, FLOAT mu, FLOAT sig) const; 00390 //! gaussian kernel with mu = 0 and sig = 1.0 00391 inline FLOAT NPgaussKernel(FLOAT z) const; 00392 //! sigmoid kernel 00393 inline FLOAT NPsigmoidKernel(FLOAT beta, FLOAT v) const; 00394 00395 }; 00396 00397 #endif 00398 00399 00400 // ###################################################################### 00401 /* So things look consistent in everyone's emacs... */ 00402 /* Local Variables: */ 00403 /* indent-tabs-mode: nil */ 00404 /* End: */