<jupyter_start><jupyter_text>Basic classification: Classify images of clothinghttps://www.tensorflow.org/tutorials/keras/classification  Import Keras<jupyter_code># TensorFlow and tf.keras
import tensorflow as tf

# Helper libraries
import numpy as np
import matplotlib.pyplot as plt

print(tf.__version__)<jupyter_output>2.4.1<jupyter_text>Import Fashion MNIST dataset<jupyter_code>fashion_mnist = tf.keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']<jupyter_output><empty_output><jupyter_text>Data Exploration<jupyter_code>train_images.shape
len(train_labels)
train_labels<jupyter_output><empty_output><jupyter_text>Data Preprocessing<jupyter_code>plt.figure()
plt.imshow(train_images[0])
plt.colorbar()
plt.grid(False)
plt.show()<jupyter_output><empty_output><jupyter_text>Scale values to 0, 1 range normalization<jupyter_code>train_images, test_images = train_images / 255.0, test_images / 255.0<jupyter_output><empty_output><jupyter_text>Data verification, correct image format in first 25 images<jupyter_code>plt.figure(figsize=(10, 10))
for i in range(25):
    plt.subplot(5, 5, 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()<jupyter_output><empty_output><jupyter_text>Build Model<jupyter_code>model = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(28,28)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10)
])
model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>Train Model<jupyter_code>model.fit(train_images, train_labels, epochs=20)<jupyter_output>Epoch 1/20
1875/1875 [==============================] - 4s 2ms/step - loss: 0.0434 - accuracy: 0.9836
Epoch 2/20
1875/1875 [==============================] - 4s 2ms/step - loss: 0.0447 - accuracy: 0.9834
Epoch 3/20
1875/1875 [==============================] - 4s 2ms/step - loss: 0.0415 - accuracy: 0.9846
Epoch 4/20
1875/1875 [==============================] - 4s 2ms/step - loss: 0.0400 - accuracy: 0.9856
Epoch 5/20
1875/1875 [==============================] - 4s 2ms/step - loss: 0.0404 - accuracy: 0.9845
Epoch 6/20
1875/1875 [==============================] - 4s 2ms/step - loss: 0.0429 - accuracy: 0.9840
Epoch 7/20
1875/1875 [==============================] - 4s 2ms/step - loss: 0.0413 - accuracy: 0.9853
Epoch 8/20
1875/1875 [==============================] - 4s 2ms/step - loss: 0.0414 - accuracy: 0.9845
Epoch 9/20
1875/1875 [==============================] - 4s 2ms/step - loss: 0.0399 - accuracy: 0.9847
Epoch 10/20
1875/1875 [==============================] - 4s 2ms/step - loss: 0.038[...]<jupyter_text>Accuracy Evaluation<jupyter_code>test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=10)
print("\nTest Accuracy: ", test_acc, " Test Loss: ", test_loss)<jupyter_output>Test Accuracy:  0.8895999789237976  Test Loss:  0.891833484172821<jupyter_text>Make Predictions Convert the classification model to prediction model with attaching a softmax layer from logits to probabilities, which are easier to interpret.<jupyter_code>probability_model = tf.keras.Sequential([model, tf.keras.layers.Softmax()])
predictions = probability_model.predict(test_images)<jupyter_output><empty_output><jupyter_text>The model gives 10 kinds of class prediction probabilities.<jupyter_code>predictions[0]<jupyter_output><empty_output><jupyter_text>Convert probability confidence to the highest confidence value:<jupyter_code>np.argmax(predictions[0])
test_labels[0]<jupyter_output><empty_output><jupyter_text>Verify Predictions:<jupyter_code>def plot_image(i, predictions_array, true_label, img):
    true_label, img = true_label[i], img[i]
    plt.grid(False)
    plt.xticks([])
    plt.yticks([])
    
    plt.imshow(img, cmap=plt.cm.binary)
    
    predicted_label = np.argmax(predictions_array)
    if predicted_label == true_label:
        color = 'blue'
    else:
        color = 'red'
    plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
                                100*np.max(predictions_array),
                                class_names[true_label]),
                                color=color)

def plot_value_array(i, predictions_array, true_label):
    true_label = true_label[i]
    plt.grid(False)
    plt.xticks(range(10))
    plt.yticks([])
    thisplot = plt.bar(range(10), predictions_array, color="#777777")
    plt.ylim([0, 1])
    predicted_label = np.argmax(predictions_array)
    
    thisplot[predicted_label].set_color('red')
    thisplot[true_label].set_color('blue')
i = 0
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions[i], test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions[i],  test_labels)
plt.show()
i = 12
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions[i], test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions[i],  test_labels)
plt.show()
# Plot the first X test images, their predicted labels, and the true labels.
# Color correct predictions in blue and incorrect predictions in red.
num_rows = 5
num_cols = 3
num_images = num_rows*num_cols
plt.figure(figsize=(2*2*num_cols, 2*num_rows))
for i in range(num_images):
    plt.subplot(num_rows, 2*num_cols, 2*i+1)
    plot_image(i, predictions[i], test_labels, test_images)
    plt.subplot(num_rows, 2*num_cols, 2*i+2)
    plot_value_array(i, predictions[i], test_labels)
plt.tight_layout()
plt.show()<jupyter_output><empty_output><jupyter_text>Use the Trained Model<jupyter_code>img = test_images[1]
print(img.shape)
plt.figure()
plt.imshow(img, cmap=plt.cm.binary)
plt.colorbar()
plt.grid(False)
plt.show()
img = (np.expand_dims(img, 0))
plt.figure()
plt.imshow(img[0], cmap=plt.cm.binary)
plt.colorbar()
plt.grid(False)
plt.show()
predictions_single = probability_model.predict(img)
print("Prediction Result: ", class_names[np.argmax(predictions_single[0])])
plot_value_array(1, predictions_single[0], test_labels)
_ = plt.xticks(range(10), class_names, rotation = 45)<jupyter_output>Prediction Result:  Pullover
{'fraction_non_alphanumeric': 0.17746697746697745, 'fraction_numerical': 0.0560994560994561, 'mean_word_length': 5.62821833161689, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 53, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '202287', 'license': 'MIT', 'n_tokens_mistral': 2593, 'n_tokens_neox': 2319, 'n_words': 409, 'path': 'src/tensorflow_lr/images/tensorflow_basic_images_classification.ipynb', 'repo_name': 'BichengWang/python-notebook', 'size': '6435'}