def testRunPCANode(self): from nupic.engine import * numpy.random.RandomState(37) inputSize = 8 net = Network() Network.registerRegion(ImageSensor) net.addRegion('sensor', 'py.ImageSensor' , '{ width: %d, height: %d }' % (inputSize, inputSize)) params = """{bottomUpCount: %d, SVDSampleCount: 5, SVDDimCount: 2}""" % inputSize pca = net.addRegion('pca', 'py.PCANode', params) #nodeAbove = CreateNode("py.ImageSensor", phase=0, categoryOut=1, dataOut=3, # width=3, height=1) #net.addElement('nodeAbove', nodeAbove) linkParams = '{ mapping: in, rfSize: [%d, %d] }' % (inputSize, inputSize) net.link('sensor', 'pca', 'UniformLink', linkParams, 'dataOut', 'bottomUpIn') net.initialize() for i in range(10): pca.getSelf()._testInputs = numpy.random.random([inputSize]) net.run(1)
def createNetwork(): """ Set up the following simple network and return it: ImageSensorRegion -> SP -> KNNClassifier Region """ net = Network() # Register the ImageSensor region with the network Network.registerRegion(ImageSensor) # Add the three regions net.addRegion("sensor", "py.ImageSensor", yaml.dump(DEFAULT_IMAGESENSOR_PARAMS)) net.addRegion("SP", "py.SPRegion", yaml.dump(DEFAULT_SP_PARAMS)) net.addRegion("classifier","py.KNNClassifierRegion", yaml.dump(DEFAULT_CLASSIFIER_PARAMS)) # Link up the regions. Note that we need to create a link from the sensor # to the classifier to send in the category labels. net.link("sensor", "SP", "UniformLink", "", srcOutput = "dataOut", destInput = "bottomUpIn") net.link("SP", "classifier", "UniformLink", "", srcOutput = "bottomUpOut", destInput = "bottomUpIn") net.link("sensor", "classifier", "UniformLink", "", srcOutput = "categoryOut", destInput = "categoryIn") return net
def testGetSelf(self): # Create network net = Network() # Register ImageSensor region Network.registerRegion(ImageSensor) # Add sensor sensor = net.addRegion("sensor", "py.ImageSensor", "{width: 100, height: 50}") pysensor = sensor.getSelf() # Verify set parameters self.assertTrue(isinstance(pysensor, ImageSensor)) self.assertEqual(pysensor.height, 50) self.assertEqual(pysensor.width, 100) self.assertEqual(pysensor.width, sensor.getParameter('width')) self.assertEqual(pysensor.height, sensor.getParameter('height')) sensor.setParameter('width', 444) sensor.setParameter('height', 444) self.assertEqual(pysensor.width, 444) self.assertEqual(pysensor.height, 444) # Verify py object is not a copy sensor.getSelf().height = 100 sensor.getSelf().width = 200 self.assertEqual(pysensor.height, 100) self.assertEqual(pysensor.width, 200) pysensor.height = 50 pysensor.width = 100 self.assertEqual(sensor.getSelf().height, 50) self.assertEqual(sensor.getSelf().width, 100)
def createNet(self): """ Set up the structure of the network """ net = Network() Network.unregisterRegion(ImageSensor.__name__) Network.registerRegion(ImageSensor) imageSensorParams = copy.deepcopy(DEFAULT_IMAGESENSOR_PARAMS) if self.loggingDir is not None: imageSensorParams["logDir"] = "sensorImages/" + self.loggingDir imageSensorParams["logOutputImages"] = 1 imageSensorParams["logOriginalImages"] = 1 imageSensorParams["logFilteredImages"] = 1 imageSensorParams["logLocationImages"] = 1 imageSensorParams["logLocationOnOriginalImage"] = 1 net.addRegion("sensor", "py.ImageSensor", yaml.dump(imageSensorParams)) net.addRegion("SP", "py.SPRegion", yaml.dump(DEFAULT_SP_PARAMS)) net.addRegion("classifier","py.KNNClassifierRegion", yaml.dump(DEFAULT_CLASSIFIER_PARAMS)) net.link("sensor", "SP", "UniformLink", "", srcOutput = "dataOut", destInput = "bottomUpIn") net.link("SP", "classifier", "UniformLink", "", srcOutput = "bottomUpOut", destInput = "bottomUpIn") net.link("sensor", "classifier", "UniformLink", "", srcOutput = "categoryOut", destInput = "categoryIn") self.net = net self.networkSensor = self.net.regions["sensor"] self.networkSP = self.net.regions["SP"] self.networkClassifier = self.net.regions["classifier"]
def runExperiment(): Network.unregisterRegion("ImageSensor") Network.registerRegion(ImageSensor) Network.registerRegion(PCANode) inputSize = 8 net = Network() sensor = net.addRegion( "sensor", "py.ImageSensor", "{ width: %d, height: %d }" % (inputSize, inputSize)) params = ("{bottomUpCount: %s, " " SVDSampleCount: 5, " " SVDDimCount: 2}" % inputSize) pca = net.addRegion("pca", "py.PCANode", params) linkParams = "{ mapping: in, rfSize: [%d, %d] }" % (inputSize, inputSize) net.link("sensor", "pca", "UniformLink", linkParams, "dataOut", "bottomUpIn") net.initialize() for i in range(10): pca.getSelf()._testInputs = numpy.random.random([inputSize]) net.run(1) print s.sendRequest("nodeOPrint pca_node")
def testRunPCANode(self): from nupic.engine import * numpy.random.RandomState(37) inputSize = 8 net = Network() Network.registerRegion(ImageSensor) net.addRegion('sensor', 'py.ImageSensor', '{ width: %d, height: %d }' % (inputSize, inputSize)) params = """{bottomUpCount: %d, SVDSampleCount: 5, SVDDimCount: 2}""" % inputSize pca = net.addRegion('pca', 'py.PCANode', params) #nodeAbove = CreateNode("py.ImageSensor", phase=0, categoryOut=1, dataOut=3, # width=3, height=1) #net.addElement('nodeAbove', nodeAbove) linkParams = '{ mapping: in, rfSize: [%d, %d] }' % (inputSize, inputSize) net.link('sensor', 'pca', 'UniformLink', linkParams, 'dataOut', 'bottomUpIn') net.initialize() for i in range(10): pca.getSelf()._testInputs = numpy.random.random([inputSize]) net.run(1)
def runExperiment(): Network.unregisterRegion("ImageSensor") Network.registerRegion(ImageSensor) Network.registerRegion(PCANode) inputSize = 8 net = Network() sensor = net.addRegion( "sensor", "py.ImageSensor" , "{ width: %d, height: %d }" % (inputSize, inputSize)) params = ("{bottomUpCount: %s, " " SVDSampleCount: 5, " " SVDDimCount: 2}" % inputSize) pca = net.addRegion("pca", "py.PCANode", params) linkParams = "{ mapping: in, rfSize: [%d, %d] }" % (inputSize, inputSize) net.link("sensor", "pca", "UniformLink", linkParams, "dataOut", "bottomUpIn") net.initialize() for i in range(10): pca.getSelf()._testInputs = numpy.random.random([inputSize]) net.run(1) print s.sendRequest("nodeOPrint pca_node")
def createSensorRegion(network, sensorType, encoders, dataSource, numCats): """ Initializes the sensor region with an encoder and data source. @param network (Network) @param sensorType (str) Specific type of region, e.g. "py.RecordSensor"; possible options can be found in /nupic/regions/. @param encoders (dict, encoder) If adding multiple encoders, pass a dict as specified in createEncoder() docstring. Otherwise an encoder object is expected. @param dataSource (RecordStream) Sensor region reads data from here. @param numCats (int) Maximum number of categories of the input data. @return (Region) Sensor region of the network. """ # Sensor region may be non-standard, so add custom region class to the network sensorName = sensorType.split(".")[1] sensorModule = sensorName # conveniently have the same name if sensorName not in PY_REGIONS: # Add new region class to the network try: module = __import__(sensorModule, {}, {}, sensorName) sensorClass = getattr(module, sensorName) Network.registerRegion(sensorClass) # Add region to list of registered PyRegions PY_REGIONS.append(sensorName) except ImportError: raise RuntimeError("Could not find sensor \'{}\' to import.". format(sensorName)) try: # Add region to network regionParams = json.dumps({"verbosity": _VERBOSITY, "numCategories": numCats}) network.addRegion("sensor", sensorType, regionParams) except RuntimeError: print ("Custom region not added correctly. Possible issues are the spec is " "wrong or the region class is not in the Python path.") return # getSelf() returns the actual region, instead of a region wrapper sensorRegion = network.regions["sensor"].getSelf() # Specify how the sensor encodes input values if isinstance(encoders, dict): # Add encoder(s) from params dict: sensorRegion.encoder = createEncoder(encoders) else: sensorRegion.encoder = encoders # Specify the dataSource as a file RecordStream instance sensorRegion.dataSource = dataSource return sensorRegion
def createNetwork(dataSource): """Create the Network instance. The network has a sensor region reading data from `dataSource` and passing the encoded representation to an Identity Region. :param dataSource: a RecordStream instance to get data from :returns: a Network instance ready to run """ network = Network() # Our input is sensor data from the gym file. The RecordSensor region # allows us to specify a file record stream as the input source via the # dataSource attribute. network.addRegion("sensor", "py.RecordSensor", json.dumps({"verbosity": _VERBOSITY})) sensor = network.regions["sensor"].getSelf() # The RecordSensor needs to know how to encode the input values sensor.encoder = createEncoder() # Specify the dataSource as a file record stream instance sensor.dataSource = dataSource # CUSTOM REGION # Add path to custom region to PYTHONPATH # NOTE: Before using a custom region, please modify your PYTHONPATH # export PYTHONPATH="<path to custom region module>:$PYTHONPATH" # In this demo, we have modified it using sys.path.append since we need it to # have an effect on this program. sys.path.append(os.path.dirname(os.path.abspath(__file__))) from custom_region.identity_region import IdentityRegion # Add custom region class to the network Network.registerRegion(IdentityRegion) # Create a custom region network.addRegion("identityRegion", "py.IdentityRegion", json.dumps(I_PARAMS)) # Link the Identity region to the sensor input network.link("sensor", "identityRegion", "UniformLink", "", srcOutput="sourceOut", destInput="in") network.initialize() return network
def createNet(self): """ Set up the structure of the network """ net = Network() Network.unregisterRegion(SaccadeSensor.__name__) Network.registerRegion(SaccadeSensor) Network.registerRegion(TMRegion) imageSensorParams = copy.deepcopy(DEFAULT_IMAGESENSOR_PARAMS) if self.loggingDir is not None: imageSensorParams["logDir"] = "sensorImages/" + self.loggingDir imageSensorParams["logOutputImages"] = 1 imageSensorParams["logOriginalImages"] = 1 imageSensorParams["logFilteredImages"] = 1 imageSensorParams["logLocationImages"] = 1 imageSensorParams["logLocationOnOriginalImage"] = 1 net.addRegion("sensor", "py.SaccadeSensor", yaml.dump(imageSensorParams)) sensor = net.regions["sensor"].getSelf() DEFAULT_SP_PARAMS["columnCount"] = sensor.getOutputElementCount("dataOut") net.addRegion("SP", "py.SPRegion", yaml.dump(DEFAULT_SP_PARAMS)) sp = net.regions["SP"].getSelf() DEFAULT_TM_PARAMS["columnDimensions"] = (sp.getOutputElementCount("bottomUpOut"),) net.addRegion("TM", "py.TMRegion", yaml.dump(DEFAULT_TM_PARAMS)) net.addRegion("classifier","py.KNNClassifierRegion", yaml.dump(DEFAULT_CLASSIFIER_PARAMS)) net.link("sensor", "SP", "UniformLink", "", srcOutput="dataOut", destInput="bottomUpIn") net.link("SP", "TM", "UniformLink", "", srcOutput="bottomUpOut", destInput="activeColumns") net.link("sensor", "TM", "UniformLink", "", srcOutput="saccadeOut", destInput="activeExternalCells") net.link("TM", "classifier", "UniformLink", "", srcOutput="predictedActiveCells", destInput="bottomUpIn") net.link("sensor", "classifier", "UniformLink", "", srcOutput="categoryOut", destInput="categoryIn") self.net = net self.networkSensor = self.net.regions["sensor"] self.networkSP = self.net.regions["SP"] self.networkTM = self.net.regions["TM"] self.networkClassifier = self.net.regions["classifier"]
def createNetwork(dataSource): """Create the Network instance. The network has a sensor region reading data from `dataSource` and passing the encoded representation to an Identity Region. :param dataSource: a RecordStream instance to get data from :returns: a Network instance ready to run """ network = Network() # Our input is sensor data from the gym file. The RecordSensor region # allows us to specify a file record stream as the input source via the # dataSource attribute. network.addRegion("sensor", "py.RecordSensor", json.dumps({"verbosity": _VERBOSITY})) sensor = network.regions["sensor"].getSelf() # The RecordSensor needs to know how to encode the input values sensor.encoder = createEncoder() # Specify the dataSource as a file record stream instance sensor.dataSource = dataSource # CUSTOM REGION # Add path to custom region to PYTHONPATH # NOTE: Before using a custom region, please modify your PYTHONPATH # export PYTHONPATH="<path to custom region module>:$PYTHONPATH" # In this demo, we have modified it using sys.path.append since we need it to # have an effect on this program. sys.path.append(os.path.dirname(os.path.abspath(__file__))) from custom_region.identity_region import IdentityRegion # Add custom region class to the network Network.registerRegion(IdentityRegion) # Create a custom region network.addRegion("identityRegion", "py.IdentityRegion", json.dumps({ "dataWidth": sensor.encoder.getWidth(), })) # Link the Identity region to the sensor output network.link("sensor", "identityRegion", "UniformLink", "") network.initialize() return network
def testParameters(self): # Test setting and getting parameters net = Network() # Register ImageSensor region Network.registerRegion(ImageSensor) # Add sensor to the network sensor = net.addRegion("sensor", "py.ImageSensor", "{width: 100, height: 50}") # Verify get parameters self.assertEqual(sensor.getParameter("height"), 50) self.assertEqual(sensor.getParameter("width"), 100) # Verify set parameters sensor.setParameter("width", 42) self.assertEqual(sensor.getParameter("width"), 42)
def createSensorRegion(network, sensorType, encoders, dataSource): """ Initializes the sensor region with an encoder and data source. @param network (Network) @param sensorType (str) Specific type of region, e.g. "py.RecordSensor"; possible options can be found in /nupic/regions/. @param encoders (dict, encoder) If adding multiple encoders, pass a dict as specified in createEncoder() docstring. Otherwise an encoder object is expected. @param dataSource (RecordStream) Sensor region reads data from here. @return (Region) Sensor region of the network. """ # Sensor region may be non-standard, so add custom region class to the network if sensorType.split(".")[1] not in PY_REGIONS: # Add new region class to the network Network.registerRegion(EEGSensor) try: # Add region to network regionParams = json.dumps({"verbosity": _VERBOSITY}) network.addRegion("sensor", sensorType, regionParams) except RuntimeError: print ("Custom region not added correctly. Possible issues are the spec is " "wrong or the region class is not in the Python path.") return # getSelf() returns the actual region, instead of a region wrapper sensorRegion = network.regions["sensor"].getSelf() # Specify how RecordSensor encodes input values if isinstance(encoders, dict): # Multiple encoders to add sensorRegion.encoder = createEncoder(encoders) else: sensorRegion.encoder = encoders # Specify the dataSource as a file RecordStream instance sensorRegion.dataSource = dataSource return sensorRegion
def testParameters(self): # Test setting and getting parameters net = Network() # Register ImageSensor region Network.registerRegion(ImageSensor) # Add sensor to the network sensor = net.addRegion("sensor", "py.ImageSensor", "{width: 100, height: 50}") # Verify get parameters self.assertEqual(sensor.getParameter('height'), 50) self.assertEqual(sensor.getParameter('width'), 100) # Verify set parameters sensor.setParameter('width', 42) self.assertEqual(sensor.getParameter('width'), 42)
def _registerRegion(regionTypeName, moduleName=None): """ A region may be non-standard, so add custom region class to the network. @param regionTypeName: (str) type name of the region. E.g SensorRegion. @param moduleName: (str) location of the region class, only needed if registering a region that is outside the expected "regions/" dir. """ if moduleName is None: # the region is located in the regions/ directory moduleName = "htmresearch.regions." + regionTypeName if regionTypeName not in _PY_REGIONS: # Add new region class to the network. module = __import__(moduleName, {}, {}, regionTypeName) unregisteredClass = getattr(module, regionTypeName) Network.registerRegion(unregisteredClass) # Add region to list of registered PyRegions _PY_REGIONS.append(regionTypeName)
def loadFromFile(self, filename): """ Load a serialized network :param filename: Where the network should be loaded from """ print "Loading network from {file}...".format(file=filename) Network.unregisterRegion(SaccadeSensor.__name__) Network.registerRegion(SaccadeSensor) Network.registerRegion(TMRegion) self.net = Network(filename) self.networkSensor = self.net.regions["sensor"] self.networkSensor.setParameter("numSaccades", SACCADES_PER_IMAGE_TESTING) self.networkSP = self.net.regions["SP"] self.networkClassifier = self.net.regions["classifier"] self.numCorrect = 0
def _registerRegion(regionTypeName, moduleName=None): """ A region may be non-standard, so add custom region class to the network. @param regionTypeName: (str) type name of the region. E.g SensorRegion. """ if moduleName is None: moduleName = regionTypeName if regionTypeName not in _PY_REGIONS: # Add new region class to the network. try: module = __import__(moduleName, {}, {}, regionTypeName) unregisteredClass = getattr(module, regionTypeName) Network.registerRegion(unregisteredClass) # Add region to list of registered PyRegions _PY_REGIONS.append(regionTypeName) except ImportError: raise RuntimeError( "Could not import sensor \'{}\'.".format(regionTypeName))
def loadFromFile(self, filename): """ Load a serialized network :param filename: Where the network should be loaded from """ print "Loading network from {file}...".format(file=filename) Network.unregisterRegion(SaccadeSensor.__name__) Network.registerRegion(SaccadeSensor) Network.registerRegion(ExtendedTMRegion) self.net = Network(filename) self.networkSensor = self.net.regions["sensor"] self.networkSensor.setParameter("numSaccades", SACCADES_PER_IMAGE_TESTING) self.networkSP = self.net.regions["SP"] self.networkClassifier = self.net.regions["classifier"] self.numCorrect = 0
def registerResearchRegion(regionTypeName, moduleName=None): """ Register this region so that NuPIC can later find it. @param regionTypeName: (str) type name of the region. E.g LanguageSensor. @param moduleName: (str) location of the region class, only needed if registering a region that is outside the expected "regions/" dir. """ global _PY_REGIONS if moduleName is None: # the region is located in the regions/ directory moduleName = "htmresearch.regions." + regionTypeName if regionTypeName not in _PY_REGIONS: # Add new region class to the network. module = __import__(moduleName, {}, {}, regionTypeName) unregisteredClass = getattr(module, regionTypeName) Network.registerRegion(unregisteredClass) # Add region to list of registered PyRegions _PY_REGIONS.append(regionTypeName)
def testLoadImages(self): # Create a simple network with an ImageSensor. You can't actually run # the network because the region isn't connected to anything net = Network() Network.registerRegion(ImageSensor) net.addRegion("sensor", "py.ImageSensor", "{width: 32, height: 32}") sensor = net.regions['sensor'] # Create a dataset with two categories, one image in each category # Each image consists of a unique rectangle tmpDir = tempfile.mkdtemp() os.makedirs(os.path.join(tmpDir, '0')) os.makedirs(os.path.join(tmpDir, '1')) im0 = Image.new("L", (32, 32)) draw = ImageDraw.Draw(im0) draw.rectangle((10, 10, 20, 20), outline=255) im0.save(os.path.join(tmpDir, '0', 'im0.png')) im1 = Image.new("L", (32, 32)) draw = ImageDraw.Draw(im1) draw.rectangle((15, 15, 25, 25), outline=255) im1.save(os.path.join(tmpDir, '1', 'im1.png')) # Load the dataset and check we loaded the correct number sensor.executeCommand(["loadMultipleImages", tmpDir]) numImages = sensor.getParameter('numImages') self.assertEqual(numImages, 2) # Load a single image (this will replace the previous images) sensor.executeCommand( ["loadSingleImage", os.path.join(tmpDir, '1', 'im1.png')]) numImages = sensor.getParameter('numImages') self.assertEqual(numImages, 1) # Cleanup the temp files os.unlink(os.path.join(tmpDir, '0', 'im0.png')) os.unlink(os.path.join(tmpDir, '1', 'im1.png')) os.removedirs(os.path.join(tmpDir, '0')) os.removedirs(os.path.join(tmpDir, '1'))
def testLoadImages(self): # Create a simple network with an ImageSensor. You can't actually run # the network because the region isn't connected to anything net = Network() Network.registerRegion(ImageSensor) net.addRegion("sensor", "py.ImageSensor", "{width: 32, height: 32}") sensor = net.regions['sensor'] # Create a dataset with two categories, one image in each category # Each image consists of a unique rectangle tmpDir = tempfile.mkdtemp() os.makedirs(os.path.join(tmpDir,'0')) os.makedirs(os.path.join(tmpDir,'1')) im0 = Image.new("L",(32,32)) draw = ImageDraw.Draw(im0) draw.rectangle((10,10,20,20), outline=255) im0.save(os.path.join(tmpDir,'0','im0.png')) im1 = Image.new("L",(32,32)) draw = ImageDraw.Draw(im1) draw.rectangle((15,15,25,25), outline=255) im1.save(os.path.join(tmpDir,'1','im1.png')) # Load the dataset and check we loaded the correct number sensor.executeCommand(["loadMultipleImages", tmpDir]) numImages = sensor.getParameter('numImages') self.assertEqual(numImages, 2) # Load a single image (this will replace the previous images) sensor.executeCommand(["loadSingleImage", os.path.join(tmpDir,'1','im1.png')]) numImages = sensor.getParameter('numImages') self.assertEqual(numImages, 1) # Cleanup the temp files os.unlink(os.path.join(tmpDir,'0','im0.png')) os.unlink(os.path.join(tmpDir,'1','im1.png')) os.removedirs(os.path.join(tmpDir,'0')) os.removedirs(os.path.join(tmpDir,'1'))
def testSimpleImageNetwork(self): Network.registerRegion(ImageSensor) # Create the network and get region instances net = Network() net.addRegion("sensor", "py.ImageSensor", "{width: 32, height: 32}") net.addRegion("classifier","py.KNNClassifierRegion", "{distThreshold: 0.01, maxCategoryCount: 2}") net.link("sensor", "classifier", "UniformLink", "", srcOutput = "dataOut", destInput = "bottomUpIn") net.link("sensor", "classifier", "UniformLink", "", srcOutput = "categoryOut", destInput = "categoryIn") net.initialize() sensor = net.regions["sensor"] classifier = net.regions["classifier"] # Create a dataset with two categories, one image in each category # Each image consists of a unique rectangle tmpDir = tempfile.mkdtemp() os.makedirs(os.path.join(tmpDir,"0")) os.makedirs(os.path.join(tmpDir,"1")) im0 = Image.new("L",(32,32)) draw = ImageDraw.Draw(im0) draw.rectangle((10,10,20,20), outline=255) im0.save(os.path.join(tmpDir,"0","im0.png")) im1 = Image.new("L",(32,32)) draw = ImageDraw.Draw(im1) draw.rectangle((15,15,25,25), outline=255) im1.save(os.path.join(tmpDir,"1","im1.png")) # Load the dataset sensor.executeCommand(["loadMultipleImages", tmpDir]) numImages = sensor.getParameter("numImages") self.assertEqual(numImages, 2) # Ensure learning is turned ON self.assertEqual(classifier.getParameter("learningMode"), 1) # Train the network (by default learning is ON in the classifier) # and then turn off learning and turn on inference mode net.run(2) classifier.setParameter("inferenceMode", 1) classifier.setParameter("learningMode", 0) # Check to make sure learning is turned OFF and that the classifier learned # something self.assertEqual(classifier.getParameter("learningMode"), 0) self.assertEqual(classifier.getParameter("inferenceMode"), 1) self.assertEqual(classifier.getParameter("categoryCount"),2) self.assertEqual(classifier.getParameter("patternCount"),2) # Now test the network to make sure it categories the images correctly numCorrect = 0 for i in range(2): net.run(1) inferredCategory = classifier.getOutputData("categoriesOut").argmax() if sensor.getOutputData("categoryOut") == inferredCategory: numCorrect += 1 self.assertEqual(numCorrect,2) # Cleanup the temp files os.unlink(os.path.join(tmpDir,"0","im0.png")) os.unlink(os.path.join(tmpDir,"1","im1.png")) os.removedirs(os.path.join(tmpDir,"0")) os.removedirs(os.path.join(tmpDir,"1"))
def createNet(self): """ Set up the structure of the network """ net = Network() Network.unregisterRegion(SaccadeSensor.__name__) Network.registerRegion(SaccadeSensor) Network.unregisterRegion(ExtendedTMRegion.__name__) Network.registerRegion(ExtendedTMRegion) Network.unregisterRegion(ColumnPoolerRegion.__name__) Network.registerRegion(ColumnPoolerRegion) imageSensorParams = copy.deepcopy(DEFAULT_IMAGESENSOR_PARAMS) if self.loggingDir is not None: imageSensorParams["logDir"] = "sensorImages/" + self.loggingDir imageSensorParams["logOutputImages"] = 1 imageSensorParams["logOriginalImages"] = 1 imageSensorParams["logFilteredImages"] = 1 imageSensorParams["logLocationImages"] = 1 imageSensorParams["logLocationOnOriginalImage"] = 1 net.addRegion("sensor", "py.SaccadeSensor", yaml.dump(imageSensorParams)) sensor = net.regions["sensor"].getSelf() DEFAULT_SP_PARAMS["columnCount"] = sensor.getOutputElementCount( "dataOut") net.addRegion("SP", "py.SPRegion", yaml.dump(DEFAULT_SP_PARAMS)) sp = net.regions["SP"].getSelf() DEFAULT_TM_PARAMS["columnDimensions"] = ( sp.getOutputElementCount("bottomUpOut"), ) DEFAULT_TM_PARAMS["basalInputWidth"] = sensor.getOutputElementCount( "saccadeOut") net.addRegion("TM", "py.ExtendedTMRegion", yaml.dump(DEFAULT_TM_PARAMS)) net.addRegion("TP", "py.ColumnPoolerRegion", yaml.dump(DEFAULT_TP_PARAMS)) net.addRegion("classifier", "py.KNNClassifierRegion", yaml.dump(DEFAULT_CLASSIFIER_PARAMS)) net.link("sensor", "SP", "UniformLink", "", srcOutput="dataOut", destInput="bottomUpIn") net.link("SP", "TM", "UniformLink", "", srcOutput="bottomUpOut", destInput="activeColumns") net.link("sensor", "TM", "UniformLink", "", srcOutput="saccadeOut", destInput="basalInput") net.link("TM", "TP", "UniformLink", "", srcOutput="predictedActiveCells", destInput="feedforwardInput") net.link("TP", "TM", "UniformLink", "", srcOutput="feedForwardOutput", destInput="apicalInput") net.link("TP", "classifier", "UniformLink", "", srcOutput="feedForwardOutput", destInput="bottomUpIn") #net.link("TM", "classifier", "UniformLink", "", # srcOutput="predictedActiveCells", destInput="bottomUpIn") net.link("sensor", "classifier", "UniformLink", "", srcOutput="categoryOut", destInput="categoryIn") self.net = net self.networkSensor = self.net.regions["sensor"] self.networkSP = self.net.regions["SP"] self.networkTM = self.net.regions["TM"] self.networkTP = self.net.regions["TP"] self.networkClassifier = self.net.regions["classifier"]
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # See the GNU Affero Public License for more details. # # You should have received a copy of the GNU Affero Public License # along with this program. If not, see http://www.gnu.org/licenses. # # http://numenta.org/licenses/ # ---------------------------------------------------------------------- """Example showing usage of PCANode.""" from nupic.engine import Network from nupic.vision.regions.ImageSensor import ImageSensor from nupic.vision.regions.PCANode import PCANode Network.registerRegion(ImageSensor) Network.registerRegion(PCANode) def runExperiment(): Network.unregisterRegion("ImageSensor") Network.registerRegion(ImageSensor) Network.registerRegion(PCANode) inputSize = 8 net = Network() sensor = net.addRegion( "sensor", "py.ImageSensor", "{ width: %d, height: %d }" % (inputSize, inputSize)) params = ("{bottomUpCount: %s, "
def testSimpleImageNetwork(self): Network.registerRegion(ImageSensor) # Create the network and get region instances net = Network() net.addRegion("sensor", "py.ImageSensor", "{width: 32, height: 32}") net.addRegion("classifier", "py.KNNClassifierRegion", "{distThreshold: 0.01, maxCategoryCount: 2}") net.link("sensor", "classifier", "UniformLink", "", srcOutput="dataOut", destInput="bottomUpIn") net.link("sensor", "classifier", "UniformLink", "", srcOutput="categoryOut", destInput="categoryIn") net.initialize() sensor = net.regions["sensor"] classifier = net.regions["classifier"] # Create a dataset with two categories, one image in each category # Each image consists of a unique rectangle tmpDir = tempfile.mkdtemp() os.makedirs(os.path.join(tmpDir, "0")) os.makedirs(os.path.join(tmpDir, "1")) im0 = Image.new("L", (32, 32)) draw = ImageDraw.Draw(im0) draw.rectangle((10, 10, 20, 20), outline=255) im0.save(os.path.join(tmpDir, "0", "im0.png")) im1 = Image.new("L", (32, 32)) draw = ImageDraw.Draw(im1) draw.rectangle((15, 15, 25, 25), outline=255) im1.save(os.path.join(tmpDir, "1", "im1.png")) # Load the dataset sensor.executeCommand(["loadMultipleImages", tmpDir]) numImages = sensor.getParameter("numImages") self.assertEqual(numImages, 2) # Ensure learning is turned ON self.assertEqual(classifier.getParameter("learningMode"), 1) # Train the network (by default learning is ON in the classifier) # and then turn off learning and turn on inference mode net.run(2) classifier.setParameter("inferenceMode", 1) classifier.setParameter("learningMode", 0) # Check to make sure learning is turned OFF and that the classifier learned # something self.assertEqual(classifier.getParameter("learningMode"), 0) self.assertEqual(classifier.getParameter("inferenceMode"), 1) self.assertEqual(classifier.getParameter("categoryCount"), 2) self.assertEqual(classifier.getParameter("patternCount"), 2) # Now test the network to make sure it categories the images correctly numCorrect = 0 for i in range(2): net.run(1) inferredCategory = classifier.getOutputData( "categoriesOut").argmax() if sensor.getOutputData("categoryOut") == inferredCategory: numCorrect += 1 self.assertEqual(numCorrect, 2) # Cleanup the temp files os.unlink(os.path.join(tmpDir, "0", "im0.png")) os.unlink(os.path.join(tmpDir, "1", "im1.png")) os.removedirs(os.path.join(tmpDir, "0")) os.removedirs(os.path.join(tmpDir, "1"))
def setUp(self): Network.registerRegion(ImageSensor) Network.registerRegion(PCANode)
def createNetwork(): net = Network() # Register Regions Network.registerRegion(PluggableUniverseSensor) Network.registerRegion(AgentStateRegion) Network.registerRegion(ReinforcementRegion) Network.registerRegion(MRegion) Network.registerRegion(SensoryIntegrationRegion) # HTMResearch regions Network.registerRegion(ExtendedTMRegion) Network.registerRegion(MyTMRegion) Network.registerRegion(MySPRegion) Network.registerRegion(MyTemporalPoolerRegion) sensor = net.addRegion("sensor", 'py.PluggableUniverseSensor', "").getSelf() sensor.encoder = UniverseEncoder(**SENSOR_PARAMS) # ~~~~~~~~~~~~~~~ Create Regions ~~~~~~~~~~~~~~~ # Make sure the SP input width matches the sensor output width. L4_SP_PARAMS["inputWidth"] = sensor.encoder.getWidth() L4_SP = net.addRegion("L4_SP", "py.MySPRegion", json.dumps(L4_SP_PARAMS)).getSelf() #L4_TM = net.addRegion("L4_TM", "py.ExtendedTMRegion", json.dumps(L4_TM_PARAMS)).getSelf(); L4_TM = net.addRegion("L4_TM", "py.SensoryIntegrationRegion", json.dumps(L4_WEIGHTED_PARAMS)).getSelf() TP_PARAMS["inputWidth"] = L4_TM.getOutputElementCount("activeCells") L3_TP = net.addRegion("L3_TP", "py.MyTemporalPoolerRegion", json.dumps(TP_PARAMS)).getSelf() L3_TM = net.addRegion("L3_TM", "py.MyTMRegion", json.dumps(DEFAULT_TM_PARAMS)).getSelf() #net.regions["L3_TM"].setParameter("inferenceMode", True) TP_PARAMS["inputWidth"] = L3_TM.getOutputElementCount("activeCells") L2_TP = net.addRegion("L2_TP", "py.MyTemporalPoolerRegion", json.dumps(TP_PARAMS)).getSelf() L2_TM = net.addRegion("L2_TM", "py.MyTMRegion", json.dumps(DEFAULT_TM_PARAMS)).getSelf() net.regions["L2_TM"].setParameter("inferenceMode", True) # DEFAULT_ETM_PARAMS["basalInputWidth"] = L4_TM.getOutputElementCount("activeCells") DEFAULT_SP_PARAMS["inputWidth"] = L4_TM.getOutputElementCount( "activeCells") L5_SP = net.addRegion("L5_SP", "py.MySPRegion", json.dumps(DEFAULT_SP_PARAMS)).getSelf() DEFAULT_ETM_PARAMS["basalInputWidth"] = L4_TM.getOutputElementCount( "activeCells") + L2_TM.getOutputElementCount("activeCells") L5_TM = net.addRegion("L5_TM", "py.AgentStateRegion", json.dumps(DEFAULT_ETM_PARAMS)).getSelf() DEFAULT_SP_PARAMS["inputWidth"] = L5_SP.getOutputElementCount( "bottomUpOut") D1_SP = net.addRegion("D1_SP", "py.MySPRegion", json.dumps(DEFAULT_SP_PARAMS)).getSelf() TD_ETM_PARAMS["basalInputWidth"] = L5_TM.getOutputElementCount( "activeCells") D1_TM = net.addRegion("D1_TM", "py.ReinforcementRegion", json.dumps(TD_ETM_PARAMS)).getSelf() D2_SP = net.addRegion("D2_SP", "py.MySPRegion", json.dumps(DEFAULT_SP_PARAMS)).getSelf() D2_TM = net.addRegion("D2_TM", "py.ReinforcementRegion", json.dumps(TD_ETM_PARAMS)).getSelf() MOTOR_PARAMS["basalInputWidth"] = L5_TM.getOutputElementCount( "activeCells") MOTOR_PARAMS["apicalInputWidth"] = D1_TM.getOutputElementCount( "activeCells") Motor = net.addRegion("Motor", "py.MRegion", json.dumps(MOTOR_PARAMS)).getSelf() # ~~~~~~~~~~~~~~~ Link Regions ~~~~~~~~~~~~~~~ # L4: Proximal: Sensor -> L4 # Distal: Motor -> L4 (alt. Motor & Sensor for distal input) net.link("sensor", "L4_SP", "UniformLink", "", srcOutput="encoded", destInput="bottomUpIn") net.link("L4_SP", "L4_TM", "UniformLink", "", srcOutput="bottomUpOut", destInput="activeColumns") net.link("Motor", "L4_TM", "UniformLink", "", srcOutput="winnerCells", destInput="basalInput") # Temporal Pooling --> Overlap_i = (PrevOverlap_i + Overlap_i) * Overlap_decay (0.9 in this study) # L3: Proximal: L4 -> L3 # Distal: L3 -> L3 net.link("L4_TM", "L3_TP", "UniformLink", "", srcOutput="activeCells", destInput="activeCells") net.link("L4_TM", "L3_TP", "UniformLink", "", srcOutput="predictedActiveCells", destInput="predictedActiveCells") net.link("L3_TP", "L3_TM", "UniformLink", "", srcOutput="mostActiveCells", destInput="bottomUpIn") # L2: Proximal: L3 -> L2 # Distal: L2 -> L2 net.link("L3_TM", "L2_TP", "UniformLink", "", srcOutput="activeCells", destInput="activeCells") net.link("L3_TM", "L2_TP", "UniformLink", "", srcOutput="predictedActiveCells", destInput="predictedActiveCells") net.link("L2_TP", "L2_TM", "UniformLink", "", srcOutput="mostActiveCells", destInput="bottomUpIn") # L5: Proximal: L4 -> L5 # Distal: L4(t-1), L2(t-1) -> L5 net.link("L4_TM", "L5_SP", "UniformLink", "", srcOutput="activeCells", destInput="bottomUpIn") net.link("L5_SP", "L5_TM", "UniformLink", "", srcOutput="bottomUpOut", destInput="activeColumns") # net.link("L2_TM", "L5_TM", "UniformLink", "", # srcOutput="activeCells", destInput="basalInput") net.link("L4_TM", "L5_TM", "UniformLink", "", srcOutput="activeCells", destInput="basalInput") # D1: Proximal: L5 -> D1 # Distal: L5 -> D1 net.link("L5_SP", "D1_SP", "UniformLink", "", srcOutput="bottomUpOut", destInput="bottomUpIn") net.link("D1_SP", "D1_TM", "UniformLink", "", srcOutput="bottomUpOut", destInput="activeColumns") net.link("L5_TM", "D1_TM", "UniformLink", "", srcOutput="activeCells", destInput="basalInput") net.link("sensor", "D1_TM", "UniformLink", "", srcOutput="reward", destInput="reward") # D2: Proximal: L5 -> D2 # Distal: L5 -> D2 net.link("L5_SP", "D2_SP", "UniformLink", "", srcOutput="bottomUpOut", destInput="bottomUpIn") net.link("D2_SP", "D2_TM", "UniformLink", "", srcOutput="bottomUpOut", destInput="activeColumns") net.link("L5_TM", "D2_TM", "UniformLink", "", srcOutput="activeCells", destInput="basalInput") net.link("sensor", "D2_TM", "UniformLink", "", srcOutput="reward", destInput="reward") # Motor: Apical: D1, D2 -> Motor (TD Learning) # State-copy: L5 SP and Neural, Basal activation # Extra: (TDError) D1,D2 -> Motor net.link("D1_TM", "Motor", "UniformLink", "", srcOutput="activeCells", destInput="apicalInputD1") net.link("D2_TM", "Motor", "UniformLink", "", srcOutput="activeCells", destInput="apicalInputD2") net.link("D1_TM", "Motor", "UniformLink", "", srcOutput="TDError", destInput="TDErrorD1") net.link("D2_TM", "Motor", "UniformLink", "", srcOutput="TDError", destInput="TDErrorD2") # # Copy of L5 state (active columns and depolarizedBasalCells) net.link("L5_SP", "Motor", "UniformLink", "", srcOutput="bottomUpOut", destInput="activeColumns") net.link("L5_TM", "Motor", "UniformLink", "", srcOutput="predictedCells", destInput="depolarizedBasalCells") net.link("L5_TM", "Motor", "UniformLink", "", srcOutput="activeCells", destInput="activeCells") # Link Sensor.resetOut -> resetIn (All) net.link("sensor", "L4_SP", "UniformLink", "", srcOutput="resetOut", destInput="resetIn") net.link("sensor", "L4_TM", "UniformLink", "", srcOutput="resetOut", destInput="resetIn") net.link("sensor", "L3_TP", "UniformLink", "", srcOutput="resetOut", destInput="resetIn") net.link("sensor", "L3_TM", "UniformLink", "", srcOutput="resetOut", destInput="resetIn") net.link("sensor", "L2_TP", "UniformLink", "", srcOutput="resetOut", destInput="resetIn") net.link("sensor", "L2_TM", "UniformLink", "", srcOutput="resetOut", destInput="resetIn") net.link("sensor", "L5_SP", "UniformLink", "", srcOutput="resetOut", destInput="resetIn") net.link("sensor", "L5_TM", "UniformLink", "", srcOutput="resetOut", destInput="resetIn") net.link("sensor", "D1_SP", "UniformLink", "", srcOutput="resetOut", destInput="resetIn") net.link("sensor", "D1_TM", "UniformLink", "", srcOutput="resetOut", destInput="resetIn") net.link("sensor", "D2_SP", "UniformLink", "", srcOutput="resetOut", destInput="resetIn") net.link("sensor", "D2_TM", "UniformLink", "", srcOutput="resetOut", destInput="resetIn") net.link("sensor", "Motor", "UniformLink", "", srcOutput="resetOut", destInput="resetIn") layers = extractRegions(net) return (net, layers)