int GLWidget::stepNuPIC(vector<UInt>& inputSDR, bool learn) { // clear the active columns indicies array m_activeColumnIndicies.assign(NUM_COLUMNS, 0); gs_SP.compute(inputSDR.data(), learn, m_activeColumnIndicies.data()); gs_SP.stripUnlearnedColumns(m_activeColumnIndicies.data()); gs_TM.compute(m_activeColumnIndicies.size(), m_activeColumnIndicies.data(), learn); return 0; }
void init_Spatial_Pooler(py::module& m) { py::class_<SpatialPooler> py_SpatialPooler(m, "SpatialPooler"); py_SpatialPooler.def( py::init<vector<UInt> , vector<UInt> , UInt , Real , bool , Real , UInt , UInt , Real , Real , Real , Real , UInt , Real , Int , UInt , bool>() , py::arg("inputDimensions") = vector<UInt>({ 32, 32 }) , py::arg("columnDimensions") = vector<UInt>({ 64, 64 }) , py::arg("potentialRadius") = 16 , py::arg("potentialPct") = 0.5 , py::arg("globalInhibition") = false , py::arg("localAreaDensity") = -1.0 , py::arg("numActiveColumnsPerInhArea") = 10 , py::arg("stimulusThreshold") = 0 , py::arg("synPermInactiveDec") = 0.01 , py::arg("synPermActiveInc") = 0.1 , py::arg("synPermConnected") = 0.1 , py::arg("minPctOverlapDutyCycle") = 0.001 , py::arg("dutyCyclePeriod") = 1000 , py::arg("boostStrength") = 0.0 , py::arg("seed") = -1 , py::arg("spVerbosity") = 0 , py::arg("wrapAround") = true ); py_SpatialPooler.def("initialize", &SpatialPooler::initialize , py::arg("inputDimensions") = vector<UInt>({ 32, 32 }) , py::arg("columnDimensions") = vector<UInt>({ 64, 64 }) , py::arg("potentialRadius") = 16 , py::arg("potentialPct") = 0.5 , py::arg("globalInhibition") = false , py::arg("localAreaDensity") = -1.0 , py::arg("numActiveColumnsPerInhArea") = 10 , py::arg("stimulusThreshold") = 0 , py::arg("synPermInactiveDec") = 0.01 , py::arg("synPermActiveInc") = 0.1 , py::arg("synPermConnected") = 0.1 , py::arg("minPctOverlapDutyCycle") = 0.001 , py::arg("dutyCyclePeriod") = 1000 , py::arg("boostStrength") = 0.0 , py::arg("seed") = -1 , py::arg("spVerbosity") = 0 , py::arg("wrapAround") = true); py_SpatialPooler.def("getColumnDimensions", &SpatialPooler::getColumnDimensions); py_SpatialPooler.def("getInputDimensions", &SpatialPooler::getInputDimensions); py_SpatialPooler.def("getNumColumns", &SpatialPooler::getNumColumns); py_SpatialPooler.def("getNumInputs", &SpatialPooler::getNumInputs); py_SpatialPooler.def("getPotentialRadius", &SpatialPooler::getPotentialRadius); py_SpatialPooler.def("setPotentialRadius", &SpatialPooler::setPotentialRadius); py_SpatialPooler.def("getPotentialPct", &SpatialPooler::getPotentialPct); py_SpatialPooler.def("setPotentialPct", &SpatialPooler::setPotentialPct); py_SpatialPooler.def("getGlobalInhibition", &SpatialPooler::getGlobalInhibition); py_SpatialPooler.def("setGlobalInhibition", &SpatialPooler::setGlobalInhibition); py_SpatialPooler.def("getNumActiveColumnsPerInhArea", &SpatialPooler::getNumActiveColumnsPerInhArea); py_SpatialPooler.def("setNumActiveColumnsPerInhArea", &SpatialPooler::setNumActiveColumnsPerInhArea); py_SpatialPooler.def("getLocalAreaDensity", &SpatialPooler::getLocalAreaDensity); py_SpatialPooler.def("setLocalAreaDensity", &SpatialPooler::setLocalAreaDensity); py_SpatialPooler.def("getStimulusThreshold", &SpatialPooler::getStimulusThreshold); py_SpatialPooler.def("setStimulusThreshold", &SpatialPooler::setStimulusThreshold); py_SpatialPooler.def("getInhibitionRadius", &SpatialPooler::getInhibitionRadius); py_SpatialPooler.def("setInhibitionRadius", &SpatialPooler::setInhibitionRadius); py_SpatialPooler.def("getDutyCyclePeriod", &SpatialPooler::getDutyCyclePeriod); py_SpatialPooler.def("setDutyCyclePeriod", &SpatialPooler::setDutyCyclePeriod); py_SpatialPooler.def("getBoostStrength", &SpatialPooler::getBoostStrength); py_SpatialPooler.def("setBoostStrength", &SpatialPooler::setBoostStrength); py_SpatialPooler.def("getIterationNum", &SpatialPooler::getIterationNum); py_SpatialPooler.def("setIterationNum", &SpatialPooler::setIterationNum); py_SpatialPooler.def("getIterationLearnNum", &SpatialPooler::getIterationLearnNum); py_SpatialPooler.def("setIterationLearnNum", &SpatialPooler::setIterationLearnNum); py_SpatialPooler.def("getSpVerbosity", &SpatialPooler::getSpVerbosity); py_SpatialPooler.def("setSpVerbosity", &SpatialPooler::setSpVerbosity); py_SpatialPooler.def("getWrapAround", &SpatialPooler::getWrapAround); py_SpatialPooler.def("setWrapAround", &SpatialPooler::setWrapAround); py_SpatialPooler.def("getUpdatePeriod", &SpatialPooler::getUpdatePeriod); py_SpatialPooler.def("setUpdatePeriod", &SpatialPooler::setUpdatePeriod); py_SpatialPooler.def("getSynPermActiveInc", &SpatialPooler::getSynPermActiveInc); py_SpatialPooler.def("setSynPermActiveInc", &SpatialPooler::setSynPermActiveInc); py_SpatialPooler.def("getSynPermInactiveDec", &SpatialPooler::getSynPermInactiveDec); py_SpatialPooler.def("setSynPermInactiveDec", &SpatialPooler::setSynPermInactiveDec); py_SpatialPooler.def("getSynPermBelowStimulusInc", &SpatialPooler::getSynPermBelowStimulusInc); py_SpatialPooler.def("setSynPermBelowStimulusInc", &SpatialPooler::setSynPermBelowStimulusInc); py_SpatialPooler.def("getSynPermConnected", &SpatialPooler::getSynPermConnected); py_SpatialPooler.def("getSynPermMax", &SpatialPooler::getSynPermMax); py_SpatialPooler.def("getMinPctOverlapDutyCycles", &SpatialPooler::getMinPctOverlapDutyCycles); py_SpatialPooler.def("setMinPctOverlapDutyCycles", &SpatialPooler::setMinPctOverlapDutyCycles); // loadFromString py_SpatialPooler.def("loadFromString", [](SpatialPooler& self, const std::string& inString) { std::istringstream inStream(inString); self.load(inStream); }); // writeToString py_SpatialPooler.def("writeToString", [](const SpatialPooler& self) { std::ostringstream os; os.flags(ios::scientific); os.precision(numeric_limits<double>::digits10 + 1); self.save(os); return os.str(); }); // compute py_SpatialPooler.def("compute", [](SpatialPooler& self, py::array& x, bool learn, py::array& y) { if (py::isinstance<py::array_t<std::uint32_t>>(x) == false) { throw runtime_error("Incompatible format. Expect uint32"); } if (py::isinstance<py::array_t<std::uint32_t>>(y) == false) { throw runtime_error("Incompatible format. Expect uint32"); } self.compute(get_it<UInt>(x), learn, get_it<UInt>(y)); }); // stripUnlearnedColumns py_SpatialPooler.def("stripUnlearnedColumns", [](SpatialPooler& self, py::array_t<UInt>& x) { self.stripUnlearnedColumns(get_it(x)); }); // setBoostFactors py_SpatialPooler.def("setBoostFactors", [](SpatialPooler& self, py::array_t<Real>& x) { self.setBoostFactors(get_it(x)); }); // getBoostFactors py_SpatialPooler.def("getBoostFactors", [](const SpatialPooler& self, py::array_t<Real>& x) { self.getBoostFactors(get_it(x)); }); // setOverlapDutyCycles py_SpatialPooler.def("setOverlapDutyCycles", [](SpatialPooler& self, py::array_t<Real>& x) { self.setOverlapDutyCycles(get_it(x)); }); // getOverlapDutyCycles py_SpatialPooler.def("getOverlapDutyCycles", [](const SpatialPooler& self, py::array_t<Real>& x) { self.getOverlapDutyCycles(get_it(x)); }); // setActiveDutyCycles py_SpatialPooler.def("setActiveDutyCycles", [](SpatialPooler& self, py::array_t<Real>& x) { self.setActiveDutyCycles(get_it(x)); }); // getActiveDutyCycles py_SpatialPooler.def("getActiveDutyCycles", [](const SpatialPooler& self, py::array_t<Real>& x) { self.getActiveDutyCycles(get_it(x)); }); // setMinOverlapDutyCycles py_SpatialPooler.def("setMinOverlapDutyCycles", [](SpatialPooler& self, py::array_t<Real>& x) { self.setMinOverlapDutyCycles(get_it(x)); }); // getMinOverlapDutyCycles py_SpatialPooler.def("getMinOverlapDutyCycles", [](const SpatialPooler& self, py::array_t<Real>& x) { self.getMinOverlapDutyCycles(get_it(x)); }); // setPotential py_SpatialPooler.def("setPotential", [](SpatialPooler& self, UInt column, py::array_t<UInt>& x) { self.setPotential(column, get_it(x)); }); // getPotential py_SpatialPooler.def("getPotential", [](const SpatialPooler& self, UInt column, py::array_t<UInt>& x) { self.getPotential(column, get_it(x)); }); // setPermanence py_SpatialPooler.def("setPermanence", [](SpatialPooler& self, UInt column, py::array_t<Real>& x) { self.setPermanence(column, get_it(x)); }); // getPermanence py_SpatialPooler.def("getPermanence", [](const SpatialPooler& self, UInt column, py::array_t<Real>& x) { self.getPermanence(column, get_it(x)); }); // getConnectedSynapses py_SpatialPooler.def("getConnectedSynapses", [](const SpatialPooler& self, UInt column, py::array_t<UInt>& x) { self.getConnectedSynapses(column, get_it(x)); }); // getConnectedCounts py_SpatialPooler.def("getConnectedCounts", [](const SpatialPooler& self, py::array_t<UInt>& x) { self.getConnectedCounts(get_it(x)); }); // getOverlaps py_SpatialPooler.def("getOverlaps", [](SpatialPooler& self) { auto overlaps = self.getOverlaps(); return py::array_t<UInt>( overlaps.size(), overlaps.data()); }); // getBoostedOverlaps py_SpatialPooler.def("getBoostedOverlaps", [](SpatialPooler& self) { auto overlaps = self.getBoostedOverlaps(); return py::array_t<Real>( overlaps.size(), overlaps.data()); }); //////////////////// // inhibitColumns auto inhibitColumns_func = [](SpatialPooler& self, py::array_t<Real>& overlaps) { std::vector<nupic::Real> overlapsVector(get_it(overlaps), get_end(overlaps)); std::vector<nupic::UInt> activeColumnsVector; self.inhibitColumns_(overlapsVector, activeColumnsVector); return py::array_t<UInt>( activeColumnsVector.size(), activeColumnsVector.data()); }; py_SpatialPooler.def("_inhibitColumns", inhibitColumns_func); py_SpatialPooler.def("inhibitColumns_", inhibitColumns_func); ////////////////////// // getIterationLearnNum py_SpatialPooler.def("getIterationLearnNum", &SpatialPooler::getIterationLearnNum); // pickle py_SpatialPooler.def(py::pickle( [](const SpatialPooler& sp) { std::stringstream ss; sp.save(ss); return ss.str(); }, [](std::string& s) { std::istringstream ss(s); SpatialPooler sp; sp.load(ss); return sp; })); }
void testSP() { Random random(10); nupic::Timer testTimer; const UInt inputSize = 500; const UInt numColumns = 500; const UInt w = 50; vector<UInt> inputDims{inputSize}; vector<UInt> colDims{numColumns}; SpatialPooler sp1; sp1.initialize(inputDims, colDims); UInt input[inputSize]; for (UInt i = 0; i < inputSize; ++i) { if (i < w) { input[i] = 1; } else { input[i] = 0; } } UInt output[numColumns]; for (UInt i = 0; i < 10000; ++i) { random.shuffle(input, input + inputSize); sp1.compute(input, true, output); } // Now we reuse the last input to test after serialization vector<UInt> activeColumnsBefore; for (UInt i = 0; i < numColumns; ++i) { if (output[i] == 1) { activeColumnsBefore.push_back(i); } } // Save initial trained model ofstream osA("outA.proto", ofstream::binary); sp1.write(osA); osA.close(); ofstream osC("outC.proto", ofstream::binary); sp1.save(osC); osC.close(); SpatialPooler sp2; long timeA = 0, timeC = 0; for (UInt i = 0; i < 100; ++i) { // Create new input random.shuffle(input, input + inputSize); // Get expected output UInt outputBaseline[numColumns]; sp1.compute(input, true, outputBaseline); UInt outputA[numColumns]; UInt outputC[numColumns]; // A - First do iostream version { SpatialPooler spTemp; testTimer.start(); // Deserialize ifstream is("outA.proto", ifstream::binary); spTemp.read(is); is.close(); // Feed new record through spTemp.compute(input, true, outputA); // Serialize ofstream os("outA.proto", ofstream::binary); spTemp.write(os); os.close(); testTimer.stop(); timeA = timeA + testTimer.getElapsed(); } // C - Next do old version { SpatialPooler spTemp; testTimer.start(); // Deserialize ifstream is("outC.proto", ifstream::binary); spTemp.load(is); is.close(); // Feed new record through spTemp.compute(input, true, outputC); // Serialize ofstream os("outC.proto", ofstream::binary); spTemp.save(os); os.close(); testTimer.stop(); timeC = timeC + testTimer.getElapsed(); } for (UInt i = 0; i < numColumns; ++i) { NTA_ASSERT(outputBaseline[i] == outputA[i]); NTA_ASSERT(outputBaseline[i] == outputC[i]); } } remove("outA.proto"); remove("outC.proto"); cout << "Time for iostream capnp: " << ((Real)timeA / 1000.0) << endl; cout << "Time for old method: " << ((Real)timeC / 1000.0) << endl; }
GLWidget::GLWidget(const QString & inAudioFileName, QWidget *parent) : QGLWidget(parent)//, staticBuffer(0), dynamicBuffer(0), indexBuffer(0) { cout << "Num texture windows: " << NUM_TEXTURE_WINDOWS << endl; cout << "Texture window size: " << TEXTURE_WINDOW_SIZE << " ms" << endl; cout << "Samples per ms: " << SAMPLES_PER_MS << endl; cout << "Timer delta: " << TIMER_DELTA << " ms" << endl; cout << "Memory size: " << MEMORY_SIZE << endl; max_data.create(SPECTRUM_BUFFER_SIZE); for (int i = 0; i < SPECTRUM_BUFFER_SIZE; i++) { max_data(i) = -999999.9; } // // Create the MarSystem to play and analyze the data // MarSystemManager mng; // A series to contain everything MarSystem* net = mng.create("Series", "net"); m_marSystem = net; // Note that you can only add one Marsystem to an Accumulator // any additional Systems added are simply ignored outputwise !! // e.g. if you want to use multiple Marsystems in a row and accumulate // their combined output, you need to put them in a series which you add // to the accumulator MarSystem *accum = mng.create("Accumulator", "accum"); net->addMarSystem(accum); MarSystem *accum_series = mng.create("Series", "accum_series"); accum->addMarSystem(accum_series); accum_series->addMarSystem(mng.create("SoundFileSource/src")); accum_series->addMarSystem(mng.create("Stereo2Mono", "stereo2mono")); accum_series->addMarSystem(mng.create("AudioSink", "dest")); MarSystem* fanout = mng.create("Fanout", "fanout"); accum_series->addMarSystem(fanout); // Power spectrum MarSystem* spectrumMemory = mng.create("Series", "spectrumMemory"); fanout->addMarSystem(spectrumMemory); { MarSystem* net = spectrumMemory; net->addMarSystem(mng.create("Windowing", "ham")); net->addMarSystem(mng.create("Spectrum", "spk")); net->addMarSystem(mng.create("PowerSpectrum", "pspk")); } // Cochlear based/inspired MarSystem* cochlearFeatures = mng.create("Series", "cochlearFeatures"); //fanout->addMarSystem(cochlearFeatures); { MarSystem* net = cochlearFeatures; // Stabilised auditory image, from CARFAC net->addMarSystem(mng.create("CARFAC", "carfac")); } MarSystem* spatialFeatures = mng.create("Series", "spatialFeatures"); fanout->addMarSystem(spatialFeatures); { MarSystem* net = spatialFeatures; // Into time-domain net->addMarSystem(mng.create("Windowing", "ham")); net->addMarSystem(mng.create("Spectrum", "spk")); net->addMarSystem(mng.create("PowerSpectrum", "pspk")); net->addMarSystem(mng.create("ShiftInput", "si")); // DC offset? // Energy measures net->addMarSystem(mng.create("Centroid", "centroid")); net->addMarSystem(mng.create("Rolloff", "rolloff")); net->addMarSystem(mng.create("Flux", "flux")); net->addMarSystem(mng.create("ZeroCrossings", "zc")); // Mel-Frequency Cepstral Coefficients net->addMarSystem(mng.create("MFCC", "mfcc")); // Five MFCC coefficients required net->updControl("MFCC/mfcc/mrs_natural/coefficients", 5); // A resulting feature vector for describing timbral texture consists of the // following features: means and variances of spectral centroid, rolloff, flux, // zero crossings over the texture window (8), low energy (1), and means and // variances of the first five MFCC coefficients over the texture window // (excluding the coefficient corresponding to the DC component) resulting // in a 19-dimensional feature vector, __as a starting point__. } net->addMarSystem(mng.create("AutoCorrelation", "auto")); // Setup texture windows, and window memory size net->updControl( "Accumulator/accum/mrs_natural/nTimes", NUM_TEXTURE_WINDOWS); net->updControl( "Accumulator/accum/Series/accum_series/Fanout/fanout/" \ "Series/spatialFeatures/ShiftInput/si/mrs_natural/winSize", int(MEMORY_SIZE)); net->updControl("mrs_real/israte", SAMPLE_RATE); //m_marSystem->put_html(cout); //cout << *m_marSystem; ofstream oss; oss.open("audioAnalysis.mpl"); oss << *m_marSystem; // Create a Qt wrapper that provides thread-safe control of the MarSystem: m_system = new MarsyasQt::System(m_marSystem); // Get controls m_fileNameControl = m_system->control( "Accumulator/accum/Series/accum_series/" \ "SoundFileSource/src/mrs_string/filename"); m_initAudioControl = m_system->control( "Accumulator/accum/Series/accum_series/" \ "AudioSink/dest/mrs_bool/initAudio"); m_spectrumSource = m_system->control("mrs_realvec/processedData"); m_SAIbinauralSAI = m_system->control( "Accumulator/accum/Series/accum_series/Fanout/fanout/" \ "Series/cochlearFeatures/CARFAC/carfac/mrs_realvec/sai_output_binaural_sai"); m_SAIthreshold = m_system->control( "Accumulator/accum/Series/accum_series/Fanout/fanout/" \ "Series/cochlearFeatures/CARFAC/carfac/mrs_realvec/sai_output_threshold"); m_SAIstrobes = m_system->control( "Accumulator/accum/Series/accum_series/Fanout/fanout/" \ "Series/cochlearFeatures/CARFAC/carfac/mrs_realvec/sai_output_strobes"); // Initialize SP and TM vector<UInt> inputDimensions = {DIM_SDR}; vector<UInt> columnDimension = {NUM_COLUMNS}; m_inputSDR.reserve(DIM_SDR); gs_SP.initialize(inputDimensions, columnDimension, DIM_SDR, 0.5, true, -1.0, int(0.02*NUM_COLUMNS)); gs_SP.setSynPermActiveInc(0.01); gs_TM.initialize(columnDimension, CELLS_PER_COLUMN); // Connect the animation timer that periodically redraws the screen. // It is activated in the 'play()' function. connect( &m_updateTimer, SIGNAL(timeout()), this, SLOT(animate()) ); // Queue given audio file //play(inAudioFileName); m_audioFileName = inAudioFileName; m_fileNameControl->setValue(m_audioFileName, NO_UPDATE); m_system->update(); }