def Broadcast(vector, len): if len > 1: for i in range(0, len - 1): comm.Write(MOD_NM, COMP, vector[i]) # to test #comm.WriteAddr(0x01000001, l-1, vector); comm.Write(MOD_NM, LCOMP, vector[len - 1])
def ReadNeuron(index): TempNSR = comm.Read(MOD_NM, NSR) # backup the NSR comm.Write(MOD_NM, NSR, 0x0010) # set the NSR to Save and Restore mode comm.Write(MOD_NM, RESETCHAIN, 0) # move to the neuron pointed by index in the chain for i in range(0, index): Temp = comm.Read(MOD_NM, CAT) # comm.Read the registers and memory of the neuron in focus neuron = [0 for i in range(0, 264)] Temp = comm.Read(MOD_NM, NCR) neuron[0] = (Temp & 0xFF00) >> 8 neuron[1] = Temp & 0x00FF for i in range(0, 256): neuron[i + 2] = comm.Read(MOD_NM, COMP) Temp = comm.Read(MOD_NM, AIF) neuron[258] = (Temp & 0xFF00) >> 8 neuron[259] = Temp & 0x00FF Temp = comm.Read(MOD_NM, MINIF) neuron[260] = (Temp & 0xFF00) >> 8 neuron[261] = Temp & 0x00FF Temp = comm.Read(MOD_NM, CAT) neuron[262] = (Temp & 0xFF00) >> 8 neuron[263] = Temp & 0x00FF comm.Write(1, NSR, TempNSR) # restore the NSR return (neuron)
def Classify(vector, len, K): Broadcast(vector, len) bytearray = ctypes.c_int * K dists = bytearray() cats = bytearray() nids = bytearray() recoNbr = 0 while recoNbr < K: dists[recoNbr] = comm.Read(MOD_NM, DIST) cats[recoNbr] = comm.Read(MOD_NM, CAT) & 0x7FFF nids[recoNbr] = comm.Read(MOD_NM, NID) if dists[recoNbr] == 65535: break else: recoNbr = recoNbr + 1 return (recoNbr, dists, cats, nids)
def ReviewNeuron(index): TempNSR = comm.Read(MOD_NM, NSR) # backup the NSR comm.Write(MOD_NM, NSR, 0x0010) # set the NSR to Save and Restore mode comm.Write(MOD_NM, RESETCHAIN, 0) # move to the neuron pointed by index in the chain for i in range(0, index): Temp = comm.Read(MOD_NM, CAT) # comm.Read the registers and memory of the neuron in focus ncr = comm.Read(MOD_NM, NCR) model = [0 for i in range(0, 256)] for i in range(0, 256): model[i] = comm.Read(MOD_NM, COMP) aif = comm.Read(MOD_NM, AIF) minif = comm.Read(MOD_NM, MINIF) cat = comm.Read(MOD_NM, CAT) comm.Write(1, NSR, TempNSR) # restore the NSR return (ncr, model, aif, minif, cat)
def ClearNeurons(): comm.Write(MOD_NM, NSR, 16) comm.Write(MOD_NM, TESTCAT, 1) comm.Write(MOD_NM, NSR, 0) for i in range(0, 255): comm.Write(MOD_NM, INDEXCOMP, i) comm.Write(MOD_NM, TESTCOMP, 0) comm.Write(MOD_NM, FORGET, 0)
def DisplayNeurons(len): print("\nDisplaying the neurons' content") ncount = comm.Read(MOD_NM, NCOUNT) print("Committed neurons= " + repr(ncount)) #should return the value 3 comm.Write(MOD_NM, NSR, 16) #Set network to Save and Restore mode comm.Write(MOD_NM, RESETCHAIN, 0) # Resetchain to point at 1st committed neuron for i in range(0, ncount): print("Neuron %u: [" % (i + 1), end=" ") for i in range(0, len): print(comm.Read(MOD_NM, COMP), end=" ") print("] AIF= %3u CAT %3u" % (comm.Read(MOD_NM, AIF), comm.Read(MOD_NM, CAT) & 0x7FFF)) comm.Write(MOD_NM, NSR, 0)
def ReadNeurons(ncount): TempNSR = comm.Read(MOD_NM, NSR) # backup the NSR comm.Write(MOD_NM, NSR, 0x0010) # set the NSR to Save and Restore mode comm.Write(MOD_NM, RESETCHAIN, 0) neurons = [0 for i in range(0, ncount * 264)] for i in range(0, ncount): Temp = comm.Read(MOD_NM, NCR) neurons[(i * 264)] = (Temp & 0xFF00) >> 8 neurons[(i * 264) + 1] = Temp & 0x00FF for j in range(0, 256): neurons[(i * 264) + j + 2] = comm.Read(MOD_NM, COMP) Temp = comm.Read(MOD_NM, AIF) neurons[(i * 264) + 258] = (Temp & 0xFF00) >> 8 neurons[(i * 264) + 259] = Temp & 0x00FF Temp = comm.Read(MOD_NM, MINIF) neurons[(i * 264) + 260] = (Temp & 0xFF00) >> 8 neurons[(i * 264) + 261] = Temp & 0x00FF Temp = comm.Read(MOD_NM, CAT) neurons[(i * 264) + 262] = (Temp & 0xFF00) >> 8 neurons[(i * 264) + 263] = Temp & 0x00FF comm.Write(1, NSR, TempNSR) return (neurons)
## ## Test SPI ## ## Simple Test verifying SPI access to the NeuroShield ## ## Adjust the SPI clock speed in NeuroMem.py ## if necessary for communication stability import GVcomm_SPI as comm from GVconstants import * print ("NeuroShield Test SPI with single RW\n") comm.Connect() comm.Write(MOD_NM,FORGET,0) value=comm.Read(MOD_NM,MINIF) print("Minif=%u" % value) value=comm.Read(MOD_NM,MAXIF) print("Maxif=%u" % value) value=comm.Read(MOD_NM,GCR) print("GCR=%u" % value) value1= 1 value2= 4096 iteration=0 error=0 while iteration<5:
min = vector[p] p=p+1 if ((normalize == 1) & (max > min)): for i in range (0, p): Sum= (vector[i] - min) * 255 vector[i] = (int)(Sum / (max - min)) # return the length of the vector which must be less or equal to 256 return p, vector #------------------------------------------------- # Select a NeuroMem platform # 0=simu, 1=NeuroStack, 2=NeuroShield, 4=Brilliant #------------------------------------------------- if (comm.Connect()!=0): print ("Cannot connect to NeuroShield\n") sys.exit() nm.ClearNeurons() # Load an image for analysis imsrc=cv.imread('face.jpg') cv.imshow('Image Source', imsrc) cv.waitKey(3) imgl = cv.cvtColor(imsrc, cv.COLOR_BGR2GRAY) imW=imgl.shape[1] imH=imgl.shape[0] print("Image = " + repr(imW) + " x " + repr(imH)) imCtrX= imW/2
def BestMatch(vector, len): Broadcast(vector, len) distance = comm.Read(MOD_NM, DIST) category = comm.Read(MOD_NM, CAT) nid = comm.Read(MOD_NM, NID) return (distance, category, nid)
def Learn(vector, len, category): Broadcast(vector, len) comm.Write(MOD_NM, CAT, category) return (comm.Read(MOD_NM, NCOUNT))
def WriteNeurons(neurons, ncount): TempNSR = comm.Read(MOD_NM, NSR) TempGCR = comm.Read(MOD_NM, GCR) comm.Write(MOD_NM, FORGET, 0) comm.Write(MOD_NM, NSR, 0x0010) comm.Write(MOD_NM, RESETCHAIN, 0) for i in range(0, ncount): comm.Write(MOD_NM, NCR, (neurons[i * 264] << 8) + neurons[(i * 264) + 1]) for j in range(0, 256): comm.Write(MOD_NM, COMP, neurons[(i * 264) + 2 + j]) comm.Write(MOD_NM, AIF, (neurons[(i * 264) + 258] << 8) + neurons[(i * 264) + 259]) comm.Write(MOD_NM, MINIF, (neurons[(i * 264) + 260] << 8) + neurons[(i * 264) + 261]) comm.Write(MOD_NM, CAT, (neurons[(i * 264) + 262] << 8) + neurons[(i * 264) + 263]) comm.Write(MOD_NM, NSR, TempNSR) comm.Write(MOD_NM, GCR, TempGCR)
## ## Test SPI ## ## Simple Test verifying SPI access to the NeuroShield ## ## Adjust the SPI clock speed in NeuroMem.py ## if necessary for communication stability import GVcomm_SPI as comm from GVconstants import * print("NeuroShield Test SPI with single and multiple RW\n") error = comm.Connect() if (error != 0): print("Cannot connect to NeuroShield\n") # sys.exit() comm.Write(MOD_NM, FORGET, 0) value = comm.Read(MOD_NM, MINIF) print("Minif=%u" % value) value = comm.Read(MOD_NM, MAXIF) print("Maxif=%u" % value) value = comm.Read(MOD_NM, GCR) print("GCR=%u" % value) value1 = 1 value2 = 4096 value3 = 10 length = 10
min = 255 max = 0 if (max < vector[p]): max = vector[p] if (min > vector[p]): min = vector[p] p=p+1 if ((normalize == 1) & (max > min)): for i in range (0, p): Sum= (vector[i] - min) * 255 vector[i] = (int)(Sum / (max - min)) # return the length of the vector which must be less or equal to 256 return p, vector if (comm.Connect()!=2): print ("Cannot connect to NeuroShield\n") sys.exit() #------------------------------------------------- # Select a NeuroMem platform # 0=simu, 1=NeuroStack, 2=NeuroShield, 4=Brilliant #------------------------------------------------- nm.ClearNeurons() # initialize the camera and grab a reference image camera = PiCamera() camera.resolution=(600,400) time.sleep(2) # camera warm up rawCapture = PiRGBArray(camera) # grab an image from the camera