Exemplo n.º 1
0
    def test_create_children(self):
        eg_img1 = example_images.ExampleImages(self.SNR, 2, 0)
        training_eg = eg_img1.load_examples('Training')
        pos_training_eg = training_eg[0:2]
        treenode1 = tree_node.TreeNode(pos_training_eg, depth=0, maxdepth=2)
        treenode1.create_children()
        #should be perfectly classified, since only positive examples are given
        nose.tools.assert_equal(treenode1.isaleaf, True)

        #both positive and negative examples
        eg_img2 = example_images.ExampleImages(self.SNR, 2, 2)
        training_eg = eg_img2.load_examples('Training')
        treenode2 = tree_node.TreeNode(training_eg, depth=0, maxdepth=2)
        treenode2.create_children()
        left_examples = treenode2.leftnode.training_set
        right_examples = treenode2.rightnode.training_set

        for example in left_examples:
            nose.tools.assert_equal(example.label, False)
        for example in right_examples:
            nose.tools.assert_equal(example.label, True)

        #we had given 2 postive and 2 negative examples
        nose.tools.assert_equal(len(left_examples), 2)
        nose.tools.assert_equal(len(right_examples), 2)
        #since maxdepth is 2, the left and righnode must be leaves
        nose.tools.assert_equal(treenode2.isaleaf, False)
        nose.tools.assert_equal(treenode2.leftnode.isaleaf, True)
        nose.tools.assert_equal(treenode2.rightnode.isaleaf, True)

        return
Exemplo n.º 2
0
    def test_compute_posterior(self):
        eg_img1 = example_images.ExampleImages(self.SNR, 3, 3)
        training_eg2 = eg_img1.load_examples('Training')

        boosting_tree1 = boosting_tree.BoostingTree(training_eg2, 2)
        boosting_tree1.train_tree()
        eg_img2 = example_images.ExampleImages(self.SNR, 3, 3)
        test_examples = eg_img2.load_examples('Testing')
        test_pos_examples = test_examples[0:3]
        test_neg_examples = test_examples[3:6]

        #the positives should be very close to 1.,negatives should be
        #very close to 0.
        #I would be concerned if they are not, given the high SNR value
        #keep the print statements to make sure they are

        print "Posterior values for positive examples:"

        for example in test_pos_examples:
            prob_val = boosting_tree1.compute_posterior(example)
            print prob_val
            nose.tools.assert_true(prob_val > 0.5)

        print "Posterior values for negative examples:"

        for example in test_neg_examples:
            prob_val = boosting_tree1.compute_posterior(example)
            print prob_val
            nose.tools.assert_true(prob_val < 0.5)

        return
Exemplo n.º 3
0
 def setup(self):
     #pass
     #keep SNR high
     self.SNR = 10
     self.num_pos_eg = 3
     self.num_neg_eg = 3
     self.eg_img = example_images.ExampleImages(self.SNR, 3, 3)
     self.training_eg = self.eg_img.load_examples('Training')
Exemplo n.º 4
0
    def test_highnoise_example(self):
        SNRval = 0.5
        eg_img1 = example_images.ExampleImages(0.5, 60, 60)
        training_eg2 = eg_img1.get_new_examples()

        boosting_tree1 = boosting_tree.BoostingTree(training_eg2, 5)
        #check printed values
        boosting_tree1.train_tree()
        return
Exemplo n.º 5
0
    def setup(self):
        #keep SNR high
        self.SNR = 10
        self.eg_img = example_images.ExampleImages(self.SNR, 2, 2)
        training_examples = self.eg_img.load_examples('Training')
        self.eg_img.save_examples('Training')

        #default treenode
        self.treenode = tree_node.TreeNode(training_examples, 0)
Exemplo n.º 6
0
    def test_calc_empirical_dist(self):
        eg_img1 = example_images.ExampleImages(self.SNR, 1, 1)
        training_examples = eg_img1.load_examples('Training')
        treenode1 = tree_node.TreeNode([], 1)
        treenode1.training_set = training_examples
        treenode1.calc_empirical_dist()

        for i in range(2):
            nose.tools.assert_almost_equal(treenode1.empirical_dist[i], 0.5)
        return
Exemplo n.º 7
0
    def get_training_examples(self):

        no_pos_eg = 10
        no_neg_eg = 10
        eg_imgs = example_images.ExampleImages(self.SNR, no_pos_eg, no_neg_eg)
        #should this be self?
        training_examples = eg_imgs.load_examples('Training')

        self.trainer.training_examples = training_examples

        return
Exemplo n.º 8
0
    def test_check_weights(self):
        eg_img1 = example_images.ExampleImages(self.SNR, 2, 0)
        training_eg = eg_img1.load_examples('Training')
        training_eg[0].weight = 0.5
        training_eg[1].weight = 0.5

        treenode1 = tree_node.TreeNode(training_eg, 1)
        nose.tools.assert_equal(treenode1.check_weights(), True)

        treenode1 = tree_node.TreeNode(training_eg, 1)
        treenode1.training_set[1].weight = 1
        nose.tools.assert_equal(treenode1.check_weights(), False)
        return
Exemplo n.º 9
0
    def test_get_precision_recall(self):

        eg_img1 = example_images.ExampleImages(10, 1, 1)
        example_list = eg_img1.load_examples('Training')
        self.conf_mat.add_to_matrix(example_list[0], False)
        #Wrong value
        self.conf_mat.add_to_matrix(example_list[1], True)
        #wrong value
        [precision, recall] = self.conf_mat.get_precision_recall()
        nose.tools.assert_equal(precision, 0)
        nose.tools.assert_equal(recall, 0)
        [tpr, fpr] = self.conf_mat.get_tpr_fpr()
        nose.tools.assert_equal(tpr, 0)
        nose.tools.assert_equal(fpr, 1)
Exemplo n.º 10
0
 def test_add_to_matrix(self):
     eg_img1 = example_images.ExampleImages(10, 1, 1)
     example_list = eg_img1.load_examples('Training')
     pos_example = example_list[0]
     self.conf_mat.add_to_matrix(pos_example, False)
     nose.tools.assert_equal(self.conf_mat._false_negative, 1)
     self.conf_mat.add_to_matrix(pos_example, True)
     nose.tools.assert_equal(self.conf_mat._true_positive, 1)
     neg_example = example_list[1]
     self.conf_mat.add_to_matrix(neg_example, True)
     nose.tools.assert_equal(self.conf_mat._false_positive, 1)
     self.conf_mat.add_to_matrix(neg_example, False)
     nose.tools.assert_equal(self.conf_mat._true_negative, 1)
     return
Exemplo n.º 11
0
    def test_trainer_messy_example(self):
        mytrainer = trainer.Trainer(10)
        eg_imgs = example_images.ExampleImages(0.5, 30, 30)
        #should this be self?
        training_examples = eg_imgs.load_examples('Training')
        mytrainer.training_examples = training_examples
        adaboost_rule = mytrainer.run_adaboost()

        print "Hard example. should have more than one rounds"

        for i in range(len(adaboost_rule)):
            [alpha, feature] = adaboost_rule[i]
            print "threshold: " + str(feature.threshold)
            print "error: " + str(feature.error)
            print "toggle: " + str(feature.toggle)

        #test the strong detector
        testing_eg = eg_imgs.load_examples('Testing')
        detector = strong_detector.Detector(adaboost_rule)

        print "Posterior Probabilities for examples at low SNR: "
        poslist = []
        neglist = []
        for example in testing_eg:
            decision = detector.get_decision_stump(example)
            q_pos = 1. / (1. + np.exp(-2 * decision))
            if example.label == True:
                poslist.append(q_pos)
                #print "True: "+str(q_pos);
            else:
                neglist.append(q_pos)
                #print "False:"+str(q_pos);
        plt.figure()
        plt.hist(np.array(poslist),
                 bins=10,
                 range=(0, 1),
                 color='r',
                 label='Positive')

        plt.hist(np.array(neglist),
                 bins=10,
                 range=(0, 1),
                 color='b',
                 label='Negative')
        plt.xlabel("Posterior Probability")
        plt.ylabel("Number of Examples")
        plt.legend()
        plt.title('Low SNR')
        plt.show()
        return
Exemplo n.º 12
0
    def test_normalize_weight(self):
        treenode1 = tree_node.TreeNode([], 1)
        eg_img1 = example_images.ExampleImages(self.SNR, 2, 0)
        pos_examples = eg_img1.load_examples('Training')
        pos_examples[0].weight = 1.
        pos_examples[1].weight = 1.
        treenode1.left_training_set = pos_examples
        treenode1.normalize_weight('left')
        for example in treenode1.left_training_set:
            nose.tools.assert_almost_equal(example.weight, 0.50, places=2)
        treenode1.right_training_set = pos_examples
        pos_examples[0].weight = 1.
        pos_examples[1].weight = 1.
        treenode1.normalize_weight('right')
        for example in treenode1.right_training_set:
            nose.tools.assert_almost_equal(example.weight, 0.50, places=2)

        #nose.tools.raises(ValueError,treenode1.normalize_weight,'other');
        return
Exemplo n.º 13
0
    def test_assign_child_training_set(self):
        treenode1 = tree_node.TreeNode([], 1)
        eg_img1 = example_images.ExampleImages(self.SNR, 1, 0)
        training_examples = eg_img1.load_examples('Training')
        example = training_examples[0]
        treenode1.assign_child_training_set(example, .9)
        nose.tools.assert_equal(treenode1.right_training_set, [example])
        nose.tools.assert_equal(treenode1.left_training_set, None)

        treenode1.right_training_set = None
        treenode1.assign_child_training_set(example, .1)
        nose.tools.assert_equal(treenode1.left_training_set, [example])
        nose.tools.assert_equal(treenode1.right_training_set, None)

        #When close to 0.5, does example go to both child sets?
        #in right node, example is a deepcopy, so not equal
        treenode1.left_training_set = None
        treenode1.assign_child_training_set(example, .51)
        nose.tools.assert_equal(treenode1.left_training_set, [example])
        nose.tools.assert_not_equal(treenode1.right_training_set, [example])
        nose.tools.assert_equal(len(treenode1.right_training_set), 1)
        return
Exemplo n.º 14
0
 def setup(self):
     self.SNR = 2
     self.num_pos_eg = 2
     self.num_neg_eg = 3
     self.eg_img = example_images.ExampleImages(self.SNR, self.num_pos_eg,
                                                self.num_neg_eg)