Wednesday, January 31, 2024

ML: SVM vs. Random Forest for image segmentation

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# https://github.com/bnsreenu/python_for_microscopists/blob/master/068b-ML_06_04_TRAIN_ML_segmentation_All_filters_RForest_SVM.py


import numpy as np
import cv2
import pandas as pd

img=cv2.imread("D://Mask//IMG_0686.JPG")
img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

img2=img.reshape(-1)
df=pd.DataFrame()
df['Original Image']=img2

# Generate Gabor features
# To count numbers up in order to give Gabor features a label in the data frame
num=1
kernels=[]

for theta in range(2):  # Define number of thetas
    theta=theta/4.*np.pi
    for sigma in (1, 3):
        for lamda in np.arange(0, np.pi, np.pi/4):
            for gamma in (0.05, 0.5):
                gabor_label='Gabor'+str(num)
                ksize=9
                kernel=cv2.getGaborKernel((ksize, ksize),sigma,theta,lamda,gamma, 0, ktype=cv2.CV_32F)
                kernels.append(kernel)
                fimg=cv2.filter2D(img2, cv2.CV_8UC3, kernel)
                filtered_img=fimg.reshape(-1)
                df[gabor_label]=filtered_img
                print(gabor_label, ': theta=', theta, ': sigma, ', sigma, ': lamda=', lamda, ':gamma=', gamma)
                num=num+1


# Canny Edge
edges=cv2.Canny(img, 100, 200)  # Image, min and max values
edges1=edges.reshape(-1)
df['Canny Edge']=edges1

from skimage.filters import roberts, sobel, scharr, prewitt


# Robert Edge
edge_roberts=roberts(img)
edge_roberts1=edge_roberts.reshape(-1)
df['Roberts']=edge_roberts1


# Sobel
edge_sobel=sobel(img)
edge_sobel1=edge_sobel.reshape(-1)
df['Sobel']=edge_sobel1

# Scharr
edge_scharr=scharr(img)
edge_scharr1=edge_scharr.reshape(-1)
df['Scharr']=edge_scharr1

# Prewitt
edge_prewitt=prewitt(img)
edge_prewitt1=edge_prewitt.reshape(-1)
df['Prewitt']=edge_prewitt1

# Gaussian with sigma=3
from scipy import ndimage as nd
gaussian_img=nd.gaussian_filter(img, sigma=3)
gaussian_img1=gaussian_img.reshape(-1)
df['Gaussian s3']=gaussian_img1

# Gaussian with sigma=7
gaussian_img2=nd.gaussian_filter(img, sigma=7)
gaussian_img3=gaussian_img2.reshape(-1)
df['Gaussian s7']=gaussian_img3

# Median with sigma=3
median_img=nd.median_filter(img, size=3)
median_img1=median_img.reshape(-1)
df['Median s3']=median_img1


# Variance with size=3
variance_img=nd.generic_filter(img, np.var, size=3)
variance_img1=variance_img.reshape(-1)
df['Variance s3']=variance_img1

# Now, add a column in the data frame for the labels
# For this, we need to import the labeled image
labeled_img=cv2.imread("D://Mask//mask.tif")
# Remember that you can load an image with partial labels
# But, drop the rows with unlabeled data

labeled_img=cv2.cvtColor(labeled_img, cv2.COLOR_BGR2GRAY)
labeled_img1=labeled_img.reshape(-1)
df['Labels']=labeled_img1

print(df.head())



# Define the dependent variable that needs to be predicted (labels)
Y=df["Labels"].values
# Define the independent variables
X=df.drop(labels=["Labels"], axis=1)

# Split data into train and test to verify accuracy after fitting the model
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.4, random_state=20)

# Import the model we are using
# RandomForestRegressor is for regression type of problems
# For classification we use RandomForestClassifier
# Both yield similar results except for regressor the result is float
# and for classifier it is an integer

from sklearn.ensemble import RandomForestClassifier
model=RandomForestClassifier(n_estimators=100, random_state=42)


# Train the model on training data
model.fit(X_train, y_train)


# Testing the model by predicting on test data
# and Calculate the accuracy score
# First test predication on the training data itself. Should be good
prediction_test_train=model.predict(X_train)

# Test prediction on testing data
prediction_test=model.predict(X_test)


# Let us check the accuracy on test data
from sklearn import metrics

# First check the accuracy on training data. This will be higher than test data predication accuracy
print("Accuracy on training data= ", metrics.accuracy_score(y_train, prediction_test_train))
print("Accuracy= ", metrics.accuracy_score(y_test, prediction_test))

No comments:

Post a Comment