Image Classification - 60,000 Training, 10,000 Test, 10 Class
# For data processing
import numpy as np
import pandas as pd
# For Deep Learning
import keras
# Visualisation
import matplotlib.pyplot as plt
%matplotlib inline
import vis
from keras.datasets import fashion_mnist
(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
x_train.shape, y_train.shape, x_test.shape, y_test.shape
labels = vis.fashion_mnist_label()
labels
See an image
x_train[0].shape
vis.imshow(x_train[0])
Let us see the first 10 unique image
import importlib
importlib.reload(vis)
vis.imshow_unique(x_train, y_train, labels)
vis.imshow_sprite(x_train[:500])
# Normalize the data
x_train = x_train.astype("float32") / 255
x_test = x_test.astype("float32") /255
# Flatten the input
x_train_flatten = x_train.reshape(60000, 28*28)
x_test_flatten = x_test.reshape(10000, 28*28)
x_train_flatten.shape, x_test_flatten.shape
from keras.utils import to_categorical
# Output data to Categorical Encoding
y_train_class = to_categorical(y_train, 10)
y_test_class = to_categorical(y_test, 10)
y_train.shape, y_train_class.shape
y_train[0], y_train_class[0]
from keras.models import Sequential
from keras.layers import Dense, Activation
mlp = Sequential()
mlp.add(Dense(100, activation="relu", input_dim=28*28))
mlp.add(Dense(50, activation="relu"))
mlp.add(Dense(10, activation="softmax"))
mlp.summary()
mlp.compile(loss="categorical_crossentropy", optimizer="sgd", metrics=['accuracy'])
%%time
output_mlp = mlp.fit(x_train_flatten, y_train_class, epochs=10, verbose=2,
validation_data=(x_test_flatten, y_test_class))
vis.metrics(output_mlp.history)
score = mlp.evaluate(x_test_flatten, y_test_class)
print('Test loss', score[0])
print('Test accuracy', score[1])
predict_classes = mlp.predict_classes(x_test_flatten)
pd.crosstab(y_test, predict_classes)
proba = mlp.predict_proba(x_test_flatten)
i = 4
vis.imshow(x_test[i], labels[y_test[i]]) | vis.predict(proba[i], y_test[i], labels)