import tensorflow as tf
from tensorflow import keras
import datetime
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import requests
from io import BytesIO
import cv2
We will use 60,000 images to train the network and 10,000 photos to evaluate how accurately the network learned to classify the images. We can access Fashion MNIST directly from TensorFlow.
We import and upload Fashion MNIST data directly from TensorFlow:
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
plt.figure()
plt.imshow(train_images[0])
plt.colorbar()
plt.grid(False)
plt.show()
The labels are a matrix of whole numbers, ranging from 0 to 9. These correspond to the kind of clothing that the image represents:
class_names = ["T-shirt", "Pants", "sweater", "dress", "coat", "sandal", "shirt", "sneaker", "bag", "boots"]
train_images.shape
(60000, 28, 28)
test_images.shape
(10000, 28, 28)
train_labels.shape
(60000,)
test_labels.shape
(10000,)
test_images[0]
array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 0, 0, 7, 0, 37, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 27, 84, 11, 0, 0, 0, 0, 0, 0, 119, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 88, 143, 110, 0, 0, 0, 0, 22, 93, 106, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 53, 129, 120, 147, 175, 157, 166, 135, 154, 168, 140, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 11, 137, 130, 128, 160, 176, 159, 167, 178, 149, 151, 144, 0, 0], [ 0, 0, 0, 0, 0, 0, 1, 0, 2, 1, 0, 3, 0, 0, 115, 114, 106, 137, 168, 153, 156, 165, 167, 143, 157, 158, 11, 0], [ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 3, 0, 0, 89, 139, 90, 94, 153, 149, 131, 151, 169, 172, 143, 159, 169, 48, 0], [ 0, 0, 0, 0, 0, 0, 2, 4, 1, 0, 0, 0, 98, 136, 110, 109, 110, 162, 135, 144, 149, 159, 167, 144, 158, 169, 119, 0], [ 0, 0, 2, 2, 1, 2, 0, 0, 0, 0, 26, 108, 117, 99, 111, 117, 136, 156, 134, 154, 154, 156, 160, 141, 147, 156, 178, 0], [ 3, 0, 0, 0, 0, 0, 0, 21, 53, 92, 117, 111, 103, 115, 129, 134, 143, 154, 165, 170, 154, 151, 154, 143, 138, 150, 165, 43], [ 0, 0, 23, 54, 65, 76, 85, 118, 128, 123, 111, 113, 118, 127, 125, 139, 133, 136, 160, 140, 155, 161, 144, 155, 172, 161, 189, 62], [ 0, 68, 94, 90, 111, 114, 111, 114, 115, 127, 135, 136, 143, 126, 127, 151, 154, 143, 148, 125, 162, 162, 144, 138, 153, 162, 196, 58], [ 70, 169, 129, 104, 98, 100, 94, 97, 98, 102, 108, 106, 119, 120, 129, 149, 156, 167, 190, 190, 196, 198, 198, 187, 197, 189, 184, 36], [ 16, 126, 171, 188, 188, 184, 171, 153, 135, 120, 126, 127, 146, 185, 195, 209, 208, 255, 209, 177, 245, 252, 251, 251, 247, 220, 206, 49], [ 0, 0, 0, 12, 67, 106, 164, 185, 199, 210, 211, 210, 208, 190, 150, 82, 8, 0, 0, 0, 178, 208, 188, 175, 162, 158, 151, 11], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
train_images = train_images / 255
test_images = test_images / 255
plt.figure()
plt.imshow(train_images[0])
plt.colorbar()
plt.grid(False)
plt.show()
plt.figure(figsize=(10,10))
for i in range(50):
plt.subplot(5,10, i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(train_images[i], cmap=plt.cm.binary)
plt.xlabel(class_names[train_labels[i]])
plt.show()
model = keras.Sequential([
keras.layers.Flatten(input_shape = (28,28)),
keras.layers.Dense(15, activation = "relu"),
keras.layers.Dense(10, activation = "softmax")
])
model.summary()
Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= flatten (Flatten) (None, 784) 0 _________________________________________________________________ dense (Dense) (None, 15) 11775 _________________________________________________________________ dense_1 (Dense) (None, 10) 160 ================================================================= Total params: 11,935 Trainable params: 11,935 Non-trainable params: 0 _________________________________________________________________
#Compile Model
model.compile(optimizer = "adam",
loss = "sparse_categorical_crossentropy",
metrics = ["accuracy"])
checkpoint_path = "checkpoint/check";
log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
cp_callbacks = tf.keras.callbacks.ModelCheckpoint(checkpoint_path, save_weights_only = True, verbose=1)
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir, histogram_freq = 1)
model.fit(train_images,
train_labels,
epochs = 50,
validation_data = (test_images, test_labels),
callbacks = [cp_callbacks, tensorboard_callback])
Epoch 1/50 1875/1875 [==============================] - 2s 903us/step - loss: 0.6136 - accuracy: 0.7925 - val_loss: 0.4780 - val_accuracy: 0.8340 Epoch 00001: saving model to checkpoint\check Epoch 2/50 1875/1875 [==============================] - 2s 852us/step - loss: 0.4451 - accuracy: 0.8446 - val_loss: 0.4908 - val_accuracy: 0.8297 Epoch 00002: saving model to checkpoint\check Epoch 3/50 1875/1875 [==============================] - 1s 757us/step - loss: 0.4159 - accuracy: 0.8541 - val_loss: 0.4713 - val_accuracy: 0.8328oss: 0 Epoch 00003: saving model to checkpoint\check Epoch 4/50 1875/1875 [==============================] - 1s 794us/step - loss: 0.4027 - accuracy: 0.8587 - val_loss: 0.4477 - val_accuracy: 0.8442 Epoch 00004: saving model to checkpoint\check Epoch 5/50 1875/1875 [==============================] - 1s 768us/step - loss: 0.3880 - accuracy: 0.8619 - val_loss: 0.4337 - val_accuracy: 0.8484 Epoch 00005: saving model to checkpoint\check Epoch 6/50 1875/1875 [==============================] - 1s 766us/step - loss: 0.3790 - accuracy: 0.8662 - val_loss: 0.4196 - val_accuracy: 0.8544 Epoch 00006: saving model to checkpoint\check Epoch 7/50 1875/1875 [==============================] - 1s 758us/step - loss: 0.3704 - accuracy: 0.8696 - val_loss: 0.4112 - val_accuracy: 0.8573 Epoch 00007: saving model to checkpoint\check Epoch 8/50 1875/1875 [==============================] - 1s 746us/step - loss: 0.3583 - accuracy: 0.8731 - val_loss: 0.4062 - val_accuracy: 0.8585 Epoch 00008: saving model to checkpoint\check Epoch 9/50 1875/1875 [==============================] - 1s 761us/step - loss: 0.3528 - accuracy: 0.8746 - val_loss: 0.4371 - val_accuracy: 0.8499 Epoch 00009: saving model to checkpoint\check Epoch 10/50 1875/1875 [==============================] - 2s 846us/step - loss: 0.3478 - accuracy: 0.8770 - val_loss: 0.4199 - val_accuracy: 0.8502 Epoch 00010: saving model to checkpoint\check Epoch 11/50 1875/1875 [==============================] - 2s 870us/step - loss: 0.3429 - accuracy: 0.8786 - val_loss: 0.4032 - val_accuracy: 0.8599 Epoch 00011: saving model to checkpoint\check Epoch 12/50 1875/1875 [==============================] - 2s 918us/step - loss: 0.3389 - accuracy: 0.8788 - val_loss: 0.4025 - val_accuracy: 0.8554 Epoch 00012: saving model to checkpoint\check Epoch 13/50 1875/1875 [==============================] - 1s 776us/step - loss: 0.3334 - accuracy: 0.8819 - val_loss: 0.3936 - val_accuracy: 0.8600 Epoch 00013: saving model to checkpoint\check Epoch 14/50 1875/1875 [==============================] - 1s 753us/step - loss: 0.3292 - accuracy: 0.8822 - val_loss: 0.4187 - val_accuracy: 0.8544 Epoch 00014: saving model to checkpoint\check Epoch 15/50 1875/1875 [==============================] - 1s 752us/step - loss: 0.3239 - accuracy: 0.8838 - val_loss: 0.3896 - val_accuracy: 0.8626 Epoch 00015: saving model to checkpoint\check Epoch 16/50 1875/1875 [==============================] - 1s 736us/step - loss: 0.3228 - accuracy: 0.8834 - val_loss: 0.3876 - val_accuracy: 0.8647 Epoch 00016: saving model to checkpoint\check Epoch 17/50 1875/1875 [==============================] - 1s 749us/step - loss: 0.3191 - accuracy: 0.8851 - val_loss: 0.4012 - val_accuracy: 0.8602 Epoch 00017: saving model to checkpoint\check Epoch 18/50 1875/1875 [==============================] - 1s 751us/step - loss: 0.3180 - accuracy: 0.8858 - val_loss: 0.3993 - val_accuracy: 0.8617 Epoch 00018: saving model to checkpoint\check Epoch 19/50 1875/1875 [==============================] - 1s 781us/step - loss: 0.3131 - accuracy: 0.8867 - val_loss: 0.4052 - val_accuracy: 0.8530 Epoch 00019: saving model to checkpoint\check Epoch 20/50 1875/1875 [==============================] - 2s 859us/step - loss: 0.3117 - accuracy: 0.8881 - val_loss: 0.4303 - val_accuracy: 0.8542 Epoch 00020: saving model to checkpoint\check Epoch 21/50 1875/1875 [==============================] - 2s 810us/step - loss: 0.3092 - accuracy: 0.8887 - val_loss: 0.3955 - val_accuracy: 0.8640 Epoch 00021: saving model to checkpoint\check Epoch 22/50 1875/1875 [==============================] - 1s 725us/step - loss: 0.3067 - accuracy: 0.8900 - val_loss: 0.3885 - val_accuracy: 0.8625 Epoch 00022: saving model to checkpoint\check Epoch 23/50 1875/1875 [==============================] - 2s 846us/step - loss: 0.3063 - accuracy: 0.8899 - val_loss: 0.3968 - val_accuracy: 0.8607 Epoch 00023: saving model to checkpoint\check Epoch 24/50 1875/1875 [==============================] - 1s 737us/step - loss: 0.3018 - accuracy: 0.8909 - val_loss: 0.4032 - val_accuracy: 0.8566 Epoch 00024: saving model to checkpoint\check Epoch 25/50 1875/1875 [==============================] - 1s 786us/step - loss: 0.3017 - accuracy: 0.8915 - val_loss: 0.4013 - val_accuracy: 0.8619 Epoch 00025: saving model to checkpoint\check Epoch 26/50 1875/1875 [==============================] - 1s 765us/step - loss: 0.2979 - accuracy: 0.8937 - val_loss: 0.3966 - val_accuracy: 0.8584 Epoch 00026: saving model to checkpoint\check Epoch 27/50 1875/1875 [==============================] - 1s 761us/step - loss: 0.2976 - accuracy: 0.8935 - val_loss: 0.3887 - val_accuracy: 0.8642 Epoch 00027: saving model to checkpoint\check Epoch 28/50 1875/1875 [==============================] - 1s 767us/step - loss: 0.2968 - accuracy: 0.8932 - val_loss: 0.3936 - val_accuracy: 0.8607 Epoch 00028: saving model to checkpoint\check Epoch 29/50 1875/1875 [==============================] - 1s 758us/step - loss: 0.2966 - accuracy: 0.8929 - val_loss: 0.4064 - val_accuracy: 0.8601 Epoch 00029: saving model to checkpoint\check Epoch 30/50 1875/1875 [==============================] - 1s 749us/step - loss: 0.2940 - accuracy: 0.8935 - val_loss: 0.3873 - val_accuracy: 0.8642 Epoch 00030: saving model to checkpoint\check Epoch 31/50 1875/1875 [==============================] - 1s 760us/step - loss: 0.2929 - accuracy: 0.8950 - val_loss: 0.4084 - val_accuracy: 0.8577 Epoch 00031: saving model to checkpoint\check Epoch 32/50 1875/1875 [==============================] - 1s 755us/step - loss: 0.2917 - accuracy: 0.8950 - val_loss: 0.4034 - val_accuracy: 0.8602cy: 0.89 Epoch 00032: saving model to checkpoint\check Epoch 33/50 1875/1875 [==============================] - 1s 736us/step - loss: 0.2893 - accuracy: 0.8957 - val_loss: 0.3931 - val_accuracy: 0.8636 Epoch 00033: saving model to checkpoint\check Epoch 34/50 1875/1875 [==============================] - 2s 890us/step - loss: 0.2888 - accuracy: 0.8960 - val_loss: 0.3954 - val_accuracy: 0.8649 Epoch 00034: saving model to checkpoint\check Epoch 35/50 1875/1875 [==============================] - 2s 807us/step - loss: 0.2867 - accuracy: 0.8972 - val_loss: 0.3921 - val_accuracy: 0.8659 Epoch 00035: saving model to checkpoint\check Epoch 36/50 1875/1875 [==============================] - 1s 785us/step - loss: 0.2852 - accuracy: 0.8976 - val_loss: 0.3977 - val_accuracy: 0.8631 Epoch 00036: saving model to checkpoint\check Epoch 37/50 1875/1875 [==============================] - 1s 767us/step - loss: 0.2853 - accuracy: 0.8976 - val_loss: 0.4045 - val_accuracy: 0.8608 Epoch 00037: saving model to checkpoint\check Epoch 38/50 1875/1875 [==============================] - 1s 761us/step - loss: 0.2835 - accuracy: 0.8972 - val_loss: 0.3992 - val_accuracy: 0.8645 Epoch 00038: saving model to checkpoint\check Epoch 39/50 1875/1875 [==============================] - 1s 752us/step - loss: 0.2823 - accuracy: 0.8982 - val_loss: 0.4206 - val_accuracy: 0.8623 Epoch 00039: saving model to checkpoint\check Epoch 40/50 1875/1875 [==============================] - 1s 742us/step - loss: 0.2824 - accuracy: 0.8971 - val_loss: 0.4101 - val_accuracy: 0.8623 Epoch 00040: saving model to checkpoint\check Epoch 41/50 1875/1875 [==============================] - 1s 761us/step - loss: 0.2808 - accuracy: 0.8994 - val_loss: 0.3983 - val_accuracy: 0.8656 Epoch 00041: saving model to checkpoint\check Epoch 42/50 1875/1875 [==============================] - 1s 753us/step - loss: 0.2790 - accuracy: 0.8991 - val_loss: 0.4062 - val_accuracy: 0.8626 Epoch 00042: saving model to checkpoint\check Epoch 43/50 1875/1875 [==============================] - 1s 752us/step - loss: 0.2785 - accuracy: 0.9000 - val_loss: 0.4057 - val_accuracy: 0.8632 Epoch 00043: saving model to checkpoint\check Epoch 44/50 1875/1875 [==============================] - 1s 791us/step - loss: 0.2782 - accuracy: 0.8982 - val_loss: 0.4067 - val_accuracy: 0.8662 Epoch 00044: saving model to checkpoint\check Epoch 45/50 1875/1875 [==============================] - 2s 819us/step - loss: 0.2766 - accuracy: 0.8991 - val_loss: 0.4191 - val_accuracy: 0.8564 Epoch 00045: saving model to checkpoint\check Epoch 46/50 1875/1875 [==============================] - 1s 754us/step - loss: 0.2766 - accuracy: 0.8996 - val_loss: 0.4080 - val_accuracy: 0.8627 Epoch 00046: saving model to checkpoint\check Epoch 47/50 1875/1875 [==============================] - 1s 766us/step - loss: 0.2763 - accuracy: 0.9008 - val_loss: 0.4079 - val_accuracy: 0.8634 Epoch 00047: saving model to checkpoint\check Epoch 48/50 1875/1875 [==============================] - 1s 748us/step - loss: 0.2749 - accuracy: 0.9008 - val_loss: 0.4159 - val_accuracy: 0.8603 Epoch 00048: saving model to checkpoint\check Epoch 49/50 1875/1875 [==============================] - 2s 803us/step - loss: 0.2742 - accuracy: 0.9003 - val_loss: 0.4190 - val_accuracy: 0.8630 Epoch 00049: saving model to checkpoint\check Epoch 50/50 1875/1875 [==============================] - 2s 836us/step - loss: 0.2735 - accuracy: 0.9017 - val_loss: 0.4096 - val_accuracy: 0.8639 Epoch 00050: saving model to checkpoint\check
<keras.callbacks.History at 0x16ee7a3abc8>
test_loss, test_acc = model.evaluate(test_images, test_labels)
print("Precition: ", test_acc)
313/313 [==============================] - 0s 533us/step - loss: 0.4096 - accuracy: 0.8639 Precition: 0.8639000058174133
predictions = model.predict(test_images)
predictions[1]
array([7.5846961e-05, 1.7605760e-17, 9.9580753e-01, 1.3488039e-18, 3.9522718e-03, 6.0178058e-29, 1.6434648e-04, 1.5591778e-31, 4.9268589e-09, 8.4349939e-35], dtype=float32)
np.argmax(predictions[0])
9
# Load picture from Internet
response = requests.get("https://www.bfgcdn.com/1500_1500_90/016-1833-0311/la-sportiva-square-t-shirt-t-shirt.jpg")
img = Image.open(BytesIO(response.content))
np_img = np.array(img)
# Show Picture
plt.figure()
plt.imshow(np_img)
plt.colorbar()
plt.grid(False)
plt.show()
# inverse picture
np_img = 255 - np_img
plt.figure()
plt.imshow(np_img)
plt.colorbar()
plt.grid(False)
plt.show()
# Remove picture color
def rgb2gray(rgb):
return np.dot(rgb[...,:3],[0.333,0.333,0.333,])
sg_image = rgb2gray(np_img)
plt.figure()
plt.imshow(sg_image)
plt.colorbar()
plt.grid(False)
plt.show()
# Resize the picture
res = cv2.resize(sg_image, dsize = (28,28),interpolation = cv2.INTER_CUBIC)
plt.figure()
plt.imshow(res)
plt.colorbar()
plt.grid(False)
plt.show()
# Rescaling picture
res = res / 255
plt.figure()
plt.imshow(res)
plt.colorbar()
plt.grid(False)
plt.show()
x = np.zeros((1,28,28))
x[0] = np.array(res)
predictions = model.predict(x)
predictions
array([[9.9637991e-01, 3.6007770e-12, 1.5886854e-04, 1.2321790e-06, 8.5336787e-09, 1.3949688e-29, 3.4600515e-03, 2.9926421e-18, 1.9868908e-10, 3.2893106e-24]], dtype=float32)
index = np.arange(len(class_names))
plt.bar(index, predictions[0])
plt.ylabel("Reliability", fontsize = 15)
plt.xticks(index, class_names, fontsize = 15, rotation = 90)
plt.title("Predictions")
plt.show()
model2 = keras.Sequential([
keras.layers.Flatten(input_shape =(28,28)),
keras.layers.Dense(15, activation = "relu"),
keras.layers.Dense(10, activation = "softmax")
])
model2.compile(optimizer = "adam", loss = "sparse_categorical_crossentropy", metrics = ["accuracy"])
loss, acc = model2.evaluate(test_images, test_labels)
313/313 [==============================] - 0s 599us/step - loss: 2.3450 - accuracy: 0.1483
# Recovery weights
model2.load_weights(checkpoint_path)
oss, acc = model2.evaluate(test_images, test_labels)
acc
313/313 [==============================] - 0s 564us/step - loss: 0.4096 - accuracy: 0.8639
0.8639000058174133