Ejemplo n.º 1
0
    def test_evaluateNxModel(self):
        """Check that NxModel can be evaluated like a Keras Model."""

        batchSize = 10
        batchInputShape = (batchSize, 7, 7, 1)
        inputLayer = NxInputLayer(batch_input_shape=batchInputShape)
        layer = NxConv2D(2, 3)(inputLayer.input)
        model = NxModel(inputLayer.input, layer)

        model.compile('sgd', 'mse')
        model.evaluate(np.random.random_sample(model.input_shape),
                       np.random.random_sample(model.output_shape))
        model.clearTemp()
Ejemplo n.º 2
0
    def test_saveLoadNxModel(self):
        """Check that NxModel can be saved and loaded like a Keras Model."""

        inputLayer = NxInputLayer(batch_input_shape=(1, 10, 10, 3))
        layer = NxConv2D(2, 3)(inputLayer.input)
        model1 = NxModel(inputLayer.input, layer)
        model1.compile('sgd', 'mse')
        model1.compileModel()
        model1.clearTemp()
        filename = os.path.abspath(
            os.path.join(os.path.dirname(os.path.realpath(__file__)),
                         '../../..', 'temp', str(hash(model1))))
        model1.save(filename)
        model2 = loadNxModel(filename)
        os.remove(filename)

        x = np.random.random_sample(model1.input_shape)
        y1 = model1.predict(x)
        y2 = model2.predict(x)

        self.assertTrue(np.array_equal(y1, y2))
Ejemplo n.º 3
0
num_input_neurons = 1024
num_output_neurons = 1

input_layer = NxInputLayer(num_input_neurons,
                           compartmentKwargs=compartment_kwargs,
                           inputMode=InputModes.AEDAT)

layer = NxDense(num_output_neurons,
                compartmentKwargs=compartment_kwargs)(input_layer.input)

nxmodel = NxModel(input_layer.input, layer)

nxmodel.summary()

nxmodel.compile()

num_steps_per_img = num_input_neurons + 1

nxmodel_composable = ComposableDNN(nxmodel, num_steps_per_img)
input_generator = SpikeInputGenerator(name='SpikeGen',
                                      packetSize=256,
                                      queueSize=64,
                                      numSnipsPerChip=1)
input_generator.connect(nxmodel_composable)
input_generator.processes.inputEncoder.executeAfter(
    nxmodel_composable.processes.reset)
model = ComposableModel('debug')
model.add(nxmodel_composable)
model.add(input_generator)
Ejemplo n.º 4
0
num_neurons = num_classes
biases = np.zeros(num_neurons)
layer = NxDense(num_neurons,
                weights=[weights, biases],
                compartmentKwargs=compartment_kwargs,
                connectionKwargs=conn_kwargs,
                resetMode=reset_mode,
                name=name)(layer)

# COMPILATION #
###############

# Compile complete model.
nxmodel = NxModel(input_layer.input, layer)
nxmodel.summary()
nxmodel.compile()  # Keras compile function. Not yet invoking NxTF compiler.

# Apply pooling weights.
for name, weights in pool_weights.items():
    nxmodel.get_layer(name).set_weights(weights)

# Use the composablility interface of NxSDK to wrap our NxTF model. Allows
# more efficient input injection, output readout, and resetting via snips.
nxmodel_composable = ComposableDNN(nxmodel,
                                   num_steps_per_img,
                                   enable_reset=reset_between_samples)
model = ComposableModel('gestures_cnn')
input_generator = SpikeInputGenerator(name='SpikeGen',
                                      packetSize=256,
                                      numSnipsPerChip=3,
                                      queueSize=512)
Ejemplo n.º 5
0
    def test_NxInputLayer(self):
        """
        Test input layer in soft-reset mode.
        """

        plot = False
        verbose = False

        resetMode = 'soft'
        neuronSize = 2 if resetMode == 'soft' else 1

        input_shape = (32, 32, 3)
        inputSize = np.prod(input_shape)
        inputLayer = NxInputLayer(input_shape,
                                  probeSpikes=True,
                                  vThMant=255,
                                  resetMode=resetMode)
        out = inputLayer.input

        model = NxModel(out, out)

        model.compile('adam', 'categorical_crossentropy', ['accuracy'])

        model.summary()

        layers = [inputLayer]

        input_image = np.linspace(
            0, 256, endpoint=False,
            num=inputSize).reshape(input_shape).astype(int)
        x_test = [input_image]
        y_test = [0]

        mapper = model.compileModel()
        if verbose:
            printLayerMappings(layers, mapper, synapses=True, inputAxons=True)
            printLayers(layers)

        layerProbes = []
        numProbes = 32
        for i, layer in enumerate(layers):
            shape = layer.output_shape[1:]

            # Define probes to read out currents.
            vProbes = []
            sProbes = []
            uProbes = []

            toProbe = numProbes if i == (len(layers) -
                                         1) else numProbes * neuronSize
            toProbe = min(toProbe,
                          int(np.asscalar(np.prod(shape))) * neuronSize)

            for j in range(toProbe):
                vProbes.append(layer[j].probe(ProbableStates.VOLTAGE))
                sProbes.append(layer[j].probe(ProbableStates.ACTIVITY))
                uProbes.append(layer[j].probe(ProbableStates.CURRENT))

            layerProbes.append([uProbes, vProbes, sProbes])

        # How many time steps to run each sample.
        num_steps = 1000
        # How many samples to test.
        num_samples_to_test = 1

        # Iterate over samples in testset.
        for i, (input_image, target) in enumerate(zip(x_test, y_test)):
            if i == num_samples_to_test:
                break

            if plot:
                plt.hist(input_image.ravel())
                plt.show()

            # Set input bias currents.
            for j, b in enumerate(np.ravel(input_image, 'F')):
                inputLayer[j * neuronSize].biasMant = b
                inputLayer[j * neuronSize].biasExp = 6
                inputLayer[j * neuronSize].phase = 2

            # Run model.
            model.run(num_steps)

        # Clean up.
        data = [[extract(probe) for probe in lp] for lp in layerProbes]
        spikesRates = []
        for i, (layer, d) in enumerate(zip(layers, data)):
            sData = d[2][:, (neuronSize - 1)::neuronSize]
            spikecount = (sData // 127).sum(0)
            spikesRates.append(spikecount / num_steps)

        model.disconnect()

        layer_activations = [x_test[0]]

        if plot:
            for activations, spikerate in zip(layer_activations, spikesRates):
                plt.figure()
                scale = np.max(activations)
                spikesFlat = spikerate  #spikerate.flatten()
                plt.plot(
                    activations.flatten('F')[:len(spikesFlat)] / scale,
                    spikesFlat, '.')

            plotLayerProbes(layers, data, neuronSize)

        cor = np.corrcoef(np.ravel(spikesRates[-1]),
                          np.ravel(layer_activations[0],
                                   'F')[:numProbes // 2])[0, 1]
        self.assertGreater(cor, 0.99)