def classify(images, classes_list, train_set, test_set, pos_fold, descriptor, parameters): """ Performs the classification of the test_set according to the train_set. Implementation of Support Vector Machine classifier using sklearn. """ print "Classification: SVMDBC" #Get parameters SVMDBC_approach = svm_approach(parameters['Approach']) #Paths dirname = os.path.abspath(os.path.join(os.path.dirname(__file__))) temp_path = os.path.abspath(os.path.join(dirname, "..", "..", "temp")) model_path = os.path.join(temp_path, "iteration:" + str(iteration) + \ "-SVMDBC_" + str(pos_fold) + ".model") #Preprocess each class to a unique value to the classification label_encoder = preprocessing.LabelEncoder() label_encoder.fit(classes_list) #SVMDBC is a binary classifier, it cannot have more than two classes print label_encoder.classes_ if len(label_encoder.classes_) != 2: print "Classifier needs exactly 2 classes" return None, None, None, None, None #Read the train list and save the list of class and the list #of feature vectors list_class = [] list_fv = [] for img in train_set: list_class.append(images[img][POS_CLASSES][INDEX_ZERO]) list_fv.append(images[img][POS_FV][INDEX_ZERO]) list_train = numpy.array(list_fv) list_train_class = numpy.array(list_class) #Given a list of classes, transform each value in this list to a integer list_train_class = label_encoder.transform(list_train_class) print "List of classes of this experiment:", label_encoder.classes_ #Read the test list and save the list of class and the list #of feature vectors list_img = test_set list_class = [] list_fv = [] for img in test_set: list_class.append(images[img][POS_CLASSES][INDEX_ZERO]) list_fv.append(numpy.array(images[img][POS_FV][INDEX_ZERO])) list_test = numpy.array(list_fv) list_test_class = numpy.array(list_class) #Classification #-------------------------------------------------------------------------- svmdbc = SVM.SVMDBC(approach=SVMDBC_approach) #SVMDBC Fit print "\tFit: Beginning" svmdbc.fit(list_train, list_train_class) print "\tFit: Done!" #Save configuration of the SVMDBC print svmdbc model_paths = [] #Predict print "\tPredict: Beginning" list_predict = svmdbc.classify(list_test) print "\tPredict: Done" #Mapping the results into integers list_predict = [ int(item) if item is not None else int(label_encoder.transform([None])) for item in list_predict ] #Returning the result to strings list_predict = label_encoder.inverse_transform(list_predict) list_result = [] for predict in list_predict: img_result = [0] * len(label_encoder.classes_) #Find all predict in the list label_encoder.classes_ and grab the #first index pos = label_encoder.classes_.tolist().index(predict) img_result[pos] = 1 list_result.append(img_result) #-------------------------------------------------------------------------- return list_img, list_test_class, list_result, label_encoder.classes_, \ model_paths