summaryrefslogtreecommitdiff
path: root/OASIS/python
diff options
context:
space:
mode:
authorEric Dao <eric@erickhangdao.com>2023-05-12 14:09:07 -0400
committerEric Dao <eric@erickhangdao.com>2023-05-12 14:09:07 -0400
commit191e4bd8beae134295d481773823142d2fdc6a98 (patch)
treeaaca4e7f8e229f9bd551d9bb1583b8ecd7a59d4a /OASIS/python
parent186daed7e179241377c117e9d208ccd301d4d712 (diff)
downloadura-master.tar.gz
ura-master.tar.bz2
ura-master.zip
refactored tool chain setup, able to debug and run from vscode nowHEADmaster
Diffstat (limited to 'OASIS/python')
-rw-r--r--OASIS/python/PCA_Accuracy.pngbin0 -> 86181 bytes
-rw-r--r--OASIS/python/main.py105
2 files changed, 105 insertions, 0 deletions
diff --git a/OASIS/python/PCA_Accuracy.png b/OASIS/python/PCA_Accuracy.png
new file mode 100644
index 0000000..a1a6cd7
--- /dev/null
+++ b/OASIS/python/PCA_Accuracy.png
Binary files differ
diff --git a/OASIS/python/main.py b/OASIS/python/main.py
new file mode 100644
index 0000000..d104716
--- /dev/null
+++ b/OASIS/python/main.py
@@ -0,0 +1,105 @@
+import os
+import cv2
+import numpy as np
+from sklearn.decomposition import PCA
+from sklearn.model_selection import train_test_split
+from sklearn import svm
+from sklearn.preprocessing import StandardScaler
+import matplotlib.pyplot as plt
+
+FEATURE_VECTOR_SIZE = 25344 # 144x176
+
+dneg_folder_path = "/home/eric/URA/OASIS/Dataset/BMP/DNEG/"
+dpos_folder_path = "/home/eric/URA/OASIS/Dataset/BMP/DPOS/"
+
+dneg_files = []
+dpos_files = []
+
+for file in os.listdir(dneg_folder_path):
+ dneg_file_path = os.path.join(dneg_folder_path, file)
+ if os.path.isfile(dneg_file_path):
+ dneg_img = cv2.imread(dneg_file_path, 0)
+ dneg_files.append(dneg_img)
+
+for file in os.listdir(dpos_folder_path):
+ dpos_file_path = os.path.join(dpos_folder_path, file)
+ if os.path.isfile(dpos_file_path):
+ dpos_img = cv2.imread(dpos_file_path, 0)
+ dpos_files.append(dpos_img)
+
+# Create labels
+dneg_labeled_files = [0] * len(dneg_files)
+dpos_labeled_files = [1] * len(dpos_files)
+
+# Concatenate arrays
+data = dneg_files + dpos_files
+labels = dneg_labeled_files + dpos_labeled_files
+
+# Convert arrays to numpy arrays
+data = np.array(data)
+labels = np.array(labels)
+
+# Flatten and scale data
+data_flatten = data.reshape(len(data), FEATURE_VECTOR_SIZE)
+scaler = StandardScaler()
+data_flatten = scaler.fit_transform(data_flatten)
+
+# Reduce dimensions
+pca = PCA(n_components=115)
+pca.fit(data_flatten)
+data_flatten = pca.transform(data_flatten)
+# print(data_flatten)
+
+# PC_values = np.arange(pca.n_components_) + 1
+# plt.plot(PC_values, pca.explained_variance_ratio_, 'o-', linewidth=2, color='blue')
+# plt.title('Scree Plot')
+# plt.xlabel('Principal Component')
+# plt.ylabel('Variance Explained')
+# plt.show()
+
+for x in range(50):
+ # Split data into training and testing sets
+ data_training, data_testing, labels_training, labels_testing = train_test_split(data_flatten, labels, test_size = 0.2)
+
+ # Train and evaluate SVM
+ clf = svm.SVC()
+ clf.fit(data_training, labels_training)
+ print("C value: ", clf.get_params()['C'])
+ print("Gamma value: ", clf.get_params()['gamma'])
+ print(clf.score(data_testing, labels_testing))
+
+ # Plot data
+ # plt.scatter(data_flatten[:, 0], data_flatten[:, 1], c=labels, cmap='viridis')
+ # plt.show()
+
+ # accuracies = []
+ # for num_components in range(2, 234):
+ # # Flatten and scale data
+ # data_flatten = data.reshape(len(data), FEATURE_VECTOR_SIZE)
+ # scaler = StandardScaler()
+ # data_flatten = scaler.fit_transform(data_flatten)
+
+ # # Reduce dimensions
+ # pca = PCA(n_components=num_components)
+ # pca.fit(data_flatten)
+ # data_flatten_pca = pca.transform(data_flatten)
+
+ # # Calculate accuracy 10 times
+ # accuracy = 0
+ # for i in range(100):
+ # # Split data into training and testing sets
+ # data_training, data_testing, labels_training, labels_testing = train_test_split(data_flatten_pca, labels, test_size = 0.2)
+
+ # # Train and evaluate SVM
+ # clf = svm.SVC()
+ # clf.fit(data_training, labels_training)
+ # accuracy += clf.score(data_testing, labels_testing)
+ # accuracy /= 100
+ # accuracies.append(accuracy)
+
+ # # Plot results
+ # plt.plot(range(2, 234), accuracies)
+ # plt.title('Number of PCA Components vs. SVM Accuracy')
+ # plt.xlabel('Number of PCA Components')
+ # plt.ylabel('Accuracy')
+ # plt.show()