def _test_iobase_device(device_type): from openre import OpenRE config = { 'layers': [ { 'name': 'V1', 'width': 16, 'height': 10, 'relaxation': 0, }, ], 'domains': [ { 'name' : 'D1', 'device' : { 'type': device_type, 'width': 16, 'height': 10, 'output': [ # [c1 c2] # [c3 c4] {'name': 'c1', 'shape': [0, 0, 8, 5]}, {'name': 'c2', 'shape': [8, 0, 8, 5]}, {'name': 'c3', 'shape': [0, 5, 8, 5]}, {'name': 'c4', 'shape': [8, 5, 8, 5]}, ], }, }, { 'name' : 'D2', 'device' : { 'type': 'OpenCL', }, 'layers' : [ {'name': 'V1', 'input': 'c1', 'shape': [0, 0, 8, 5]}, {'name': 'V1', 'input': 'c2', 'shape': [8, 0, 8, 5]}, {'name': 'V1', 'input': 'c3', 'shape': [0, 5, 8, 5]}, {'name': 'V1', 'input': 'c4', 'shape': [8, 5, 8, 5]}, ], }, ], } ore = OpenRE(config) ore.deploy() device1 = ore.domains[0].device device2 = ore.domains[1].device D1 = ore.domains[0] D2 = ore.domains[1] D2.neurons.level.data[:] = 0 D2.neurons.level.to_device(device2) ore.tick() D1.neurons.from_device(device1) D2.neurons.from_device(device2) for layer_index, layer in enumerate(D2.layers): for x in xrange(8): for y in xrange(5): assert layer.neurons_metadata.level[x, y] == layer_index
def test_output_index(): index = OutputIndex() from openre import OpenRE from openre import neurons config = { 'layers': [ { 'name': 'V1', 'width': 16, 'height': 10, 'threshold': 255, 'relaxation': 0, }, ], 'domains': [ { 'name' : 'D1', 'device' : { 'type': 'OpenCL', 'threshold_inc': 0, 'threshold_dec': 0 }, 'layers' : [ {'name': 'V1', 'output': 'o1', 'shape': [0, 0, 8, 5]}, {'name': 'V1', 'shape': [8, 0, 8, 5]}, {'name': 'V1', 'output': 'o3', 'shape': [0, 5, 8, 5]}, {'name': 'V1', 'shape': [8, 5, 8, 5]}, ], }, ], } ore = OpenRE(config) ore.deploy() D1 = ore.domains[0] assert len(index.address.data) == 0 D1.neurons.level.data[:] = 0 device1 = ore.domains[0].device for layer in D1.layers: index.add(layer) index.shrink() assert len(index.address.data) == 80 assert len(index.data.data) == len(index.address.data) assert len(index.tick.data) == len(index.address.data) assert index.data.data.dtype.type == types.output assert list(index.address.data) \ == range(40) + [80 + x for x in range(40)] for i in xrange(len(index.data.data)): index.data.data[i] = i assert index.data_to_send() was = {} for source_id, data in index.data_to_send(): if source_id == 'o1': assert list(data) == range(40) if source_id == 'o3': assert list(data) == [40 + x for x in range(40)] was[source_id] = 1 assert was == {'o1': 1, 'o3':1} assert list(D1.output_index.address.data) == list(index.address.data) assert len(D1.output_index.address.data) == len(index.address.data) for _ in xrange(255): num = -1 for layer in D1.layers: for i in xrange(len(layer.neurons_metadata.level)): num += 1 layer.neurons_metadata.level[i] += num D1.neurons.level.to_device(device1) ore.tick() D1.neurons.level.from_device(device1) D1.neurons.flags.from_device(device1) oi = D1.output_index oi.data.from_device(device1) oi.tick.from_device(device1) for index, neuron_address in enumerate(oi.address.data): if D1.neurons.flags[neuron_address] & neurons.IS_SPIKED: assert oi.tick[index] == D1.ticks assert oi.tick[0] == 0 assert oi.tick.data[1] == 255 assert oi.data.data[1] == 1 assert oi.tick.data[2] == 255 assert oi.data.data[2] == 129
def test_camera(): from openre import OpenRE import os config = { 'layers': [ { 'name': 'V1', 'relaxation': 0, 'width': 16, 'height': 10, }, ], 'domains': [ { 'name' : 'Camera', 'device' : { 'type': 'GrayVideo', 'device': os.path.join( os.path.dirname(__file__), './templates/device/camera-test.avi' ), 'width': 16, 'height': 10, #'window': 'Cam input', 'output': [ # [c1 c2] # [c3 c4] {'name': 'c1', 'shape': [0, 0, 8, 5]}, {'name': 'c2', 'shape': [8, 0, 8, 5]}, {'name': 'c3', 'shape': [0, 5, 8, 5]}, {'name': 'c4', 'shape': [8, 5, 8, 5]}, ], }, }, { 'name' : 'D2', 'device' : { 'type': 'OpenCL', }, 'layers' : [ {'name': 'V1', 'input': 'c1', 'shape': [0, 0, 8, 5]}, {'name': 'V1', 'input': 'c2', 'shape': [8, 0, 8, 5]}, {'name': 'V1', 'input': 'c3', 'shape': [0, 5, 8, 5]}, {'name': 'V1', 'input': 'c4', 'shape': [8, 5, 8, 5]}, ], }, ], } ore = OpenRE(config) ore.deploy() device1 = ore.domains[0].device device2 = ore.domains[1].device D1 = ore.domains[0] D2 = ore.domains[1] D2.neurons.level.data[:] = 0 assert sum(D2.neurons.level.data) == 0 D2.neurons.level.to_device(device2) ore.tick() D1.neurons.from_device(device1) D2.neurons.from_device(device2) arr = D2.neurons.level.data a1 = arr[0:40] a2 = arr[40:80] a3 = arr[80:120] a4 = arr[120:160] check = numpy.array( [[ 0, 0, 0, 0, 0, 0, 0, 0, 20, 20, 20, 20, 20, 20, 20, 20], [ 0, 0, 0, 0, 0, 0, 0, 0, 20, 20, 20, 20, 20, 20, 20, 20], [ 2, 2, 2, 2, 2, 2, 2, 2, 21, 21, 21, 21, 21, 21, 21, 21], [ 2, 2, 2, 2, 2, 2, 2, 2, 21, 21, 21, 21, 21, 21, 21, 21], [ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19], [115, 115, 115, 115, 115, 115, 115, 115, 245, 245, 245, 245, 245, 245, 245, 245], [116, 116, 116, 116, 116, 116, 116, 116, 254, 254, 254, 254, 254, 254, 254, 254], [116, 116, 116, 116, 116, 116, 116, 116, 254, 254, 254, 254, 254, 254, 254, 254], [114, 114, 114, 114, 114, 114, 114, 114, 255, 255, 255, 255, 255, 255, 255, 255], [114, 114, 114, 114, 114, 114, 114, 114, 255, 255, 255, 255, 255, 255, 255, 255],], dtype=numpy.uint8 ) assert list(numpy.ravel(check[0:5, 0:8])) == list(a1) assert list(numpy.ravel(check[0:5, 8:16])) == list(a2) assert list(numpy.ravel(check[5:10, 0:8])) == list(a3) assert list(numpy.ravel(check[5:10, 8:16])) == list(a4) assert sum(D2.neurons.level.data) > 0 ore.clean()
def test_output(): from openre import OpenRE config = { 'layers': [ { 'name': 'V1', 'relaxation': 0, 'width': 16, 'height': 10, }, { 'name': 'V2', 'relaxation': 0, 'width': 16, 'height': 10, }, ], 'domains': [ { 'name' : 'D1', 'device' : { 'type': 'OpenCL', }, 'layers' : [ {'name': 'V2', 'output': 'o1', 'shape': [0, 0, 8, 5]}, {'name': 'V2', 'output': 'o2', 'shape': [8, 0, 8, 5]}, {'name': 'V2', 'output': 'o3', 'shape': [0, 5, 8, 5]}, {'name': 'V2', 'output': 'o4', 'shape': [8, 5, 8, 5]}, ], }, { 'name' : 'D2', 'device' : { 'type': 'OpenCL', }, 'layers' : [ {'name': 'V1', 'input': 'o1', 'shape': [0, 0, 8, 5]}, {'name': 'V1', 'input': 'o2', 'shape': [8, 0, 8, 5]}, {'name': 'V1', 'input': 'o3', 'shape': [0, 5, 8, 5]}, {'name': 'V1', 'input': 'o4', 'shape': [8, 5, 8, 5]}, ], }, ], } ore = OpenRE(config) ore.deploy() device1 = ore.domains[0].device device2 = ore.domains[1].device D1 = ore.domains[0] D2 = ore.domains[1] D1.neurons.level.data[0] = 35000 D2.neurons.level.data[:] = 0 D1.neurons.level.to_device(device1) D2.neurons.level.to_device(device2) ore.tick() D1.neurons.from_device(device1) D2.neurons.from_device(device2) assert D2.neurons.level.data[0] == 255 D1.neurons.level.data[1] = 35000 D1.neurons.level.to_device(device1) ore.tick() D1.neurons.from_device(device1) D2.neurons.from_device(device2) assert D2.neurons.level.data[0] == 255 + 254 assert D2.neurons.level.data[1] == 254
def check_input(expire): import numpy from openre import OpenRE from openre.vector import StandaloneVector from openre import neurons import logging # remote domains config = { 'layers': [ { 'name': 'Input', 'width': 10, 'height': 10, 'is_inhibitory': False, 'threshold': 128, 'relaxation': 0, 'connect': [ { 'name': 'V2', 'radius': 1, 'shift': [0, 0], }, ], }, { 'name': 'V2', 'width': 10, 'height': 10, 'relaxation': 0, }, ], 'domains': [ { 'name' : 'D1', 'device' : { 'type': 'OpenCL', 'threshold_inc': 0, 'threshold_dec': 0 }, 'layers' : [ {'name': 'Input', 'shape': [0, 0, 5, 5], 'expire': expire}, {'name': 'Input', 'shape': [0, 5, 5, 5], 'expire': expire}, {'name': 'Input', 'shape': [5, 0, 5, 5], 'expire': expire}, {'name': 'Input', 'shape': [5, 5, 5, 5], 'expire': expire}, ], }, { 'name' : 'D2', 'device' : { 'type': 'OpenCL', 'threshold_inc': 0, 'threshold_dec': 0 }, 'layers' : [ {'name': 'V2'}, ], }, ], } ore = OpenRE(config) ore.deploy() device1 = ore.domains[0].device device2 = ore.domains[1].device D1 = ore.domains[0] D2 = ore.domains[1] arr = numpy.arange(0, 200, 2, dtype=numpy.uint8) #arr = StandaloneVector(numpy.reshape(arr, (10, 10))) arr = numpy.reshape(arr, (10, 10)) l0 = arr[0:5, 0:5] l1 = arr[5:10, 0:5] l2 = arr[0:5, 5:10] l3 = arr[5:10, 5:10] # somewhere here we will send packet to the input device # and in input device will receive it: layer_data = [l0, l1, l2, l3] for layer_index, data in enumerate(layer_data): if layer_index % 2: D1.register_input_layer_data( layer_index, StandaloneVector(data).bytes() ) else: D1.register_input_layer_data( layer_index, data ) neurons_length = 0 for layer_index, data in enumerate(layer_data): layer = D1.layers[layer_index] vector = StandaloneVector() if isinstance(layer.input_data, basestring): vector.from_bytes(layer.input_data) else: vector.set_data(layer.input_data) assert list(vector.data) == list(numpy.ravel(data)) assert len(vector.data) == len(numpy.ravel(data)) neurons_length += len(vector.data) assert len(D1.neurons.level) == neurons_length D1.neurons.level.data[:] = 0 D1.neurons.level.to_device(device1) D2.neurons.level.data[:] = 0 D2.neurons.level.to_device(device2) ore.tick() D1.neurons.from_device(device1) D2.neurons.from_device(device2) level_check \ = numpy.ravel(numpy.concatenate(layer_data)).astype(numpy.uint32) level_check[level_check >= 128] -= 128 assert list(D1.neurons.level.data) == list(level_check) flags_check = numpy.ravel(numpy.concatenate(layer_data)) flags_check[flags_check < 128] = neurons.IS_TRANSMITTER flags_check[flags_check >= 128] = neurons.IS_TRANSMITTER | neurons.IS_SPIKED assert list(flags_check) == list(D1.neurons.flags.data) # check D2 neuron level flags2_check = numpy.ravel(numpy.concatenate(layer_data)) level2_check = numpy.copy(D2.synapses.level.data) level2_check[flags2_check < 128] = 0 neurons2_level = numpy.reshape( D2.neurons.level.data[0:len(level2_check)], (10, 10) ) l20 = neurons2_level[0:5, 0:5] l21 = neurons2_level[5:10, 0:5] l22 = neurons2_level[0:5, 5:10] l23 = neurons2_level[5:10, 5:10] layer2_data = [l20, l21, l22, l23] # D2 neuron level should be eq to synapses levels assert list(numpy.ravel(numpy.concatenate(layer2_data))) \ == list(level2_check) try: for pass_num in range(2, 4): ore.tick() D1.neurons.from_device(device1) D2.neurons.from_device(device2) if expire and expire >= pass_num: level_check = level_check \ + numpy.ravel(numpy.concatenate(layer_data)) level_check[level_check >= 128] -= 128 assert list(D1.neurons.level.data) == list(level_check) if not expire or expire < pass_num: for layer_index, data in enumerate(layer_data): assert layer.input_data_cache is None assert layer.input_data is None except AssertionError: logging.warn('Expire #%s, pass #%s', expire, pass_num) raise
def test_random_device(): from openre import OpenRE # remote domains config = { 'layers': [ { 'name': 'R1', 'width': 20, 'height': 20, 'is_inhibitory': False, 'connect': [ { 'name': 'V2', 'radius': 1, 'shift': [0, 0], }, ], }, { 'name': 'V2', 'width': 20, 'height': 20, }, ], 'domains': [ { 'name' : 'D1', 'device' : { 'type': 'Random', 'method': 'randint', 'args': [0, 60000], 'threshold': 30000, }, 'layers' : [ {'name': 'R1'}, ], }, { 'name' : 'D2', 'device' : { 'type': 'OpenCL', }, 'layers' : [ {'name': 'V2'}, ], }, ], } ore = OpenRE(config) ore.deploy() device2 = ore.domains[1].device D1 = ore.domains[0] D2 = ore.domains[1] ore.tick() D2.neurons.from_device(device2) # receiver neurons in D2 is spiked the same as spikes data in # D1.transmitter_index assert list((D1.transmitter_index.is_spiked.data + 1) & neurons.IS_SPIKED) \ == list(D2.neurons.flags.data[400:] & neurons.IS_SPIKED) ore.tick() D2.neurons.from_device(device2) # at least one spike should happen assert sum(list(D2.neurons.flags.data[0:400] & neurons.IS_SPIKED))