""" Using 3x3 on detector, 3x3 off centre dector, 6x6 on centre, 6x6 off center detectors, 9x9 on centre, and 9x9 off centre detectors externally fed by one image read. """ #imports for PyNN from pyNN.utility import get_script_args, normalized_filename from neo.io import PyNNTextIO #imports for image reading import numpy from PIL import Image INPUT_NEURONS_HEIGHT = 50 INPUT_NEURONS_WIDTH = 50 #DELAY = 10.0 DELAY = 1.0 SIM_LENGTH = 200.0 #----------setup connections for input to 1 3x3 on centre off surround detector def set3x3OnConnections(X,Y): excite = 25.0 inhib = -2.0 retinaNumber = INPUT_NEURONS_WIDTH*(X)+ Y inhib1 = (INPUT_NEURONS_WIDTH*(X-1))+Y-1 inhib2 = (INPUT_NEURONS_WIDTH*(X-1))+Y inhib3 = (INPUT_NEURONS_WIDTH*(X-1))+Y+1 inhib4 = (INPUT_NEURONS_WIDTH*(X))+Y-1 inhib6 = (INPUT_NEURONS_WIDTH*(X))+Y+1 inhib7 = (INPUT_NEURONS_WIDTH*(X+1))+Y-1 inhib8 = (INPUT_NEURONS_WIDTH*(X+1))+Y inhib9 = (INPUT_NEURONS_WIDTH*(X+1))+Y+1 connector = [ (retinaNumber, retinaNumber, excite, DELAY), (inhib1, retinaNumber, inhib, DELAY), (inhib2, retinaNumber, inhib, DELAY), (inhib3, retinaNumber, inhib, DELAY), (inhib4, retinaNumber, inhib, DELAY), (inhib6, retinaNumber, inhib, DELAY), (inhib7, retinaNumber, inhib, DELAY), (inhib8, retinaNumber, inhib, DELAY), (inhib9, retinaNumber, inhib, DELAY), ] return connector; #----------setup connections for input to 1 3x3 off centre on surround detector def set3x3OffConnections(X,Y): excite = 3.0 inhib = -25.0 retinaNumber = INPUT_NEURONS_WIDTH*(X)+ Y surround1 = (INPUT_NEURONS_WIDTH*(X-1))+Y-1 surround2 = (INPUT_NEURONS_WIDTH*(X-1))+Y surround3 = (INPUT_NEURONS_WIDTH*(X-1))+Y+1 surround4 = (INPUT_NEURONS_WIDTH*(X))+Y-1 surround6 = (INPUT_NEURONS_WIDTH*(X))+Y+1 surround7 = (INPUT_NEURONS_WIDTH*(X+1))+Y-1 surround8 = (INPUT_NEURONS_WIDTH*(X+1))+Y surround9 = (INPUT_NEURONS_WIDTH*(X+1))+Y+1 connector = [ (retinaNumber, retinaNumber, inhib, DELAY), (surround1, retinaNumber, excite, DELAY), (surround2, retinaNumber, excite, DELAY), (surround3, retinaNumber, excite, DELAY), (surround4, retinaNumber, excite, DELAY), (surround6, retinaNumber, excite, DELAY), (surround7, retinaNumber, excite, DELAY), (surround8, retinaNumber, excite, DELAY), (surround9, retinaNumber, excite, DELAY), ] return connector; #----------setup connections for input to 1 6x6 on centre off surround detector def set6x6OnConnections(X,Y): centreWeight = 7.0 surroundWeight = -2.0 retinaNumber = INPUT_NEURONS_WIDTH*(X)+ Y topLeftCentre = INPUT_NEURONS_WIDTH*(X)+ Y connector = [ (topLeftCentre, retinaNumber, centreWeight, DELAY), (topLeftCentre+1, retinaNumber, centreWeight, DELAY), (topLeftCentre+INPUT_NEURONS_WIDTH, retinaNumber, centreWeight, DELAY), (topLeftCentre+INPUT_NEURONS_WIDTH+1,retinaNumber,centreWeight, DELAY) ] #rows above for inpRow in range (X-2,X): for inpCol in range (Y-2,Y+4): inpCell = (inpRow*INPUT_NEURONS_WIDTH) + inpCol newConnector = [(inpCell, retinaNumber, surroundWeight, DELAY)] connector = connector + newConnector #left newConnector = [ (topLeftCentre-2, retinaNumber, surroundWeight, DELAY), (topLeftCentre-1, retinaNumber, surroundWeight, DELAY), (topLeftCentre+INPUT_NEURONS_WIDTH-2,retinaNumber,surroundWeight,DELAY), (topLeftCentre+INPUT_NEURONS_WIDTH-1,retinaNumber,surroundWeight,DELAY) ] connector = connector + newConnector #right newConnector = [ (topLeftCentre+2, retinaNumber, surroundWeight, DELAY), (topLeftCentre+3, retinaNumber, surroundWeight, DELAY), (topLeftCentre+INPUT_NEURONS_WIDTH+2,retinaNumber,surroundWeight,DELAY), (topLeftCentre+INPUT_NEURONS_WIDTH+3,retinaNumber,surroundWeight,DELAY) ] connector = connector + newConnector #rows below for inpRow in range (X+2,X+4): for inpCol in range (Y-2,Y+4): inpCell = (inpRow*INPUT_NEURONS_WIDTH) + inpCol newConnector = [(inpCell, retinaNumber, surroundWeight, DELAY)] connector = connector + newConnector return connector; #----------setup connections for input to 1 6x6 off centre on surround detector def set6x6OffConnections(X,Y): centreWeight = -10.0 surroundWeight = 1.0 retinaNumber = INPUT_NEURONS_WIDTH*(X)+ Y topLeftCentre = INPUT_NEURONS_WIDTH*(X)+ Y connector = [ (topLeftCentre, retinaNumber, centreWeight, DELAY), (topLeftCentre+1, retinaNumber, centreWeight, DELAY), (topLeftCentre+INPUT_NEURONS_WIDTH, retinaNumber, centreWeight, DELAY), (topLeftCentre+INPUT_NEURONS_WIDTH+1,retinaNumber,centreWeight, DELAY) ] #rows above for inpRow in range (X-2,X): for inpCol in range (Y-2,Y+4): inpCell = (inpRow*INPUT_NEURONS_WIDTH) + inpCol newConnector = [(inpCell, retinaNumber, surroundWeight, DELAY)] connector = connector + newConnector #left newConnector = [ (topLeftCentre-2, retinaNumber, surroundWeight, DELAY), (topLeftCentre-1, retinaNumber, surroundWeight, DELAY), (topLeftCentre+INPUT_NEURONS_WIDTH-2,retinaNumber,surroundWeight,DELAY), (topLeftCentre+INPUT_NEURONS_WIDTH-1,retinaNumber,surroundWeight,DELAY) ] connector = connector + newConnector #right newConnector = [ (topLeftCentre+2, retinaNumber, surroundWeight, DELAY), (topLeftCentre+3, retinaNumber, surroundWeight, DELAY), (topLeftCentre+INPUT_NEURONS_WIDTH+2,retinaNumber,surroundWeight,DELAY), (topLeftCentre+INPUT_NEURONS_WIDTH+3,retinaNumber,surroundWeight,DELAY) ] connector = connector + newConnector #rows below for inpRow in range (X+2,X+4): for inpCol in range (Y-2,Y+4): inpCell = (inpRow*INPUT_NEURONS_WIDTH) + inpCol newConnector = [(inpCell, retinaNumber, surroundWeight, DELAY)] connector = connector + newConnector return connector; #----------setup connections for input to 1 9x9 off centre on surround detector def set9x9OffConnections(X,Y): centreWeight = -6.0 surroundWeight = 0.4 retinaNumber = INPUT_NEURONS_WIDTH*(X)+ Y topLeftCentre = INPUT_NEURONS_WIDTH*(X)+ Y connector = [ (topLeftCentre, retinaNumber, centreWeight, DELAY), (topLeftCentre+1, retinaNumber, centreWeight, DELAY), (topLeftCentre+2, retinaNumber, centreWeight, DELAY), (topLeftCentre+INPUT_NEURONS_WIDTH, retinaNumber, centreWeight, DELAY), (topLeftCentre+INPUT_NEURONS_WIDTH+1,retinaNumber,centreWeight, DELAY), (topLeftCentre+INPUT_NEURONS_WIDTH+2,retinaNumber,centreWeight, DELAY), (topLeftCentre+(INPUT_NEURONS_WIDTH*2),retinaNumber,centreWeight,DELAY), (topLeftCentre+(INPUT_NEURONS_WIDTH*2)+1,retinaNumber,centreWeight,DELAY), (topLeftCentre+(INPUT_NEURONS_WIDTH*2)+2,retinaNumber,centreWeight,DELAY), ] #rows above for inpRow in range (X-3,X): for inpCol in range (Y-3,Y+6): inpCell = (inpRow*INPUT_NEURONS_WIDTH) + inpCol newConnector = [(inpCell, retinaNumber, surroundWeight, DELAY)] connector = connector + newConnector # #left newConnector = [ (topLeftCentre-3, retinaNumber, surroundWeight, DELAY), (topLeftCentre-2, retinaNumber, surroundWeight, DELAY), (topLeftCentre-1, retinaNumber, surroundWeight, DELAY), (topLeftCentre+INPUT_NEURONS_WIDTH-3,retinaNumber,surroundWeight,DELAY), (topLeftCentre+INPUT_NEURONS_WIDTH-2,retinaNumber,surroundWeight,DELAY), (topLeftCentre+INPUT_NEURONS_WIDTH-1,retinaNumber,surroundWeight,DELAY), (topLeftCentre+(INPUT_NEURONS_WIDTH*2)-3,retinaNumber,surroundWeight,DELAY), (topLeftCentre+(INPUT_NEURONS_WIDTH*2)-2,retinaNumber,surroundWeight,DELAY), (topLeftCentre+(INPUT_NEURONS_WIDTH*2)-1,retinaNumber,surroundWeight,DELAY) ] connector = connector + newConnector #right newConnector = [ (topLeftCentre+3, retinaNumber, surroundWeight, DELAY), (topLeftCentre+4, retinaNumber, surroundWeight, DELAY), (topLeftCentre+5, retinaNumber, surroundWeight, DELAY), (topLeftCentre+INPUT_NEURONS_WIDTH+3,retinaNumber,surroundWeight,DELAY), (topLeftCentre+INPUT_NEURONS_WIDTH+4,retinaNumber,surroundWeight,DELAY), (topLeftCentre+INPUT_NEURONS_WIDTH+5,retinaNumber,surroundWeight,DELAY), (topLeftCentre+(INPUT_NEURONS_WIDTH*2)+3,retinaNumber,surroundWeight,DELAY), (topLeftCentre+(INPUT_NEURONS_WIDTH*2)+4,retinaNumber,surroundWeight,DELAY), (topLeftCentre+(INPUT_NEURONS_WIDTH*2)+5,retinaNumber,surroundWeight,DELAY) ] connector = connector + newConnector #rows below for inpRow in range (X+3,X+6): for inpCol in range (Y-3,Y+6): inpCell = (inpRow*INPUT_NEURONS_WIDTH) + inpCol newConnector = [(inpCell, retinaNumber, surroundWeight, DELAY)] connector = connector + newConnector return connector; #----------setup connections for input to 1 9x9 on centre off surround detector def set9x9OnConnections(X,Y): centreWeight = 3.0 surroundWeight = 1.0 retinaNumber = INPUT_NEURONS_WIDTH*(X)+ Y topLeftCentre = INPUT_NEURONS_WIDTH*(X)+ Y connector = [ (topLeftCentre, retinaNumber, centreWeight, DELAY), (topLeftCentre+1, retinaNumber, centreWeight, DELAY), (topLeftCentre+2, retinaNumber, centreWeight, DELAY), (topLeftCentre+INPUT_NEURONS_WIDTH, retinaNumber, centreWeight, DELAY), (topLeftCentre+INPUT_NEURONS_WIDTH+1,retinaNumber,centreWeight, DELAY), (topLeftCentre+INPUT_NEURONS_WIDTH+2,retinaNumber,centreWeight, DELAY), (topLeftCentre+(INPUT_NEURONS_WIDTH*2),retinaNumber,centreWeight,DELAY), (topLeftCentre+(INPUT_NEURONS_WIDTH*2)+1,retinaNumber,centreWeight,DELAY), (topLeftCentre+(INPUT_NEURONS_WIDTH*2)+2,retinaNumber,centreWeight,DELAY), ] #rows above for inpRow in range (X-3,X): for inpCol in range (Y-3,Y+6): inpCell = (inpRow*INPUT_NEURONS_WIDTH) + inpCol newConnector = [(inpCell, retinaNumber, surroundWeight, DELAY)] connector = connector + newConnector # #left newConnector = [ (topLeftCentre-3, retinaNumber, surroundWeight, DELAY), (topLeftCentre-2, retinaNumber, surroundWeight, DELAY), (topLeftCentre-1, retinaNumber, surroundWeight, DELAY), (topLeftCentre+INPUT_NEURONS_WIDTH-3,retinaNumber,surroundWeight,DELAY), (topLeftCentre+INPUT_NEURONS_WIDTH-2,retinaNumber,surroundWeight,DELAY), (topLeftCentre+INPUT_NEURONS_WIDTH-1,retinaNumber,surroundWeight,DELAY), (topLeftCentre+(INPUT_NEURONS_WIDTH*2)-3,retinaNumber,surroundWeight,DELAY), (topLeftCentre+(INPUT_NEURONS_WIDTH*2)-2,retinaNumber,surroundWeight,DELAY), (topLeftCentre+(INPUT_NEURONS_WIDTH*2)-1,retinaNumber,surroundWeight,DELAY) ] connector = connector + newConnector #right newConnector = [ (topLeftCentre+3, retinaNumber, surroundWeight, DELAY), (topLeftCentre+4, retinaNumber, surroundWeight, DELAY), (topLeftCentre+5, retinaNumber, surroundWeight, DELAY), (topLeftCentre+INPUT_NEURONS_WIDTH+3,retinaNumber,surroundWeight,DELAY), (topLeftCentre+INPUT_NEURONS_WIDTH+4,retinaNumber,surroundWeight,DELAY), (topLeftCentre+INPUT_NEURONS_WIDTH+5,retinaNumber,surroundWeight,DELAY), (topLeftCentre+(INPUT_NEURONS_WIDTH*2)+3,retinaNumber,surroundWeight,DELAY), (topLeftCentre+(INPUT_NEURONS_WIDTH*2)+4,retinaNumber,surroundWeight,DELAY), (topLeftCentre+(INPUT_NEURONS_WIDTH*2)+5,retinaNumber,surroundWeight,DELAY) ] connector = connector + newConnector #rows below for inpRow in range (X+3,X+6): for inpCol in range (Y-3,Y+6): inpCell = (inpRow*INPUT_NEURONS_WIDTH) + inpCol newConnector = [(inpCell, retinaNumber, surroundWeight, DELAY)] connector = connector + newConnector return connector; #------Image Stuff def openImageFile(): try: im = Image.open("screenshot1414.jpg") except: print "fail to open image" im = im.crop((0,0,600,600)) #print im.size #shrink it size = (INPUT_NEURONS_WIDTH,INPUT_NEURONS_HEIGHT) im.thumbnail(size) #get the pixels pixels = im.load() ##convert rgb to grey scale inputMatrix = numpy.zeros((50,50)) for inpRow in range (0,50): for inpCol in range (0,50): rgb = pixels[inpCol,inpRow] for i in range (0,3): inputMatrix[inpCol,inpRow] += rgb[i] inputMatrix[inpCol,inpRow]/=3 ##set result image array resultImage = numpy.zeros((50,50)) THRESHOLD = 120 resultImageArray = [] for outRow in range (0,50): for outColumn in range (0,50): if inputMatrix[outColumn,outRow] < THRESHOLD : #print outRow, outColumn pixelOn = (outRow*INPUT_NEURONS_WIDTH)+outColumn resultImageArray = resultImageArray + [pixelOn] return resultImageArray #------------Main Body--------------- simulator_name = get_script_args(1)[0] exec("from pyNN.%s import *" % simulator_name) setup(timestep=DELAY,min_delay=DELAY,max_delay=DELAY,debug=0) #input neurons cInputNeurons = INPUT_NEURONS_HEIGHT * INPUT_NEURONS_WIDTH #default a .02, b .2, c -65, d 2.0, i_offset 0 inp_cell_params = {'a' : 0.0, 'b' :0.2, 'c' : -65, 'd' : 0.0} inpPop = Population(cInputNeurons,Izhikevich(**inp_cell_params)) inpPop.initialize(v=-58.0) #create retinal neurons off centre different than on centre retina_cell_params = {'a' : 0.2, 'b' :0.2, 'c' : -65, 'd' : 2.0} retina3x3OnPop = Population(cInputNeurons,Izhikevich(**retina_cell_params)) retina6x6OnPop = Population(cInputNeurons,Izhikevich(**retina_cell_params)) retina9x9OnPop = Population(cInputNeurons,Izhikevich(**retina_cell_params)) retina_cell_params = {'a' : 0.2, 'b' :0.2, 'c' : -65, 'd' : 2.0,'i_offset':1.0} retina3x3OffPop = Population(cInputNeurons,Izhikevich(**retina_cell_params)) retina6x6OffPop = Population(cInputNeurons,Izhikevich(**retina_cell_params)) retina9x9OffPop = Population(cInputNeurons,Izhikevich(**retina_cell_params)) #----------------setup connections synapseArray = [] #3x3On for row in range (1,49): for col in range (1,49): newSynapses = set3x3OnConnections(col,row) synapseArray = synapseArray + newSynapses Projection(inpPop,retina3x3OnPop,FromListConnector(synapseArray),StaticSynapse()) #3x3Off synapseArray = [] for row in range (1,49): for col in range (1,49): newSynapses = set3x3OffConnections(col,row) synapseArray = synapseArray + newSynapses Projection(inpPop,retina3x3OffPop,FromListConnector(synapseArray),StaticSynapse()) #6x6On synapseArray = [] for row in range (2,47): for col in range (2,47): newSynapses = set6x6OnConnections(col,row) synapseArray = synapseArray + newSynapses Projection(inpPop,retina6x6OnPop,FromListConnector(synapseArray),StaticSynapse()) #6x6Off synapseArray = [] for row in range (2,47): for col in range (2,47): newSynapses = set6x6OffConnections(col,row) synapseArray = synapseArray + newSynapses Projection(inpPop,retina6x6OffPop,FromListConnector(synapseArray),StaticSynapse()) #9x9Off synapseArray = [] for row in range (3,45): for col in range (3,45): newSynapses = set9x9OffConnections(col,row) synapseArray = synapseArray + newSynapses Projection(inpPop,retina9x9OffPop,FromListConnector(synapseArray),StaticSynapse()) #9x9On synapseArray = [] for row in range (3,45): for col in range (3,45): newSynapses = set9x9OnConnections(col,row) synapseArray = synapseArray + newSynapses Projection(inpPop,retina9x9OnPop,FromListConnector(synapseArray),StaticSynapse()) #setup recording inpPop.record(['spikes','v']) retina3x3OnPop.record(['spikes','v']) retina3x3OffPop.record(['spikes','v']) retina6x6OnPop.record(['spikes','v']) retina6x6OffPop.record(['spikes','v']) retina9x9OnPop.record(['spikes','v']) retina9x9OffPop.record(['spikes','v']) #print inpPop[0].get_parameters() #open image file imagePixels = openImageFile() #set up clamped input from image pulse = DCSource(amplitude=0.0038, start=0.0, stop=SIM_LENGTH) #inputAssembly = Assembly(inpPop[51,52]) inputAssembly = Assembly(inpPop[imagePixels]) pulse.inject_into(inputAssembly) run(SIM_LENGTH) #--------------print results #outAss = Assembly(retina3x3OnPop[161,162]) #outAss = retina3x3OnPop #outAss = retina3x3OffPop #outAss = retina6x6OnPop #outAss = retina6x6OffPop outAss = retina9x9OnPop #outAss = retina9x9OffPop outDat = outAss.get_data() for seg in outDat.segments: print(seg.spiketrains) end()