Exemplo n.º 1
0
def test_adj_motion_classifier(classifier_dict, adjective_name,
                               test_feature_objs, feature_list, scaler_dict,
                               classifier_text_report):
    '''
    Pass in the trained adjective classifiers that are motion specific and return the results
    of the motion adjectives compared to the test set
    '''

    # For each adjective
    object_run_probability = dict()
    object_run_scores = dict()
    object_run_prediction = dict()

    # for each motion
    for motion in test_feature_objs:
        results = []
        truth_vector = []
        motion_test_obj_list = test_feature_objs[motion]
        object_run_probability[motion] = []

        for test_obj in motion_test_obj_list:
            # import pdb; pdb.set_trace()
            if test_obj.labels == None:
                prediction, probability = utilities.compute_adjective_probability_score(
                    classifier_dict, test_obj, feature_list, adjective_name,
                    scaler_dict)
            else:
                prediction, probability, truth = utilities.compute_adjective_probability_score(
                    classifier_dict, test_obj, feature_list, adjective_name,
                    scaler_dict)
                results.append(prediction)
                truth_vector.append(truth)
            # Store the probabilities
            object_run_probability[motion].append(probability)
            object_run_prediction[motion] = prediction

        print "Motion is: %s" % motion
        if len(results) > 0:
            #print "f1 score is %s" %classification_report(truth_vector, results)
            classifier_text_report.write("Adjective: " + adjective_name +
                                         "\nMotion: " + motion + "\n")
            classifier_text_report.write(
                classification_report(truth_vector, results) + "\n")

            # Store the scores
            object_run_scores[motion] = (recall_score(truth_vector, results))

    # import pdb; pdb.set_trace()
    # Return the % of motions in a n x 5 vector (5 motions)
    probability_feature_vector = []
    for val in xrange(len(object_run_probability[motion])):

        one_run_vector = []
        for motion_name in object_run_probability:
            one_run_vector.append(object_run_probability[motion_name][val])

        probability_feature_vector.append(one_run_vector)

    return (np.array(probability_feature_vector), object_run_scores,
            object_run_prediction, truth_vector)
def test_adj_motion_classifier(classifier_dict, adjective_name, test_feature_objs, feature_list, scaler_dict, classifier_text_report):
    '''
    Pass in the trained adjective classifiers that are motion specific and return the results
    of the motion adjectives compared to the test set
    '''

    # For each adjective
    object_run_probability = dict()
    object_run_scores = dict()
    object_run_prediction = dict()

    

    # for each motion
    for motion in test_feature_objs:
        results = []
        truth_vector = []
        motion_test_obj_list = test_feature_objs[motion]
        object_run_probability[motion] = []

        for test_obj in motion_test_obj_list:
            # import pdb; pdb.set_trace()
            if test_obj.labels == None:
                prediction, probability = utilities.compute_adjective_probability_score(classifier_dict, test_obj, feature_list, adjective_name, scaler_dict)
            else:
                prediction, probability, truth = utilities.compute_adjective_probability_score(classifier_dict, test_obj, feature_list, adjective_name, scaler_dict)
                results.append(prediction)  
                truth_vector.append(truth)
            # Store the probabilities
            object_run_probability[motion].append(probability)
            object_run_prediction[motion] = prediction

        print "Motion is: %s" % motion
        if len(results) > 0: 
            #print "f1 score is %s" %classification_report(truth_vector, results)
            classifier_text_report.write("Adjective: " + adjective_name+ "\nMotion: " + motion+"\n") 
            classifier_text_report.write(classification_report(truth_vector, results)+ "\n")

            # Store the scores
            object_run_scores[motion] = (recall_score(truth_vector, results))
    
    # import pdb; pdb.set_trace()
    # Return the % of motions in a n x 5 vector (5 motions)
    probability_feature_vector = [] 
    for val in xrange(len(object_run_probability[motion])):
       
        one_run_vector = []
        for motion_name in object_run_probability:
            one_run_vector.append(object_run_probability[motion_name][val])

        probability_feature_vector.append(one_run_vector)

    return (np.array(probability_feature_vector), object_run_scores, object_run_prediction, truth_vector)
    def compute_probability_vector(self, bolt_obj):
        if bolt_obj.state  == bolt_obj.TAP:
            # Store results as they come in
            self.adjective_vectors = dict() 
            self.all_motion_results = dict()
        
        # Store dictionary of strings
        self.state_string = {bolt_obj.DISABLED:'disabled',
                    bolt_obj.THERMAL_HOLD:'thermal_hold',
                    bolt_obj.SLIDE:'slide',
                    bolt_obj.SQUEEZE:'squeeze',
                    bolt_obj.TAP:'tap',
                    bolt_obj.DONE:'done',
                    bolt_obj.SLIDE_FAST:'slide_fast',
                    bolt_obj.CENTER_GRIPPER:'center_gripper'
                    }   
       
        
        current_motion = self.state_string[bolt_obj.state] 
       
        # Create feature vector 
        self.bolt_object = bolt_obj 
        utilities.normalize_data(self.bolt_object)
        self.bolt_feature_object = extract_features.extract_features(self.bolt_object, self.pca_model[current_motion]) 

        # Create a dictionary to store the results in
        for adj in self.all_classifiers:
            results, prob = utilities.compute_adjective_probability_score(self.all_classifiers[adj], self.bolt_feature_object, self.feature_list, adj, self.scaler_dict)
            
            # Store off adjective probabilities for ensemble 
            if adj in self.adjective_vectors:
                pass 
            else:
                self.adjective_vectors[adj] = list()
           
            self.adjective_vectors[adj].append(prob)
           
            # Store classifier score based on best motion
            best_motion = self.best_motion_dict[adj][1]
            if current_motion  == best_motion:
                rospy.loginfo("Best Motion is: %s" % best_motion)
                self.all_motion_results[adj] = results
        
        print len(self.adjective_vectors[adj])
        if len(self.adjective_vectors[adj]) == 5:
            ensembled_results = dict() 
            print self.adjective_vectors 
            #for adj in self.adjective_vectors: 
            #    ensembled_results[adj] = self.ensemble_classifiers[adj].predict(self.adjective_vectors[adj])

            # Store off the adjectives that returned true
            adjectives_found = []
            for adj in self.all_motion_results:
                if self.all_motion_results[adj] == 1:
                    adjectives_found.append(adj)

            print "Results from max classification"
            print self.all_motion_results
            print str(adjectives_found) 
            self.adjectives_pub.publish(str(adjectives_found))
Exemplo n.º 4
0
    def compute_probability_vector(self, bolt_obj):
        
        if bolt_obj.state  == bolt_obj.TAP:
            # Store results as they come in
            self.adjective_vectors = dict() 
            self.all_motion_results = dict()
        
        # Store dictionary of strings
        self.state_string = {bolt_obj.DISABLED:'disabled',
                    bolt_obj.THERMAL_HOLD:'thermal_hold',
                    bolt_obj.SLIDE:'slide',
                    bolt_obj.SQUEEZE:'squeeze',
                    bolt_obj.TAP:'tap',
                    bolt_obj.DONE:'done',
                    bolt_obj.SLIDE_FAST:'slide_fast',
                    bolt_obj.CENTER_GRIPPER:'center_gripper'
                    }   
       
        # Get the current motion 
        current_motion = self.state_string[bolt_obj.state] 
       
        # Build the feature vector
        self.bolt_object = bolt_obj 
        utilities.normalize_data(self.bolt_object)
       
        if self.bolt_object.state == bolt_obj.DISABLED: 
            return
        else:
            self.bolt_feature_object = extract_features.extract_features(self.bolt_object, self.pca_model[current_motion]) 

        # Create a dictionary to store the results in
        for adj in self.all_classifiers:
            results, prob = utilities.compute_adjective_probability_score(self.all_classifiers[adj], self.bolt_feature_object, self.feature_list, adj, self.scaler_dict)
            
            # Store off adjective probabilities for ensemble 
            if adj in self.adjective_vectors:
                pass 
            else:
                self.adjective_vectors[adj] = list()
           
            self.adjective_vectors[adj].append(prob)
           
            # Store classifier score based on best motion
            best_motion = self.best_motion_dict[adj][1]
            if current_motion  == best_motion:
                rospy.loginfo("Best Motion is: %s"% best_motion)
                self.all_motion_results[adj] = results
        
        print len(self.adjective_vectors[adj])
        if len(self.adjective_vectors[adj]) == 5:
            ensembled_results = dict() 
            
            #print self.adjective_vectors 
            for adj in self.ensemble_classifiers: 
                ensembled_results[adj] = self.ensemble_classifiers[adj].predict(self.adjective_vectors[adj])[0]

            # Store off the adjectives that returned true
            adjectives_found = []
            for adj in self.all_motion_results:
                if self.all_motion_results[adj] == 1:
                    adjectives_found.append(adj)

            # Store off the adjectives that returns true for ensemble
            adjectives_ensemble = []
            for adj in ensembled_results:
                if ensembled_results[adj] == 1:
                    adjectives_ensemble.append(adj)

            publish_string = AdjList()
            publish_string = adjective_found

            print "Results from max classification"
            print self.all_motion_results
            print str(adjectives_found) 
            self.adjectives_pub.publish(str(adjectives_found))

            print "Results from ensemble"
            print ensembled_results 
            print adjectives_ensemble