I am currently starting to work on a research project which classifies image categories. First part of the research is image segmentation using the Random Forest algorithm. I'm having huge difficulties with segmenting the image with this algorithm. Can someone help me with how to segment an image with Python using Random Forest algorithm?
I have tried it with K-means clustering. But I need the Random Forest way of doing this.
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()
Here is a random forest implementation in python.
If you need it for image segmentation I suggest you to use ITKsnap, supervised learning, segmentation package which uses random forest and is implemented in python. It is easy and you can insert or define your labels and train your data. You can play around your learning parameters such as the number or depth of the trees. This is an example of how the segmentation works on the brain data:
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_
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.