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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fashion_mnist = keras.datasets.fashion_mnist
|
| 2 |
+
(x_train_full, y_train_full), (x_test, y_test) = fashion_mnist.load_data()
|
| 3 |
+
|
| 4 |
+
x_valid, x_train = x_train_full[:5000], x_train_full[5000:]
|
| 5 |
+
y_valid, y_train = y_train_full[:5000], y_train_full[5000:]
|
| 6 |
+
|
| 7 |
+
x_train.shape
|
| 8 |
+
import matplotlib as mpl
|
| 9 |
+
import matplotlib.pyplot as plt
|
| 10 |
+
plt.figure(figsize=(14,12))
|
| 11 |
+
|
| 12 |
+
plt.subplot(3,3,1)
|
| 13 |
+
some_image = x_train[0]
|
| 14 |
+
plt.imshow(some_image, cmap=mpl.cm.binary)
|
| 15 |
+
|
| 16 |
+
plt.subplot(3,3,2)
|
| 17 |
+
some_image = x_train[1]
|
| 18 |
+
plt.imshow(some_image, cmap=mpl.cm.binary)
|
| 19 |
+
|
| 20 |
+
plt.subplot(3,3,3)
|
| 21 |
+
some_image = x_train[2]
|
| 22 |
+
plt.imshow(some_image, cmap=mpl.cm.binary)
|
| 23 |
+
|
| 24 |
+
plt.subplot(3,3,4)
|
| 25 |
+
some_image = x_train[3]
|
| 26 |
+
plt.imshow(some_image, cmap=mpl.cm.binary)
|
| 27 |
+
|
| 28 |
+
plt.subplot(3,3,5)
|
| 29 |
+
some_image = x_train[4]
|
| 30 |
+
plt.imshow(some_image, cmap=mpl.cm.binary)
|
| 31 |
+
|
| 32 |
+
plt.subplot(3,3,6)
|
| 33 |
+
some_image = x_train[5]
|
| 34 |
+
plt.imshow(some_image, cmap=mpl.cm.binary)
|
| 35 |
+
|
| 36 |
+
plt.subplot(3,3,7)
|
| 37 |
+
some_image = x_train[6]
|
| 38 |
+
plt.imshow(some_image, cmap=mpl.cm.binary)
|
| 39 |
+
|
| 40 |
+
plt.subplot(3,3,8)
|
| 41 |
+
some_image = x_train[7]
|
| 42 |
+
plt.imshow(some_image, cmap=mpl.cm.binary)
|
| 43 |
+
|
| 44 |
+
plt.subplot(3,3,9)
|
| 45 |
+
some_image = x_train[8]
|
| 46 |
+
plt.imshow(some_image, cmap=mpl.cm.binary)
|
| 47 |
+
|
| 48 |
+
plt.show()
|
| 49 |
+
|
| 50 |
+
class_names = ["T-shirt/top","Trouser","Pullover", "Dress","Coat","Sandals","Shirt","Sneaker","Bag","Ankle boot"]
|
| 51 |
+
class_names[y_train[3]]
|
| 52 |
+
|
| 53 |
+
pd_y_train = pd.DataFrame(y_train)
|
| 54 |
+
|
| 55 |
+
frequency = pd_y_train.value_counts()
|
| 56 |
+
category = frequency.index.tolist()
|
| 57 |
+
counts = frequency.values.tolist()
|
| 58 |
+
# Visualization of train set
|
| 59 |
+
frequency.plot(kind='bar')
|
| 60 |
+
plt.title ('Bar plot')
|
| 61 |
+
plt.xlabel ('Category')
|
| 62 |
+
plt.ylabel ('Frequency')
|
| 63 |
+
|
| 64 |
+
img_shape = x_train.shape
|
| 65 |
+
n_samples = img_shape[0]
|
| 66 |
+
width = img_shape[1]
|
| 67 |
+
height = img_shape[2]
|
| 68 |
+
|
| 69 |
+
print("n_samples: ",n_samples)
|
| 70 |
+
print("width: ",width)
|
| 71 |
+
print("height: ",height)
|
| 72 |
+
|
| 73 |
+
#flatten each 2d mnist image into 1d array and checing dimensions
|
| 74 |
+
x_train_flatten = x_train.reshape(n_samples, width*height)
|
| 75 |
+
print("x_train_flatten.shape: ",x_train_flatten.shape)
|
| 76 |
+
from sklearn.preprocessing import StandardScaler
|
| 77 |
+
from sklearn.neighbors import KNeighborsClassifier
|
| 78 |
+
|
| 79 |
+
# feature scaling
|
| 80 |
+
standardscaler = StandardScaler()
|
| 81 |
+
X_train_scale = standardscaler.fit_transform(x_train_flatten)
|
| 82 |
+
|
| 83 |
+
# import KNN classifier from sklearn
|
| 84 |
+
KNN_classifier_scale = KNeighborsClassifier(n_neighbors=5)
|
| 85 |
+
KNN_classifier_scale. fit(X_train_scale,y_train)
|
| 86 |
+
|
| 87 |
+
X_test_stand = standardscaler.transform(x_test_flatten)
|
| 88 |
+
y_pred = KNN_classifier_scale.predict(X_test_stand)
|
| 89 |
+
# Cross validation
|
| 90 |
+
from sklearn.model_selection import cross_val_score
|
| 91 |
+
from sklearn.neighbors import KNeighborsClassifier
|
| 92 |
+
|
| 93 |
+
# define one KNN model
|
| 94 |
+
KNN_classifier = KNeighborsClassifier(n_neighbors=5, metric = 'euclidean')
|
| 95 |
+
|
| 96 |
+
# call cross-val_score
|
| 97 |
+
CV_scores = cross_val_score(estimator = KNN_classifier, X = x_train_flatten, y = y_train, cv = 3, scoring = 'accuracy')
|
| 98 |
+
print("CV_scores: ", CV_scores)
|
| 99 |
+
# Training
|
| 100 |
+
|
| 101 |
+
from sklearn.model_selection import cross_val_score
|
| 102 |
+
from sklearn.neighbors import KNeighborsClassifier
|
| 103 |
+
|
| 104 |
+
import time
|
| 105 |
+
start = time.time()
|
| 106 |
+
|
| 107 |
+
KNN_classifier = KNeighborsClassifier(n_neighbors=5, metric = 'euclidean')
|
| 108 |
+
KNN_classifier.fit(x_train_flatten, y_train)
|
| 109 |
+
y_valid_predicted_label = KNN_classifier.predict(x_valid_flatten)
|
| 110 |
+
|
| 111 |
+
end = time.time()
|
| 112 |
+
time_duration = end-start
|
| 113 |
+
print("Program finishes in {} seconds:".format(time_duration))
|
| 114 |
+
# Saving the data
|
| 115 |
+
from joblib import dump, load
|
| 116 |
+
dump(KNN_classifier, 'KNN_fashionmnist.joblib')
|
| 117 |
+
# loading the data
|
| 118 |
+
KNN_classifier = load('KNN_fashionmnist.joblib')
|
| 119 |
+
|
| 120 |
+
# organize the predicted classes and actual classes into Pandas dataframe
|
| 121 |
+
summary = pd.DataFrame({'predection':y_valid_predicted_label,'Original':y_valid})
|
| 122 |
+
summary
|
| 123 |
+
|
| 124 |
+
# Overall accuracy of the validation predictions
|
| 125 |
+
from sklearn import metrics
|
| 126 |
+
metrics.accuracy_score(y_valid,y_valid_predicted_label)
|
| 127 |
+
|
| 128 |
+
# Calculate the per-class accuracy of the predictions
|
| 129 |
+
from sklearn.metrics import confusion_matrix
|
| 130 |
+
matrix = confusion_matrix(y_valid,y_valid_predicted_label)
|
| 131 |
+
accuracy_score = matrix.diagonal()/matrix.sum(axis=1)
|
| 132 |
+
|
| 133 |
+
print('accuracy of t-shirt is',accuracy_score[0])
|
| 134 |
+
print('accuracy of Trouser is',accuracy_score[1])
|
| 135 |
+
print('accuracy of pullover is',accuracy_score[2])
|
| 136 |
+
print('accuracy of Dress is',accuracy_score[3])
|
| 137 |
+
print('accuracy of coat is',accuracy_score[4])
|
| 138 |
+
print('accuracy of sandal is',accuracy_score[5])
|
| 139 |
+
print('accuracy of shirt is',accuracy_score[6])
|
| 140 |
+
print('accuracy of sneaker is',accuracy_score[7])
|
| 141 |
+
print('accuracy of bag is',accuracy_score[8])
|
| 142 |
+
print('accuracy of boot is',accuracy_score[9])
|
| 143 |
+
|
| 144 |
+
# visualize the classification confusion matrix to check the details of the validation predictions for each class
|
| 145 |
+
import matplotlib.pyplot as plt
|
| 146 |
+
from sklearn.metrics import ConfusionMatrixDisplay
|
| 147 |
+
ConfusionMatrixDisplay.from_predictions(y_valid, y_valid_predicted_label)
|
| 148 |
+
plt.title("Classification Confusion matrix")
|
| 149 |
+
plt.show()
|
| 150 |
+
|
| 151 |
+
# Task 4.1.9 Different K values, and select the best model that has highest validation accuracy
|
| 152 |
+
# visualize the classification confusion matrix on the test set to report the details of predictions over every class
|
| 153 |
+
from sklearn.neighbors import KNeighborsClassifier
|
| 154 |
+
KNN_classifier = KNeighborsClassifier(n_neighbors=3, metric = 'euclidean')
|
| 155 |
+
KNN_classifier.fit(x_train_flatten, y_train)
|
| 156 |
+
y_valid_predicted_label = KNN_classifier.predict(x_valid_flatten)
|
| 157 |
+
|
| 158 |
+
y_valid_predicted_label
|
| 159 |
+
from sklearn import metrics
|
| 160 |
+
metrics.accuracy_score(y_valid, y_valid_predicted_label)
|
| 161 |
+
|
| 162 |
+
import matplotlib.pyplot as plt
|
| 163 |
+
from sklearn.metrics import ConfusionMatrixDisplay
|
| 164 |
+
ConfusionMatrixDisplay.from_predictions(y_test, y_test_pred)
|
| 165 |
+
plt.title("classifiaction confusion matrix")
|
| 166 |
+
plt.show()
|
| 167 |
+
# Task 4.1.10: Calculate the overall accuracy of the predictions over validation set and test set using the best model
|
| 168 |
+
from sklearn import metrics
|
| 169 |
+
metrics.accuracy_score(y_test, y_test_pred)
|
| 170 |
+
# discriminant analysis
|
| 171 |
+
import numpy as np
|
| 172 |
+
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
|
| 173 |
+
clf = LinearDiscriminantAnalysis()
|
| 174 |
+
clf.fit(x_train_flatten, y_train)
|
| 175 |
+
|
| 176 |
+
start = time.time()
|
| 177 |
+
predicted_labels = clf.predict(x_valid_flatten)
|
| 178 |
+
end = time.time()
|
| 179 |
+
time_duration = end-start
|
| 180 |
+
print("Program finishes in {} seconds:".format(time_duration))
|
| 181 |
+
|
| 182 |
+
y_test_pred = clf.predict(x_test_flatten)
|
| 183 |
+
print("Accuracy of testing Set: ", metrics.accuracy_score(y_test, y_test_pred))
|
| 184 |
+
y_valid_pred = clf.predict(x_valid_flatten)
|
| 185 |
+
print("Accuracy of validation Set: ", metrics.accuracy_score(y_valid, y_valid_pred))
|
| 186 |
+
|
| 187 |
+
from sklearn.metrics import confusion_matrix
|
| 188 |
+
matrix = confusion_matrix(y_test,y_test_pred)
|
| 189 |
+
accuracy_score = matrix.diagonal()/matrix.sum(axis=1)
|
| 190 |
+
|
| 191 |
+
print('accuracy of t-shirt is',accuracy_score[0])
|
| 192 |
+
print('accuracy of Trouser is',accuracy_score[1])
|
| 193 |
+
print('accuracy of pullover is',accuracy_score[2])
|
| 194 |
+
print('accuracy of Dress is',accuracy_score[3])
|
| 195 |
+
print('accuracy of coat is',accuracy_score[4])
|
| 196 |
+
print('accuracy of sandal is',accuracy_score[5])
|
| 197 |
+
print('accuracy of shirt is',accuracy_score[6])
|
| 198 |
+
print('accuracy of sneaker is',accuracy_score[7])
|
| 199 |
+
print('accuracy of bag is',accuracy_score[8])
|
| 200 |
+
print('accuracy of boot is',accuracy_score[9])
|
| 201 |
+
|
| 202 |
+
from gradio.outputs import Label
|
| 203 |
+
import gradio as gr
|
| 204 |
+
import cv2
|
| 205 |
+
import tensorflow as tf
|
| 206 |
+
|
| 207 |
+
def caption(image,input_module1):
|
| 208 |
+
class_names = ["T-shirt/top", "Trouser", "Pullover", "Dress", "Coat",
|
| 209 |
+
"Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot"]
|
| 210 |
+
image=image.reshape(1,28*28)
|
| 211 |
+
if input_module1=="KNN":
|
| 212 |
+
output1=KNN_classifier.predict(image)[0]
|
| 213 |
+
predictions=KNN_classifier.predict_proba(image)[0]
|
| 214 |
+
|
| 215 |
+
elif input_module1==("Linear discriminant analysis"):
|
| 216 |
+
output1=clf.predict(image)[0]
|
| 217 |
+
predictions=clf.predict_proba(image)[0]
|
| 218 |
+
|
| 219 |
+
elif input_module1==("Quadratic discriminant analysis"):
|
| 220 |
+
output1=qda.predict(image)[0]
|
| 221 |
+
predictions=qda.predict_proba(image)[0]
|
| 222 |
+
|
| 223 |
+
elif input_module1=="Naive Bayes classifier":
|
| 224 |
+
output1=gnb.predict(image)[0]
|
| 225 |
+
predictions=gnb.predict_proba(image)[0]
|
| 226 |
+
|
| 227 |
+
output2 = {}
|
| 228 |
+
|
| 229 |
+
for i in range(len(predictions)):
|
| 230 |
+
output2[class_names[i]] = predictions[i]
|
| 231 |
+
return output1 ,output2
|
| 232 |
+
|
| 233 |
+
input_module = gr.inputs.Image(label = "Input Image",image_mode="L",shape=(28,28))
|
| 234 |
+
input_module1 = gr.inputs.Dropdown(choices=["KNN","Linear discriminant analysis", "Quadratic discriminant analysis","Naive Bayes classifier"], label = "Method")
|
| 235 |
+
output1 = gr.outputs.Textbox(label = "Predicted Class")
|
| 236 |
+
output2=gr.outputs.Label(label= "probability of class")
|
| 237 |
+
gr.Interface(fn=caption, inputs=[input_module,input_module1], outputs=[output1,output2]).launch(debug=True)
|