예제 #1
0
    def traingNetwork(self, nn, numberOfSet):
        print "Training starting:"

        imageNumberList = loadData.getTrainingImageNumberList(numberOfSet)

        d, t = loadData.getImageAndTarget(imageNumberList[0])

        for i in range(1, len(imageNumberList)):
            #print "Forwardpass"

            nn.Calculate(d)

            if (i % (numberOfSet / 10) == 0):
                print "Number of iterations:", i
                nn.learningRate -= 0.000001

            nn.Backpropagate(nn.outputVector, t)

            d, t = loadData.getImageAndTarget(imageNumberList[i])

        saveWeights("1", str(numberOfSet), nn.layers[1].weights)
        saveWeights("2", str(numberOfSet), nn.layers[2].weights)
        saveWeights("3", str(numberOfSet), nn.layers[3].weights)
        saveWeights("4", str(numberOfSet), nn.layers[4].weights)
        print "Training completed. Weights are saved.\n"

        return nn
def traingNetwork(nn,numberOfSet):
	print "Training starting:"

	imageNumberList = loadData.getTrainingImageNumberList(numberOfSet)

	d,t = loadData.getImageAndTarget(imageNumberList[0])

	for i in range(1,len(imageNumberList)):
		#print "Forwardpass"
		
		nn.ForwardPass(d)
		

		if(i%(numberOfSet/10)==0):
			print "Number of iterations:",i
			nn.learningRate -=0.00001
		

		nn.Backpropagate(nn.outputVector,t)


		d,t = loadData.getImageAndTarget(imageNumberList[i])


	saveWeights("1",str(numberOfSet),nn.layers[1].weights)
	saveWeights("2",str(numberOfSet),nn.layers[2].weights)
	saveWeights("3",str(numberOfSet),nn.layers[3].weights)
	saveWeights("4",str(numberOfSet),nn.layers[4].weights)
	print "Training completed. Weights are saved.\n"

	return nn
예제 #3
0
    def runCNN(self, image):

        d, t = loadData.getImageAndTarget(100)
        # Forward-pass
        nn.Calculate(d)

        return nn.outputVector.index(max(nn.outputVector))
예제 #4
0
    def testNetwork(self, nn, numberOfSet, numberOfTest):

        print "Testing starting:"

        # Set weights from file
        nn = setWeights(nn, numberOfSet)

        imageNumberList = loadData.getTestImageNumberList(numberOfTest)

        correct = 0
        for i in range(0, len(imageNumberList)):

            # Get random picture
            d, t = loadData.getImageAndTarget(imageNumberList[i])

            # Forward-pass
            nn.Calculate(d)

            correctGuess = False
            # Check if result is correct
            if (nn.outputVector.index(max(nn.outputVector)) == t.index(
                    max(t))):
                correct += 1
                correctGuess = True

            print "CNN:", nn.outputVector.index(max(
                nn.outputVector)), "Target:", t.index(max(t)), correctGuess

        print "\nNumber of correct:", correct
        print "Number of pictures", numberOfTest
        print "Percentage", (correct * 1.0 / numberOfTest) * 100
def visualiseNetwork(nn, numberOfSet):
    nn = setWeights(nn, numberOfSet)

    d, t = loadData.getImageAndTarget(random.randint(0, 60000))
    nn.Calculate(d)

    visualise.visualise(nn)
def visualiseNetwork(nn,numberOfSet):
	nn = setWeights(nn,numberOfSet)

	d,t = loadData.getImageAndTarget(random.randint(0,60000))
	nn.ForwardPass(d)

	visualise.visualise(nn)
def testNetwork(nn,numberOfSet,numberOfTest,modification=False):

	print "Testing starting:"

	# Set weights from file
	nn = setWeights(nn,numberOfSet)
	
	imageNumberList = loadData.getTestImageNumberList(numberOfTest)

	correct = 0
	for i in range(0,len(imageNumberList)):

		# Get random picture
		d,t = loadData.getImageAndTarget(imageNumberList[i])

		# Forward-pass
		nn.ForwardPass(d)
		
		correctGuess = False
		# Check if result is correct
		if(nn.outputVector.index(max(nn.outputVector))==t.index(max(t))):
			correct+=1
			correctGuess = True

		print "CNN:",nn.outputVector.index(max(nn.outputVector)),"Target:",t.index(max(t)),correctGuess


	print "\nNumber of correct:",correct
	print "Number of pictures",numberOfTest
	print "Percentage",(correct*1.0/numberOfTest) * 100
	def runCNN(self,image):


		d,t = loadData.getImageAndTarget(100)
		# Forward-pass
		nn.Calculate(d)
		
		return nn.outputVector.index(max(nn.outputVector))
def getNetworkImage(nn,number):
	d,t = loadData.getImageAndTarget(number)
	nn.ForwardPass(d)
	
	out = visualise.getNeuronOutputs(nn)

	out = np.array(out[0])

	for i in range(0,len(out)):
		
		for j in range(0,len(out[i])):

			out[i][j] = out[i][j]*100

	s = base64.b64encode(out)
	s = "data:image/png;base64,"+s

	return s
def getNetworkImage(nn, number):
    d, t = loadData.getImageAndTarget(number)
    nn.ForwardPass(d)

    out = visualise.getNeuronOutputs(nn)

    out = np.array(out[0])

    for i in range(0, len(out)):

        for j in range(0, len(out[i])):

            out[i][j] = out[i][j] * 100

    s = base64.b64encode(out)
    s = "data:image/png;base64," + s

    return s
def runCNN(nn,image):

	print image
	#
	# This method takes an image and returns the classified label of that image
	#


	#
	# Converts the image to png and saves it localy
	#
	image = image.replace('data:image/png;base64,','')
	fh = open("imageToSave.png", "wb")
	image = image.decode('base64')
	fh.write(image)
	fh.close()


	#
	# Downsample image to 29x29 pixels and saves it
	#


	import PIL
	from PIL import Image

	basewidth = 29	
	img = Image.open('imageToSave.png')
	wpercent = (basewidth/float(img.size[0]))
	hsize = int((float(img.size[1])*float(wpercent)))
	img = img.resize((basewidth,hsize), PIL.Image.ANTIALIAS)
	img.save('imageToSave.png')



	# Load the image
	img = matplotlib.image.imread("imageToSave.png")


	#
	# Imported images is on wrong format. [[0,0,0],[0,0,0],[0,0,0]], have to have it in one list
	#
	c = 0
	newimage= []
	for i in range(len(img)):
		for j in range(len(img[i])):
			c = 0
			for k in range(len(img[i][j])):
				c+= img[i][j][k]
			newimage.append(c)


	#
	# Since the image created digital, the image don't have the "natural" features as a handwritten image have
	# Have to do some stuff to make it more applicable.
	#


	# Input picture is something like this:[[0,0,0]
	#										[0,1,0]
	#										[0,0,0]]
	#
	#
	# Output will be something like this:  [[0,   52,  0]
	#										[102, 250, 69]
	#										[0,   94,  0]]
	#


	#
	# For every pixel with value in, the neighbours on all sides of that pixel will have a random value from 50 to 150. 
	#
	padd = [0]*len(newimage)
	for i in range(29*2,len(newimage)-29*2):
		if(newimage[i]>0.12):
			padd[i-1] = random.randint(50,150)
			padd[i+1] = random.randint(50,150)
			#padd[i-2] = 100
			#padd[i+2] = 100

			padd[i-29] = random.randint(50,150)
			padd[i+29] = random.randint(50,150)
			#padd[i-29*2] = 100
			#padd[i+29*2] = 100


	for i in range(len(newimage)):
		if(newimage[i]>0.12 and newimage[i] != 150):
			padd[i] = random.randint(240,255)

	newimage = padd


	#
	# This part is just to look how the created picture is compared to one from the training/testing set
	#
	d,t = loadData.getImageAndTarget(random.randint(0,60000))


	#
	# Get better printing format for both images
	#
	
	#for i in range(0,29):
	#	a = []
	#	for j in range(0,29):
	#		a.append(d[i*29+j])
	#	print a


	#print t.index(max(t))
	#print " "
	#for i in range(0,29):
	#	a = []
	#	for j in range(0,29):
	#		a.append(newimage[i*29+j])
	#	print a
	
	# Forward-pass
	nn.ForwardPass(newimage)

	#
	# Return dictionary to server
	#
	sort = sorted(nn.outputVector[:])[::-1]
	ranked = {}
	for i in range(0,len(nn.outputVector)):
		ranked[i] = (nn.outputVector[nn.outputVector.index(sort[i])],(nn.outputVector.index(sort[i])))

	return ranked
def runCNN(nn, image):

    print image
    #
    # This method takes an image and returns the classified label of that image
    #

    #
    # Converts the image to png and saves it localy
    #
    image = image.replace('data:image/png;base64,', '')
    fh = open("imageToSave.png", "wb")
    image = image.decode('base64')
    fh.write(image)
    fh.close()

    #
    # Downsample image to 29x29 pixels and saves it
    #

    import PIL
    from PIL import Image

    basewidth = 29
    img = Image.open('imageToSave.png')
    wpercent = (basewidth / float(img.size[0]))
    hsize = int((float(img.size[1]) * float(wpercent)))
    img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS)
    img.save('imageToSave.png')

    # Load the image
    img = matplotlib.image.imread("imageToSave.png")

    #
    # Imported images is on wrong format. [[0,0,0],[0,0,0],[0,0,0]], have to have it in one list
    #
    c = 0
    newimage = []
    for i in range(len(img)):
        for j in range(len(img[i])):
            c = 0
            for k in range(len(img[i][j])):
                c += img[i][j][k]
            newimage.append(c)

    #
    # Since the image created digital, the image don't have the "natural" features as a handwritten image have
    # Have to do some stuff to make it more applicable.
    #

    # Input picture is something like this:[[0,0,0]
    #										[0,1,0]
    #										[0,0,0]]
    #
    #
    # Output will be something like this:  [[0,   52,  0]
    #										[102, 250, 69]
    #										[0,   94,  0]]
    #

    #
    # For every pixel with value in, the neighbours on all sides of that pixel will have a random value from 50 to 150.
    #
    padd = [0] * len(newimage)
    for i in range(29 * 2, len(newimage) - 29 * 2):
        if (newimage[i] > 0.12):
            padd[i - 1] = random.randint(50, 150)
            padd[i + 1] = random.randint(50, 150)
            #padd[i-2] = 100
            #padd[i+2] = 100

            padd[i - 29] = random.randint(50, 150)
            padd[i + 29] = random.randint(50, 150)
            #padd[i-29*2] = 100
            #padd[i+29*2] = 100

    for i in range(len(newimage)):
        if (newimage[i] > 0.12 and newimage[i] != 150):
            padd[i] = random.randint(240, 255)

    newimage = padd

    #
    # This part is just to look how the created picture is compared to one from the training/testing set
    #
    d, t = loadData.getImageAndTarget(random.randint(0, 60000))

    #
    # Get better printing format for both images
    #

    #for i in range(0,29):
    #	a = []
    #	for j in range(0,29):
    #		a.append(d[i*29+j])
    #	print a

    #print t.index(max(t))
    #print " "
    #for i in range(0,29):
    #	a = []
    #	for j in range(0,29):
    #		a.append(newimage[i*29+j])
    #	print a

    # Forward-pass
    nn.ForwardPass(newimage)

    #
    # Return dictionary to server
    #
    sort = sorted(nn.outputVector[:])[::-1]
    ranked = {}
    for i in range(0, len(nn.outputVector)):
        ranked[i] = (nn.outputVector[nn.outputVector.index(sort[i])],
                     (nn.outputVector.index(sort[i])))

    return ranked