如何通过python使用随机森林进行图像分割

问题描述 投票:0回答:2

我目前正在开始开展一个对图像类别进行分类的研究项目。研究的第一部分是使用“随机森林”算法进行“图像分割”。我在用这个算法分割图像时遇到了巨大的困难。有人可以帮助我如何使用 Python 使用 Random Forest 算法分割图像吗? 我已经尝试过 K 均值聚类。但我需要随机森林的方式来做到这一点。

import time import numpy as np import scipy as sp import matplotlib.pyplot as plt from sklearn.ensemble import RandomForestClassifier from sklearn.feature_extraction import image from sklearn.cluster import spectral_clustering from sklearn.utils.testing import SkipTest from sklearn.utils.fixes import sp_version if sp_version < (0, 12): raise SkipTest("Skipping because SciPy version earlier than 0.12.0 and " "thus does not include the scipy.misc.face() image.") # load the raccoon face as a numpy array try: face = sp.face(gray=True) except AttributeError: # Newer versions of scipy have face in misc from scipy import misc face = misc.face(gray=True) # Resize it to 10% of the original size to speed up the processing face = sp.misc.imresize(face, 0.10) / 255. rm = RandomForestClassifier # Convert the image into a graph with the value of the gradient on the # edges. graph = image.img_to_graph(face) # Take a decreasing function of the gradient: an exponential # The smaller beta is, the more independent the segmentation is of the # actual image. For beta=1, the segmentation is close to a voronoi beta = 5 eps = 1e-6 graph.data = np.exp(-beta * graph.data / graph.data.std()) + eps # Apply spectral clustering (this step goes much faster if you have pyamg # installed) N_REGIONS = 25 ############################################################################# # Visualize the resulting regions for assign_labels in ('kmeans', 'discretize'): t0 = time.time() labels = spectral_clustering(graph, n_clusters=N_REGIONS, assign_labels=assign_labels, random_state=1) t1 = time.time() labels = labels.reshape(face.shape) plt.figure(figsize=(5, 5)) plt.imshow(face, cmap=plt.cm.gray) for l in range(N_REGIONS): plt.contour(labels == l, contours=1, colors=[plt.cm.spectral(l / float(N_REGIONS))]) plt.xticks(()) plt.yticks(()) title = 'Spectral clustering: %s, %.2fs' % (assign_labels, (t1 - t0)) print(title) plt.title(title) plt.show()


这是一个Python中的随机森林实现。 
python algorithm random-forest image-segmentation
2个回答
0
投票

import numpy as np import csv as csv from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import GridSearchCV from sklearn.cross_validation import StratifiedKFold # Add important libs # Training: train=[] test=[] #Array Definition path1 = r'D:\random forest\data set\train.csv' #Address Definition path2 = r'D:\random forest\data set\test.csv' with open(path1, 'r') as f1: #Open File as read by 'r' reader = csv.reader(f1) next(reader, None) #Skip header because file header is not needed for row in reader: #fill array by file info by for loop train.append(row) train = np.array(train) with open(path2, 'r') as f2: reader2 = csv.reader(f2) next(reader2, None) for row2 in reader2: test.append(row2) test = np.array(test) train = np.delete(train,[0],1) test = np.delete(test,[0],1) # Optimization parameter_gridsearch = { 'max_depth' : [3, 4], #depth of each decision tree 'n_estimators': [50, 20], #count of decision tree 'max_features': ['sqrt', 'auto', 'log2'], 'min_samples_split': [2], 'min_samples_leaf': [1, 3, 4], 'bootstrap': [True, False], } # RF classification randomForestClassifier() crossvalidation = StratifiedKFold(train[0::,0] , n_folds=5) gridsearch = GridSearchCV(randomforest, #grid search for algorithm optimization scoring='accuracy', param_grid=parameter_gridsearch, cv=crossvalidation) gridsearch.fit(train[0::,1::], train[0::,0]) #train[0::,0] is as target model = gridsearch parameters = gridsearch.best_params_

我不明白这一点,如果它应该是图像集,为什么要使用 .csv 文件作为训练测试

0
投票

© www.soinside.com 2019 - 2024. All rights reserved.