Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

phANNSystem.h

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002     Phission :
00003         Realtime Vision Processing System
00004 
00005     Copyright (C) 2003 Philip D.S. Thoren (pthoren@cs.uml.edu)
00006     University of Massachusetts at Lowell,
00007     Laboratory for Artificial Intelligence and Robotics
00008 
00009     This file is part of Phission.
00010 
00011     Phission is free software; you can redistribute it and/or modify
00012     it under the terms of the GNU General Public License as published by
00013     the Free Software Foundation; either version 2 of the License, or
00014     (at your option) any later version.
00015 
00016     Phission is distributed in the hope that it will be useful,
00017     but WITHOUT ANY WARRANTY; without even the implied warranty of
00018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019     GNU General Public License for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with Phission; if not, write to the Free Software
00023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024 
00025  ---------------------------------------------------------------------------*/
00026 #ifndef PHANNSYSTEM_H
00027 #define PHANNSYSTEM_H
00028 
00029 #if USE_FILETYPE
00030 #else
00031     #include <sys/file.h>
00032     #include <sys/types.h>
00033     #include <sys/stat.h>
00034     #include <fcntl.h>
00035     #include <unistd.h>
00036 #endif
00037 
00038 #include <image_collection.h>
00039 #include <ml_class_stats.h>
00040 #include <phission.h>
00041 #include <fann.h>
00042 
00043 /* ------------------------------------------------------------------------ */
00044 /* IMAGE PROCESSING SYSTEM */
00045 /* ------------------------------------------------------------------------ */
00046 /*
00047  *               image display--/
00048  * original image              /      /- rgb hist display
00049  * \->[ brightness adjust ]--\/      /     hsv hist display--\
00050  *                            |-> [ rgbhist | hsvconv | hsvhist ]
00051  *                            |-> [ canny ] -> cannydisp
00052  *                            |-> [ sobel ] -> sobeldisp
00053  */
00054 /* ------------------------------------------------------------------------ */
00055 /* ------------------------------------------------------------------------ */
00056 /*
00057    R               -\      | | |\        /-- Out1
00058    G               --\     | | |-\      /--- Out2
00059    B               ---\    | | |--\    /---- Out3
00060    H               ----\   | | |---\  /----- Out4
00061    S               -----\--Hidden---\/------ Out5
00062    V               -----/  Layers---/ \----- Out6
00063    Canny           ----/   | | |---/   \---- ... 
00064    Grey Luminance  ---/    | | |--/     \--- ... 
00065    Ratio of w to h --/     | | |-/       \-- OutN
00066    RGB Histogram   -/      | | |/                
00067    HSV Histogram   /                             
00068  */
00069 /* ------------------------------------------------------------------------ */
00070 /* ------------------------------------------------------------------------ */
00071 #define phML_RGB        (1<<0)
00072 #define phML_HSV        (1<<1)
00073 #define phML_GREY       (1<<2)
00074 #define phML_CANNY      (1<<3)
00075 #define phML_SOBEL      (1<<4)
00076 #define phML_RATIO      (1<<5)
00077 #define phML_HISTRGB    (1<<6)
00078 #define phML_HISTHSV    (1<<7)
00079 #define phML_ALL        (phML_RGB | phML_HSV | phML_CANNY | phML_SOBEL |\
00080                          phML_GREY | phML_RATIO | phML_HISTRGB | phML_HISTHSV )
00081 
00082 /* ------------------------------------------------------------------------ */
00083 class phANNSystem
00084 {
00085 private:
00086     /* ------------------------------------------------------------------ */
00087     int         m_no_displays;
00088     int32_t     m_flags;
00089     uint32_t    m_width;
00090     uint32_t    m_height;
00091     uint32_t    m_img_inputs;
00092     uint32_t    m_indiv_inputs;
00093     
00094     FILE       *m_logfp;
00095     char       *m_logfile;
00096     char       *m_ann_datafile;
00097     
00098     image_data_collection m_data;
00099 
00100     /* ------------------------------------------------------------------ */
00101     uint32_t m_total_displays;
00102     uint32_t m_total_pipelines;
00103     uint32_t m_total_histograms;
00104     uint32_t m_total_images;
00105 
00106     /* ------------------------------------------------------------------ */
00107     uint32_t m_histrgb_index;
00108     uint32_t m_histhsv_index;
00109 
00110     uint32_t m_bright_pipe;
00111     uint32_t m_canny_pipe;
00112     uint32_t m_sobel_pipe;
00113     uint32_t m_hist_pipe;
00114 
00115     uint32_t m_img_disp;
00116     uint32_t m_canny_disp;
00117     uint32_t m_sobel_disp;
00118     uint32_t m_histrgb_disp;
00119     uint32_t m_histhsv_disp;
00120 
00121     uint32_t m_input_image;
00122     uint32_t m_rgb_image;
00123     uint32_t m_hsv_image;
00124     uint32_t m_grey_image;
00125     uint32_t m_canny_image;
00126     uint32_t m_sobel_image;
00127 
00128     /* ------------------------------------------------------------------ */
00129     uint32_t    m_ptr_count;
00130     uint8_t   **m_ptr_array;
00131     uint32_t   *m_ptr_size_array;
00132     uint32_t   *m_ptr_stepping_array;
00133     char      **m_ptr_name_array;
00134 
00135     /* ------------------------------------------------------------------ */
00136     brightness_Filter  *m_bright;
00137     int32_t             m_bright_stepping;
00138     int32_t             m_bright_level;
00139     
00140     /* ------------------------------------------------------------------ */
00141     histogram_Filter  **m_histograms;
00142     phHistogramData   **m_histogram_data;
00143     uint32_t            m_histogram_bins;
00144     double              m_width_box;
00145     double              m_height_box;
00146     
00147     resize_Filter      *m_sobel_resize;
00148     resize_Filter      *m_canny_resize;
00149     
00150     resize_Filter      *m_hist_resize;
00151     convert_Filter     *m_hsv_convert;
00152     gaussianBlur_Filter *m_gauss;
00153     
00154     /* ------------------------------------------------------------------ */
00155 #if PH_HAVE_OPENCV
00156     cv_canny_Filter    *m_canny;
00157 #else
00158     canny_Filter       *m_canny;
00159 #endif
00160     convert_Filter     *m_canny_grey_conv;
00161     sobel_Filter       *m_sobel;
00162     convert_Filter     *m_sobel_grey_conv;
00163 
00164     /* ------------------------------------------------------------------ */
00165     phSystem            *m_system;
00166     phPipeline          **m_pipelines;
00167     phDisplayInterface  **m_displays;
00168     phImage             **m_images;
00169 
00170     /* ------------------------------------------------------------------ */
00171     int allocate            ( );
00172     int connect_elements    ( );
00173     int connect_displays    ( );
00174     int disconnect_displays ( );
00175     int pipeline_add        ( uint32_t index, phFilter *filter );
00176     int add_filters         ( );
00177 
00178     /* ------------------------------------------------------------------ */
00179     fann_type  *m_input_nodes;
00180     uint32_t    m_input_nodes_size;
00181     uint32_t    m_input_nodes_count;
00182     fann_type  *m_output_nodes;
00183     uint32_t    m_output_nodes_size;
00184     uint32_t    m_output_nodes_count;
00185     float       m_image_ratio;
00186    
00187     /* ------------------------------------------------------------------ */
00188     int32_t         m_training_algorithm;
00189     float           m_connection_rate;
00190     float           m_learning_rate;
00191     uint32_t        m_num_layers;
00192     uint32_t        m_num_hidden;
00193     uint32_t        m_num_output;
00194     char           *m_ann_file;
00195     int             m_ann_source;
00196     struct fann    *m_ann;
00197 
00198     /* allocates the network with the current ANN settings */
00199     int createNetwork();
00200     int destroyNetwork();
00201 
00202 public:
00203     phANNSystem(image_data_collection   data, 
00204                 int32_t                 train_flags );
00205     ~phANNSystem();
00206      
00207     /* Start up the vision system and create the network 
00208      * using the settings from the setNetwork methods */
00209     int startup();
00210     int shutdown();
00211 
00212     /* For efficiency reasons, disabling the displays is necessary.
00213      * When the displays are disabled, other verbose printing is also
00214      * reduced because wanting efficiency is assumed */
00215     int useDisplays( int use = 1 );
00216 
00217     /* The vision system preprocesses the image by setting a brightness 
00218      * level. This allows the ANN to learn on varying 
00219      * light intensity levels */
00220     int setBrightnessStep ( int step  );
00221     int setBrightnessLevel( int level );
00222     
00223     /* Given the m_flags, returns the number of input nodes to the ANN that
00224      * will result */
00225     uint32_t getInputCount();
00226     
00227     /* sets up the input nodes as fann_types from the output
00228      * of the vision processing system */
00229     int      update();
00230     
00231     /* sets up the output nodes as fann_types by comparing the
00232      * output tags to the this->m_data tags */
00233     int setOutputNodes( const uint32_t n_output_tags,
00234                         const uint32_t *output_tags );
00235     /* Step 1 */
00236     /* used by the training/testing routine */
00237     int setInputImage( char            *filename,
00238                        float            ratio,
00239                        const uint32_t   n_outputtags,
00240                        const uint32_t  *output_tags );
00241     int setInputImage( phImage         &image,
00242                        float            ratio,
00243                        const uint32_t   n_outputtags,
00244                        const uint32_t  *output_tags );
00245     /* used by the classification routine */
00246     int setInputImage( char *filename, float ratio );
00247     int setInputImage( phImage &image, float ratio );
00248 
00249     /* Step 2 */
00250     int      getInputNodes( uint32_t       *input_count,
00251                             uint32_t       *input_size,
00252                             fann_type     **pinput_node_values );
00253     int      getOutputNodes(uint32_t       *output_count,
00254                             uint32_t       *output_size,
00255                             fann_type     **poutput_node_values );
00256     int      getNodes(uint32_t       *input_count,
00257                       uint32_t       *input_size,
00258                       fann_type     **pinput_node_values,
00259                       uint32_t       *output_count,
00260                       uint32_t       *output_size,
00261                       fann_type     **poutput_node_values );
00262     int      getNodes(fann_type     **pinput_node_values,
00263                       fann_type     **poutput_node_values );
00264     
00265     FILE *openLogFile( const char *prefix,
00266                        const char *suffix );
00267     int closeLogFile( );
00268     
00269     int saveNetwork( const char *prefix,
00270                      const char *suffix );
00271 
00272     int setNetwork( char *ann_startfile );
00273 
00274     int setNetwork( float       connection_rate, 
00275                     float       learning_rate, 
00276                     int32_t     training_algorithm,
00277                     uint32_t    num_layers,
00278                     uint32_t    num_hidden );
00279 
00280     void print_epoch_info( const char        *label,
00281                            ml_class_stats    *stats );
00282 
00283 
00284     /* Generate the node input and output arrays.
00285      *
00286      * preload_images: load the entire file-set of images for each
00287      *                 training/testing set of images.
00288      * generate_random: 1 == call random_image to create image
00289      *                  2 == call randomize_pixels to create image
00290      */
00291     int generateNodes(int      preload_images,
00292                       int      generate_random, 
00293                       uint32_t train_set_size,
00294                       uint32_t test_set_size,
00295                       uint32_t *ptrain_array_count,
00296                       uint32_t *ptest_array_count,
00297                       uint32_t *pinput_array_count,
00298                       fann_type ***pinput_nodes,
00299                       fann_type ***prand_input_nodes,
00300                       uint32_t *poutput_array_count,
00301                       fann_type ***poutput_nodes,
00302                       fann_type ***prand_output_nodes );
00303     
00304     int train( int      preload_images,
00305                int      use_random,
00306                uint32_t train_set_size,
00307                uint32_t test_set_size,
00308                uint32_t max_epochs,
00309                float    desired_error,
00310                uint32_t start_epoch
00311              );
00312     
00313     uint32_t nodesToString( fann_type *output_nodes,
00314                             uint32_t  *string_size,
00315                             char      **string );
00316     int classify( const char     *input_label,
00317                   const int      input_type,
00318                   const uint32_t total_files );
00319 
00320 };
00321 
00322 
00323 
00324 #endif /* PHANNSYSTEM_H */
00325 




Copyright (C) 2002 - 2007 Philip D.S. Thoren ( pthoren@users.sourceforge.net )
University Of Massachusetts at Lowell
Robotics Lab
SourceForge.net Logo

Generated on Sat Jun 16 02:44:06 2007 for phission by  doxygen 1.4.4