Exemple #1
0
    def validateFace(self, dataFile, labelsFile, n, width, height, itemNo = -1):
        items = readData.loadDataFile(dataFile, n, width, height)
        labels = readData.loadLabelsFile(labelsFile, n)

        function = 0
        guess = 0
        correct = 0

        if itemNo != -1:
            image = items[itemNo]
            features = getFeatures.getFeatures(image.pixels, width, height)
            for j in range(len(features)):
                function += (self.weightsFace[j] * features[j])
            function += self.weightsFace[len(features)]
            if function >= 0: return 1
            else: return 0

        for i in range(len(items)):
            image = items[i]
            features = getFeatures.getFeatures(image.pixels, width, height)
            for j in range(len(features)):
                function += (self.weightsFace[j] * features[j])
            function += self.weightsFace[len(features)]
            if function >= 0: guess = 1
            else: guess = 0
            if guess == labels[i]: correct += 1
            function = 0
            
        return correct/n
Exemple #2
0
    def validateDigit(self, dataFile, labelsFile, n, width, height, itemNo = -1):
        items = readData.loadDataFile(dataFile, n, width, height)
        labels = readData.loadLabelsFile(labelsFile, n)

        function = [0,0,0,0,0,0,0,0,0,0]
        guess = 0
        correct = 0

        if itemNo != -1:
            for k in range(10):
                image = items[itemNo]
                features = getFeatures.getFeaturesPixels(image.pixels, width, height)
                for j in range(len(features)):
                    function[k] += (self.weightsDigit[j][k] * features[j])
                function[k] += self.weightsDigit[len(features)][k]

            return function.index(max(function))

        for i in range(len(items)):
            for k in range(10):
                image = items[i]
                features = getFeatures.getFeaturesPixels(image.pixels, width, height)
                for j in range(len(features)):
                    function[k] += (self.weightsDigit[j][k] * features[j])
                function[k] += self.weightsDigit[len(features)][k]

            guess = function.index(max(function))
            if guess == labels[i]: correct += 1
            function = [0,0,0,0,0,0,0,0,0,0]
        return correct/n
Exemple #3
0
    def trainFace(self, dataFile, labelsFile, n, width, height, randomList):
        items = readData.loadDataFile(dataFile, n, width, height)
        labels = readData.loadLabelsFile(labelsFile, n)
        self.featurepd = []
        numberFaces = 0

        # featureTimes is a list of tuples where the first number is the number of times the feature occurs when the image is not a face
        # and the second number is the number of times the features occurs when the image is a face
        featureTimes = []
        numberFeatures = (math.ceil(height / getFeatures.DIM)) * math.ceil(
            (width / getFeatures.DIM))
        for i in range(numberFeatures):
            featureTimes.append([0.0, 0.0])

        for count in randomList:
            image = items[count]
            if labels[count] == 1:
                numberFaces += 1.0
            features = getFeatures.getFeatures(image.pixels, width, height)
            for i in range(len(features)):
                if features[i] > 0:
                    if labels[count] == 1:
                        featureTimes[i][1] += 1
                    else:
                        featureTimes[i][0] += 1

        for i in range(len(featureTimes)):
            self.featurepd.append([
                (featureTimes[i][0] + self.k) /
                (len(randomList) - numberFaces + self.k),
                (featureTimes[i][1] + self.k) / (numberFaces + self.k)
            ])

        self.faceProb = numberFaces / len(randomList)
Exemple #4
0
    def validateDigit(self, dataFile, labelsFile, n, width, height, itemNo=-1):
        items = readData.loadDataFile(dataFile, n, width, height)
        labels = readData.loadLabelsFile(labelsFile, n)

        like = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        guess = 0
        correct = 0

        if itemNo != -1:
            image = items[itemNo]
            features = getFeatures.getFeaturesPixels(image.pixels, width,
                                                     height)
            productTrue = 1
            productFalse = 1
            for k in range(10):

                for j in range(len(self.featurepdDig)):
                    if features[j] > 0:
                        productTrue *= self.featurepdDig[j][k][0]
                        productFalse *= self.featurepdDig[j][k][1]
                    else:
                        productTrue *= (1 - self.featurepdDig[j][k][0])
                        productFalse *= (1 - self.featurepdDig[j][k][1])

                like[k] = (productTrue *
                           self.digitProb[k]) / (productFalse *
                                                 (1 - self.digitProb[k]))
                productTrue = 1
                productFalse = 1
            guess = like.index(max(like))
            return guess

        for i in range(len(items)):
            image = items[i]
            features = getFeatures.getFeaturesPixels(image.pixels, width,
                                                     height)
            productTrue = 1
            productFalse = 1
            for k in range(10):

                for j in range(len(self.featurepdDig)):
                    if features[j] > 0:
                        productTrue *= self.featurepdDig[j][k][0]
                        productFalse *= self.featurepdDig[j][k][1]
                    else:
                        productTrue *= (1 - self.featurepdDig[j][k][0])
                        productFalse *= (1 - self.featurepdDig[j][k][1])

                like[k] = (productTrue *
                           self.digitProb[k]) / (productFalse *
                                                 (1 - self.digitProb[k]))
                productTrue = 1
                productFalse = 1
            guess = like.index(max(like))

            if guess == labels[i]:
                correct += 1
        return correct / n
Exemple #5
0
    def validateFace(self, dataFile, labelsFile, n, width, height, itemNo=-1):
        items = readData.loadDataFile(dataFile, n, width, height)
        labels = readData.loadLabelsFile(labelsFile, n)

        like = 0.0
        guess = 0
        correct = 0

        if itemNo != -1:
            image = items[itemNo]
            features = getFeatures.getFeatures(image.pixels, width, height)
            productTrue = pow(10, 300.0)
            productFalse = pow(10, 300.0)
            for j in range(len(self.featurepd)):
                if features[j] > 0:
                    productTrue *= self.featurepd[j][1]
                    productFalse *= self.featurepd[j][0]
                else:
                    productTrue *= (1 - self.featurepd[j][1])
                    productFalse *= (1 - self.featurepd[j][0])

            like = (productTrue * self.faceProb) / (productFalse *
                                                    (1 - self.faceProb))

            if like >= 1:
                return 1
            else:
                return 0

        for i in range(len(items)):
            image = items[i]
            features = getFeatures.getFeatures(image.pixels, width, height)
            productTrue = pow(10, 300.0)
            productFalse = pow(10, 300.0)
            for j in range(len(self.featurepd)):
                if features[j] > 0:
                    productTrue *= self.featurepd[j][1]
                    productFalse *= self.featurepd[j][0]
                else:
                    productTrue *= (1 - self.featurepd[j][1])
                    productFalse *= (1 - self.featurepd[j][0])

            like = (productTrue * self.faceProb) / (productFalse *
                                                    (1 - self.faceProb))

            if like >= 1:
                guess = 1
            else:
                guess = 0
            if guess == labels[i]:
                correct += 1

        return correct / n
Exemple #6
0
 def trainFace(self, dataFile, labelsFile, n, width, height, rList):
     items = readData.loadDataFile(dataFile, n, width, height)
     old_labels = readData.loadLabelsFile(labelsFile, n)
     self.weightsFace = []
     
     numberFeatures = (math.ceil(height/getFeatures.DIM)) * math.ceil((width/getFeatures.DIM))
     for i in range(numberFeatures + 1):
         self.weightsFace.append(0)
         
     labels = []
     for r in rList:
         labels.append(old_labels[r])
         
     # this array contains the features for every image
     features = []
     for i in rList:
         features.append(getFeatures.getFeatures(items[i].pixels, width, height))
     loop = 0
     index = 0
     # traverses all the items and updates the weights accordingly
     # stops when a full loop is completed without updates
     iterations = 0
     while loop < len(rList) and iterations <  10 *len(rList):
         iterations +=1
         function = 0
         for i in range(numberFeatures):
             function += self.weightsFace[i] * features[index][i]
         function += self.weightsFace[numberFeatures]
         if (function >= 0 and labels[index] == 1) or (function < 0 and labels[index] == 0):
             loop += 1
         elif function < 0 and labels[index] == 1:
             for i in range(numberFeatures):
                 self.weightsFace[i] += features[index][i]
             self.weightsFace[numberFeatures] += 1
             loop = 0
         else:
             for i in range(numberFeatures):
                 self.weightsFace[i] -= features[index][i]
             self.weightsFace[numberFeatures] -= 1
             loop = 0
         if index == len(rList)-1: index = 0
         else: index += 1
Exemple #7
0
    def trainDigit(self, dataFile, labelsFile, n, width, height, rList):
        items = readData.loadDataFile(dataFile, n, width, height)
        old_labels = readData.loadLabelsFile(labelsFile, n)
        self.weightsDigit = []
        
        numberFeatures = height * width
        for i in range(numberFeatures + 1):
            self.weightsDigit.append([0,0,0,0,0,0,0,0,0,0])

        # this array contains the features for every image
        features = []
        labels = []
        for i in rList:
            features.append(getFeatures.getFeaturesPixels(items[i].pixels, width, height))
            labels.append(old_labels[i])
        loop = 0
        index = 0
        # traverses all the items and updates the weights accordingly
        # stops when a full loop is completed without updates

        guess = 0
        iterations = 0
        while loop < len(rList) and iterations <  10 * len(rList):
            function = [0,0,0,0,0,0,0,0,0,0]
            iterations += 1
            for k in range(10):
                for i in range(numberFeatures):
                    function[k] += self.weightsDigit[i][k] * features[index][i]
                function[k] += self.weightsDigit[numberFeatures][k]
            guess = function.index(max(function))
            if guess == labels[index]:
                loop += 1
            else:
                for i in range(numberFeatures):
                    self.weightsDigit[i][labels[index]] += features[index][i]
                self.weightsDigit[numberFeatures][labels[index]] += 1
                for i in range(numberFeatures):
                    self.weightsDigit[i][guess] -= features[index][i]
                self.weightsDigit[numberFeatures][guess] -= 1
                loop = 0
            if index == len(rList)-1: index = 0
            else: index += 1
Exemple #8
0
    def trainDigit(self, dataFile, labelsFile, n, width, height, randomList):
        items = readData.loadDataFile(dataFile, n, width, height)
        labels = readData.loadLabelsFile(labelsFile, n)
        self.featurepdDig = []
        numberDigits = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

        # featureTimes is a list of tuples where the first number is the number of times the feature occurs when the image is not a face
        # and the second number if the number of times the features occurs when the image is a face
        featureTimes = []
        numberFeatures = height * width
        for i in range(numberFeatures):
            featureTimes.append([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

        for count in randomList:
            image = items[count]
            numberDigits[labels[count]] += 1
            features = getFeatures.getFeaturesPixels(image.pixels, width,
                                                     height)
            for i in range(len(features)):
                if features[i] > 0:
                    featureTimes[i][labels[count]] += 1

        pd = []
        totalF = 0
        for i in range(len(featureTimes)):
            for m in range(10):
                totalF += featureTimes[i][m]
            for j in range(10):
                pd.append([
                    (featureTimes[i][j] + self.k) / (numberDigits[j] + self.k),
                    (totalF - featureTimes[i][j] + self.k) /
                    (len(randomList) - numberDigits[j] + self.k)
                ])
            self.featurepdDig.append(pd)
            pd = []
            totalF = 0

        for k in range(10):
            self.digitProb[k] = numberDigits[k] / len(randomList)