Fashion Mnist Image Classification using Deep Learning for Beginners
Introduction:
It contains the following parts:
- Setup your environment
- Build your Fashion image classification model
- Model Prediction
To run the program on your local computer, install the following required libraries, These libraries are
- python 3.8.0
- numpy
- pandas
- matplotlib
- tensorflow 2.0
- keras 2.3.0
Build your Fashion image classification model
Step 1: Understand the data
The first step of model prediction is to understand the data. It is more important to all machine learning and deep learning projects. You can find more information about the data, go to Fashion MNIST Kaggle. (Note: If you don't know Kaggle, please check this blog What is Kaggle? How to use it?).
Step 2: Import the Packages
Create a python file (for example model.py). After installed the required packages, import packages in your python file.
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow import keras
Step 3: Import the dataNext, import the data using pandas
mnist = keras.datasets.fashion_mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()
Then, Normalize the data values to the range [0, 1].
X_train = X_train/X_train.max()
X_test = X_test/X_test.max()
Step 6: Create Deep Learning Model
We create model for image classification using feed forward network. The feed forward network is first and simple type of deep learning networks. It is a classification algorithm. In this network, information moves only forward not from a cycle.
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Flatten, Dense
model = Sequential()
model.add(Flatten(input_shape=[28,28]))
model.add(Dense(100, activation='relu'))
model.add(Dense(10, activation='softmax'))
- Sequential - appropriate for a plain stack of layers where each layer has exactly one input tensor and one output tensor.
- Flatten - matrix flatten to one dimensional array.
- Dense - fully connected neural network layer and it implement the operations.
- Activation - used through an activation layer, or through the activation argument supported by all forward layers.
model.compile(loss="sparse_categorical_crossentropy",
optimizer="adam",
metrics=["accuracy"])
model.fit(X_train, y_train, epochs=2)
Out[]:
Train on 60000 samples
Epoch 1/2
60000/60000 [=============] - 11s 191us/sample
- loss: 0.2524 - accuracy: 0.9275
Epoch 2/2
60000/60000 [=============] - 11s 186us/sample
- loss: 0.2458 - accuracy: 0.9290
test_loss, test_acc = model.evaluate(X_test,
y_test,
verbose=0)
print('Loss:', test_loss)
print('Accuracy:', test_acc)
Out[]:
Loss: 0.3907148540019989
Accuracy: 0.8590999841690063
Finally, We predict the label of data on the basis of trained model. It returns the labels of the data passed as argument based upon the learned or trained data obtained from the model.
class_labels = ["T-shirt/top",
"Trouser",
"Pullover",
"Dress",
"Coat",
"Sandal",
"Shirt",
"Sneaker",
"Bag",
"Ankle boot"]
Then, we predict the model using test dataset.
y_pred = model.predict(X_test)
class_labels[np.argmax(y_pred[0])]
Out[]:
'Ankle boot'
import matplotlib.pyplot as plt
plt.matshow(X_test[0])
Thank you
ReplyDelete