-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoasis_db.py
More file actions
157 lines (130 loc) · 6.45 KB
/
Copy pathoasis_db.py
File metadata and controls
157 lines (130 loc) · 6.45 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
##### Importing relevant libraries ##############################
import cv2
from skimage.feature import local_binary_pattern
from sklearn.svm import LinearSVC, SVC
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import label_binarize
import numpy as np
import os
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import SGDClassifier
from sklearn.multiclass import OneVsRestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.neural_network import MLPClassifier
import pandas as pd
from sklearn.metrics import confusion_matrix, roc_auc_score, roc_curve, ConfusionMatrixDisplay, auc
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import label_binarize
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.impute import SimpleImputer
from sklearn import svm
from sklearn.linear_model import LinearRegression
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, precision_recall_curve
################################################################
############################# Data #############################
# image resizing variables
dimensions = (64, 64)
scaling_factor = 2.0
# local binary pattern @param
radius= 1
n_points= 8 * radius
# thresholding @param
region_size= 20
threshold_factor = 0.7
normalization_radius= 3
# model training
labels= []
################################################################
########################## Helper functions ####################
# Method to divide LBP image in non-overlapping regions
def divide_into_regions(_lbp, _region_size):
height, width = _lbp.shape
regions = []
for i in range(0, height, _region_size):
for j in range(0, width, _region_size):
region= _lbp[i:i+_region_size, j:j+_region_size]
regions.append(region)
return regions
#######################################################
# Method to normalize LBP histogram ###################
def normalize_histogram(_histogram):
return _histogram / np.sum(_histogram)
#######################################################
# Method to threshold computed LBP histogram ##########
def threshold_histogram(_histogram, _threshold_factor):
_histogram = normalize_histogram(_histogram)
threshold = _threshold_factor * np.mean(_histogram)
_histogram[_histogram < threshold] = 0
_histogram[_histogram >= threshold] = 1
return _histogram
#######################################################
# Method to normalize LBP feature map/LBP image ##########
def normalize_lbp_image(lbp_image, new_min=0, new_max=255):
min_val = np.min(lbp_image)
max_val = np.max(lbp_image)
# Avoiding zero division
if min_val == max_val:
return np.full_like(lbp_image, new_min)
normalized_image = ((lbp_image - min_val) / (max_val - min_val)) * (new_max - new_min) + new_min
return normalized_image.astype(np.uint8)
###########################################################################
def extract_features(image_path, threshold_factor):
img= cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
lbp = local_binary_pattern(img, n_points, radius, method='uniform')
# lbp = normalize_lbp_image(lbp)
# lbp = np.ravel(lbp)
##### region-based thresholding
regions= divide_into_regions(lbp, region_size)
thresholded_regions= []
for region in regions:
histogram, _= np.histogram(region, bins=np.arange(0, 10), density=True)
histogram = threshold_histogram(histogram, threshold_factor)
thresholded_regions.append(histogram)
thresholded_image= np.concatenate(thresholded_regions)
return thresholded_image.ravel()
# return normalized_lbp.ravel()
# return lbp.ravel()
# return np.ravel(img)
# return lbp_histogram
############################ Main function FOR OASIS MRI DATABASE ##########################################################
if __name__=="__main__":
texture_directory = 'C:/Users/HP/Desktop/Python_AI/lbp-descriptor-textureRecog/assets/OASIS_MRI_DB'
features = []
labels = []
for category in range(1, 5):
category_path = os.path.join(texture_directory, f'OASIS_Cross_{category}_converted')
for filename in os.listdir(category_path):
if filename.endswith('.jpg'):
image_path = os.path.join(category_path, filename)
feature = extract_features(image_path, 0.3)
features.append(feature)
labels.append(category)
# features=np.array(features)
# print("Features list: ", features)
# labels=np.array(labels)
# print("Labels list: ", labels)
# features=features.flatten()
X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.2, random_state=42)
# classifier = make_pipeline(StandardScaler(), SimpleImputer(strategy='mean'), SVC(kernel='linear', C=1.0))
# classifier = make_pipeline(StandardScaler(), SimpleImputer(strategy='mean'), RandomForestClassifier(n_estimators=100, random_state=42))
# classifier = make_pipeline(StandardScaler(), SimpleImputer(strategy='mean'), MLPClassifier(hidden_layer_sizes=(100, 100), max_iter=1000, random_state=42))
# classifier = make_pipeline(StandardScaler(), SimpleImputer(strategy='mean'), KNeighborsClassifier(n_neighbors=3))
classifier= OneVsRestClassifier(svm.SVC())
classifier.fit(X_train, y_train)
y_pred = classifier.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"\nAccuracy on the test set: {accuracy * 100:.2f}%") ##########################################################
print("\nPrecision: ", precision_score(y_test, y_pred, average="weighted") * 100, "%")
print("\nRecall: ", recall_score(y_test, y_pred, average="weighted") * 100, "%")
print("\nF1 Score: ", f1_score(y_test, y_pred, average="weighted") * 100, "%")
#####################################################################################################################################