00001 /*!@file ModelNeuron/NeuralSimModule.C Implementation for the superior colliculus model */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00005 // University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: David J. Berg <dberg@usc.edu> 00034 // $HeadURL: svn://isvn.usc.edu:/software/invt/trunk/saliency/src/ModelNeuron/NeuralSimModule.C $ 00035 00036 #ifdef INVT_USE_CPPOX//we need c++ 0X features for this to work 00037 00038 #include "Component/OptionManager.H" 00039 #include "Transport/FrameInfo.H" 00040 #include "Transport/FrameOstream.H" 00041 #include "Simulation/SimEventQueue.H" 00042 #include "Util/log.H" 00043 00044 #include "ModelNeuron/NeuralSimModule.H" 00045 #include "ModelNeuron/SimStructureOpts.H" 00046 #include "ModelNeuron/SimStructures.H" 00047 #include "ModelNeuron/NeuralDecoder.H" 00048 00049 // ###################################################################### 00050 // an interface to components build from the ModelNeuron tools 00051 // ###################################################################### 00052 template <class T> 00053 NeuralSimModule<T>::NeuralSimModule(OptionManager& mgr, 00054 const std::string& descrName, 00055 const std::string& tagName) : 00056 ModelComponent(mgr, descrName, tagName), 00057 itsSCtimestep(&OPT_SCSimTimeStep, this), 00058 itsSCdims(&OPT_SCDims, this), 00059 itsUseSpaceVariantBoundary("UseSpaceVariantBoundary", this, false), 00060 itsDecoderType(&OPT_SCDisplayDecoder, this), 00061 itsProbe(&OPT_SCProbe,this), 00062 itsPlotLength(&OPT_SCPlotLength,this), 00063 its2DPlotDepth(&OPT_SC2DPlotDepth,this), 00064 itsProbeDepth(&OPT_SCProbeDepth,this), 00065 itsDisplayOutput(&OPT_SCUseDisplayOutput,this), 00066 itsDisplayRange(&OPT_SCDisplayRange,this), 00067 its2DPlotSize(&OPT_SC2DPlotSize,this), 00068 itsStructure(), itsPlot(), itsInput(), itsInputGain(), itsInputDims() 00069 { } 00070 00071 // ###################################################################### 00072 template <class T> 00073 NeuralSimModule<T>::~NeuralSimModule() 00074 { } 00075 00076 // ###################################################################### 00077 template <class T> 00078 uint NeuralSimModule<T>::size() const 00079 { 00080 return itsInput.size() - 1; 00081 } 00082 00083 // ###################################################################### 00084 template <class T> 00085 void NeuralSimModule<T>::setInput(const Image<float>& current, const int layer) 00086 { 00087 Image<float> inp = current; 00088 00089 //reset module if its currently not valid 00090 if (!itsStructure.is_valid()) 00091 { 00092 if (itsSCdims.getVal() != Dims(0,0)) 00093 setModel(tagName(), itsSCdims.getVal()); 00094 else 00095 setModel(tagName(), current.getDims()); 00096 00097 itsInputDims = current.getDims(); 00098 } 00099 00100 //rescale input if needed 00101 inp = rescaleBilinear(inp, itsStructure->getOutDims()); 00102 itsInput[layer+1] = inp;//implicit conversion frol Image<float> to image<double> 00103 } 00104 00105 // ###################################################################### 00106 template <class T> 00107 void NeuralSimModule<T>::setInputGain(const float& weight, const int layer) 00108 { 00109 itsInputGain[layer+1] = (double)weight; 00110 } 00111 00112 // ###################################################################### 00113 template <class T> 00114 Image<float> NeuralSimModule<T>::getV(const int layer) const 00115 { 00116 Image<float> out = itsStructure->getOutput(layer);//conversion from double to float 00117 00118 //rescale to the inputu size if necessary 00119 out = rescaleBilinear(out, itsInputDims); 00120 return out; 00121 } 00122 00123 // ###################################################################### 00124 template <class T> 00125 ImageSet<float> NeuralSimModule<T>::getSubV() const 00126 { 00127 ImageSet<float> out(itsStructure->numSubs()); 00128 if (itsStructure->numSubs() > 0) 00129 for (uint ii = 0; ii < itsStructure->numSubs(); ++ii) 00130 { 00131 out[ii] = itsStructure->getOutput(ii); //conversion from double to float 00132 rescaleBilinear(out[ii], itsInputDims); 00133 } 00134 else 00135 { 00136 Image<float> temp = itsStructure->getOutput(); 00137 rescaleBilinear(temp, itsInputDims); 00138 out.push_back(temp);//conversion from double to float 00139 } 00140 return out; 00141 } 00142 00143 // ###################################################################### 00144 template <class T> 00145 void NeuralSimModule<T>::update(const SimTime& time) 00146 { 00147 //reset module if its currently not valid 00148 if (!itsStructure.is_valid()) 00149 LFATAL("The module must recieve input before it updated."); 00150 00151 for (uint ii = 0; ii < itsInput.size(); ++ii) 00152 if (itsInput.getImage(ii).initialized()) 00153 itsStructure->input(itsInput.getImage(ii)*itsInputGain[ii], ii - 1); 00154 00155 itsStructure->evolve(time); 00156 } 00157 00158 // ###################################################################### 00159 template <class T> 00160 void NeuralSimModule<T>::reset() 00161 { 00162 if (itsStructure.is_valid()) 00163 itsStructure->initialize(); 00164 if (itsPlot.is_valid()) 00165 itsPlot->reset(); 00166 00167 itsInput = ImageSet<double>(itsStructure->numSubs()+1); 00168 } 00169 00170 // ###################################################################### 00171 template <class T> 00172 void NeuralSimModule<T>::setModel(const std::string& model_name, const Dims& dims, const SimTime& starttime) 00173 { 00174 //set our border policy based on wether we are using space variant boundaries or not 00175 BorderPolicy bp = (itsUseSpaceVariantBoundary.getVal()) ? CROSS_HEMI : NONE; 00176 00177 //change any factory parameters 00178 uint w = dims.w(); uint h = dims.h(); 00179 nsu::setParameter(SimStructure::Factory::instance(), bp, itsSCtimestep.getVal(), w, h); 00180 00181 //reset the module 00182 LINFO("model type: %s", model_name.c_str()); 00183 itsStructure.reset(SimStructure::Factory::instance().createConvert<T*>(model_name)); 00184 itsStructure->setTime(starttime); 00185 00186 //setup plotting range 00187 NormalizeType ntype; 00188 Range<double> itsRange = itsDisplayRange.getVal(); 00189 if ((itsRange.min() < 0) && (itsRange.max() < 0))//if both are less than 0 scale 00190 { 00191 ntype = SCALE; 00192 itsRange = Range<double>(0.0,0.0); 00193 } 00194 else if ((itsRange.min() == 0) && (itsRange.max() == 0))//set to min/max of data 00195 ntype = RANGE; 00196 else //set to auto scale at each time 00197 ntype = SET_RANGE; 00198 00199 //set a decode if desired and initialize plotting 00200 if (itsDecoderType.getVal().compare("None") != 0) 00201 { 00202 NeuralDecoder* nd = NeuralDecoder::Factory::instance().create(itsDecoderType.getVal()); 00203 itsPlot.reset(new StructurePlot(*itsStructure, *nd, its2DPlotDepth.getVal(), ntype, 00204 itsRange.min(), itsRange.max())); 00205 delete nd; 00206 } 00207 else 00208 itsPlot.reset(new StructurePlot(*itsStructure, its2DPlotDepth.getVal(), ntype, 00209 itsRange.min(), itsRange.max())); 00210 00211 //update our probe position and set sampling rate for display text 00212 itsPlot->setSamplingRate(itsStructure->getTimeStep()); 00213 Location location(itsProbe.getVal()); 00214 itsPlot->setProbe(location); 00215 00216 //setup image set to hold input 00217 const uint depth = (itsStructure->numSubs() < 1) ? 2 : itsStructure->numSubs()+1; 00218 itsInput = ImageSet<double>(depth); 00219 itsInputGain = std::vector<double>(depth, 1.0); 00220 } 00221 00222 // ###################################################################### 00223 template <class T> 00224 Layout<PixRGB<byte> > NeuralSimModule<T>::getDisplay() const 00225 { 00226 return itsPlot->draw(*itsStructure, its2DPlotSize.getVal().w(), 00227 its2DPlotSize.getVal().h(), Dims(0,1), 00228 itsPlotLength.getVal(), itsProbeDepth.getVal(), 00229 itsDisplayOutput.getVal()); 00230 } 00231 00232 // ###################################################################### 00233 template <class T> 00234 rutz::shared_ptr<T> NeuralSimModule<T>::getStructure() 00235 { 00236 return itsStructure; 00237 } 00238 00239 #include "ModelNeuron/SC.H" 00240 template class NeuralSimModule<SCInterface>; 00241 template class NeuralSimModule<SimStructure>; 00242 #endif 00243 // ###################################################################### 00244 /* So things look consistent in everyone's emacs... */ 00245 /* Local Variables: */ 00246 /* indent-tabs-mode: nil */ 00247 /* End: */