-
Notifications
You must be signed in to change notification settings - Fork 0
/
A2_M.py
142 lines (109 loc) · 4.83 KB
/
A2_M.py
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
# Import required libraries
import os
import cv2
import numpy as np
import sklearn
import matplotlib.pyplot as plt
# Import necessary modules
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report, roc_auc_score, roc_curve, auc
# Keras specific
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.utils import to_categorical
Dataset_dir = "./"
def load_process_imgs(thresh, IMG_SIZE=100):
Categories = ["Negative", "Positive"]
features = []
target = []
for category in Categories:
path = os.path.join(Dataset_dir, category)
target_val = Categories.index(category)
for img in os.listdir(path):
img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE)
rez_img = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
im_bin = (rez_img > thresh) * 255
features.append(im_bin.flatten())
target.append(target_val)
return features, target
Features, Target = load_process_imgs(200, 150)
features = np.array(Features)
target = np.array(Target)
X_train, X_test, y_train, y_test = train_test_split(features, target, test_size=0.30, random_state=0)
def SVM_Model(kernel, X_train, X_test, y_train, y_test):
clf_SVM = SVC(C=10, kernel=kernel)
clf_SVM.fit(X_train, y_train)
y_pred = clf_SVM.predict(X_test)
accuracy = accuracy_score(y_pred, y_test) * 100
print(f"The SVM model with {kernel} kernel is {accuracy:.2f}% accurate")
print("SVM Model Summary:")
print(clf_SVM)
print("SVM Model Classification Report:")
print(classification_report(y_test, y_pred))
roc_auc = roc_auc_score(y_test, y_pred)
print("SVM Model ROC AUC:", roc_auc)
fpr, tpr, thresholds = roc_curve(y_test, y_pred)
roc_auc = auc(fpr, tpr)
print("SVM Model ROC AUC:", roc_auc)
# Plot ROC curve
plt.figure()
plt.plot(fpr, tpr, color='darkorange', lw=2, label='ROC curve (area = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic')
plt.legend(loc="lower right")
plt.show()
def NN_Keras_Model(X_train, X_test, y_train, y_test, numOfHidden_neurons):
model = Sequential()
model.add(Dense(500, activation='relu', input_dim=22500))
model.add(Dense(numOfHidden_neurons, activation='relu'))
model.add(Dense(1, activation='sigmoid')) # Use 'sigmoid' activation for binary classification
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
y_train_binary = y_train.astype(int)
y_test_binary = y_test.astype(int)
model.fit(X_train, y_train_binary, epochs=20)
y_pred_probs = model.predict(X_test)
y_pred = np.round(y_pred_probs) # Round to get binary predictions
accuracy = accuracy_score(y_test_binary, y_pred) * 100
print(f"The NN keras model is {accuracy:.2f}% accurate")
# ROC curve and AUC
fpr, tpr, thresholds = roc_curve(y_test_binary, y_pred_probs)
roc_auc = auc(fpr, tpr)
plt.figure()
plt.plot(fpr, tpr, color='darkorange', lw=2, label='ROC curve (area = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic')
plt.legend(loc="lower right")
plt.show()
model = Sequential()
model.add(Dense(500, activation='relu', input_dim=22500))
model.add(Dense(numOfHidden_neurons, activation='relu'))
model.add(Dense(2, activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(X_train, y_train, epochs=20)
y_pred_probs = model.predict(X_test)
y_pred = np.argmax(y_pred_probs, axis=1)
accuracy = accuracy_score(y_test, y_pred)
print(f"The CNN model is {accuracy * 100}% accurate")
# test cases
SVM_Model('rbf', X_train, X_test, y_train, y_test)
SVM_Model('poly', X_train, X_test, y_train, y_test)
SVM_Model('linear', X_train, X_test, y_train, y_test)
SVM_Model('sigmoid', X_train, X_test, y_train, y_test)
#NN_model_MLPClassifier(X_train, X_test, y_train, y_test, 12)
#NN_model_MLPClassifier(X_train, X_test, y_train, y_test, 44)
#NN_model_MLPClassifier(X_train, X_test, y_train, y_test, 86)
#NN_Keras_Model(X_train, X_test, y_train, y_test, numOfHidden_neurons=50)
#NN_Keras_Model(X_train, X_test, y_train, y_test, numOfHidden_neurons=90)
NN_Keras_Model(X_train, X_test, y_train, y_test, numOfHidden_neurons=128)