예제 #1
0
def main():
    if len(sys.argv) == 4:
        dynamic_path, static_path, out_path = sys.argv[1:]
        begin_train(dynamic_path, static_path, out_path)

    if len(sys.argv) == 3:
        base, n_jobs = sys.argv[1:]
        n_jobs = int(n_jobs)
        static_path = os.path.join(os.path.join(base, "static"),
                                   "adjective_phase_set")
        dynamic_path = os.path.join(os.path.join(base, "dynamic"),
                                    "adjective_phase_set")

        static_features = load_adjective_phase(static_path)
        dynamic_features = load_adjective_phase(dynamic_path)

        p = Parallel(n_jobs=n_jobs, verbose=0)
        p(
            delayed(begin_train_phase)(base, adjective, phase, static_features,
                                       dynamic_features)
            for adjective, phase in itertools.product(adjectives, phases))

    else:
        print "Usage:"
        print "%s path adjective phase n_jobs" % sys.argv[0]
        print "%s path adjective n_jobs" % sys.argv[0]
        print "%s path n_jobs" % sys.argv[0]
    def __init__(self):
        rospy.loginfo("Loading the mkl adjective classifiers")

        # Create paths to files
        DIR = roslib.packages.get_pkg_dir("hadjective_mkl_classifier",
                                          required=True) + "/classifiers/"
        all_classifiers_file = os.path.join(DIR, "all_trained_classifiers.pkl")
        hmm_chains_file = os.path.join(DIR, "loaded_chains.pkl")
        dynamic_train_file = os.path.join(DIR, "adjective_phase_set_dynamic")
        static_train_file = os.path.join(DIR, "adjective_phase_set_static")

        # Load pickle files containing classifiers and chains
        self.all_classifiers = cPickle.load(open(all_classifiers_file))
        self.hmm_chains = cPickle.load(open(hmm_chains_file))

        # Load all training features for later use in kernel building
        self.training_dynamic = utilities.load_adjective_phase(
            dynamic_train_file)
        self.training_static = utilities.load_adjective_phase(
            static_train_file)

        # Setup subscriber and publisher nodes
        rospy.Subscriber("new_hadjective_motion_pickle", String, self.callback)
        self.adjectives_pub = rospy.Publisher("/hadjective_speech", AdjList)

        rospy.loginfo("All MKL classifiers loaded")
예제 #3
0
def compute_probability_density_kernel(base_directory, classifiers, adjective,
                                       motion):
    """
    Compute the probability density of positive and 
    negative adjective per motion
    """

    # Load static and dynamic
    dynamic_features = utilities.load_adjective_phase(
        '/media/data_storage/vchu/all_classifiers/icra2014/dynamic/adjective_phase_set/'
    )
    static_features = utilities.load_adjective_phase(
        '/media/data_storage/vchu/all_classifiers/icra2014/static/adjective_phase_set/'
    )

    dynamic_train = dynamic_features[adjective][motion]['train']['features']
    dynamic_train_scaler = preprocessing.StandardScaler().fit(dynamic_train)
    dynamic_train_scaled_X = dynamic_train_scaler.transform(dynamic_train)
    dynamic_train_kernel = linear_kernel(dynamic_train_scaled_X, -2)
    #dynamic_train_kernel = standardize(dynamic_train_kernel)

    #Static Train
    static_train = static_features[adjective][motion]['train']['features']
    static_train_scaler = preprocessing.StandardScaler().fit(static_train)
    static_train_scaled_X = static_train_scaler.transform(static_train)
    static_train_kernel = linear_kernel(static_train_scaled_X, -2)

    # Load alphas
    classifier = []
    for mkl in classifiers:
        if (mkl['adjective'] == adjective) and (mkl['phase'] == motion):
            classifier = mkl

    alpha = classifier['alpha']

    # Pull out features for adjective/motion
    #all_features = load_adjective_phase(base_directory)
    #adj_motion_feat = all_features[adjective][motion]['train']
    #features = adj_motion_feat['features']
    #selected_components = compute_kernel(features)

    train_X = (alpha) * static_train_kernel + (1 -
                                               alpha) * dynamic_train_kernel
    labels = dynamic_features[adjective][motion]['train']['labels']

    selected_components = compute_kernel(train_X)

    # Now split them according to positive and negative
    #labels = adj_motion_feat['labels']
    positive_components = selected_components[labels == 1]
    negative_components = selected_components[labels == 0]

    return (positive_components, negative_components)
def compute_probability_density_kernel(base_directory, classifiers, adjective, motion):
    """
    Compute the probability density of positive and 
    negative adjective per motion
    """
  
    # Load static and dynamic
    dynamic_features = utilities.load_adjective_phase('/media/data_storage/vchu/all_classifiers/icra2014/dynamic/adjective_phase_set/')
    static_features = utilities.load_adjective_phase('/media/data_storage/vchu/all_classifiers/icra2014/static/adjective_phase_set/')

    dynamic_train = dynamic_features[adjective][motion]['train']['features']
    dynamic_train_scaler = preprocessing.StandardScaler().fit(dynamic_train)
    dynamic_train_scaled_X = dynamic_train_scaler.transform(dynamic_train)
    dynamic_train_kernel = linear_kernel(dynamic_train_scaled_X, -2)
    #dynamic_train_kernel = standardize(dynamic_train_kernel)

    #Static Train
    static_train = static_features[adjective][motion]['train']['features']
    static_train_scaler = preprocessing.StandardScaler().fit(static_train)
    static_train_scaled_X = static_train_scaler.transform(static_train)
    static_train_kernel = linear_kernel(static_train_scaled_X, -2)

    # Load alphas
    classifier = []
    for mkl in classifiers:
        if (mkl['adjective']== adjective) and (mkl['phase'] == motion):
            classifier = mkl

    alpha = classifier['alpha']

    # Pull out features for adjective/motion
    #all_features = load_adjective_phase(base_directory)
    #adj_motion_feat = all_features[adjective][motion]['train'] 
    #features = adj_motion_feat['features']
    #selected_components = compute_kernel(features)
   
    train_X = (alpha)*static_train_kernel + (1-alpha)*dynamic_train_kernel
    labels = dynamic_features[adjective][motion]['train']['labels']
   
    selected_components = compute_kernel(train_X)

    # Now split them according to positive and negative
    #labels = adj_motion_feat['labels'] 
    positive_components = selected_components[labels == 1]
    negative_components = selected_components[labels == 0]

    return (positive_components, negative_components)
    def __init__(self):
        rospy.loginfo("Loading the mkl adjective classifiers")

        # Create paths to files
        DIR = roslib.packages.get_pkg_dir("hadjective_mkl_classifier", required=True) + "/classifiers/" 
        all_classifiers_file = os.path.join(DIR, "all_trained_classifiers.pkl")
        hmm_chains_file = os.path.join(DIR, "loaded_chains.pkl")
        dynamic_train_file = os.path.join(DIR,"adjective_phase_set_dynamic")
        static_train_file = os.path.join(DIR, "adjective_phase_set_static")

        # Load pickle files containing classifiers and chains
        self.all_classifiers = cPickle.load(open(all_classifiers_file))
        self.hmm_chains = cPickle.load(open(hmm_chains_file))

        # Load all training features for later use in kernel building
        self.training_dynamic = utilities.load_adjective_phase(dynamic_train_file)
        self.training_static = utilities.load_adjective_phase(static_train_file)

        # Setup subscriber and publisher nodes
        rospy.Subscriber("new_hadjective_motion_pickle", String, self.callback)
        self.adjectives_pub = rospy.Publisher("/hadjective_speech", AdjList)

        rospy.loginfo("All MKL classifiers loaded")
def main():
    if len(sys.argv) == 4:
        dynamic_path, static_path, out_path = sys.argv[1:]
        begin_train(dynamic_path, static_path, out_path)
   
    if len(sys.argv) == 3:
        base, n_jobs = sys.argv[1:]
        n_jobs = int(n_jobs)      
        static_path = os.path.join(os.path.join(base, "static"),"adjective_phase_set")
        dynamic_path = os.path.join(os.path.join(base, "dynamic"), "adjective_phase_set")
     
        static_features = load_adjective_phase(static_path)
        dynamic_features = load_adjective_phase(dynamic_path) 

        p = Parallel(n_jobs=n_jobs,verbose=0)
        p(delayed(begin_train_phase)(base, adjective, phase, static_features,dynamic_features)
            for adjective, phase in itertools.product(adjectives,
                                                      phases))

    else:
        print "Usage:"
        print "%s path adjective phase n_jobs" % sys.argv[0]
        print "%s path adjective n_jobs" % sys.argv[0]
        print "%s path n_jobs" % sys.argv[0]
def test_adjective(classifier, adjective_report, store_values):
       
    adjective = classifier['adjective']
    phase = classifier['phase']
  
    dynamic_features = utilities.load_adjective_phase('/media/data_storage/vchu/all_classifiers/icra2014/dynamic/adjective_phase_set/')
    static_features = utilities.load_adjective_phase('/media/data_storage/vchu/all_classifiers/icra2014/static/adjective_phase_set/')

    true_positives = 0.0
    true_negatives = 0.0
    false_positives = 0.0
    false_negatives = 0.0

    false_positive_list = []
    false_negative_list = []
    true_positive_list = []
    true_negative_list = []

    print '\n \nTesting Adjective: %s and phase %s' % (adjective, phase)

    #Pull out test features/labels
   
    dynamic_train = dynamic_features[adjective][phase]['train']['features']
    dynamic_train_scaler = preprocessing.StandardScaler().fit(dynamic_train)
    dynamic_train_scaled_X = dynamic_train_scaler.transform(dynamic_train)
    dynamic_train_kernel = linear_kernel(dynamic_train_scaled_X, -2)
    #dynamic_train_kernel = standardize(dynamic_train_kernel)
    
    #Static Train
    static_train = static_features[adjective][phase]['train']['features']
    static_train_scaler = preprocessing.StandardScaler().fit(static_train)
    static_train_scaled_X = static_train_scaler.transform(static_train)
    static_train_kernel = linear_kernel(static_train_scaled_X, -2)

    dynamic_test = dynamic_features[adjective][phase]['test']['features'] 
    dynamic_test_scaled_X = classifier['dynamic_scaler'].transform(dynamic_test)
    dynamic_kernel = linear_kernel_test(dynamic_test_scaled_X, dynamic_train_scaled_X, -2)
    #dynamic_kernel = (dynamic_kernel - classifier['dynamic_kernel_mean']) / classifier['dynamic_kernel_std']
    
    static_test = static_features[adjective][phase]['test']['features'] 
    static_test_scaled_X = classifier['static_scaler'].transform(static_test)
    static_kernel = linear_kernel_test(static_test_scaled_X, static_train_scaled_X, -2)   

    alpha = classifier['alpha']

    test_X = (alpha)*static_kernel + (1-alpha)*dynamic_kernel
    test_Y = dynamic_features[adjective][phase]['test']['labels']
    
    object_ids = dynamic_features[adjective][phase]['test']['object_ids']
    object_names = dynamic_features[adjective][phase]['test']['object_names']

    # Pull out the classifier and merge features
    clf = classifier['classifier']

    # Predict the labels!
    output = clf.predict(test_X)
    
    # Determine if the true label and classifier prediction match
    for val in xrange(len(test_Y)):
        true_label = test_Y[val]
        predict_label = output[val]

        if true_label == 1:
            if predict_label == 1:
                true_positives += 1.0
                true_positive_list.append(object_names[val])
            else:
                false_negatives += 1.0
                false_negative_list.append(object_names[val])
        else: # label is 0
            if predict_label == 1:
                false_positives += 1.0
                false_positive_list.append(object_names[val])
            else:
                true_negatives += 1.0
                true_negative_list.append(object_names[val])
    
    # Compute statistics for the adjective
    try: 
        precision = true_positives / (true_positives + false_positives)
        recall = true_positives / (true_positives + false_negatives)
    
    except ZeroDivisionError: # The case when none are found
        precision = 0
        recall = 0
    try:
        f1 = 2.0 * precision*recall / (precision + recall)
    except ZeroDivisionError:
        f1 = 0
    
    store_values[adjective+'_'+phase] = (f1,precision, recall)
    print "%d False Positive Objects are: %s \n" % (false_positives, sorted(false_positive_list))
    print "%d False Negative Objects are: %s \n" % (false_negatives, sorted(false_negative_list))
    print "%d True Positive Objects are: %s\n" % (true_positives, sorted(true_positive_list))
    print "%d True Negative Objects are: %s\n" % (true_negatives, sorted(true_negative_list))

    
    return (precision, recall, f1)
def begin_train(dynamic_path, static_path, out_path):

    dynamic_features = utilities.load_adjective_phase(dynamic_path)
    static_features = utilities.load_adjective_phase(static_path)
    adjective_list = utilities.adjectives
    for adjective in adjective_list:
        # File name 
        dataset_file_name = "_".join(("trained", adjective))+".pkl"
        newpath = os.path.join(out_path, "trained_adjectives")
        path_name = os.path.join(newpath, dataset_file_name)
        if os.path.exists(path_name):
            print "File %s already exists, skipping it." % path_name
            continue

        overall_best_score = 0.0
        dataset = defaultdict()
        dataset['classifier'] = None
        dataset['training_score'] = overall_best_score

        dynamic_train = utilities.get_all_train_test_features(adjective, dynamic_features) 
        y_labels = dynamic_train[1]
        object_ids = dynamic_train[2]
        dynamic_scaler = preprocessing.StandardScaler().fit(dynamic_train[0])
        dynamic_X = dynamic_scaler.transform(dynamic_train[0])
        dynamic_kernel = linear_kernel(dynamic_X, -2)
        #dynamic_kernel = standardize(dynamic_kernel)

        static_train = utilities.get_all_train_test_features(adjective, static_features) 
        static_scaler = preprocessing.StandardScaler().fit(static_train[0])
        static_X = static_scaler.transform(static_train[0])
        static_kernel = linear_kernel(static_X, -2)
        #static_kernel = standardize(static_kernel)

        alpha_range = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
        #alpha_range = [0.5]
        for alpha in alpha_range:
            print "Beginning %s, alpha %1.1f at %s" % (adjective,alpha, time.asctime())
            combined_kernel = (alpha)*static_kernel + (1-alpha)*dynamic_kernel;
            trained_clf, best_score = gram_grid_search(combined_kernel, labels=y_labels, object_ids=object_ids, refined_range = adjective)
            print "F1: %1.5f at %s" % (best_score, time.asctime())
            if best_score > overall_best_score:
                overall_best_score = best_score
                dataset['classifier'] = trained_clf
                dataset['training_score'] = best_score
                dataset['alpha'] = alpha

        dataset['dynamic_features'] = dynamic_features[adjective]
        dataset['static_features'] = static_features[adjective]
        dataset['adjective'] = adjective
        dataset['dynamic_scaler'] = dynamic_scaler
        dataset['dynamic_train_scaled'] = dynamic_X;
        dataset['dynamic_kernel_mean'] = np.mean(dynamic_kernel)
        dataset['dynamic_kernel_std'] = np.std(dynamic_kernel)
        dataset['static_scaler'] = static_scaler
        dataset['static_train_scaled'] = static_X;
        dataset['static_kernel_mean'] = np.mean(static_kernel)
        dataset['static_kernel_std'] = np.std(static_kernel)
   
        print "Saving trained_classifier" 

        # Save the results in the folder
        with open(path_name, "w") as f:
            print "Saving file: ", path_name
            cPickle.dump(dataset, f, protocol=cPickle.HIGHEST_PROTOCOL)
예제 #9
0
def test_adjective(classifier, adjective_report, store_values):

    adjective = classifier['adjective']
    phase = classifier['phase']

    dynamic_features = utilities.load_adjective_phase(
        '/media/data_storage/vchu/all_classifiers/icra2014/dynamic/adjective_phase_set/'
    )
    static_features = utilities.load_adjective_phase(
        '/media/data_storage/vchu/all_classifiers/icra2014/static/adjective_phase_set/'
    )

    true_positives = 0.0
    true_negatives = 0.0
    false_positives = 0.0
    false_negatives = 0.0

    false_positive_list = []
    false_negative_list = []
    true_positive_list = []
    true_negative_list = []

    print '\n \nTesting Adjective: %s and phase %s' % (adjective, phase)

    #Pull out test features/labels

    dynamic_train = dynamic_features[adjective][phase]['train']['features']
    dynamic_train_scaler = preprocessing.StandardScaler().fit(dynamic_train)
    dynamic_train_scaled_X = dynamic_train_scaler.transform(dynamic_train)
    dynamic_train_kernel = linear_kernel(dynamic_train_scaled_X, -2)
    #dynamic_train_kernel = standardize(dynamic_train_kernel)

    #Static Train
    static_train = static_features[adjective][phase]['train']['features']
    static_train_scaler = preprocessing.StandardScaler().fit(static_train)
    static_train_scaled_X = static_train_scaler.transform(static_train)
    static_train_kernel = linear_kernel(static_train_scaled_X, -2)

    dynamic_test = dynamic_features[adjective][phase]['test']['features']
    dynamic_test_scaled_X = classifier['dynamic_scaler'].transform(
        dynamic_test)
    dynamic_kernel = linear_kernel_test(dynamic_test_scaled_X,
                                        dynamic_train_scaled_X, -2)
    #dynamic_kernel = (dynamic_kernel - classifier['dynamic_kernel_mean']) / classifier['dynamic_kernel_std']

    static_test = static_features[adjective][phase]['test']['features']
    static_test_scaled_X = classifier['static_scaler'].transform(static_test)
    static_kernel = linear_kernel_test(static_test_scaled_X,
                                       static_train_scaled_X, -2)

    alpha = classifier['alpha']

    test_X = (alpha) * static_kernel + (1 - alpha) * dynamic_kernel
    test_Y = dynamic_features[adjective][phase]['test']['labels']

    object_ids = dynamic_features[adjective][phase]['test']['object_ids']
    object_names = dynamic_features[adjective][phase]['test']['object_names']

    # Pull out the classifier and merge features
    clf = classifier['classifier']

    # Predict the labels!
    output = clf.predict(test_X)

    # Determine if the true label and classifier prediction match
    for val in xrange(len(test_Y)):
        true_label = test_Y[val]
        predict_label = output[val]

        if true_label == 1:
            if predict_label == 1:
                true_positives += 1.0
                true_positive_list.append(object_names[val])
            else:
                false_negatives += 1.0
                false_negative_list.append(object_names[val])
        else:  # label is 0
            if predict_label == 1:
                false_positives += 1.0
                false_positive_list.append(object_names[val])
            else:
                true_negatives += 1.0
                true_negative_list.append(object_names[val])

    # Compute statistics for the adjective
    try:
        precision = true_positives / (true_positives + false_positives)
        recall = true_positives / (true_positives + false_negatives)

    except ZeroDivisionError:  # The case when none are found
        precision = 0
        recall = 0
    try:
        f1 = 2.0 * precision * recall / (precision + recall)
    except ZeroDivisionError:
        f1 = 0

    store_values[adjective + '_' + phase] = (f1, precision, recall)
    print "%d False Positive Objects are: %s \n" % (
        false_positives, sorted(false_positive_list))
    print "%d False Negative Objects are: %s \n" % (
        false_negatives, sorted(false_negative_list))
    print "%d True Positive Objects are: %s\n" % (true_positives,
                                                  sorted(true_positive_list))
    print "%d True Negative Objects are: %s\n" % (true_negatives,
                                                  sorted(true_negative_list))

    return (precision, recall, f1)
예제 #10
0
def begin_train(dynamic_path, static_path, out_path):

    dynamic_features = utilities.load_adjective_phase(dynamic_path)
    static_features = utilities.load_adjective_phase(static_path)
    adjective_list = utilities.adjectives
    for adjective in adjective_list:
        # File name
        dataset_file_name = "_".join(("trained", adjective)) + ".pkl"
        newpath = os.path.join(out_path, "trained_adjectives")
        path_name = os.path.join(newpath, dataset_file_name)
        if os.path.exists(path_name):
            print "File %s already exists, skipping it." % path_name
            continue

        overall_best_score = 0.0
        dataset = defaultdict()
        dataset['classifier'] = None
        dataset['training_score'] = overall_best_score

        dynamic_train = utilities.get_all_train_test_features(
            adjective, dynamic_features)
        y_labels = dynamic_train[1]
        object_ids = dynamic_train[2]
        dynamic_scaler = preprocessing.StandardScaler().fit(dynamic_train[0])
        dynamic_X = dynamic_scaler.transform(dynamic_train[0])
        dynamic_kernel = linear_kernel(dynamic_X, -2)
        #dynamic_kernel = standardize(dynamic_kernel)

        static_train = utilities.get_all_train_test_features(
            adjective, static_features)
        static_scaler = preprocessing.StandardScaler().fit(static_train[0])
        static_X = static_scaler.transform(static_train[0])
        static_kernel = linear_kernel(static_X, -2)
        #static_kernel = standardize(static_kernel)

        alpha_range = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
        #alpha_range = [0.5]
        for alpha in alpha_range:
            print "Beginning %s, alpha %1.1f at %s" % (adjective, alpha,
                                                       time.asctime())
            combined_kernel = (alpha) * static_kernel + (
                1 - alpha) * dynamic_kernel
            trained_clf, best_score = gram_grid_search(combined_kernel,
                                                       labels=y_labels,
                                                       object_ids=object_ids,
                                                       refined_range=adjective)
            print "F1: %1.5f at %s" % (best_score, time.asctime())
            if best_score > overall_best_score:
                overall_best_score = best_score
                dataset['classifier'] = trained_clf
                dataset['training_score'] = best_score
                dataset['alpha'] = alpha

        dataset['dynamic_features'] = dynamic_features[adjective]
        dataset['static_features'] = static_features[adjective]
        dataset['adjective'] = adjective
        dataset['dynamic_scaler'] = dynamic_scaler
        dataset['dynamic_train_scaled'] = dynamic_X
        dataset['dynamic_kernel_mean'] = np.mean(dynamic_kernel)
        dataset['dynamic_kernel_std'] = np.std(dynamic_kernel)
        dataset['static_scaler'] = static_scaler
        dataset['static_train_scaled'] = static_X
        dataset['static_kernel_mean'] = np.mean(static_kernel)
        dataset['static_kernel_std'] = np.std(static_kernel)

        print "Saving trained_classifier"

        # Save the results in the folder
        with open(path_name, "w") as f:
            print "Saving file: ", path_name
            cPickle.dump(dataset, f, protocol=cPickle.HIGHEST_PROTOCOL)
def test_adjective(classifier, adjective_report ):
    
    true_positives = 0.0
    true_negatives = 0.0
    false_positives = 0.0
    false_negatives = 0.0

    false_positive_list = []
    false_negative_list = []
    true_positive_list = []
    true_negative_list = []

    adjective = classifier['adjective']

    dynamic_features = utilities.load_adjective_phase('/home/imcmahon/Desktop/mkl/dynamic/adjective_phase_set')
    static_features = utilities.load_adjective_phase('/home/imcmahon/Desktop/mkl/static/adjective_phase_set')
    #dynamic_features = utilities.load_adjective_phase(dynamic_path)
    #static_features = utilities.load_adjective_phase(static_path)
    

    #import pdb; pdb.set_trace()
    #Dynamic Train
    dynamic_train = utilities.get_all_train_test_features(adjective, dynamic_features, train=True)
    dynamic_train_scaler = preprocessing.StandardScaler().fit(dynamic_train[0])
    dynamic_train_scaled_X = dynamic_train_scaler.transform(dynamic_train[0])
    dynamic_train_kernel = linear_kernel(dynamic_train_scaled_X, -2) 
    #dynamic_train_kernel = standardize(dynamic_train_kernel)
    #Static Train
    static_train = utilities.get_all_train_test_features(adjective, static_features, train=True)
    static_train_scaler = preprocessing.StandardScaler().fit(static_train[0])
    static_train_scaled_X = static_train_scaler.transform(static_train[0])
    static_train_kernel = linear_kernel(static_train_scaled_X, -2) 
    #static_train_kernel = standardize(static_train_kernel)
    #Recompute the GRAM matrix
    #alpha = classifier['alpha'];
    #train_X = (alpha)*static_train_kernel + (1-alpha)*dynamic_train_kernel

    #import pdb; pdb.set_trace()
    dynamic_test = utilities.get_all_train_test_features(adjective, dynamic_features, train=False)
    dynamic_test_scaled_X = classifier['dynamic_scaler'].transform(dynamic_test[0])
    dynamic_kernel = linear_kernel_test(dynamic_test_scaled_X, dynamic_train_scaled_X, -2)
    #dynamic_kernel = (dynamic_kernel - classifier['dynamic_kernel_mean']) / classifier['dynamic_kernel_std']
    
    static_test = utilities.get_all_train_test_features(adjective, static_features, train=False)
    static_test_scaled_X = classifier['static_scaler'].transform(static_test[0])
    static_kernel = linear_kernel_test(static_test_scaled_X, static_train_scaled_X, -2)
    #static_kernel = (static_kernel - classifier['static_kernel_mean']) / classifier['static_kernel_std']

    alpha = classifier['alpha'];

    test_X = (alpha)*static_kernel + (1-alpha)*dynamic_kernel

    print '\n \nTesting Adjective: %s' % classifier['adjective']
    
    #Pull out test features/labels

    #for phase in phases:
    #import pdb; pdb.set_trace()
    #test_set = classifier['test']
    #test_X.append(test_set['features'])
    #test_X.append(test_set['features'])
    test_Y = dynamic_test[1]
    object_ids = dynamic_test[2]
    #object_names = test_set['object_names']

    
    # Pull out the classifier and merge features
    #test_X = np.concatenate(test_X, axis=1)
    #import pdb; pdb.set_trace()
    
    clf = classifier['classifier']
    c_dict[classifier['adjective']] = clf.C
    #import pdb; pdb.set_trace()
    print clf

    # Predict the labels!
    #if 'scaler' in classifier:
    #    if type(classifier['scaler']) == preprocessing.Scaler:
    #        test_X = classifier['scaler'].transform(test_X)
            
    #import pdb; pdb.set_trace() 
    output = clf.predict(test_X)
    # Determine if the true label and classifier prediction match
    for val in xrange(len(test_Y)):
        true_label = test_Y[val]
        predict_label = output[val]

        if true_label == 1:
            if predict_label == 1:
                true_positives += 1.0
                #true_positive_list.append(object_names[val])
            else:
                false_negatives += 1.0
                #false_negative_list.append(object_names[val])
        else: # label is 0
            if predict_label == 1:
                false_positives += 1.0
                #false_positive_list.append(object_names[val])
            else:
                true_negatives += 1.0
                #true_negative_list.append(object_names[val])
    
    # Compute statistics for the adjective
    try: 
        precision = true_positives / (true_positives + false_positives)
        recall = true_positives / (true_positives + false_negatives)
    
    except ZeroDivisionError: # The case when none are found
        precision = 0
        recall = 0
    try:
        f1 = 2.0 * precision*recall / (precision + recall)
    except ZeroDivisionError:
        f1 = 0
    print "Precision: %f, Recall: %f, F1: %f \n" % (precision, recall, f1)
    print "Alpha = %1.1f" % alpha
    adjective_report.write("%s, %1.1f, %f, %f, %f\n" % (classifier['adjective'], alpha, precision, recall, f1))

    print "%d False Positive Objects\n" % false_positives
    print "%d False Negative Objects\n" % false_negatives
    print "%d True Positive Objects\n" % true_positives
    print "%d True Negative Objects\n" % true_negatives
    
    return (alpha, precision, recall, f1)
def test_adjective(classifier, adjective_report):

    true_positives = 0.0
    true_negatives = 0.0
    false_positives = 0.0
    false_negatives = 0.0

    false_positive_list = []
    false_negative_list = []
    true_positive_list = []
    true_negative_list = []

    adjective = classifier['adjective']

    dynamic_features = utilities.load_adjective_phase(
        '/home/imcmahon/Desktop/mkl/dynamic/adjective_phase_set')
    static_features = utilities.load_adjective_phase(
        '/home/imcmahon/Desktop/mkl/static/adjective_phase_set')
    #dynamic_features = utilities.load_adjective_phase(dynamic_path)
    #static_features = utilities.load_adjective_phase(static_path)

    #import pdb; pdb.set_trace()
    #Dynamic Train
    dynamic_train = utilities.get_all_train_test_features(adjective,
                                                          dynamic_features,
                                                          train=True)
    dynamic_train_scaler = preprocessing.StandardScaler().fit(dynamic_train[0])
    dynamic_train_scaled_X = dynamic_train_scaler.transform(dynamic_train[0])
    dynamic_train_kernel = linear_kernel(dynamic_train_scaled_X, -2)
    #dynamic_train_kernel = standardize(dynamic_train_kernel)
    #Static Train
    static_train = utilities.get_all_train_test_features(adjective,
                                                         static_features,
                                                         train=True)
    static_train_scaler = preprocessing.StandardScaler().fit(static_train[0])
    static_train_scaled_X = static_train_scaler.transform(static_train[0])
    static_train_kernel = linear_kernel(static_train_scaled_X, -2)
    #static_train_kernel = standardize(static_train_kernel)
    #Recompute the GRAM matrix
    #alpha = classifier['alpha'];
    #train_X = (alpha)*static_train_kernel + (1-alpha)*dynamic_train_kernel

    #import pdb; pdb.set_trace()
    dynamic_test = utilities.get_all_train_test_features(adjective,
                                                         dynamic_features,
                                                         train=False)
    dynamic_test_scaled_X = classifier['dynamic_scaler'].transform(
        dynamic_test[0])
    dynamic_kernel = linear_kernel_test(dynamic_test_scaled_X,
                                        dynamic_train_scaled_X, -2)
    #dynamic_kernel = (dynamic_kernel - classifier['dynamic_kernel_mean']) / classifier['dynamic_kernel_std']

    static_test = utilities.get_all_train_test_features(adjective,
                                                        static_features,
                                                        train=False)
    static_test_scaled_X = classifier['static_scaler'].transform(
        static_test[0])
    static_kernel = linear_kernel_test(static_test_scaled_X,
                                       static_train_scaled_X, -2)
    #static_kernel = (static_kernel - classifier['static_kernel_mean']) / classifier['static_kernel_std']

    alpha = classifier['alpha']

    test_X = (alpha) * static_kernel + (1 - alpha) * dynamic_kernel

    print '\n \nTesting Adjective: %s' % classifier['adjective']

    #Pull out test features/labels

    #for phase in phases:
    #import pdb; pdb.set_trace()
    #test_set = classifier['test']
    #test_X.append(test_set['features'])
    #test_X.append(test_set['features'])
    test_Y = dynamic_test[1]
    object_ids = dynamic_test[2]
    #object_names = test_set['object_names']

    # Pull out the classifier and merge features
    #test_X = np.concatenate(test_X, axis=1)
    #import pdb; pdb.set_trace()

    clf = classifier['classifier']
    c_dict[classifier['adjective']] = clf.C
    #import pdb; pdb.set_trace()
    print clf

    # Predict the labels!
    #if 'scaler' in classifier:
    #    if type(classifier['scaler']) == preprocessing.Scaler:
    #        test_X = classifier['scaler'].transform(test_X)

    #import pdb; pdb.set_trace()
    output = clf.predict(test_X)
    # Determine if the true label and classifier prediction match
    for val in xrange(len(test_Y)):
        true_label = test_Y[val]
        predict_label = output[val]

        if true_label == 1:
            if predict_label == 1:
                true_positives += 1.0
                #true_positive_list.append(object_names[val])
            else:
                false_negatives += 1.0
                #false_negative_list.append(object_names[val])
        else:  # label is 0
            if predict_label == 1:
                false_positives += 1.0
                #false_positive_list.append(object_names[val])
            else:
                true_negatives += 1.0
                #true_negative_list.append(object_names[val])

    # Compute statistics for the adjective
    try:
        precision = true_positives / (true_positives + false_positives)
        recall = true_positives / (true_positives + false_negatives)

    except ZeroDivisionError:  # The case when none are found
        precision = 0
        recall = 0
    try:
        f1 = 2.0 * precision * recall / (precision + recall)
    except ZeroDivisionError:
        f1 = 0
    print "Precision: %f, Recall: %f, F1: %f \n" % (precision, recall, f1)
    print "Alpha = %1.1f" % alpha
    adjective_report.write(
        "%s, %1.1f, %f, %f, %f\n" %
        (classifier['adjective'], alpha, precision, recall, f1))

    print "%d False Positive Objects\n" % false_positives
    print "%d False Negative Objects\n" % false_negatives
    print "%d True Positive Objects\n" % true_positives
    print "%d True Negative Objects\n" % true_negatives

    return (alpha, precision, recall, f1)