Example #1
0
class VehicleLaneAlignmentNetworkIn(BaseNetwork):
    def __init__(self, topic='/laneletInformation'):
        super(VehicleLaneAlignmentNetworkIn, self).__init__()

        self._vehicle_alignment_pop = Population(1, IF_curr_alpha, {'i_offset': 0, 'v_thresh': 100,
                                                                    'tau_refrac': 0.1})

        detector = nest.nest.Create('spike_detector', params={'withgid': True, 'withtime': True})[0]

        self.detectors[self._vehicle_alignment_pop[0]] = detector
        nest.nest.Connect(self._vehicle_alignment_pop[0], detector)

        self._state = None
        self._sub_lanelet = rospy.Subscriber(topic, Float64MultiArray, self._update_state, queue_size=1)
        self.output_pop = self._vehicle_alignment_pop

        # make sure we get at least one message before starting the training
        rospy.wait_for_message(topic, Float64MultiArray)

    def _update_state(self, state):
        if len(state.data) > 0:
            self._state = LaneletInformation(state.data)

    def before_simulation(self):
        i_offset = np.pi - np.abs(self._state.angle_vehicle_lane)
        i_offset *= 30/np.pi
        print "I-Offset Vehicle Alignment: ", i_offset
        self._vehicle_alignment_pop.set(i_offset=i_offset)
def main(args):

    setup(timestep=0.1)

    random_image = np.random.rand(2,2)
    size = random_image.size


    input_population_arr = Population(random_image.size, SpikeSourceArray, {'spike_times': [0 for i in range(0, random_image.size)]})

    cell_params = {'tau_refrac': 2.0, 'v_thresh': -50.0, 'tau_syn_E': 2.0, 'tau_syn_I': 2.0}
    output_population = Population(1, IF_curr_alpha, cell_params, label="output")

    projection = Projection(input_population_arr, output_population, AllToAllConnector())
    projection.setWeights(1.0)

    input_population_arr.record('spikes')
    output_population.record('spikes')

    tstop = 1000.0

    run(tstop)

    output_population.write_data("simpleNetwork_output.pkl",'spikes')
    input_population_arr.write_data("simpleNetwork_input.pkl",'spikes')
    #output_population.print_v("simpleNetwork.v")
    end()
Example #3
0
def createNetwork():
    
    # the dimesion is X x Y x maxDisparity+1 because disparity 0 means no shift in pixel location
    # however the network should still exist and contain only one disparity map. 
    assert dimensionRetinaX > maxDisparity >= 0, "Maximum Disparity Constant is illegal!"
    print "Creating Cooperative Network..."
    
    print "\t Creating Populations..."
    from SimulationAndNetworkSettings import t_synE, t_synI, t_memb, vResetInh, vResetCO
    from pyNN.nest import record
    
    network = []
    for x in range(0, dimensionRetinaX):
        for disp in range(0, maxDisparity+1):
            inhLeftPop = Population(dimensionRetinaY, IF_curr_exp, {'tau_syn_E':t_synE, 'tau_syn_I':t_synI, 'tau_m':t_memb, 'v_reset':vResetInh},
                label="Blocker Left {0}".format(x*dimensionRetinaX + disp))
            inhRightPop = Population(dimensionRetinaY, IF_curr_exp, {'tau_syn_E':t_synE, 'tau_syn_I':t_synI, 'tau_m':t_memb, 'v_reset':vResetInh},
                label="Blocker Right {0}".format(x*dimensionRetinaX + disp))
            cellOutputPop = Population(dimensionRetinaY, IF_curr_exp, {'tau_syn_E':t_synE, 'tau_syn_I':t_synI, 'tau_m':t_memb, 'v_reset':vResetCO},
                label="Cell Output {0}".format(x*dimensionRetinaX + disp))
            
            # reocrd data for plotting purposes
            cellOutputPop.record('v')
            
            network.append((inhLeftPop, inhRightPop, cellOutputPop))
    
    interconnectNetworkNeurons(network)
    
    return network
Example #4
0
 def test_set_with_standard_celltype(self):
     p = Population(10, MockStandardCellType)
     p.all_cells = numpy.array([MockID()]*10, dtype=object) #numpy.arange(10)
     p._mask_local = numpy.ones((10,), bool)
     p.set("foo", 32)
     assert_equal(nest.SetStatus.call_args[0][1], {"FOO": 32.0})
     p.set("hoo", 33.0)
     assert_equal(nest.SetStatus.call_args[0][1], {"HOO": 99.0})
     p.set("woo", 6.0)
     assert_equal(nest.SetStatus.call_args[0][1], {})
     p.all_cells[0].set_parameters.assert_called_with(woo=6.0)
Example #5
0
 def test_set_with_native_celltype(self):
     gd_orig = nest.GetDefaults
     nest.GetDefaults = Mock(return_value={"FOO": 1.2, "HOO": 3.4, "WOO": 5.6})
     p = Population(10, MockNativeCellType)
     p.all_cells = numpy.array([MockID()]*10, dtype=object) #numpy.arange(10)
     p._mask_local = numpy.ones((10,), bool)
     p.set("FOO", 32)
     assert_equal(nest.SetStatus.call_args[0][1], {"FOO": 32.0})
     p.set("HOO", 33.0)
     assert_equal(nest.SetStatus.call_args[0][1], {"HOO": 33.0})
     p.set("WOO", 6.0)
     assert_equal(nest.SetStatus.call_args[0][1], {"WOO": 6.0})
     nest.GetDefaults = gd_orig
Example #6
0
 def test_set_with_standard_celltype(self):
     p = Population(10, MockStandardCellType)
     p.all_cells = numpy.array([MockID()] * 10, dtype=object)  # numpy.arange(10)
     p._mask_local = numpy.ones((10,), bool)
     p.set("foo", 32)
     assert_equal(nest.SetStatus.call_args[0][1], {"FOO": 32.0})
     p.set("hoo", 33.0)
     assert_equal(nest.SetStatus.call_args[0][1], {"HOO": 99.0})
     p.set("woo", 6.0)
     assert_equal(nest.SetStatus.call_args[0][1], {})
     p.all_cells[0].set_parameters.assert_called_with(woo=6.0)
Example #7
0
 def test_set_with_native_celltype(self):
     gd_orig = nest.GetDefaults
     nest.GetDefaults = Mock(return_value={"FOO": 1.2, "HOO": 3.4, "WOO": 5.6})
     p = Population(10, MockNativeCellType)
     p.all_cells = numpy.array([MockID()] * 10, dtype=object)  # numpy.arange(10)
     p._mask_local = numpy.ones((10,), bool)
     p.set("FOO", 32)
     assert_equal(nest.SetStatus.call_args[0][1], {"FOO": 32.0})
     p.set("HOO", 33.0)
     assert_equal(nest.SetStatus.call_args[0][1], {"HOO": 33.0})
     p.set("WOO", 6.0)
     assert_equal(nest.SetStatus.call_args[0][1], {"WOO": 6.0})
     nest.GetDefaults = gd_orig
Example #8
0
    def __init__(self, topic='/laneletInformation'):
        super(VehicleLaneAlignmentNetworkIn, self).__init__()

        self._vehicle_alignment_pop = Population(1, IF_curr_alpha, {'i_offset': 0, 'v_thresh': 100,
                                                                    'tau_refrac': 0.1})

        detector = nest.nest.Create('spike_detector', params={'withgid': True, 'withtime': True})[0]

        self.detectors[self._vehicle_alignment_pop[0]] = detector
        nest.nest.Connect(self._vehicle_alignment_pop[0], detector)

        self._state = None
        self._sub_lanelet = rospy.Subscriber(topic, Float64MultiArray, self._update_state, queue_size=1)
        self.output_pop = self._vehicle_alignment_pop

        # make sure we get at least one message before starting the training
        rospy.wait_for_message(topic, Float64MultiArray)
Example #9
0
    def _build_input_layer(self):
        num_neurons = self._width * self._height
        # Layer 1 (Input Image)
        self.pop_input_image_r = Population(num_neurons // 2, IF_curr_alpha, {'i_offset': np.zeros(num_neurons // 2)})
        self.pop_input_image_l = Population(num_neurons // 2, IF_curr_alpha, {'i_offset': np.zeros(num_neurons // 2)})

        # Two 4 x 4 grids, for left and right respectively
        # Layer 2: 4 x 4 Grid left
        self.pop_in_l2 = Population(4, IF_curr_alpha, {'i_offset': np.zeros(4), 'v_thresh': 100})
        conn_list_l = []
        for neuron in self.pop_input_image_l:
            neuron = neuron - self.pop_input_image_l.first_id
            if (neuron % 50) <= 25 and neuron <= 1250:  # oben links
                conn_list_l.append((neuron, 0, 1.0, 0.1))

            if (neuron % 50) > 25 and neuron <= 1250:  # oben rechts
                conn_list_l.append((neuron, 1, 1.0, 0.1))

            if (neuron % 50) <= 25 and neuron > 1250:  # unten links
                conn_list_l.append((neuron, 2, 1.0, 0.1))

            if (neuron % 50) > 25 and neuron > 1250:  # unten rechts   DOPPELT gewichtet
                conn_list_l.append((neuron, 3, 1.0, 0.1))

        # Layer 2: 4 x 4 Grid right
        self.pop_in_r2 = Population(4, IF_curr_alpha, {'i_offset': np.zeros(4), 'v_thresh': 100})
        conn_list_r = []
        for neuron in self.pop_input_image_r:
            neuron = neuron - self.pop_input_image_r.first_id
            if (neuron % 50) <= 25 and neuron <= 1250:  # oben links
                conn_list_r.append((neuron, 0, 1.0, 0.1))

            if (neuron % 50) > 25 and neuron <= 1250:  # oben recht
                conn_list_r.append((neuron, 1, 1.0, 0.1))

            if (neuron % 50) <= 25 and neuron > 1250:  # unten links DOPPELT gewichtet
                conn_list_r.append((neuron, 2, 1.0, 0.1))

            if (neuron % 50) > 25 and neuron > 1250:  # unten rechts
                conn_list_r.append((neuron, 3, 1.0, 0.1))

        # Layer 3 encoded ( The two 4x4 Grids are connected with one neuron each)
        self.pop_encoded_image_l = Population(1, IF_curr_alpha, {'tau_refrac': 0.1, 'v_thresh': -50.})
        self.pop_encoded_image_r = Population(1, IF_curr_alpha, {'tau_refrac': 0.1, 'v_thresh': -50.})

        # Connections: Layer 1 (Input Image) --> Layer 2 (4 x 4 Grids)
        self.projection_layer2_l = Projection(self.pop_input_image_l, self.pop_in_l2, FromListConnector(conn_list_l))
        self.projection_layer2_r = Projection(self.pop_input_image_r, self.pop_in_r2, FromListConnector(conn_list_r))
        self.projection_layer2_l.setWeights(1.0)
        self.projection_layer2_r.setWeights(1.0)

        # Connections: Layer 2 (4 x 4 Grids) --> Layer 3 (Encoded Image)
        # Excitatory connections to "this" side ('left' -> 'left', 'right' -> 'right')
        self.projection_out_l = Projection(self.pop_in_l2, self.pop_encoded_image_l, AllToAllConnector())
        self.projection_out_r = Projection(self.pop_in_r2, self.pop_encoded_image_r, AllToAllConnector())
        self.projection_out_l.setWeights(1.0)
        self.projection_out_r.setWeights(1.0)

        # bild seiten staerker gewichtet
        self.projection_out_l[0]._set_weight(2.0)
        self.projection_out_r[1]._set_weight(2.0)
        self.projection_out_l[2]._set_weight(4.0)
        self.projection_out_r[3]._set_weight(4.0)

        # Connections: Layer 2 (4 x 4 Grids) --> Layer 3 (Encoded Image)
        # Inhibitory connections to "other" side ('left' -> 'right', 'right' -> 'left')
        self.projection_out_l_i = Projection(self.pop_in_l2, self.pop_encoded_image_r, AllToAllConnector())
        self.projection_out_r_i = Projection(self.pop_in_r2, self.pop_encoded_image_l, AllToAllConnector())
        self.projection_out_l_i.setWeights(-1)
        self.projection_out_r_i.setWeights(-1)

        self.output_pop = [self.pop_encoded_image_l, self.pop_encoded_image_r]
Example #10
0
class BaseNetworkIn(BaseNetwork):
    """ Handles the frame injection and provides two input neurons (left, right)"""

    @property
    def last_frame(self):
        return self._last_frame

    @property
    def encoded_image_pops(self):
        return {'left': self.pop_encoded_image_l, 'right': self.pop_encoded_image_r}

    def __init__(self, image_topic='/spiky/retina_image'):
        super(BaseNetworkIn, self).__init__()

        self._postsynaptic_learning_neurons = []

        # Populations
        self.pop_input_image_r = None
        self.pop_input_image_l = None
        self.pop_encoded_image_l = None
        self.pop_encoded_image_r = None

        self.spikedetector_enc_image_l = None
        self.spikedetector_enc_image_r = None

        # Preparing sensory information subscriber
        self._bridge = CvBridge()
        self._last_frame = None
        self._retina_image_subscriber = rospy.Subscriber(image_topic, Image, self._handle_frame, queue_size=1)
        rospy.wait_for_message(image_topic, Image)

        if self._last_frame is not None:
            shape = np.shape(self._last_frame)
            self._width = shape[0]
            self._height = shape[1]

        self._build_input_layer()
        self._create_spike_detectors()

    def _build_input_layer(self):
        num_neurons = self._width * self._height
        # Layer 1 (Input Image)
        self.pop_input_image_r = Population(num_neurons // 2, IF_curr_alpha, {'i_offset': np.zeros(num_neurons // 2)})
        self.pop_input_image_l = Population(num_neurons // 2, IF_curr_alpha, {'i_offset': np.zeros(num_neurons // 2)})

        # Two 4 x 4 grids, for left and right respectively
        # Layer 2: 4 x 4 Grid left
        self.pop_in_l2 = Population(4, IF_curr_alpha, {'i_offset': np.zeros(4), 'v_thresh': 100})
        conn_list_l = []
        for neuron in self.pop_input_image_l:
            neuron = neuron - self.pop_input_image_l.first_id
            if (neuron % 50) <= 25 and neuron <= 1250:  # oben links
                conn_list_l.append((neuron, 0, 1.0, 0.1))

            if (neuron % 50) > 25 and neuron <= 1250:  # oben rechts
                conn_list_l.append((neuron, 1, 1.0, 0.1))

            if (neuron % 50) <= 25 and neuron > 1250:  # unten links
                conn_list_l.append((neuron, 2, 1.0, 0.1))

            if (neuron % 50) > 25 and neuron > 1250:  # unten rechts   DOPPELT gewichtet
                conn_list_l.append((neuron, 3, 1.0, 0.1))

        # Layer 2: 4 x 4 Grid right
        self.pop_in_r2 = Population(4, IF_curr_alpha, {'i_offset': np.zeros(4), 'v_thresh': 100})
        conn_list_r = []
        for neuron in self.pop_input_image_r:
            neuron = neuron - self.pop_input_image_r.first_id
            if (neuron % 50) <= 25 and neuron <= 1250:  # oben links
                conn_list_r.append((neuron, 0, 1.0, 0.1))

            if (neuron % 50) > 25 and neuron <= 1250:  # oben recht
                conn_list_r.append((neuron, 1, 1.0, 0.1))

            if (neuron % 50) <= 25 and neuron > 1250:  # unten links DOPPELT gewichtet
                conn_list_r.append((neuron, 2, 1.0, 0.1))

            if (neuron % 50) > 25 and neuron > 1250:  # unten rechts
                conn_list_r.append((neuron, 3, 1.0, 0.1))

        # Layer 3 encoded ( The two 4x4 Grids are connected with one neuron each)
        self.pop_encoded_image_l = Population(1, IF_curr_alpha, {'tau_refrac': 0.1, 'v_thresh': -50.})
        self.pop_encoded_image_r = Population(1, IF_curr_alpha, {'tau_refrac': 0.1, 'v_thresh': -50.})

        # Connections: Layer 1 (Input Image) --> Layer 2 (4 x 4 Grids)
        self.projection_layer2_l = Projection(self.pop_input_image_l, self.pop_in_l2, FromListConnector(conn_list_l))
        self.projection_layer2_r = Projection(self.pop_input_image_r, self.pop_in_r2, FromListConnector(conn_list_r))
        self.projection_layer2_l.setWeights(1.0)
        self.projection_layer2_r.setWeights(1.0)

        # Connections: Layer 2 (4 x 4 Grids) --> Layer 3 (Encoded Image)
        # Excitatory connections to "this" side ('left' -> 'left', 'right' -> 'right')
        self.projection_out_l = Projection(self.pop_in_l2, self.pop_encoded_image_l, AllToAllConnector())
        self.projection_out_r = Projection(self.pop_in_r2, self.pop_encoded_image_r, AllToAllConnector())
        self.projection_out_l.setWeights(1.0)
        self.projection_out_r.setWeights(1.0)

        # bild seiten staerker gewichtet
        self.projection_out_l[0]._set_weight(2.0)
        self.projection_out_r[1]._set_weight(2.0)
        self.projection_out_l[2]._set_weight(4.0)
        self.projection_out_r[3]._set_weight(4.0)

        # Connections: Layer 2 (4 x 4 Grids) --> Layer 3 (Encoded Image)
        # Inhibitory connections to "other" side ('left' -> 'right', 'right' -> 'left')
        self.projection_out_l_i = Projection(self.pop_in_l2, self.pop_encoded_image_r, AllToAllConnector())
        self.projection_out_r_i = Projection(self.pop_in_r2, self.pop_encoded_image_l, AllToAllConnector())
        self.projection_out_l_i.setWeights(-1)
        self.projection_out_r_i.setWeights(-1)

        self.output_pop = [self.pop_encoded_image_l, self.pop_encoded_image_r]

    def _handle_frame(self, frame):
        self._last_frame = self._bridge.imgmsg_to_cv2(frame)

    def _create_spike_detectors(self):
        self.spikedetector_enc_image_l = nest.nest.Create('spike_detector', params={'withgid': True, 'withtime': True})[
            0]
        self.spikedetector_enc_image_r = nest.nest.Create('spike_detector', params={'withgid': True, 'withtime': True})[
            0]

        nest.nest.Connect(self.pop_encoded_image_l[0], self.spikedetector_enc_image_l)
        nest.nest.Connect(self.pop_encoded_image_r[0], self.spikedetector_enc_image_r)

        self.detectors[self.pop_encoded_image_l[0]] = self.spikedetector_enc_image_l
        self.detectors[self.pop_encoded_image_r[0]] = self.spikedetector_enc_image_r

    def before_simulation(self):
        self.inject_frame(self.last_frame)

    def inject_frame(self, frame):
        frame_l = frame[0:50, 0:50]
        frame_r = frame[0:50, 50:100]

        self.pop_input_image_l.set(i_offset=frame_l.astype(float).flatten())
        self.pop_input_image_r.set(i_offset=frame_r.astype(float).flatten())

    def populate_plotter(self, plotter):
        plotter.add_spike_train_plot(self.pop_encoded_image_l, label='Input Left')
        plotter.add_spike_train_plot(self.pop_encoded_image_r, label='Input Right')
import pyNN
from pyNN.nest import Population, SpikeSourcePoisson, SpikeSourceArray, AllToAllConnector, run, setup, IF_curr_alpha
from pyNN.nest.projections import Projection
import matplotlib.pyplot as plt


setup(timestep=0.1)

image = cv2.imread('/fzi/ids/mlprak2/Bilder/impuls.tif')

image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
frame = np.zeros(image.size) + 255

rates_init = np.zeros(frame.size)

pop_in = Population(frame.shape, SpikeSourcePoisson, {'rate': rates_init})
pop_out = Population(1, IF_curr_alpha, {'tau_refrac': 5 })

projection = Projection(pop_in, pop_out, AllToAllConnector())
projection.setWeights(1.0)

pop_in.set(rate=image.astype(float).flatten())

pop_in.record('spikes')
pop_out.record('spikes')


tstop = 100.0
run(tstop)

spikes_in = pop_in.get_data()