<jupyter_start><jupyter_text>Image Classification using Fashion-MNIST image data<jupyter_code>import tensorflow as tf
from tensorflow import keras
print(tf.__version__)

import numpy as np
import matplotlib.pyplot as plt<jupyter_output>2.6.0<jupyter_text>Load image dataFashion-MNIST is a dataset of Zalando's article images consisting of a training set of 60,000 examples and a test set of 10,000 examples. Each example is a 28x28 grayscale image, associated with a label from 10 classes.https://www.tensorflow.org/datasets/catalog/fashion_mnisthttps://github.com/zalandoresearch/fashion-mnist<jupyter_code>fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()<jupyter_output>Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz
32768/29515 [=================================] - 0s 0us/step
40960/29515 [=========================================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz
26427392/26421880 [==============================] - 0s 0us/step
26435584/26421880 [==============================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz
16384/5148 [===============================================================================================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz
4423680/4422102 [==============================] - 0s 0us/step
4431872/4422102 [==============================] - 0s 0us/step<jupyter_text>Pixel values are between 0 and 255, 0 being black and 255 white (grey-scale image).Check the loaded data - Image<jupyter_code>print(train_images.shape)
print(train_images[0,:])
plt.figure()
plt.imshow(train_images[36])
plt.colorbar()
plt.grid(False)
plt.show()<jupyter_output><empty_output><jupyter_text>Labels are integers from 0 to 9.  Each represents a specific article of clothing.Check the loaded data - Label<jupyter_code>train_labels[:12]  # First 12 training labels<jupyter_output><empty_output><jupyter_text>Define class names<jupyter_code>class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']<jupyter_output><empty_output><jupyter_text>Data Preprocessing<jupyter_code>train_images = train_images / 255.0
test_images = test_images / 255.0

train_images = train_images.astype('float32')
test_images = test_images.astype('float32')

# Reshape the array to 4-dims so that it can work with the Keras API
train_images = train_images.reshape(train_images.shape[0], 28, 28, 1)
test_images = test_images.reshape(test_images.shape[0], 28, 28, 1)
input_shape = (28, 28, 1)<jupyter_output><empty_output><jupyter_text>Build the Model<jupyter_code>model = keras.Sequential([
    keras.layers.Conv2D(6, kernel_size=(5,5), padding='same', input_shape=input_shape),
    keras.layers.MaxPooling2D(pool_size=(2,2)),
    keras.layers.Conv2D(16, kernel_size=(5,5)),
    keras.layers.MaxPooling2D(pool_size=(2,2)),
    keras.layers.Flatten(),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dense(64, activation='relu'),
    keras.layers.Dropout(0.2),
    keras.layers.Dense(10, activation='softmax')
])
model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 28, 28, 6)         156       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 14, 14, 6)         0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 10, 10, 16)        2416      
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 5, 5, 16)          0         
_________________________________________________________________
flatten (Flatten)            (None, 400)               0         
_________________________________________________________________
dense (Dense)                (None, 128)               51328     
________________________________________________________[...]<jupyter_text>Compile the Model<jupyter_code>model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>Train the Model<jupyter_code>model.fit(train_images, train_labels, epochs=30)<jupyter_output>Epoch 1/30
1875/1875 [==============================] - 38s 20ms/step - loss: 0.3182 - accuracy: 0.8825
Epoch 2/30
1875/1875 [==============================] - 39s 21ms/step - loss: 0.3073 - accuracy: 0.8863
Epoch 3/30
1875/1875 [==============================] - 39s 21ms/step - loss: 0.2959 - accuracy: 0.8904
Epoch 4/30
1875/1875 [==============================] - 39s 21ms/step - loss: 0.2864 - accuracy: 0.8932
Epoch 5/30
1875/1875 [==============================] - 39s 21ms/step - loss: 0.2755 - accuracy: 0.8969
Epoch 6/30
1875/1875 [==============================] - 39s 21ms/step - loss: 0.2687 - accuracy: 0.8999
Epoch 7/30
1875/1875 [==============================] - 39s 21ms/step - loss: 0.2604 - accuracy: 0.9015
Epoch 8/30
1875/1875 [==============================] - 39s 21ms/step - loss: 0.2513 - accuracy: 0.9060
Epoch 9/30
1875/1875 [==============================] - 39s 21ms/step - loss: 0.2468 - accuracy: 0.9075
Epoch 10/30
1875/1875 [==============================] - 39s 21m[...]<jupyter_text>Evaluate the Model<jupyter_code>test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=1)
print('Test Accuracy:', test_acc)<jupyter_output>313/313 [==============================] - 3s 10ms/step - loss: 0.3507 - accuracy: 0.9010
Test Accuracy: 0.9010000228881836<jupyter_text>Make a Prediction<jupyter_code>predictions = model.predict(test_images)
print(predictions)
# predictions = model.predict([test_images[0]])

print(np.argmax(predictions[101]))
print(class_names[np.argmax(predictions[101])])

plt.figure()
plt.imshow(test_images[101].reshape(28,28))
plt.colorbar()
plt.grid(False)
plt.show()
COLOR = 'white'
plt.rcParams['text.color'] = COLOR
plt.rcParams['axes.labelcolor'] = COLOR 

def show_image(img, label, guess):
  img = img.reshape(28,28)
  plt.figure()
  plt.imshow(img, cmap=plt.cm.binary)
  # plt.title("Corrected label: " + label)
  # plt.xlabel("Guessed label: " + guess)
  plt.colorbar()
  plt.grid(False)
  plt.show()
  print("Correct label: " + label)
  print("Guessed label: " + guess)

def predict(model, image, correct_label):
  class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
  prediction = model.predict(np.array([image]))
  predicted_class = class_names[np.argmax(prediction)]
  show_image(image, class_names[correct_label], predicted_class)

def get_number():
  while True:
    num = input("Choose a number: ")
    if num.isdigit():
      num = int(num)
      if 0<=num<=1000:
        return int(num)
    else:
      print("Try again...")

num = get_number()
image = test_images[num]
label = test_labels[num]
predict(model, image, label)<jupyter_output>Choose a number: 888
{'fraction_non_alphanumeric': 0.20855183541750705, 'fraction_numerical': 0.07503025413473174, 'mean_word_length': 5.308736217133164, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 39, 'https://': 6, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '507779', 'license': 'MIT', 'n_tokens_mistral': 2826, 'n_tokens_neox': 2381, 'n_words': 471, 'path': 'CNN_Examples/fashion_mnist.ipynb', 'repo_name': 'cathyXie08/Deep-Learning-Course-Examples', 'size': '7437'}