def setParameter(self, name, index, value): """Set the value of a parameter.""" if name == "SVDSampleCount": self._SVDSampleCount = value elif name == "logPath": self._logPath = value else: PyRegion.setParameter(self, name, index, value)
def getParameter(self, name, index=-1): """ Get the value of the parameter. @param name -- the name of the parameter to retrieve, as defined by the Node Spec. """ # If any spec parameter name is the same as an attribute, this call # will get it automatically, e.g. self.learningMode return PyRegion.getParameter(self, name, index)
def __init__(self, columnCount, inputWidth, poolerType, **kwargs): if columnCount <= 0 or inputWidth <=0: raise TypeError("Parameters columnCount and inputWidth must be > 0") # Pull out the pooler arguments automatically # These calls whittle down kwargs and create instance variables of PoolingRegion self._poolerClass = _getPoolerClass(poolerType) pArgTuples = _buildArgs(self._poolerClass, self, kwargs) # Make a list of automatic pooler arg names for later use self._poolerArgNames = [t[0] for t in pArgTuples] PyRegion.__init__(self, **kwargs) # Defaults for all other parameters self.learningMode = True self._inputWidth = inputWidth self._columnCount = columnCount # pooler instance self._pooler = None
def __init__(self, columnCount, inputWidth, poolerType, **kwargs): if columnCount <= 0 or inputWidth <= 0: raise TypeError( "Parameters columnCount and inputWidth must be > 0") # Pull out the pooler arguments automatically # These calls whittle down kwargs and create instance variables of PoolingRegion self._poolerClass = _getPoolerClass(poolerType) pArgTuples = _buildArgs(self._poolerClass, self, kwargs) # Make a list of automatic pooler arg names for later use self._poolerArgNames = [t[0] for t in pArgTuples] PyRegion.__init__(self, **kwargs) # Defaults for all other parameters self.learningMode = True self._inputWidth = inputWidth self._columnCount = columnCount # pooler instance self._pooler = None
def setParameter(self, name, index, value): """ Set the value of the parameter. @param name -- the name of the parameter to update, as defined by the Node Spec. @param value -- the value to which the parameter is to be set. """ if name == "learningMode": self.learningMode = bool(int(value)) elif name == "inferenceMode": self.inferenceMode = bool(int(value)) else: return PyRegion.setParameter(self, name, index, value)
def getParameter(self, name, index=-1): """ Get the value of a parameter. Note: this method may be overridden by derived classes, but if so, then the derived class should invoke this base method if 'name' is unknown to the derived class. @param name -- the name of the parameter to retrieve, as defined by the Node Spec. """ if name == "SVDSampleCount": return self._SVDSampleCount elif name == "SVDDimCount": return self._SVDDimCount elif name == "fractionOfMax": return self._fractionOfMax elif name == "trainingSampleCount": return self.gettrainingSampleCount() else: # If any spec parameter name is the same as an attribute, this call # will get it automatically, e.g. self.learningMode return PyRegion.getParameter(self, name, index)
def getParameter(self, parameterName, nodeSet=""): if parameterName == 'numCategories': return len(self._categories) elif parameterName == 'categoryList': return self._categories elif parameterName == "numBlockPresentations": # Compute total number of block presentations edgeLen = 2 * self.radialLength + 1 return 0 if self.mode != "block" else len(self._categories) * edgeLen * edgeLen elif parameterName == 'outputImage': if self._canvas is None: return '' # Can't just do "return" because it shows up as "None"! outputArray = self._canvas.astype(numpy.uint8) * 255 outputArray = outputArray.reshape((self.height, self.width)) outputImage = Image.fromarray(outputArray) return serializeImage(outputImage) elif parameterName == 'locationImage': if self._fullImage is None: return '' # Can't just do "return" because it shows up as "None"! return serializeImage(self._createLocationImage()) elif parameterName == 'metadata': return str(self._lastMetadata) else: return PyRegion.getParameter(self, parameterName, nodeSet)
def _init(self, # Outputs dataOut=None, categoryOut=None, resetOut=None, distanceOut=None, # Generic parameters width=256, height=256, logPrefix='', replayMode=False, configPath=None, emitResets=True, seed=42, # Controls pattern generation mode="random", sequenceLength=32, minThickness=1, maxThickness=4, minPatternSize=32, maxPatternSize=128, minVelocity=1, maxVelocity=3, minAngularPosn=0.0, maxAngularPosn=0.0, minAngularVelocity=0.0, maxAngularVelocity=0.0, lockCategories=False, numRepetitions=1, noiseLevel=0.0, noiseWhere='fb', noiseThickness=1, radialLength=8, radialStep=1, spaceShape=None, spreadShape=None, stepSize=1, sweepOffMode=False, cropToBox=True, maxOffset=-1, **kwargs): """ @param width: Specifies the width, in pixels, of each image that is to be generated. @param height: Specifies the height, in pixels, of each image that is to be generated. @param logPrefix: Specifies the prefix to use for logging images (including directory(s) if desired); if evaluates to boolean False, then no logging of generated images will be performed. @param replayMode: If True (and if logPrefix is non-None), then logPrefix will be treated as specifying the prefix of files to read from and inject into the network, instead of the default behavior of logging. @param configPath: the PicturesGenerator configuration path to use for non-novel categories; if None (the default), then use the default PicturesGenerator configuration file. @param emitResets: If True (default) sets the 'resetOut' output on the first pattern of each sequence and clears it for all subsequent patterns in the sequence. If False, then never sets the 'resetOut' output. @param seed: Integer specifying a value to be used for seeding the pseudo random number generated used (exclusively) by PictureSensor as a source of randomness; the same value is used for seeding each explorer's private/internal PRNG. @param mode: Controls the translational positioning of generated patterns; selects a named explorer that is expected to be found and loaded from the explorer plugin directory. @param sequenceLength: Specifies the number presentations that is to constitute a single sequence (which will typically include a 'reset' signal at the beginning.) @param minThickness, maxThickness: Controls the range (in pixels) of line thickness; Each generated image will be rendered with a fixed line thickness, which will be chosen randomly from within the specified range. @param minPatternSize, maxPatternSize: Controls the size (in pixels) of each generated pattern (or "object"); in general, this may be either larger or smaller than the (width, height) of the output image. Currently, there exists a single generative parameter (pattern size) which forces all generated patterns to be drawn within a square drawing zone (although the category's specific aspect ratio will then further reduce this drawing zone and allow for non-square patterns.) @param minVelocity, maxVelocity: Controls the range (in pixels/iteration) of sweep velocity (for those explorers that support the concept of sweeps, such as "random"). Each sequence will be initialized with a velocity chosen from this range. The velocity applies to both horizontal and vertical motion. @param minAngularPosn, maxAngularPosn: Controls the range (in degrees) in which patterns are initially rotated at the beginning of each sequence, where a value of 0 implies no rotation with respect to the raw pattern produced by PictureGenerator. @param minAngularVelocity, maxAngularVelocity: Controls the range (in degrees/iteration) in which patterns will rotate throughout the course of a sequence. @param lockCategories: a boolean which if True forces a single instance of each category to be used, as opposed to randomly generating new instances (specimens) of a category for each sequence. @param numRepetitions: the number of times that each sequence should be repeated verbatim; the convention is that a value of 1 will correspond to a single presentation of each unique sequence (e.g., ABCDE), a value of 2 will correspond to a pair of identical sequences (AABBCCDDEE), etc. @param noiseLevel: specifies the probability of flipping each bit in order to simulate shot noise on the output images. @param noiseThickness: Specifies the thickness (in number of pixels) of bit flips. For example, noiseThickness = 2 means that blocks of 2x2 pixels are flipped when adding noise to an image. @param noiseWhere: where to apply noise, can be 'f' (foreground), 'b' (background), or 'fb' (foreground and background) @param radialLength: The number of presentations per radial (minus one). @param radialStep: The number of pixels to advance per radial presentation. Thus, the total "length" (in pixels) of a radial in image space is 1 + radialLength * radialStep @param spaceShape: The (height, width) of the 2-D space to explore for the blockSpread explorer. This sets the number of center-points @param spreadShape: The (height, width) of the area around each center point to explore for the blockSpread explorer. @param stepSize: The step size in pixels. This controls the spacing of both the spaceShape points and the spreadShape points @param sweepOffMode: If True, causes the position of objects within random sweep sequences (i.e., sequences generated in mode 'random') to sweep off the edge of the canvas; if False (default), objects will "bounce" off the edge of the image. @param cropToBox: If True, generated pattern is cropped to its bounding box before being centered. @param maxOffset: If set, the object won't be shifted more than this many pixels from center. Only has an effect when mode=='random'. """ PyRegion.__init__(self, **kwargs) # Make sure dataOut is correct size for the canvas reqdDataOut = height * width if dataOut is not None: if hasattr(dataOut, '__iter__'): # dataOut is a 2- or 3-dimensional vector (3 if coming from the VF) # Compute the product lenDataOut = reduce(lambda x, y: x*y, dataOut) else: lenDataOut = dataOut if lenDataOut != reqdDataOut: raise RuntimeError("The 'dataOut' output element count must be equal" " to height (%d) * width (%d), or %d" \ % (height, width, reqdDataOut)) if categoryOut is not None and categoryOut and categoryOut != 1: raise RuntimeError("The 'categoryOut' output element count must be 1.") if resetOut is not None and resetOut and resetOut != 1: raise RuntimeError("The 'resetOut' output element count must be 1.") if distanceOut is not None and distanceOut and distanceOut != 1: raise RuntimeError("The 'distanceOut' output element count must be 1.") # If running in replay mode, must have a valid logPrefix if replayMode and not logPrefix: raise RuntimeError("Replay mode requires a valid logPrefix be specified.") self.width = width self.height = height self.sequenceLength = sequenceLength self.minThickness = minThickness self.maxThickness = maxThickness self.minPatternSize = minPatternSize self.maxPatternSize = maxPatternSize self.minVelocity = minVelocity self.maxVelocity = maxVelocity self.minAngularPosn = minAngularPosn self.maxAngularPosn = maxAngularPosn self.minAngularVelocity = minAngularVelocity self.maxAngularVelocity = maxAngularVelocity self.noiseLevel = noiseLevel self.noiseWhere = noiseWhere self.noiseThickness = noiseThickness self.mode = mode self.radialLength = radialLength self.radialStep = radialStep self.spaceShape = spaceShape self.spreadShape = spreadShape self.stepSize = stepSize self.lockCategories = lockCategories self.numRepetitions = numRepetitions self.logPrefix = logPrefix self.replayMode = replayMode self.seed = seed self.configPath = configPath self.emitResets = emitResets self.sweepOffMode = sweepOffMode self.cropToBox = cropToBox self.maxOffset = maxOffset self.lockedCatMap = {} # Internal state self._initEphemerals()