Пример #1
0
def testNNDigits():
	accuracy = []
	for imageSetLength in range(int(len(digits.trainData) * 0.1), len(digits.trainData), int(len(digits.trainData) * 0.1)):
		numcorrect = 0
		numtotal = int(len(digits.testData) * 0.10)
		trainFeatureVectors = []
		for i in digits.trainData[0:imageSetLength]:
			trainFeatureVectors.append(featureVector(i.image, 4, 14, 28, 28))
		for i in range(0, numtotal):
			prediction = NNClassifier(digits.testData[i],  digits.trainData[0:imageSetLength], trainFeatureVectors, 4, 14, 28, 28)
			reality = digits.testData[i].label
			if int(prediction) == int(reality):
				numcorrect += 1
		accuracy.append((numcorrect / numtotal))
	numcorrect = 0
	numtotal = int(len(digits.testData) * 0.10)
	trainFeatureVectors = []
	for i in digits.trainData:
		trainFeatureVectors.append(featureVector(i.image, 4, 14, 28, 28))
	for i in range(0, numtotal):
		prediction = NNClassifier(digits.testData[i], digits.trainData, trainFeatureVectors, 4, 14, 28, 28)
		reality = digits.testData[i].label
		if int(prediction) == int(reality):
			numcorrect += 1
	accuracy.append((numcorrect / numtotal))
	return accuracy
def featureProbMatrixGen(numColumns, numRows, numLabels, A, Y, trainData):
    totalNumFeatures = len(
        featureVector(trainData[0].image, numRows, numColumns, A, Y))
    matrixVector = [
        ones([totalNumFeatures, len(trainData)]) for x in range(0, numLabels)
    ]
    for i in range(0, len(trainData)):
        feature = featureVector(trainData[i].image, numRows, numColumns, A,
                                Y)  #feature vector for ith training image
        for j in range(0, len(matrixVector[int(trainData[i].label)])):
            matrixVector[int(trainData[i].label)][j][feature[j]] += 1

    for i in range(0, len(matrixVector)):
        for j in range(0, len(matrixVector[i])):
            total = 0  #total of  individual feature values
            for k in range(0, len(matrixVector[i][j])):
                total += int(matrixVector[i][j][k])
            for k in range(0, len(matrixVector[i][j])):
                matrixVector[i][j][k] /= total

    #The following is to avoid a potential multiply by zero later
    for i in range(0, len(matrixVector)):
        for j in range(0, len(matrixVector[i])):
            for k in range(0, len(matrixVector[i][j])):
                if (matrixVector[i][j][k] == 0):
                    matrixVector[i][j][k] = .001
    return matrixVector
def NNClassifier(image, trainData, trainFeatureVectors, r, c, A, Y):
    imageFeatures = featureVector(image.image, r, c, A, Y)
    differenceValues = [
        (vectorDifference(imageFeatures, trainFeatureVectors[i]), i)
        for i in range(0, len(trainFeatureVectors))
    ]  #vector of how the images features compare to training images features
    return modeOfKNeighbors(trainData, differenceValues, 7)
Пример #4
0
def knnFace100(index):
    trainFeatureVectors = []
    for i in faces.trainData:
        trainFeatureVectors.append(featureVector(i.image, 7, 6, 70, 60))
    prediction = NNClassifier(faces.testData[index], faces.trainData,
                              trainFeatureVectors, 7, 6, 70, 60)
    reality = faces.testData[index].label
    print('KNN (Face): prediction: {} reality: {}'.format(prediction, reality))
Пример #5
0
def perceptronDigitClassifierTrainer(trainData):
    featureVectorList = list(
    )  #list of feature vectors for each image in training data set
    for i in trainData:
        featureVectorList.append(featureVector(i.image, r=14, c=7, A=28, Y=28))
    weightVector = [[
        random.random() for i in range(len(featureVectorList[1]))
    ] for j in range(0, 10)]
    biasVector = [random.random() for i in range(0, 10)]

    perfectRun = False  #boolean for when to stop training
    iterations = 0  #counter for number of loops through training set

    while (perfectRun is False and iterations < 100):
        successCounter = 0
        predictorVector = [
            0 for x in range(0, 10)
        ]  #holds values of prediction for each digit respectively

        for i in range(0, len(trainData)):
            if (perfectRun == True):
                break
            for k in range(0, len(predictorVector)):
                if (perfectRun == True):
                    break

                for j in range(0, len(featureVectorList[i])
                               ):  #iterating over feature values of image
                    predictorVector[
                        k] += featureVectorList[i][j] * weightVector[k][j]

                predictorVector[k] += biasVector[k]

                prediction = findHighestPrediction(predictorVector)
                if (prediction == int(
                        trainData[i].label)):  #got prediction correct
                    successCounter += 1
                if (i == len(trainData) - 1 and successCounter
                        == i):  #full pass of all correct predictions
                    perfectRun = True
                    break
                else:
                    for x in range(0, len(weightVector[prediction])
                                   ):  #punishment for wrong prediction
                        weightVector[prediction][x] -= featureVectorList[i][x]
                        biasVector[prediction] -= 1
                        weightVector[int(
                            trainData[i].label)][x] += featureVectorList[i][
                                x]  #enforcing ground truth weights
                        biasVector[int(trainData[i].label)] += 1
        if iterations % 10 == 0:
            print('iteration {} of 100'.format(iterations))
        iterations += 1

    for i in range(0, len(weightVector)):
        weightVector[i].append(biasVector[i])
    return weightVector
Пример #6
0
def isFace(image, weightVector):
    imageFeatures = featureVector(image.image, r=10, c=10, A=70, Y=60)
    predFunction = 0
    for i in range(0, len(imageFeatures)):
        predFunction += imageFeatures[i] * weightVector[i]
    predFunction += weightVector[len(weightVector) - 1]  #bias
    if (predFunction > 0):
        return 1
    return 0
Пример #7
0
def knnDigit100(index):
    trainFeatureVectors = []
    for i in digits.trainData:
        trainFeatureVectors.append(featureVector(i.image, 4, 14, 28, 28))
    prediction = NNClassifier(digits.testData[index], digits.trainData,
                              trainFeatureVectors, 4, 14, 28, 28)
    reality = digits.testData[index].label
    print('KNN (Digits): prediction: {} reality: {}'.format(
        prediction, reality))
Пример #8
0
def whichDigit(image, weights):
    imageFeatures = featureVector(image.image, r=14, c=7, A=28, Y=28)
    predictionVector = [0 for x in range(0, 10)]
    for i in range(0, len(weights)):
        for j in range(0, len(weights[i])):
            if (j != len(weights[i]) - 1):  #j is not at bias position)
                predictionVector[i] += weights[i][j] * imageFeatures[j]
            else:
                predictionVector[i] += weights[i][j]  #adding bias
    return findHighestPrediction(predictionVector)
Пример #9
0
def testNNFaces():
	accuracy = []
	for imageSetLength in range(int(len(faces.trainData) * 0.1), len(faces.trainData), int(len(faces.trainData) * 0.1)):
		numcorrect = 0
		numtotal = len(faces.testData)
		trainFeatureVectors = []
		for i in faces.trainData[0:imageSetLength]:
			trainFeatureVectors.append(featureVector(i.image, 7, 6, 70, 60))
		for i in range(0, numtotal):
			prediction = NNClassifier(faces.testData[i],  faces.trainData[0:imageSetLength], trainFeatureVectors, 7, 6, 70, 60)
			reality = faces.testData[i].label
			if int(prediction) == int(reality):
				numcorrect += 1
		accuracy.append((numcorrect / numtotal))
	return accuracy
Пример #10
0
def perceptronFaceClassifierTrainer(trainData):
    featureVectorList = list(
    )  #list of feature vectors for each image in training data set

    for i in trainData:
        featureVectorList.append(featureVector(i.image, r=10, c=10, A=70,
                                               Y=60))

    weightVector = [random.random() for i in range(len(featureVectorList[1]))]

    bias = random.random()
    perfectRun = False  #boolean for when to stop training
    iterations = 0  #counter for number of loops through training set
    while (perfectRun is False and iterations < 1000):
        successCounter = 0
        for i in range(0, len(trainData)):
            predFunction = 0

            for j in range(0, len(featureVectorList[i])
                           ):  #iterating over feature values of image i
                predFunction += featureVectorList[i][j] * weightVector[j]

            predFunction += bias
            prediction = 0  #0 means not face, 1 means face
            if (predFunction > 0):
                prediction = 1
            if ((prediction > 0 and int(trainData[i].label) == 1)
                    or (prediction <= 0 and int(trainData[i].label) == 0)):
                successCounter += 1
                if (i == len(trainData) - 1 and successCounter
                        == i):  #full pass of all correct predictions
                    perfectRun = True
                    break
            #since prediction failed we must adjust weights accordingly
            elif (prediction <= 0):
                for j in range(0, len(featureVectorList[i])):
                    weightVector[j] += featureVectorList[i][j]
                bias += 1
            elif (prediction > 0):
                for j in range(0, len(featureVectorList[i])):
                    weightVector[j] -= featureVectorList[i][j]
                bias -= 1
        if iterations % 100 == 0:
            print('iteration {} of 1000'.format(iterations))
        iterations += 1
    weightVector.append(bias)
    return weightVector
def isFaceBayes(image, trainingData, matrixVector, r, c):
    imageFeatures = featureVector(image.image, r, c, A=70, Y=60)
    faceCount = 0
    for i in trainingData:
        if (int(i.label) == 1):
            faceCount += 1
    probFace = faceCount / len(trainingData)
    probNotFace = 1 - probFace
    probImageGivenFace = 1
    for i in range(0, len(imageFeatures)):
        probImageGivenFace *= matrixVector[0][i][imageFeatures[i]]
        # the 0 in the index is the face feature matrix
    probImageGivenNotFace = 1
    for i in range(0, len(imageFeatures)):
        probImageGivenNotFace *= matrixVector[1][i][imageFeatures[i]]
        # the 1 in the index is the not face feature matrix
    probFaceGivenImage = probImageGivenFace * probFace
    probNotFaceGivenImage = probImageGivenNotFace * probNotFace
    if (probFaceGivenImage > probNotFaceGivenImage):
        return 0
    return 1
def whichDigitBayes(image, trainingData, matrixVector, c, r):
    imageFeatures = featureVector(image.image, r, c, A=28, Y=28)
    digitProbabilities = [0 for x in range(0, 10)]

    for i in range(0, len(trainingData)):
        digitProbabilities[int(trainingData[i].label)] += 1

    for i in range(0, len(digitProbabilities)):
        digitProbabilities[i] /= len(trainingData)

    probImageGivenDigits = [1 for i in range(0, 10)
                            ]  #vector of necessary conditional probabilities

    for i in range(0, len(probImageGivenDigits)):
        for j in range(0, len(imageFeatures)):
            probImageGivenDigits[i] *= matrixVector[i][j][imageFeatures[j]]

    probDigitsGivenImage = [
        probImageGivenDigits[i] * digitProbabilities[i]
        for i in range(0, len(probImageGivenDigits))
    ]

    return findHighestPrediction(probDigitsGivenImage)