To build a model for cloth classification using keras and deep learning in python.
MNIST data set. (In built data set from keras).
Confusion matrix
Classification matrix
Accuracy score
Import necessary libraries.
Load MNIST data set from keras.
Split the data into train and testing.
Build the deep learning model using kears.
Fit the train data into the model.
Predict the test results.
Finds the classification report, accuracy score.
#import necessary libraries
import warnings
warnings.filterwarnings(“ignore”)
import keras
import numpy as np
from sklearn.metrics import confusion_matrix,classification_report, accuracy_score
#load the data set
fashion_mnist = keras.datasets.fashion_mnist
#Split the data
(X_train, y_train), (X_test, y_test) = fashion_mnist.load_data()
#name of images
class_names = [‘T-shirt’,’Trouser’,’Pullover’,’Dress’,’Coat’,’Sandal’,’Shirt’,’Sneaker’,’Bag’,’Ankle boot’]
#make image size into (28,28)
X_train = X_train / 255.0
X_test = X_test / 255.0
#Build the model
model = keras.Sequential([keras.layers.Flatten(input_shape=(28, 28)),keras.layers.Dense(128, activation=’relu’),
keras.layers.Dense(128, activation=’relu’),keras.layers.Dense(10, activation=’sigmoid’)])
#Compile the model
model.compile(optimizer=’adam’,loss=’sparse_categorical_crossentropy’,metrics=[‘accuracy’])
#Fit th train data
model.fit(X_train, y_train, epochs=10)
test_loss, test_acc = model.evaluate(X_test, y_test)
print(“\n”)
#Predict the test results
prediction = model.predict_classes(X_test)
length = len(prediction)
y_label = np.array(y_test)
predict_label = np.array(prediction)
#confusion matrix and classification report
print(“Confusion Matrix\n”,confusion_matrix(y_label,predict_label))
print(“\n”)
print(“Classification Report\n”,classification_report(y_label,predict_label))
print(“\n”)
print(“Accuracy : “,accuracy_score(y_label,predict_label)*100)