The global fashion industry is valued at three trillion dollars and accounts for 2 percent of the world's GDP. The fashion industry is undergoing a dramatic transformation by adopting new computer vision, machine learning and deep learning techniques.
In this case study, the objective is to build a kind of model or artificial intelligence based on deep learning model that can classify images to different categories or different classes.
Problem:
Dataset:
Fashion training set consists of 70,000 images divided into 60,000 training and 10,000 testing samples. Dataset sample consists of 28x28 grayscale image, associated with a label from 10 classes.
Each image is 28 pixels in height and 28 pixels in width, for a total of 784 pixels in total. Each pixel has a single pixel-value associated with it, indicating the lightness or darkness of that pixel, with higher numbers meaning darker. This pixel-value is an integer between 0 and 255.
The 10 classes are as follows:
Sources:
# Import libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Import data
fashion_train_df = pd.read_csv('project_data/fashion_train.csv')
fashion_test_df = pd.read_csv('project_data/fashion_test.csv')
# The train image is already flatten
fashion_train_df.head()
# The test image is already flatten
fashion_train_df.head()
# Check the dataset shape
fashion_train_df.shape, fashion_test_df.shape
# transform dataset into arrays
training = np.array(fashion_train_df, dtype = 'float')
testing = np.array(fashion_test_df, dtype = 'float')
# View some images
import random
# select any random index from 1 to 60,000
i = random.randint(1,60000)
# reshape and plot the image
plt.imshow( training[i,1:].reshape((28,28)) )
# reshape and plot the image
plt.imshow( training[i,1:].reshape((28,28)) , cmap = 'gray')
plt.show()
# Check the label of the image
label = training[i,0]
print('The class is', label)
# 10 classes decoding is as follows:
# 0 => T-shirt/top
# 1 => Trouser
# 2 => Pullover
# 3 => Dress
# 4 => Coat
# 5 => Sandal
# 6 => Shirt
# 7 => Sneaker
# 8 => Bag
# 9 => Ankle boot
# View more images in a grid format
# Define the dimensions of the plot grid
W_grid = 15
L_grid = 15
# fig, axes = plt.subplots(L_grid, W_grid)
# subplot return the figure object and axes object
# we can use the axes object to plot specific figures at various locations
fig, axes = plt.subplots(L_grid, W_grid, figsize = (17,17))
axes = axes.ravel() # flaten the 15 x 15 matrix into 225 array
n_training = len(training) # get the length of the training dataset
# Select a random number from 0 to n_training
for i in np.arange(0, W_grid * L_grid): # create evenly spaces variables
# Select a random number
index = np.random.randint(0, n_training)
# read and display an image with the selected index
axes[i].imshow( training[index,1:].reshape((28,28)) )
axes[i].set_title(training[index,0], fontsize = 8)
axes[i].axis('off')
plt.subplots_adjust(hspace=0.4)
# Normalize the training dataset
X_train = training[:, 1:] / 255
y_train = training[:, 0]
# Normalize the training dataset
X_test = testing[:, 1:] / 255
y_test = testing[:, 0]
# Split the model
from sklearn.model_selection import train_test_split
X_train, X_validate, y_train, y_validate = train_test_split(X_train, y_train, test_size = 0.2, random_state = 100)
# Check the dataset dimension
X_train.shape,y_train.shape
# * unpack the tuple
X_train = X_train.reshape(X_train.shape[0], *(28, 28, 1))
X_test = X_test.reshape(X_test.shape[0], *(28, 28, 1))
X_validate = X_validate.reshape(X_validate.shape[0], *(28, 28, 1))
# Check the dataset dimension
X_train.shape, X_validate.shape , y_train.shape
# Import libraries
import keras
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Dropout
from keras.optimizers import Adam
from keras.callbacks import TensorBoard
# Build model
cnn_model = Sequential()
# First Layer
cnn_model.add(Conv2D(32, 3, 3, input_shape = (28, 28, 1), activation = 'relu'))
# Down sampling
cnn_model.add(MaxPooling2D(pool_size = (2,2)))
# Flattening
cnn_model.add(Flatten())
# Connecting layer
cnn_model.add(Dense(units = 32, activation = 'relu'))
# Connecting layer to output
cnn_model.add(Dense(units = 32, activation = 'softmax'))
# Compile the model
cnn_model.compile(loss ='sparse_categorical_crossentropy',
optimizer=Adam(lr=0.001),
metrics =['accuracy'])
# Fit the model with the dataset
history = cnn_model.fit(X_train,
y_train,
batch_size = 512,
epochs = 50,
verbose = 1,
validation_data = (X_validate, y_validate))
evaluation = cnn_model.evaluate(X_test, y_test)
print('Test Accuracy: {:.3f}'.format(evaluation[1]))
# Get the predictions for the test data
predicted_classes = np.argmax(cnn_model.predict(X_test), axis=-1)
predicted_classes
L = 5
W = 5
fig, axes = plt.subplots(L, W, figsize = (12,12))
axes = axes.ravel() # Multiplyting the matrices
for i in np.arange(0, L * W):
axes[i].imshow(X_test[i].reshape(28,28))
axes[i].set_title("Prediction Class = {:0.1f}\n True Class = {:0.1f}".format(predicted_classes[i], y_test[i]))
axes[i].axis('off')
plt.subplots_adjust(wspace=0.5)
# Create confusion matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, predicted_classes)
plt.figure(figsize = (14,10))
sns.heatmap(cm, annot=True)
plt.show()
# Create classification report
from sklearn.metrics import classification_report
num_classes = 10
target_names = ["Class {}".format(i) for i in range(num_classes)]
print(classification_report(y_test, predicted_classes, target_names = target_names))
The model were able to achieved 87% accuracy. Some classes can be easily predicted especially Trouser, Bag, Sandal and Ankle Boots. The class of shirt was difficult to predict by the machine probably due to its similarity of shape with coat and pull over. With that, it confuses the machine and decrease its accuracy. This can be further by uploading better texture of the images and tuning the parameters of the model.