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: MCSVM" #Get parameters MCSVM_approach = svm_approach(parameters['Approach']) #Paths temp_path = os.path.abspath(os.path.join(dirname, "..", "..", "temp")) model_path = os.path.join(temp_path, "iteration:" + str(iteration) + \ "-MCSVM_" + str(pos_fold) + ".model") #Preprocess each class to a unique value to the classification label_encoder = preprocessing.LabelEncoder() label_encoder.fit(classes_list) print "List of classes of this experiment:", label_encoder.classes_ #Read the train file 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) #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 #-------------------------------------------------------------------------- mcsvm = SVM.MCSVM(approach=MCSVM_approach) #Fit print "\tFit: Beginning" mcsvm.fit(list_train, list_train_class) print "\tFit: Done!" #Save configuration of the SVM model_paths = [] #Predict print "\tPredict: Beginning" list_predict = mcsvm.classify(list_test) print "\tPredict: Done" #Mapping the results into integers list_predict = map(int, 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