def main(self): SIDE_OF_ARRAY = 32 F = npyscreen.Form(name = "Road signs recognition via Hopfield Network",) F.add(npyscreen.TitleFixedText, name = "Using dimensions: " + str(SIDE_OF_ARRAY) + "x" + str(SIDE_OF_ARRAY),) netHebb = HopfieldNetwork(SIDE_OF_ARRAY*SIDE_OF_ARRAY) netPseudoInv = HopfieldNetwork(SIDE_OF_ARRAY*SIDE_OF_ARRAY) netDelta = HopfieldNetwork(SIDE_OF_ARRAY*SIDE_OF_ARRAY) F.add(npyscreen.TitleFixedText, name = "Network instances initialized",) F.display() signsList, imgToRecogn = self.loadParameters(sys.argv) F.add(npyscreen.TitleFixedText, name = "Image to recognize: " + imgToRecogn,) images = list() for image in signsList: imageName = image imgProc = ImgPreprocessor(image,SIDE_OF_ARRAY,127) image = imgProc.getImageForHopfield().copy() #print image F.add(npyscreen.TitleFixedText, name = "Image to learn: " + imageName,) F.display() images.append(image) netHebb.trainHebb(image) netDelta.trainDelta(image) netPseudoInv.appendTrainingVectorForPseudoInversion(image) netPseudoInv.trainPseudoInversion() F.add(npyscreen.TitleFixedText, name = "Networks teached by above images",) F.display() imgProc = ImgPreprocessor(imgToRecogn,SIDE_OF_ARRAY,127) imageRec = imgProc.getImageForHopfield() netHebb.initNeurons(imageRec) netDelta.initNeurons(imageRec) netPseudoInv.initNeurons(imageRec) hebbProgressWidget = F.add(npyscreen.TitleSlider, out_of=100, name = "Hebb") F.add(npyscreen.TitleFixedText, name = " ",) deltaProgressWidget = F.add(npyscreen.TitleSlider, out_of=100, name = "Delta") F.add(npyscreen.TitleFixedText, name = " ",) pInvProgressWidget = F.add(npyscreen.TitleSlider, out_of=100, name = "PInv") for i in range(50): netHebb.update(100) hebbProgressWidget.value = 2 + i*2 F.display() for i in range(50): netDelta.update(100) deltaProgressWidget.value = 2 + i*2 F.display() for i in range(50): netPseudoInv.update(100) pInvProgressWidget.value = 2 + i*2 F.display() networkResultHebb = netHebb.getNeuronsMatrix().copy() networkResultDelta = netDelta.getNeuronsMatrix().copy() networkResultPseudoInv = netPseudoInv.getNeuronsMatrix().copy() #netHebb.saveNetworkConfigToFile('networkConfig.txt'); """ #print "================================" matchFound = False for idx, image in enumerate(images): if np.array_equiv(networkResultHebb, image): #print "Found matching image as sample " + str(idx) matchFound = True break elif np.array_equiv(self.invertMatrix(networkResultHebb), image): #print "Found inverse matching image as sample" + str(idx) matchFound = True break if(not matchFound): pass #print "Unfortunately no match!" #print "================================" """ subplotIndex = 201 subplotIndex += 10 * (len(images)+1) for idx, image in enumerate(images): plt.subplot(subplotIndex+idx) image2d = np.reshape(image, (SIDE_OF_ARRAY,SIDE_OF_ARRAY)) plt.imshow(image2d, cmap='gray', interpolation = 'nearest') plt.xticks([]), plt.yticks([]), plt.title("Sample %d" % idx) newLineSubplotIndex = subplotIndex + len(images) plt.subplot(newLineSubplotIndex) image2d = np.reshape(imageRec, (SIDE_OF_ARRAY,SIDE_OF_ARRAY)) plt.imshow(image2d, cmap='gray', interpolation = 'nearest') plt.xticks([]), plt.yticks([]), plt.title("To recognize") plt.subplot(newLineSubplotIndex+1) image2dResult = np.reshape(networkResultHebb , (SIDE_OF_ARRAY,SIDE_OF_ARRAY)) plt.imshow(image2dResult, cmap='gray', interpolation = 'nearest') plt.xticks([]), plt.yticks([]), plt.title("Returned by net (HEBB)") plt.subplot(newLineSubplotIndex+2) image2dResult = np.reshape(networkResultDelta, (SIDE_OF_ARRAY,SIDE_OF_ARRAY)) plt.imshow(image2dResult, cmap='gray', interpolation = 'nearest') plt.xticks([]), plt.yticks([]), plt.title("Returned by net (DELTA)") plt.subplot(newLineSubplotIndex+3) image2dResult = np.reshape(networkResultPseudoInv, (SIDE_OF_ARRAY,SIDE_OF_ARRAY)) plt.imshow(image2dResult, cmap='gray', interpolation = 'nearest') plt.xticks([]), plt.yticks([]), plt.title("Returned by net (PINVERSE)") plt.show()