Train and Predict Fashion Dress using Tensorflow

Dated: 2019-12-28

In [15]:
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

Load Data

In [16]:
data = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = data.load_data()

class_names = ["T-Shirt Top", "Trouser", "Pullover", "Dress", "Coat", "Sandal", "Shirt", "Sneaker", "Bag", "Ankle Boot"]
In [17]:
train_images = train_images / 255.0
test_images = test_images / 255.0

Model

In [18]:
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28,28)),
    keras.layers.Dense(128, activation="relu"),
    keras.layers.Dense(10, activation="softmax")
])

model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=['accuracy'])

# fit model
epoch_data = model.fit(train_images, train_labels, epochs=10, verbose=1, batch_size=100, validation_data=(test_images, test_labels))
score = model.evaluate(test_images, test_labels, verbose=0)
print('Test loss and accuracy:', score)

model.save('fashion_data_model.h5')
Train on 60000 samples, validate on 10000 samples
Epoch 1/10
60000/60000 [==============================] - 2s 32us/sample - loss: 0.5440 - accuracy: 0.8128 - val_loss: 0.4514 - val_accuracy: 0.8441
Epoch 2/10
60000/60000 [==============================] - 2s 29us/sample - loss: 0.4028 - accuracy: 0.8587 - val_loss: 0.4310 - val_accuracy: 0.8474
Epoch 3/10
60000/60000 [==============================] - 2s 28us/sample - loss: 0.3617 - accuracy: 0.8722 - val_loss: 0.3812 - val_accuracy: 0.8626
Epoch 4/10
60000/60000 [==============================] - 2s 28us/sample - loss: 0.3329 - accuracy: 0.8809 - val_loss: 0.3719 - val_accuracy: 0.8696
Epoch 5/10
60000/60000 [==============================] - 2s 29us/sample - loss: 0.3124 - accuracy: 0.8873 - val_loss: 0.3502 - val_accuracy: 0.8734
Epoch 6/10
60000/60000 [==============================] - 2s 29us/sample - loss: 0.2987 - accuracy: 0.8907 - val_loss: 0.3555 - val_accuracy: 0.8711
Epoch 7/10
60000/60000 [==============================] - 2s 26us/sample - loss: 0.2862 - accuracy: 0.8964 - val_loss: 0.3413 - val_accuracy: 0.8766
Epoch 8/10
60000/60000 [==============================] - 2s 26us/sample - loss: 0.2754 - accuracy: 0.8995 - val_loss: 0.3310 - val_accuracy: 0.8807
Epoch 9/10
60000/60000 [==============================] - 2s 27us/sample - loss: 0.2665 - accuracy: 0.9026 - val_loss: 0.3386 - val_accuracy: 0.8775
Epoch 10/10
60000/60000 [==============================] - 2s 34us/sample - loss: 0.2575 - accuracy: 0.9056 - val_loss: 0.3408 - val_accuracy: 0.8769
Test loss and accuracy: [0.3408393795967102, 0.8769]

Load Model and Predict

In [30]:
lm = tf.keras.models.load_model("fashion_data_model.h5")
prediction = lm.predict(test_images)

for i in range(5):
    plt.grid(False)
    plt.imshow(test_images[i])
    plt.xlabel("Actual: " + class_names[test_labels[i]])
    plt.title("Prediction: " + class_names[np.argmax(prediction[i])])
    plt.show()