def __init__(self, pop_labels): self._length_sender = SpynnakerLiveSpikesConnection( receive_labels=None, local_port=19999, send_labels=pop_labels) self._len = 0.95 self._dlen = 0.0 self._go_on = True for label in pop_labels: self._length_sender.add_start_callback(label, self.send_input_spindle) self._count = 0
def __init__(self, n_neurons): """ creates the gui :return: """ self._started = False # Set up the live connection for sending and receiving spikes self.live_spikes_connection = SpynnakerLiveSpikesConnection( receive_labels=None, local_port=19996, send_labels=["spike_injector_forward", "spike_injector_backward"]) # Set up callbacks to occur at the start of simulation self.live_spikes_connection.add_start_callback( "spike_injector_forward", self.send_input_forward) root = tk.Tk() root.title("Injecting Spikes GUI") label = tk.Label(root, fg="dark green") label.pack() neuron_id_value = tk.IntVar() self.neuron_id = tk.Spinbox(root, from_=0, to=n_neurons - 1, textvariable=neuron_id_value) self.neuron_id.pack() pop_label_value = tk.StringVar() self.pop_label = tk.Spinbox(root, textvariable=pop_label_value, values=("spike_injector_forward", "spike_injector_backward")) self.pop_label.pack() button = tk.Button(root, text='Inject', width=25, command=self.inject_spike) button.pack() root.mainloop()
def __init__(self, n_neurons, machine_time_step, timescale_factor, database_socket, label, port=12345, virtual_key=None, spikes_per_second=0, ring_buffer_sigma=None, device_id=0, fps=60, mode="128", scale_img=True, polarity="MERGED", inhibition=False, inh_area_width=2, threshold=12, adaptive_threshold=False, min_threshold=6, max_threshold=168, threshold_delta_down=2, threshold_delta_up=12, output_type="TIME", num_bits_per_spike=4, history_weight=0.99, save_spikes=None, run_time_ms=None, local_port=19876): """ :param device_id: int for webcam modes, or string for video file :param mode: The retina "mode" :param retina_key: The value of the top 16-bits of the key :param polarity: The "polarity" of the retina data :param machine_time_step: The time step of the simulation :param timescale_factor: The timescale factor of the simulation :param label: The label for the population :param n_neurons: The number of neurons in the population """ fixed_n_neurons = n_neurons if mode == ExternalDvsEmulatorDevice.MODE_128 or \ mode == ExternalDvsEmulatorDevice.MODE_64 or \ mode == ExternalDvsEmulatorDevice.MODE_32 or \ mode == ExternalDvsEmulatorDevice.MODE_16: self._out_res = int(mode) self._res_2x = self._out_res * 2 else: raise exceptions.SpynnakerException("the model does not " "recongise this mode") if (polarity == ExternalDvsEmulatorDevice.UP_POLARITY or polarity == ExternalDvsEmulatorDevice.DOWN_POLARITY or polarity == ExternalDvsEmulatorDevice.RECTIFIED_POLARITY): fixed_n_neurons = self._out_res**2 else: fixed_n_neurons = 2 * (self._out_res**2) if fixed_n_neurons != n_neurons and n_neurons is not None: logger.warn( "The specified number of neurons for the DVS emulator" " device has been ignored {} will be used instead".format( fixed_n_neurons)) self._video_source = None self._device_id = device_id self._is_virtual_cam = False self._polarity = polarity self._polarity_n = ExternalDvsEmulatorDevice.POLARITY_DICT[polarity] self._global_max = int16(0) self._output_type = output_type self._raw_frame = None self._gray_frame = None self._tmp_frame = None self._ref_frame = 128 * np.ones( (self._out_res, self._out_res), dtype=int16) self._curr_frame = np.zeros((self._out_res, self._out_res), dtype=int16) self._spikes_frame = np.zeros((self._out_res, self._out_res, 3), dtype=uint8) self._diff = np.zeros((self._out_res, self._out_res), dtype=int16) self._abs_diff = np.zeros((self._out_res, self._out_res), dtype=int16) self._spikes = np.zeros((self._out_res, self._out_res), dtype=int16) self._adaptive_threshold = adaptive_threshold self._thresh_matrix = None if adaptive_threshold: self._thresh_matrix = np.zeros((self._out_res, self._out_res), dtype=int16) self._threshold_delta_down = int16(threshold_delta_down) self._threshold_delta_up = int16(threshold_delta_up) self._max_threshold = int16(max_threshold) self._min_threshold = int16(min_threshold) self._up_spikes = None self._down_spikes = None self._spikes_lists = None self._threshold = int16(threshold) self._data_shift = uint8(np.log2(self._out_res)) self._up_down_shift = uint8(2 * self._data_shift) self._data_mask = uint8(self._out_res - 1) if self._output_type == ExternalDvsEmulatorDevice.OUTPUT_TIME_BIN: self._num_bins = 8 #8-bit images don't need more elif self._output_type == ExternalDvsEmulatorDevice.OUTPUT_TIME_BIN_THR: self._num_bins = 6 #should be enough? else: self._num_bins = int(1000. / fps) self._num_bits_per_spike = min(num_bits_per_spike, self._num_bins) if self._output_type == ExternalDvsEmulatorDevice.OUTPUT_TIME_BIN or \ self._output_type == ExternalDvsEmulatorDevice.OUTPUT_TIME_BIN_THR: self._log2_table = gs.generate_log2_table(self._num_bits_per_spike, self._num_bins) else: self._log2_table = gs.generate_log2_table( self._num_bits_per_spike, 8) #stupid hack, compatibility issues self._scale_img = scale_img self._img_height = 0 self._img_height_crop_u = 0 self._img_height_crop_b = 0 self._img_width = 0 self._img_width_crop_l = 0 self._img_width_crop_r = 0 self._img_ratio = 0. self._img_scaled_width = 0 self._scaled_width = 0 self._fps = fps self._max_time_ms = 0 self._time_per_frame = 0. self._time_per_spike_pack_ms = 0 self._get_sizes = True self._scale_changed = False self._running = True self._label = label self._n_neurons = fixed_n_neurons self._local_port = local_port self._inh_area_width = inh_area_width self._inhibition = inhibition self._inh_coords = gs.generate_inh_coords(self._out_res, self._out_res, inh_area_width) self._history_weight = history_weight self._run_time_ms = run_time_ms ################################################################ if spinn_version == "2015.005": ReverseIpTagMultiCastSource.__init__( self, n_neurons=self._n_neurons, machine_time_step=machine_time_step, timescale_factor=timescale_factor, port=self._local_port, label=self._label, virtual_key=virtual_key) else: ReverseIpTagMultiCastSource.__init__( self, n_keys=self._n_neurons, machine_time_step=machine_time_step, timescale_factor=timescale_factor, label=self._label, receive_port=self._local_port, virtual_key=virtual_key) AbstractProvidesOutgoingConstraints.__init__(self) print("number of neurons for webcam = %d" % self._n_neurons) self._live_conn = SpynnakerLiveSpikesConnection( send_labels=[ self._label, ], local_port=self._local_port) def init(label, n_neurons, run_time_ms, machine_timestep_ms): print("Sending %d neuron sources from %s" % (n_neurons, label)) self._live_conn.add_init_callback(self._label, init) self._live_conn.add_start_callback(self._label, self.run) self._sender = None self._save_spikes = save_spikes
print "Sending backward spike", real_id print_condition.release() sender.send_spike(label, real_id) # Create a receiver of live spikes def receive_spikes(label, time, neuron_ids): for neuron_id in neuron_ids: print_condition.acquire() print "Received spike at time", time, "from", label, "-", neuron_id print_condition.release() # Set up the live connection for sending spikes live_spikes_connection_send = SpynnakerLiveSpikesConnection( receive_labels=None, local_port=19999, send_labels=["spike_injector_forward", "spike_injector_backward"]) # Set up callbacks to occur at the start of simulation live_spikes_connection_send.add_start_callback("spike_injector_forward", send_input_forward) live_spikes_connection_send.add_start_callback("spike_injector_backward", send_input_backward) if not using_c_vis: # if not using the c visualiser, then a new spynnaker live spikes connection # is created to define that there are python code which receives the # outputted spikes. live_spikes_connection_receive = SpynnakerLiveSpikesConnection( receive_labels=["pop_forward", "pop_backward"], local_port=19996,
# Activate the sending of live spikes ExternalDevices.activate_live_output_for(pop_forward, database_notify_host="localhost", database_notify_port_num=19996) # Create a receiver of live spikes def receive_spikes(label, time, neuron_ids): for neuron_id in neuron_ids: print "Received spike at time", time, "from", label, "-", neuron_id # if not using the c visualiser, then a new spynnaker live spikes connection # is created to define that there are python code which receives the # outputted spikes. live_spikes_connection = SpynnakerLiveSpikesConnection( receive_labels=["pop_forward"], local_port=19996, send_labels=None) # Set up callbacks to occur when spikes are received live_spikes_connection.add_receive_callback("pop_forward", receive_spikes) # Run the simulation on spiNNaker Frontend.run(run_time) # Retrieve spikes from the synfire chain population spikes_forward = pop_forward.getSpikes() # If there are spikes, plot using matplotlib if len(spikes_forward) != 0: pylab.figure() if len(spikes_forward) != 0: pylab.plot([i[1] for i in spikes_forward], [i[0] for i in spikes_forward], "b.") pylab.ylabel('neuron id') pylab.xlabel('Time/ms') pylab.title('spikes')
# Activate the sending of live spikes ExternalDevices.activate_live_output_for(pop_forward, database_notify_host="localhost", database_notify_port_num=19996) # Create a sender of packets for the forward population def send_input_forward(label, sender): print "Sending forward spike for neuron 0" sender.send_spike(label, 0) # if not using the c visualiser, then a new spynnaker live spikes connection # is created to define that there are python code which receives the # outputted spikes. live_spikes_connection = SpynnakerLiveSpikesConnection( local_port=19996, send_labels=["spike_injector_forward"]) # Set up callbacks to occur at the start of simulation live_spikes_connection.add_start_callback("spike_injector_forward", send_input_forward) # Run the simulation on spiNNaker Frontend.run(run_time) # Retrieve spikes from the synfire chain population spikes_forward = pop_forward.getSpikes() # If there are spikes, plot using matplotlib if len(spikes_forward) != 0: pylab.figure() if len(spikes_forward) != 0: pylab.plot([i[1] for i in spikes_forward], [i[0] for i in spikes_forward], "b.") pylab.ylabel('neuron id') pylab.xlabel('Time/ms')
print_condition.acquire() print "Sending forward spike for neuron 0" print_condition.release() sender.send_spike(label, 0) # Create a receiver of live spikes def receive_spikes(label, time, neuron_ids): for neuron_id in neuron_ids: print_condition.acquire() print "Received spike at time", time, "from", label, "-", neuron_id print_condition.release() if neuron_id == 0 and label== "pop_backward_parrot": live_spikes_connection.send_spike("spike_injector_forward", 0) elif neuron_id == 99 and label== "pop_forward_parrot": live_spikes_connection.send_spike("spike_injector_forward", 1) # Set up the live connection for sending spikes live_spikes_connection = SpynnakerLiveSpikesConnection( local_port=19996, send_labels=["spike_injector_forward"]) # Set up the live connection for sending spikes to closed loop live_spikes_connection_receiver = SpynnakerLiveSpikesConnection( receive_labels=["pop_forward_parrot", "pop_backward_parrot"], local_port=19995) # Set up callbacks to occur at the start of simulation live_spikes_connection.add_start_callback("spike_injector_forward", send_input_forward) # Set up callbacks to occur when spikes are received live_spikes_connection_receiver.add_receive_callback("pop_forward_parrot", receive_spikes) live_spikes_connection_receiver.add_receive_callback("pop_backward_parrot", receive_spikes) # Run the simulation on spiNNaker Frontend.run(run_time) # Retrieve spikes from the synfire chain population spikes_forward = pop_forward.getSpikes()
add_spikes(full_input_spikes, input_spikes[n][in_id], silence=period_length*2) input_layer = {} #~ for i in xrange(len(input_spikes)): #~ input_layer[i] = sim.Population(img_neurons, sim.SpikeSourceArray, #~ {"spike_times": input_spikes[i]}, #~ structure=grid) input_layer[in_id] = sim.Population(img_neurons, inj_cell_type, inj_cell_params, label="spike_injector") live_spikes_connection_send = SpynnakerLiveSpikesConnection( receive_labels=None, local_port=19999, send_labels=["spike_injector"]) live_spikes_connection_send.add_start_callback("spike_injector", send_input) #~ input_layer[in_id] = sim.Population(img_neurons, sim.SpikeSourceArray, #~ {"spike_times": input_spikes[in_id]} #~ ) learn_layer = {} learn_layer['exc'] = sim.Population(num_exc+1, neuron_model, cell_params_izk_exc, label="Learn layer - exc") learn_layer['inh'] = sim.Population(num_inh+1, neuron_model, cell_params_izk_inh, label="Learn layer - inh")
def _connect_spike_sources(self, retinae=None, verbose=False): if verbose: print "INFO: Connecting Spike Sources to Network." global _retina_proj_l, _retina_proj_r # left is 0--dimensionRetinaY-1; right is dimensionRetinaY--dimensionRetinaY*2-1 connListRetLBlockerL = [] connListRetLBlockerR = [] connListRetRBlockerL = [] connListRetRBlockerR = [] for y in range(0, self.dim_y): connListRetLBlockerL.append( (y, y, self.cell_params['synaptic']['wSaB'], self.cell_params['synaptic']['dSaB'])) connListRetLBlockerR.append( (y, y + self.dim_y, self.cell_params['synaptic']['wSzB'], self.cell_params['synaptic']['dSzB'])) connListRetRBlockerL.append( (y, y, self.cell_params['synaptic']['wSzB'], self.cell_params['synaptic']['dSzB'])) connListRetRBlockerR.append( (y, y + self.dim_y, self.cell_params['synaptic']['wSaB'], self.cell_params['synaptic']['dSaB'])) retinaLeft = retinae['left'].pixel_columns retinaRight = retinae['right'].pixel_columns pixel = 0 for row in _retina_proj_l: for pop in row: ps.Projection(retinaLeft[pixel], self.network[pop][1], ps.OneToOneConnector( weights=self.cell_params['synaptic']['wSC'], delays=self.cell_params['synaptic']['dSC']), target='excitatory') ps.Projection(retinaLeft[pixel], self.network[pop][0], ps.FromListConnector(connListRetLBlockerL), target='excitatory') ps.Projection(retinaLeft[pixel], self.network[pop][0], ps.FromListConnector(connListRetLBlockerR), target='inhibitory') pixel += 1 pixel = 0 for col in _retina_proj_r: for pop in col: ps.Projection(retinaRight[pixel], self.network[pop][1], ps.OneToOneConnector( weights=self.cell_params['synaptic']['wSC'], delays=self.cell_params['synaptic']['dSC']), target='excitatory') ps.Projection(retinaRight[pixel], self.network[pop][0], ps.FromListConnector(connListRetRBlockerR), target='excitatory') ps.Projection(retinaRight[pixel], self.network[pop][0], ps.FromListConnector(connListRetRBlockerL), target='inhibitory') pixel += 1 # configure for the live input streaming if desired if not (retinae['left'].use_prerecorded_input and retinae['right'].use_prerecorded_input): from spynnaker_external_devices_plugin.pyNN.connections.spynnaker_live_spikes_connection import \ SpynnakerLiveSpikesConnection all_retina_labels = retinaLeft.labels + retinaRight.labels self.live_connection_sender = SpynnakerLiveSpikesConnection( receive_labels=None, local_port=19999, send_labels=all_retina_labels) # this callback will be executed right after simulation.run() has been called. If simply a while True # is put there, the main thread will stuck there and will not complete the simulation. # One solution might be to start a thread/process which runs a "while is_running:" loop unless the main thread # sets the "is_running" to False. self.live_connection_sender.add_start_callback( all_retina_labels[0], self.start_injecting) import DVSReader as dvs # the port numbers might well be wrong self.dvs_stream_left = dvs.DVSReader( port=0, label=retinaLeft.label, live_connection=self.live_connection_sender) self.dvs_stream_right = dvs.DVSReader( port=1, label=retinaRight.label, live_connection=self.live_connection_sender) # start the threads, i.e. start reading from the DVS. However, nothing will be sent to the SNN. # See start_injecting self.dvs_stream_left.start() self.dvs_stream_right.start()
weight_to_spike = 2. rate_weight = 1.5 delay = 1 rate_delay = 16 pool_size = 1 # Create breakout population and activate live output for it breakout_pop = p.Population(1, spinn_breakout.Breakout, {}, label="breakout") ex.activate_live_output_for(breakout_pop, host="0.0.0.0", port=UDP_PORT) # Create spike injector to inject keyboard input into simulation key_input = p.Population(2, ex.SpikeInjector, {"port": 12367}, label="key_input") key_input_connection = SpynnakerLiveSpikesConnection(send_labels=["key_input"]) # Connect key spike injector to breakout population p.Projection(key_input, breakout_pop, p.OneToOneConnector(weights=2)) # Create visualiser visualiser = spinn_breakout.Visualiser(UDP_PORT, key_input_connection, x_res=X_RESOLUTION, y_res=Y_RESOLUTION, x_bits=X_BITS, y_bits=Y_BITS) direction_population = p.Population(3, p.IF_curr_exp, cell_params_lif) p.Projection( direction_population, breakout_pop,