Spaces:
Sleeping
Sleeping
Siyun He commited on
Commit ·
c456c52
1
Parent(s): 6a6f1d4
add fine tume for SVM
Browse files- classification.py +60 -1
- clf_glcm.pkl +2 -2
- clf_lbp.pkl +2 -2
- test_glcm_tsne.png +0 -0
- test_lbp_tsne.png +0 -0
- train_glcm_tsne.png +0 -0
- train_lbp_tsne.png +0 -0
classification.py
CHANGED
|
@@ -200,6 +200,7 @@ if __name__ == '__main__':
|
|
| 200 |
# Train the classifier using the training data.
|
| 201 |
# Test the classifier using the testing data.
|
| 202 |
from sklearn.svm import SVC
|
|
|
|
| 203 |
from sklearn.metrics import accuracy_score
|
| 204 |
from sklearn.metrics import precision_score
|
| 205 |
import pandas as pd
|
|
@@ -219,18 +220,57 @@ if __name__ == '__main__':
|
|
| 219 |
X_test_lbp = test_lbp.drop('class', axis=1)
|
| 220 |
y_test_lbp = test_lbp['class']
|
| 221 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
clf_glcm = SVC()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
clf_glcm.fit(X_train_glcm, y_train_glcm)
|
| 224 |
y_pred_glcm = clf_glcm.predict(X_test_glcm)
|
|
|
|
|
|
|
| 225 |
print('Accuracy for GLCM features:', accuracy_score(y_test_glcm, y_pred_glcm))
|
|
|
|
| 226 |
# calculate the precsion
|
| 227 |
precision = precision_score(y_test_glcm, y_pred_glcm, average='weighted')
|
| 228 |
print('Precision for GLCM features:', precision)
|
| 229 |
|
| 230 |
clf_lbp = SVC()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
clf_lbp.fit(X_train_lbp, y_train_lbp)
|
|
|
|
| 232 |
y_pred_lbp = clf_lbp.predict(X_test_lbp)
|
|
|
|
|
|
|
| 233 |
print('Accuracy for LBP features:', accuracy_score(y_test_lbp, y_pred_lbp))
|
|
|
|
| 234 |
# calculate the precsion
|
| 235 |
precision = precision_score(y_test_lbp, y_pred_lbp, average='weighted')
|
| 236 |
print('Precision for LBP features:', precision)
|
|
@@ -264,7 +304,7 @@ if __name__ == '__main__':
|
|
| 264 |
plt.savefig('train_lbp_distribution.png')
|
| 265 |
plt.close()
|
| 266 |
|
| 267 |
-
#Use plots to visualize feature distributions and decision boundaries of the classifiers clf_glcm, clf_lbp using t-sne
|
| 268 |
from sklearn.manifold import TSNE
|
| 269 |
|
| 270 |
tsne = TSNE(n_components=2)
|
|
@@ -285,4 +325,23 @@ if __name__ == '__main__':
|
|
| 285 |
plt.title('LBP features')
|
| 286 |
# save the plot to a file
|
| 287 |
plt.savefig('train_lbp_tsne.png')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 288 |
plt.close()
|
|
|
|
| 200 |
# Train the classifier using the training data.
|
| 201 |
# Test the classifier using the testing data.
|
| 202 |
from sklearn.svm import SVC
|
| 203 |
+
from sklearn.model_selection import GridSearchCV
|
| 204 |
from sklearn.metrics import accuracy_score
|
| 205 |
from sklearn.metrics import precision_score
|
| 206 |
import pandas as pd
|
|
|
|
| 220 |
X_test_lbp = test_lbp.drop('class', axis=1)
|
| 221 |
y_test_lbp = test_lbp['class']
|
| 222 |
|
| 223 |
+
# Define the parameter grid for tuning
|
| 224 |
+
param_grid = {
|
| 225 |
+
'C': [0.1, 1, 10, 100], # Regularization parameter
|
| 226 |
+
'kernel': ['linear', 'rbf'], # Kernels to explore: linear and RBF
|
| 227 |
+
'gamma': [1, 0.1, 0.01, 0.001] # Gamma values for RBF kernel
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
clf_glcm = SVC()
|
| 231 |
+
|
| 232 |
+
# Set up GridSearchCV with 5-fold cross-validation
|
| 233 |
+
grid_search = GridSearchCV(clf_glcm, param_grid, cv=5, scoring='accuracy', verbose=2, n_jobs=-1)
|
| 234 |
+
|
| 235 |
+
# Perform the grid search to find the best hyperparameters
|
| 236 |
+
grid_search.fit(X_train_glcm, y_train_glcm)
|
| 237 |
+
|
| 238 |
+
# Output the best parameters from the search
|
| 239 |
+
print("Best parameters for clf_glcm: ", grid_search.best_params_)
|
| 240 |
+
|
| 241 |
+
# Use the best estimator found by GridSearchCV to make predictions
|
| 242 |
+
clf_glcm = grid_search.best_estimator_
|
| 243 |
+
|
| 244 |
clf_glcm.fit(X_train_glcm, y_train_glcm)
|
| 245 |
y_pred_glcm = clf_glcm.predict(X_test_glcm)
|
| 246 |
+
|
| 247 |
+
# calculate the accuracy
|
| 248 |
print('Accuracy for GLCM features:', accuracy_score(y_test_glcm, y_pred_glcm))
|
| 249 |
+
|
| 250 |
# calculate the precsion
|
| 251 |
precision = precision_score(y_test_glcm, y_pred_glcm, average='weighted')
|
| 252 |
print('Precision for GLCM features:', precision)
|
| 253 |
|
| 254 |
clf_lbp = SVC()
|
| 255 |
+
# Set up GridSearchCV with 5-fold cross-validation
|
| 256 |
+
grid_search = GridSearchCV(clf_lbp, param_grid, cv=5, scoring='accuracy', verbose=2, n_jobs=-1)
|
| 257 |
+
|
| 258 |
+
# Perform the grid search to find the best hyperparameters
|
| 259 |
+
grid_search.fit(X_train_lbp, y_train_lbp)
|
| 260 |
+
|
| 261 |
+
# Output the best parameters from the search
|
| 262 |
+
print("Best parameters for clf_lbp: ", grid_search.best_params_)
|
| 263 |
+
|
| 264 |
+
# Use the best estimator found by GridSearchCV to make predictions
|
| 265 |
+
clf_lbp = grid_search.best_estimator_
|
| 266 |
+
|
| 267 |
clf_lbp.fit(X_train_lbp, y_train_lbp)
|
| 268 |
+
|
| 269 |
y_pred_lbp = clf_lbp.predict(X_test_lbp)
|
| 270 |
+
|
| 271 |
+
# calculate the accuracy
|
| 272 |
print('Accuracy for LBP features:', accuracy_score(y_test_lbp, y_pred_lbp))
|
| 273 |
+
|
| 274 |
# calculate the precsion
|
| 275 |
precision = precision_score(y_test_lbp, y_pred_lbp, average='weighted')
|
| 276 |
print('Precision for LBP features:', precision)
|
|
|
|
| 304 |
plt.savefig('train_lbp_distribution.png')
|
| 305 |
plt.close()
|
| 306 |
|
| 307 |
+
# Use plots to visualize feature distributions and decision boundaries of the classifiers clf_glcm, clf_lbp using t-sne
|
| 308 |
from sklearn.manifold import TSNE
|
| 309 |
|
| 310 |
tsne = TSNE(n_components=2)
|
|
|
|
| 325 |
plt.title('LBP features')
|
| 326 |
# save the plot to a file
|
| 327 |
plt.savefig('train_lbp_tsne.png')
|
| 328 |
+
plt.close()
|
| 329 |
+
|
| 330 |
+
# plot t-sne it for the testing data
|
| 331 |
+
tsne = TSNE(n_components=2, perplexity=5)
|
| 332 |
+
X_test_glcm_tsne = tsne.fit_transform(X_test_glcm)
|
| 333 |
+
X_test_lbp_tsne = tsne.fit_transform(X_test_lbp)
|
| 334 |
+
|
| 335 |
+
plt.scatter(X_test_glcm_tsne[y_test_glcm == 'grass', 0], X_test_glcm_tsne[y_test_glcm == 'grass', 1], color='red', label='grass')
|
| 336 |
+
plt.scatter(X_test_glcm_tsne[y_test_glcm == 'wood', 0], X_test_glcm_tsne[y_test_glcm == 'wood', 1], color='blue', label='wood')
|
| 337 |
+
plt.legend()
|
| 338 |
+
plt.title('GLCM features')
|
| 339 |
+
plt.savefig('test_glcm_tsne.png')
|
| 340 |
+
plt.close()
|
| 341 |
+
|
| 342 |
+
plt.scatter(X_test_lbp_tsne[y_test_lbp == 'grass', 0], X_test_lbp_tsne[y_test_lbp == 'grass', 1], color='red', label='grass')
|
| 343 |
+
plt.scatter(X_test_lbp_tsne[y_test_lbp == 'wood', 0], X_test_lbp_tsne[y_test_lbp == 'wood', 1], color='blue', label='wood')
|
| 344 |
+
plt.legend()
|
| 345 |
+
plt.title('LBP features')
|
| 346 |
+
plt.savefig('test_lbp_tsne.png')
|
| 347 |
plt.close()
|
clf_glcm.pkl
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:87bdb02aa47718d5e9f6ad188b4f02609f826f0b088b5dfaf3ccdbe76551b7ba
|
| 3 |
+
size 1537
|
clf_lbp.pkl
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:67242af0119c0f138b1f3d104bbd6144e02e7fe137fa46937714385984d8f14a
|
| 3 |
+
size 2509
|
test_glcm_tsne.png
ADDED
|
test_lbp_tsne.png
ADDED
|
train_glcm_tsne.png
CHANGED
|
|
train_lbp_tsne.png
CHANGED
|
|