def trainingForest(self, features, label): features = np.array(features, dtype=np.float32) self.model = cv2.RTrees() self.model.train(features, cv2.CV_ROW_SAMPLE, label, params=self.params) model_dir = settings.MODELS_ROOT svm_filename = os.path.join(model_dir, "rf.xml") self.model.save(svm_filename)
def __init__(self, inLength, outLength): super(MyRondomForest, self).__init__() self.in_length = inLength self.out_length = outLength self.model = cv2.RTrees() self.var_type = np.array([cv2.CV_VAR_NUMERICAL] * self.in_length + [cv2.CV_VAR_CATEGORICAL], np.uint8) self.params = dict(depth=32)
def __init__(self, img, rfFile, labels): self.encodeLabels(labels) imgFeat = np.array(self.getImageFeature(img), dtype=np.float32) self.rf = cv2.RTrees() predictedLabel = self.predict(imgFeat, rfFile) self.wordLabel = self.decodeLabels(predictedLabel)
def random_forest(data,responses,n_trees,max_depth): ''' Auto trains an OpenCV SVM. ''' data = np.float32(data) responses = np.float32(responses) params = dict(max_depth = max_depth, max_num_of_trees_in_the_forest=n_trees,termcrit_type=cv2.TERM_CRITERIA_MAX_ITER) #params = dict( kernel_type = cv2.SVM_LINEAR, svm_type = cv2.SVM_EPS_SVR , p=1.0 ) model = cv2.RTrees() model.train(data,cv2.CV_ROW_SAMPLE,responses,params=params) return model
def fit(self, X, y): # Check params self.n_features_ = X.shape[1] if isinstance(self.max_features, str): if self.max_features == "auto": max_features = max(1, int(np.sqrt(self.n_features_))) elif self.max_features == "sqrt": max_features = max(1, int(np.sqrt(self.n_features_))) elif self.max_features == "log2": max_features = max(1, int(np.log2(self.n_features_))) else: raise ValueError( 'Invalid value for max_features. Allowed string ' 'values are "auto", "sqrt" or "log2".') elif self.max_features is None: max_features = self.n_features_ elif isinstance(self.max_features, (numbers.Integral, np.integer)): max_features = self.max_features else: # float max_features = int(self.max_features * self.n_features_) params = {} params["nactive_vars"] = max_features params["max_num_of_trees_in_the_forest"] = self.n_estimators params["min_sample_count"] = self.min_samples_split params["calc_var_importance"] = False params[ "max_depth"] = 100000 if self.max_depth is None else self.max_depth params["use_surrogates"] = False params["termcrit_type"] = cv2.TERM_CRITERIA_MAX_ITER params["term_crit"] = (cv2.TERM_CRITERIA_MAX_ITER, self.n_estimators, 1) params["regression_accuracy"] = 0 var_types = np.array([cv2.CV_VAR_NUMERICAL] * self.n_features_ + [cv2.CV_VAR_CATEGORICAL], np.uint8) # Convert data self.classes_ = np.unique(y) y = np.searchsorted(self.classes_, y) X = X.astype(np.float32) y = y.astype(np.float32) # Run self.model_ = cv2.RTrees() self.model_.train(X, cv2.TERM_CRITERIA_MAX_ITER, y, varType=var_types, params=params) return self
def __init__(self): self.model = cv2.RTrees()
svm = cv2.SVM() svm.train(x_train, y_train, params=svm_params) # svm.save('svm_mnist_model.dat') ret = svm.predict_all(x_test) results = ret acurracy = (y_test == results) print "svm acurracy = ", np.mean(acurracy) svm_result = results # random trees RTtree = cv2.RTrees() rtree_params = dict(depth=32) var_type = np.array([cv2.CV_VAR_NUMERICAL] * 81 + [cv2.CV_VAR_CATEGORICAL], dtype=np.uint8) RTtree.train(x_train, cv2.CV_ROW_SAMPLE, y_train, varType=var_type, params=rtree_params) results = np.zeros(y_test.shape, dtype=y_test.dtype) for i in xrange(y_test.shape[0]): results[i, 0] = RTtree.predict(x_test[i, :])
def supervised_classification_opencv(input_band_list,sample_matrix,train_matrix,classification_type): '''Supervised classification using OpenCV library Function used to recall the supervised classification from the OpenCV library. Train and samples matrix can be extracted directly from a shapefile with the generate_training function or from a text file. Available algorithms: Support Vector Machine, Decision Tree, Gradient Boosted Tree, Normal Bayes, Random Forest, K-Nearest-Neighbors. Example: supervised_classification_opencv(input_band_list=(band1,band2,band3,band4),sample_matrix=samples_mat,train_matrix=training_mat,classification_type='svm') :param input_band_list: list of 2darrays corresponding to bands (band 1: blue) (list of numpy arrays) :param sample_matrix: 2darray with the samples to add :param train_matrix: 1darray with the corresponding classes :param classification type: definition of the desired classification algorithm ('svm','dt','gbt','bayes','rf','knn') (string) :returns: 2darray with predicted classes :raises: AttributeError, KeyError Author: Daniele De Vecchi - Daniel Aurelio Galeazzo - Mostapha Harb Last modified: 01/04/2014 ''' stack_new = np.dstack((input_band_list[0],input_band_list[1],input_band_list[2],input_band_list[3])) #stack with the original bands samples = stack_new.reshape((-1,4)) #input image as matrix with rows = (rows_original * cols_original) and columns = number of bands print classification_type if classification_type == "svm": params = dict( kernel_type = cv2.SVM_LINEAR, svm_type = cv2.SVM_C_SVC,C = 10000 ) #definition of the SVM parameters (kernel, type of algorithm and parameter related to the chosen algorithm cl = cv2.SVM() cl.train_auto(sample_matrix,train_matrix,None,None,params,100) #creation of the training set forcing the parameters optimization y_val = cl.predict_all(samples) #classification of the input image output = y_val.reshape(input_band_list[0].shape).astype(np.uint16) #reshape to the original rows and columns if classification_type == "dt": cl = tree.DecisionTreeClassifier() cl = cl.fit(sample_matrix, train_matrix) y_val = cl.predict(samples) output = y_val.reshape(input_band_list[0].shape).astype(np.uint16) if classification_type == "gbt": cl = GradientBoostingClassifier() cl = cl.fit(sample_matrix, train_matrix) y_val = cl.predict(samples) output = y_val.reshape(input_band_list[0].shape).astype(np.uint16) if classification_type == "bayes": cl = cv2.NormalBayesClassifier(sample_matrix, train_matrix) y_val = cl.predict(samples) y_val = np.array(y_val[1]) output = y_val.reshape(input_band_list[0].shape).astype(np.uint16) if classification_type == "rf": cl = cv2.RTrees() cl.train(sample_matrix, cv2.CV_ROW_SAMPLE, train_matrix) y_val = np.float32( [cl.predict(s) for s in samples] ) output = y_val.reshape(input_band_list[0].shape).astype(np.uint16) if classification_type == "knn": cl = cv2.KNearest() cl.train(sample_matrix, train_matrix) retval, results, neigh_resp, dists = cl.find_nearest(samples, k = 10) y_val = results.ravel() output = y_val.reshape(input_band_list[0].shape).astype(np.uint16) return output
########KNearest######### modelknn = cv2.KNearest() modelknn.train(train_images, train_labels) y_val_knn = modelknn.find_nearest(test_images, k=3) count_knn = 0 for item in range(test_number_of_images): if y_val_knn[1][item][0] == test_labels[item]: count_knn += 1 print 'knn:' + str(float(count_knn) / test_number_of_images) #######SVM########## modelsvm = cv2.SVM() modelsvm.train(train_images, train_labels) #, params = params y_val_svm = [modelsvm.predict(test_image) for test_image in test_images] evalfun('svm', y_val_svm, test_labels, test_number_of_images) #######RTrees########## modelRTtree = cv2.RTrees() sample_n, var_n = train_images.shape var_types = numpy.array([cv2.CV_VAR_NUMERICAL] * var_n + [cv2.CV_VAR_CATEGORICAL], numpy.uint8) params = dict(max_depth=10) modelRTtree.train(train_images, cv2.CV_ROW_SAMPLE, train_labels, varType=var_types, params=params) y_val_RTtree = numpy.float32([modelRTtree.predict(s) for s in test_images]) evalfun('RTtree', y_val_RTtree, test_labels, test_number_of_images) #######Boost######### modelBoost = cv2.Boost() sample_n, var_n = train_images.shape new_train_images = unroll_samples(train_images)
def train(self, samples, responses): self.model = cv2.RTrees() self.model.train(samples, cv2.CV_ROW_SAMPLE, responses, params=self.params)
def __init__(self): self.params = dict(max_depth=20) self.model = cv2.RTrees()
print "Image: " + str(i) ret, labels, centers = cv2.kmeans(fvec, K_TRAIN, criteria, K_ATTEMPTS, cv2.KMEANS_RANDOM_CENTERS) #Get K-Means #Reshape and save labels labels = labels.flatten() labels = labels.reshape((img_train[i].shape[0], img_train[i].shape[1])) kmean_train.append(labels) #Save centers for use during training for c in centers: center_train.append(c) # CREATE MODEL if DO_BAYES: bayes_model = cv2.NormalBayesClassifier() else: rtrees_model = cv2.RTrees() # PREPARE FOR CLASSIFICATION (CHECK FOR 50% OVERLAP) for i in range(len(kmean_train)): # Count how many of each cluster are in image region_count = [0] * K_TRAIN overlap_count = [0] * K_TRAIN for x in range(kmean_train[i].shape[0]): for y in range(kmean_train[i].shape[1]): region_count[kmean_train[i][x, y]] += 1 if img_train_gt[i][x, y] == 255: overlap_count[kmean_train[i][x, y]] += 1 #Check Strength of match ratio: a = np.array(overlap_count, dtype=np.float32) b = np.array(region_count, dtype=np.float32)
def __init__(self): self.classifier = cv2.RTrees()
(_,skin) = DefineSkin(pixels, traininggroundtruths[i], K) # append labels and centers to data structure to prepare for classifier training for i in range(len(centers)): samples.append(centers[i]) labels.append(skin[i]) # convert to numpy array for use with cv2 functions samples = np.array(samples, np.float32) labels = np.array(labels, np.float32) # generate model with training data if Bayes: model = cv2.NormalBayesClassifier() model.train(samples, labels) else: forest = cv2.RTrees() rtree_params = dict(max_depth=8, min_sample_count=8, use_surrogates=False, max_categories=5, calc_var_importance=False, nactive_vars=0, max_num_of_trees_in_the_forest=1000, termcrit_type=cv2.TERM_CRITERIA_MAX_ITER, term_crit=(cv2.TERM_CRITERIA_MAX_ITER,100,1)) forest.train(samples, cv2.CV_ROW_SAMPLE, labels, params=rtree_params) # note that there must be a matching groundtruth for each test image # with the same numerical suffix # get the groundtruths for the testing images testgroundtruths = gatherimages('./face_testing_groundtruth/*.png') # get test images testimages = gatherimages('./face_testing/*.png') # run test images through model and check performance # with the jaccard similarity measure for (imgidx,image_file) in enumerate(testimages): # run test image through kmeans to get K clusters