00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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
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
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
00101 inp = rescaleBilinear(inp, itsStructure->getOutDims());
00102 itsInput[layer+1] = inp;
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);
00117
00118
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);
00132 rescaleBilinear(out[ii], itsInputDims);
00133 }
00134 else
00135 {
00136 Image<float> temp = itsStructure->getOutput();
00137 rescaleBilinear(temp, itsInputDims);
00138 out.push_back(temp);
00139 }
00140 return out;
00141 }
00142
00143
00144 template <class T>
00145 void NeuralSimModule<T>::update(const SimTime& time)
00146 {
00147
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
00175 BorderPolicy bp = (itsUseSpaceVariantBoundary.getVal()) ? CROSS_HEMI : NONE;
00176
00177
00178 uint w = dims.w(); uint h = dims.h();
00179 nsu::setParameter(SimStructure::Factory::instance(), bp, itsSCtimestep.getVal(), w, h);
00180
00181
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
00187 NormalizeType ntype;
00188 Range<double> itsRange = itsDisplayRange.getVal();
00189 if ((itsRange.min() < 0) && (itsRange.max() < 0))
00190 {
00191 ntype = SCALE;
00192 itsRange = Range<double>(0.0,0.0);
00193 }
00194 else if ((itsRange.min() == 0) && (itsRange.max() == 0))
00195 ntype = RANGE;
00196 else
00197 ntype = SET_RANGE;
00198
00199
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
00212 itsPlot->setSamplingRate(itsStructure->getTimeStep());
00213 Location location(itsProbe.getVal());
00214 itsPlot->setProbe(location);
00215
00216
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
00245
00246
00247