Esempio n. 1
0
    def train(self, num_classifiers):

        # Use this scores array to train a weak classifier using VJ_Classifier
        # in the for loop below.
        scores = np.zeros((len(self.integralImages), len(self.haarFeatures)))
        print " -- compute all scores --"
        for i, im in enumerate(self.integralImages):
            scores[i, :] = [hf.evaluate(im) for hf in self.haarFeatures]

        weights_pos = np.ones(len(self.posImages), dtype='float') * 1.0 / (
                           2*len(self.posImages))
        weights_neg = np.ones(len(self.negImages), dtype='float') * 1.0 / (
                           2*len(self.negImages))

        weights = np.hstack((weights_pos, weights_neg))

        print " -- select classifiers --"
        excluded_feats = []
        for i in range(num_classifiers):
            # print 'Current excluded feat ids'
            # print excluded_feats
            weights = np.array([wt/np.sum(weights) for wt in weights])
            h = VJ_Classifier(scores, self.labels, weights=weights, excl_feat=excluded_feats)
            h.train()
            excluded_feats.append(h.feature)
            eps = h.error
            beta = eps/(1. - eps)
            alpha = np.log(1/beta)

            h_j = h.predict(scores.transpose())
            errors = np.array([h_j[i] != self.labels[i] for i in range(0, len(h_j))])
            # err = [int(x) for x in errors]
            self.classifiers.append(h)
            self.alphas.append(alpha)
            weights[errors] = weights[errors]*beta**(1 - (-1))
Esempio n. 2
0
    def train(self, num_classifiers):

        # Use this scores array to train a weak classifier using VJ_Classifier
        # in the for loop below.
        scores = np.zeros((len(self.integralImages), len(self.haarFeatures)))
        print " -- compute all scores --"
        for i, im in enumerate(self.integralImages):
            scores[i, :] = [hf.evaluate(im) for hf in self.haarFeatures]

        weights_pos = np.ones(len(self.posImages),
                              dtype='float') * 1.0 / (2 * len(self.posImages))
        weights_neg = np.ones(len(self.negImages),
                              dtype='float') * 1.0 / (2 * len(self.negImages))
        weights = np.hstack((weights_pos, weights_neg))

        print " -- select classifiers --"

        for i in range(num_classifiers):
            weights_pos /= weights_pos.sum()
            weights_neg /= weights_neg.sum()
            hj = VJ_Classifier(scores, self.labels, weights)
            hj.train()
            self.classifiers.append(hj)
            beta = hj.error / (1 - hj.error)
            alpha = np.log(1 / beta)
            for i in range(0, len(weights)):
                ei = 1
                if (hj.predict(scores[i]) == self.labels[i]):
                    ei = -1
                weights[i] = weights[i] * (beta**(1 - ei))

            self.alphas.append(alpha)
Esempio n. 3
0
    def train(self, num_classifiers):

        # Use this scores array to train a weak classifier using VJ_Classifier
        # in the for loop below.
        scores = np.zeros((len(self.integralImages), len(self.haarFeatures)))
        print(" -- compute all scores --")
        for i, im in enumerate(self.integralImages):
            scores[i, :] = [hf.evaluate(im) for hf in self.haarFeatures]

        weights_pos = np.ones(len(self.posImages),
                              dtype='float') * 1.0 / (2 * len(self.posImages))
        weights_neg = np.ones(len(self.negImages),
                              dtype='float') * 1.0 / (2 * len(self.negImages))
        weights = np.hstack((weights_pos, weights_neg))

        print(" -- select classifiers --")
        for i in range(num_classifiers):
            # Step 1: Normalize the weights
            weights_total = np.sum(weights)
            weights = weights / weights_total

            # Step 2: Training the VJ_Classifier
            vjc = VJ_Classifier(scores,
                                self.labels,
                                weights,
                                thresh=0,
                                feat=0,
                                polarity=1)
            vjc.train()
            error = vjc.error

            # Step 3: Append coefficients to classifier
            self.classifiers.append(vjc)

            # Step 4: Update the weights
            beta = float(error) / (1. - error)
            for i in range(len(self.integralImages)):
                if self.labels[i] == vjc.predict(scores[i]):
                    weights[i] = weights[i] * beta
                else:
                    weights[i] = weights[i]

            # Step 5: Calculate alphas
            self.alphas.append(np.log(1. / beta))
Esempio n. 4
0
    def train(self, num_classifiers):

        # Use this scores array to train a weak classifier using VJ_Classifier
        # in the for loop below.
        scores = np.zeros((len(self.integralImages), len(self.haarFeatures)))
        print(" -- compute all scores --")
        for i, im in enumerate(self.integralImages):
            scores[i, :] = [hf.evaluate(im) for hf in self.haarFeatures]

        weights_pos = np.ones(len(self.posImages),
                              dtype='float') * 1.0 / (2 * len(self.posImages))
        weights_neg = np.ones(len(self.negImages),
                              dtype='float') * 1.0 / (2 * len(self.negImages))
        weights = np.hstack((weights_pos, weights_neg))

        print(" -- select classifiers --")

        for i in range(num_classifiers):

            # TODO: Complete the Viola Jones algorithm

            sum_temp = np.sum(weights)
            weights = weights / sum_temp
            vjc = VJ_Classifier(scores,
                                self.labels,
                                weights,
                                thresh=0,
                                feat=0,
                                polarity=1)
            vjc.train()
            error = vjc.error
            self.classifiers.append(vjc)

            beta = float(error) / (1. - error)
            for j in range(len(self.integralImages)):
                if self.labels[j] == vjc.predict(scores[j]):
                    weights[j] = weights[j] * beta
                else:
                    weights[j] = weights[j]
            weights = np.asarray(weights)
            self.alphas.append(math.log((1.) / beta))
Esempio n. 5
0
    def train(self, num_classifiers):

        # Use this scores array to train a weak classifier using VJ_Classifier
        # in the for loop below.
        scores = np.zeros((len(self.integralImages), len(self.haarFeatures)))
        print(" -- compute all scores --")
        for i, im in enumerate(self.integralImages):
            scores[i, :] = [hf.evaluate(im) for hf in self.haarFeatures]

        np.save("scores.npy", scores, allow_pickle=True)
        # scores = np.load("scores.npy")

        weights_pos = np.ones(len(self.posImages),
                              dtype='float') * 1.0 / (2 * len(self.posImages))
        weights_neg = np.ones(len(self.negImages),
                              dtype='float') * 1.0 / (2 * len(self.negImages))
        weights = np.hstack((weights_pos, weights_neg))

        print(" -- select classifiers --")
        for i in range(num_classifiers):
            # normalize
            weights = weights / np.sum(weights)

            h_of_x = VJ_Classifier(scores, self.labels, weights)
            h_of_x.train()
            e_t = h_of_x.error
            B_t = e_t / (1 - e_t)
            a_t = np.log(1 / B_t)
            self.classifiers.append(h_of_x)
            self.alphas.append(a_t)

            # update weights
            predictions = [h_of_x.predict(x) for x in scores]
            incorrect_indices = np.where(self.labels != predictions)
            b_array = np.zeros_like(self.labels, dtype=np.float)
            b_array[incorrect_indices] = 1.0
            b_array[b_array == 0] = B_t
            temp_weights = np.multiply(weights, b_array)
            weights = temp_weights
            print("Training: ", str(i))
Esempio n. 6
0
    def train(self, num_classifiers):

        # Use this scores array to train a weak classifier using VJ_Classifier
        # in the for loop below.
        scores = np.zeros((len(self.integralImages), len(self.haarFeatures)))
        print " -- compute all scores --"
        for i, im in enumerate(self.integralImages):
            scores[i, :] = [hf.evaluate(im) for hf in self.haarFeatures]

        weights_pos = np.ones(len(self.posImages), dtype='float') * 1.0 / (
                           2*len(self.posImages))
        weights_neg = np.ones(len(self.negImages), dtype='float') * 1.0 / (
                           2*len(self.negImages))
        weights = np.hstack((weights_pos, weights_neg))

        print " -- select classifiers --"
        for i in range(num_classifiers):

            # normalize weights.
            weights = weights / np.sum(weights)
            # instantiate each classifier
            classifier = VJ_Classifier(scores, self.labels, weights)
            classifier.train()
            error = classifier.error
            # append
            self.classifiers.append(classifier)
            # update weights.
            B = error / (1.0 - error)
            a = np.log(1.0 / B)

            # get predictions
            preds = classifier.predict(np.transpose(scores))
            # e = -1 if same, 1 if not.
            e = [-1 if preds[i] == self.labels[i] else 1 for i in range(len(preds))]

            for wti in range(len(weights)):
                weights[wti] = weights[wti] * np.power(B, 1-e[wti])

            # append the alpha.
            self.alphas.append(a)
Esempio n. 7
0
    def train(self, num_classifiers):

        # Use this scores array to train a weak classifier using VJ_Classifier
        # in the for loop below.
        scores = np.zeros((len(self.integralImages), len(self.haarFeatures)))
        print(" -- compute all scores --")
        for i, im in enumerate(self.integralImages):
            scores[i, :] = [hf.evaluate(im) for hf in self.haarFeatures]

        weights_pos = np.ones(len(self.posImages),
                              dtype='float') * 1.0 / (2 * len(self.posImages))
        weights_neg = np.ones(len(self.negImages),
                              dtype='float') * 1.0 / (2 * len(self.negImages))
        weights = np.hstack((weights_pos, weights_neg))

        print(" -- select classifiers --")
        for i in range(num_classifiers):

            # TODO: Complete the Viola Jones algorithm
            weights = weights / sum(weights)
            scores = [
                hf.evaluate(ii) for ii in self.integralImages
                for hf in self.haarFeatures
            ]
            n_feats = len(self.haarFeatures)
            n_iis = len(self.integralImages)
            X_ = np.reshape(scores, (n_iis, n_feats))
            y_ = self.labels
            vj = VJ_Classifier(X_, y_, weights)
            vj.train()
            preds = vj.predict(X_.T)
            eps = 1 * (preds != self.labels)
            beta = vj.error / (1 - vj.error)
            print(vj.error)
            weights = weights * (beta**(1 - eps))
            alpha = np.log(1 / beta)

            self.classifiers.append(vj)
            self.alphas.append(alpha)
Esempio n. 8
0
    def train(self, num_classifiers):

        # Use this scores array to train a weak classifier using VJ_Classifier
        # in the for loop below.
        scores = np.zeros((len(self.integralImages), len(self.haarFeatures)))
        print " -- compute all scores --"
        for i, im in enumerate(self.integralImages):
            scores[i, :] = [hf.evaluate(im) for hf in self.haarFeatures]

        weights_pos = np.ones(len(self.posImages),
                              dtype='float') * 1.0 / (2 * len(self.posImages))
        weights_neg = np.ones(len(self.negImages),
                              dtype='float') * 1.0 / (2 * len(self.negImages))
        weights = np.hstack((weights_pos, weights_neg))

        print " -- select classifiers --"

        for i in range(num_classifiers):

            weights = weights / np.sum(weights)
            VJ = VJ_Classifier(scores, self.labels, weights)
            VJ.train()
            self.classifiers.append(VJ)

            B = VJ.error / (1.0 - VJ.error)
            alpha = np.log(1.0 / B)

            et = [
                0 if VJ.predict(st) == lt else 1
                for i, (st, lt) in enumerate(zip(scores, self.labels))
            ]
            weights = [w * np.power(B, 1 - e) for (w, e) in zip(weights, et)]

            self.alphas.append(alpha)

        print " -- select classifiers done --"
Esempio n. 9
0
    def train(self, num_classifiers):

        # Use this scores array to train a weak classifier using VJ_Classifier
        # in the for loop below.
        scores = np.zeros((len(self.integralImages), len(self.haarFeatures)))
        print " -- compute all scores --"
        for i, im in enumerate(self.integralImages):
            scores[i, :] = [hf.evaluate(im) for hf in self.haarFeatures]

        weights_pos = np.ones(len(self.posImages), dtype='float') * 1.0 / (
                           2*len(self.posImages))
        weights_neg = np.ones(len(self.negImages), dtype='float') * 1.0 / (
                           2*len(self.negImages))
        weights = np.hstack((weights_pos, weights_neg))

        print " -- select classifiers --"
        for i in range(num_classifiers):

            # TODO: Complete the Viola Jones algorithm
            # 1. Normalize the weights
            weights = weights / np.sum(weights)
            # 2. Instantiate a and train a classifier h_j
            h_j = VJ_Classifier(scores, self.labels, weights)
            h_j.train()
            # 3. Append h_j to the self.classifiers attribute
            self.classifiers.append(h_j)
            # 4. Update the weights
            h_x = np.array([h_j.predict(scores[x, :]) for x in range(scores.shape[0])], np.float32) 
            eps_j = h_j.error   
            beta_j = eps_j / (1 - eps_j)
            e_i = np.float32(np.not_equal(h_x, self.labels)) * 2.0 - 1.0
            weights = np.multiply(weights, np.power(beta_j, 1.0 - e_i))
            # 5. Calculate alpha_j
            alpha_j = - np.log(beta_j)
            # Append it to the self.alphas
            self.alphas.append(alpha_j)