#5, First Floor, 4th Street , Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 pro@slogix.in

Office Address

  • #5, First Floor, 4th Street Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 Landmark : Samiyar Madam
  • pro@slogix.in
  • +91- 81240 01111

Social List

Fashion MNIST classification with keras and CNN in python?
Description

To build a model for cloth classification using keras and CNN in python.

Input

MNIST data set. (In built data set from keras).

Output

Prediction result.
Accuracy score.

Process

  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 accuracy score.

Sapmle Code

#import necessary libraries
import warnings
warnings.filterwarnings(“ignore”)
import matplotlib.pyplot as plt
import keras
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten
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’]

#data normalization
X_train = X_train / 255
X_test = X_test / 255

print(“Train images\n\n”)
def plot_train(train):
plt.figure(figsize=(10,10))
for i in range(25):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(train[i],cmap=plt.cm.binary)
plt.show()
plot_train(X_train)

print(“Testing images\n\n”)
def plot_test(test):
plt.figure(figsize=(10,10))
for i in range(25):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(test[i],cmap=plt.cm.binary)
plt.show()
plot_test(X_test)

#reshape data to fit model
X_train = X_train.reshape(60000,28,28,1)
X_test = X_test.reshape(10000,28,28,1)

from keras.utils import to_categorical
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

#create model
model = Sequential()

#add model layers
model.add(Conv2D(64, kernel_size=3, activation=’relu’, input_shape=(28,28,1)))
model.add(Conv2D(32, kernel_size=3, activation=’relu’))
model.add(Flatten())
model.add(Dense(10, activation=’softmax’))

#compile the model
model.compile(optimizer=’adam’,
loss=’categorical_crossentropy’,
metrics=[‘accuracy’])

#Fit the model
model.fit(X_train, y_train,epochs=4)

#Evaluate the model
test_loss, test_acc = model.evaluate(X_test, y_test)

#Predict the test results
print(“Prediction results\n”)
prediction = model.predict(X_test[:10])
plt.imshow(prediction)
plt.show()
print(“Accuracy of the model\n”,test_acc*100)

Screenshots