示例#1
0
def exercise6():
    print("\nExercise 6: Testing\n")

    perceptron = Perceptron(4, 2, 2)
    trainImages = [
        np.array([[1, 0], [0, 0]]),
        np.array([[0, 0], [0, 1]]),
        np.array([[1, 1], [0, 0]]),
        np.array([[0, 1], [0, 1]])
    ]
    labels = [1, -1, 1, -1]
    perceptron.train(trainImages, labels, 10)

    testImages = [
        np.array([[1, 0], [0, 0]]),
        np.array([[0, 0], [0, 1]]),
        np.array([[1, 1], [0, 0]]),
        np.array([[0, 1], [0, 1]])
    ]
    expectedResults = [1, -1, 1, -1]
    print(testTesting(perceptron, testImages, expectedResults))

    testImages = [
        np.array([[0, 0], [1, 0]]),
        np.array([[0, 0], [1, 1]]),
        np.array([[1, 1], [1, 0]]),
        np.array([[0, 1], [1, 1]])
    ]
    expectedResults = [0, -1, 1, -1]
    print(testTesting(perceptron, testImages, expectedResults))
示例#2
0
def fullTest():
    trainImages, labels, testImages = loadImages()

    per = Perceptron(64, 8, 8)
    per.train(trainImages, labels, 10000)
    results = per.test(testImages)
    print(results)
示例#3
0
def exercise5():
    print("\nExercise 5: Training\n")

    perceptron = Perceptron(4, 2, 2)
    images = [np.array([[1, 0], [0, 0]])]
    labels = [1]
    print(testTraining(perceptron, images, labels))

    perceptron = Perceptron(4, 2, 2)
    images = [np.array([[1, 0], [0, 0]]), np.array([[0, 0], [0, 1]])]
    labels = [1, -1]
    print(testTraining(perceptron, images, labels))

    perceptron = Perceptron(4, 2, 2)
    images = [
        np.array([[1, 0], [0, 0]]),
        np.array([[0, 0], [0, 1]]),
        np.array([[1, 1], [0, 0]])
    ]
    labels = [1, -1, 1]
    print(testTraining(perceptron, images, labels))

    perceptron = Perceptron(4, 2, 2)
    images = [
        np.array([[1, 0], [0, 0]]),
        np.array([[0, 0], [0, 1]]),
        np.array([[1, 1], [0, 0]]),
        np.array([[0, 1], [0, 1]])
    ]
    labels = [1, -1, 1, -1]
    print(testTraining(perceptron, images, labels))
示例#4
0
def testCalcError(expectedResult, labels, results):
    perceptron = Perceptron(1, 1, 1)
    result = perceptron.calcError(labels, results)
    outString = ("Testing with labels={}, results={}: ".format(
        labels, results))
    if expectedResult != result:
        outString += ("{}KO| Expected {} got {}{}").format(
            Fore.RED, expectedResult, result, Fore.RESET)
        return outString

    outString += "{}OK{}".format(Fore.GREEN, Fore.RESET)
    return outString
示例#5
0
def fullTest():
    trainImages, labels, testImages = loadImages()

    per = Perceptron(64, 8, 8)
    per.train(trainImages, labels, 10000)
    results = per.test(testImages)
    for image, result in zip(testImages, results):
        cv2.imshow("test", image * 255)
        print(result)
        cv2.waitKey(0)

    print(results)
示例#6
0
def testForwardPass(network, image, expected):
    imageHeight, imageWidth = image.shape[:2]    
    perceptron = Perceptron(len(network), imageWidth, imageHeight)
    perceptron.network = network
    result = perceptron.forwardPass(image)
    outString = ("Testing with image:\n{}\nnetwork={}: ".format(image, network))
    if result != expected:
        outString += ("{}KO| Expected {} got {}{}").format(Fore.RED, expected, 
                                                          result, Fore.RESET)
        return outString

    outString += "{}OK{}".format(Fore.GREEN, Fore.RESET)
    return outString
示例#7
0
def testPerceptronInit(numberOfNeurons, imageWidth, imageHeight):
    perceptron = Perceptron(numberOfNeurons, imageWidth, imageHeight)
    outString = ("Testing with numberOfNeurons={}, imageWidth={}, "
                 "imageHeight={}: ".format(numberOfNeurons, imageWidth, imageHeight))
    if len(perceptron.network) != numberOfNeurons:
        outString += ("{}KO| Expected length of network to be {} but "
                      "got {}{}").format(Fore.RED, len(perceptron.network), 
                                         numberOfNeurons, Fore.RESET)
        return outString

    if type(perceptron.network) != list:
        outString += ("{}KO| Expected perceptron.network to be of type {} but "
                      "got {}{}".format(Fore.RED, list, 
                                        type(perceptron.network), Fore.RESET))
        return outString
    
    for netIndex, neuron in enumerate(perceptron.network):
        if type(neuron) != Neuron:
            outString += ("{}KO| Expected all of the objects in "
                          "perceptron.network to be of type {} but the "
                          "object at position {} is of type {}{}").format(Fore.RED,
                                                                        Neuron, 
                                                                        netIndex, 
                                                                        type(neuron),
                                                                        Fore.RESET)
            return outString

    for netIndex, neuron in enumerate(perceptron.network):
        if neuron.xPos not in range(imageWidth) or neuron.yPos not in range(imageHeight):
            outString += ("{}KO| Expected all neuron positions to be within the "
                          "range [(0, 0), ({}, {})[ but the neuron at index {} "
                          "has position ({}, {}){}".format(Fore.RED, imageWidth, 
                                                     imageHeight, netIndex, 
                                                     neuron.xPos, neuron.yPos,
                                                     Fore.RESET))
            return outString

    neuronPositions = []
    for netIndex, neuron in enumerate(perceptron.network):
        if (neuron.xPos, neuron.yPos) in neuronPositions:
            outString += ("{}KO| Expected all neuron positions to be different "
                          "but neurons at index {} and neuron at index {} "
                          "both have position ({}, {}){}".format(Fore.RED, 
                                                                 netIndex,
                                                                 neuronPositions.index((neuron.xPos, neuron.yPos)),
                                                                 neuron.xPos,
                                                                 neuron.yPos,
                                                                 Fore.RESET))
            return outString
        else:
            neuronPositions.append((neuron.xPos, neuron.yPos))

    outString += "{}OK{}".format(Fore.GREEN, Fore.RESET)
    return outString
示例#8
0
def bonus():
    perceptron1 = Perceptron(1,1,1)
    perceptron1.network = [Neuron((0,0), 0, False)]
    print(testSaveAndLoad(perceptron1))

    perceptron1.network = [Neuron((0,0), 0, True)]
    print(testSaveAndLoad(perceptron1))

    perceptron1.network = [Neuron((0,0), 0, True)]
    print(testSaveAndLoad(perceptron1))

    perceptron1.network = [Neuron((0,0), 1, True)]
    print(testSaveAndLoad(perceptron1))
    
    perceptron2 = Perceptron(2,2,2)
    perceptron2.network = [Neuron((0,0), -1, False), Neuron((1,1), 1, False)]
    print(testSaveAndLoad(perceptron2))

    perceptron2.network = [Neuron((0,0), -1, True), Neuron((1,1), 1, False)]
    print(testSaveAndLoad(perceptron2))

    perceptron2.network = [Neuron((0,0), -1, True), Neuron((1,1), 1, True)]
    print(testSaveAndLoad(perceptron2))

    perceptron2.network = [Neuron((0,0), -1, True), Neuron((1,1), 1, True)]
    print(testSaveAndLoad(perceptron2))

    perceptron2.network = [Neuron((0,0), -1, False), Neuron((1,1), 2, True)]
    print(testSaveAndLoad(perceptron2))

    perceptron2.network = [Neuron((0,0), 0, True), Neuron((1,1), 1, True)]
    print(testSaveAndLoad(perceptron2))
示例#9
0
def exercise3():
    print("\nExercise 3: Back propagation\n")

    perceptron1 = Perceptron(1,1,1)
    perceptron1.network = [Neuron((0,0), 0, False)]
    expectedNetwork = [Neuron((0,0), 0, False)]
    print(testBackProp(perceptron1, expectedNetwork, 1, 0))

    perceptron1.network = [Neuron((0,0), 0, True)]
    expectedNetwork = [Neuron((0,0), 1, False)]
    print(testBackProp(perceptron1, expectedNetwork, 1, 0))

    perceptron1.network = [Neuron((0,0), 0, True)]
    expectedNetwork = [Neuron((0,0), -1, False)]
    print(testBackProp(perceptron1, expectedNetwork, -1, 0))

    perceptron1.network = [Neuron((0,0), 1, True)]
    expectedNetwork = [Neuron((0,0), 0, False)]
    print(testBackProp(perceptron1, expectedNetwork, -1, 1))
    
    perceptron2 = Perceptron(2,2,2)
    perceptron2.network = [Neuron((0,0), -1, False), Neuron((1,1), 1, False)]
    expectedNetwork = [Neuron((0,0), -1, False), Neuron((1,1), 1, False)]
    print(testBackProp(perceptron2, expectedNetwork, -1, 0))

    perceptron2.network = [Neuron((0,0), -1, True), Neuron((1,1), 1, False)]
    expectedNetwork = [Neuron((0,0), -1, False), Neuron((1,1), 1, False)]
    print(testBackProp(perceptron2, expectedNetwork, -1, -1))

    perceptron2.network = [Neuron((0,0), -1, True), Neuron((1,1), 1, True)]
    expectedNetwork = [Neuron((0,0), -2, False), Neuron((1,1), 0, False)]
    print(testBackProp(perceptron2, expectedNetwork, -1, 0))

    perceptron2.network = [Neuron((0,0), -1, True), Neuron((1,1), 1, True)]
    expectedNetwork = [Neuron((0,0), 0, False), Neuron((1,1), 2, False)]
    print(testBackProp(perceptron2, expectedNetwork, 1, 0))

    perceptron2.network = [Neuron((0,0), -1, False), Neuron((1,1), 2, True)]
    expectedNetwork = [Neuron((0,0), -1, False), Neuron((1,1), 2, False)]
    print(testBackProp(perceptron2, expectedNetwork, 1, 1))

    perceptron2.network = [Neuron((0,0), 0, True), Neuron((1,1), 1, True)]
    expectedNetwork = [Neuron((0,0), -1, False), Neuron((1,1), 0, False)]
    print(testBackProp(perceptron2, expectedNetwork, -1, 1))