<filename>1-basic-classification/basic_classification.py<gh_stars>0
# TensorFlow & Keras
import tensorflow as tf
from tensorflow import keras

# Numpy & Matplot
import numpy as np
import matplotlib.pyplot as plt

# Version >= 1.12.0
print(tf.__version__)

# Download fashion dataset
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

# Classifications
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

# Interesting print statements
print(train_images.shape) # (60000, 28, 28)
print(len(train_labels))  # 60000
print(train_labels)       # array([9, 0, 0, ..., 3, 0, 5], dtype=uint8)
print(test_images.shape)  # (10000, 28, 28)
print(len(test_labels))   # 10000

# Preprocess data
plt.figure()
plt.imshow(train_images[0])
plt.colorbar()
plt.grid(False)

# View plot
# plt.show()

# Consistent preprocessing
train_images = train_images / 255.0
test_images = test_images / 255.0

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]])

# View plot
# plt.show()

# Setup the layers
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation=tf.nn.relu),
    keras.layers.Dense(128, activation=tf.nn.relu), # We deep learning now, dawg
    keras.layers.Dense(10, activation=tf.nn.softmax)
])

# Compile the model
model.compile(optimizer=tf.train.AdamOptimizer(),
        loss='sparse_categorical_crossentropy',
        metrics=['accuracy'])

# Train the model
model.fit(train_images, train_labels, epochs=5)

# Evaluate accuracy
test_loss, test_acc = model.evaluate(test_images, test_labels)

print('Test accuracy:', test_acc)

# Make predictions
predictions = model.predict(test_images)

print(predictions[0]) # array of 10 numbers representing the confidence in each label

print(np.argmax(predictions[0])) # 9 -> most confident in label 9 (ankle boot), which we can check...
print(test_labels[0]) # also 9...

## Fancy prediction graphs
def plot_image(i, predictions_array, true_label, img):
    predictions_array, true_label, img = predictions_array[i], 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 = '#26b0ff'
    else:
        color = '#b7b0ff'

    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):
    predictions_array, true_label = predictions_array[i], true_label[i]
    plt.grid(False)
    plt.xticks([])
    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('#b7b0ff')
    thisplot[true_label].set_color('#26b0ff')

## The famous Ankle boot
i = 0
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions, test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions, test_labels)

# View our fancy Ankle boot graphic
# plt.show() # Editor's note: this seems to also render all previous plt graphs.

## The infamous ~bag~ sandal sneaker
i = 12 # This seems redundant, DRY it up?
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions, test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions, test_labels)

# View our terrible prediction
# plt.show()

## Once more, with feeling
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, test_labels, test_images)
    plt.subplot(num_rows, 2*num_cols, 2*i+2)
    plot_value_array(i, predictions, test_labels)

# View best graphic yet
#plt.show()

## Finally, predict a single image
# Grab an image from the test dataset
i = 0 # I heard you like useless assignments to `i`, so here's another on the house
img = test_images[i]

print(img.shape) # (28, 28)

# Convert image to batch of 1 (optimizations expect batch as params)
img = (np.expand_dims(img,0))

print(img.shape) # (1, 28, 28)

# Predict the image
predictions_single = model.predict(img)

print(predictions_single) # Array of prediction values for 10 labels...

plt.figure(figsize=(6,3))
plot_value_array(0, predictions_single, test_labels)
plt.xticks(range(10), class_names, rotation=45)
plt.show()

print(np.argmax(predictions_single[0])) # 9

# Fin.

<filename>1-basic-classification/basic_classification.py<gh_stars>0
# TensorFlow & Keras
import tensorflow as tf
from tensorflow import keras

# Numpy & Matplot
import numpy as np
import matplotlib.pyplot as plt

# Version >= 1.12.0
print(tf.__version__)

# Download fashion dataset
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

# Classifications
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

# Interesting print statements
print(train_images.shape) # (60000, 28, 28)
print(len(train_labels))  # 60000
print(train_labels)       # array([9, 0, 0, ..., 3, 0, 5], dtype=uint8)
print(test_images.shape)  # (10000, 28, 28)
print(len(test_labels))   # 10000

# Preprocess data
plt.figure()
plt.imshow(train_images[0])
plt.colorbar()
plt.grid(False)

# View plot
# plt.show()

# Consistent preprocessing
train_images = train_images / 255.0
test_images = test_images / 255.0

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]])

# View plot
# plt.show()

# Setup the layers
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation=tf.nn.relu),
    keras.layers.Dense(128, activation=tf.nn.relu), # We deep learning now, dawg
    keras.layers.Dense(10, activation=tf.nn.softmax)
])

# Compile the model
model.compile(optimizer=tf.train.AdamOptimizer(),
        loss='sparse_categorical_crossentropy',
        metrics=['accuracy'])

# Train the model
model.fit(train_images, train_labels, epochs=5)

# Evaluate accuracy
test_loss, test_acc = model.evaluate(test_images, test_labels)

print('Test accuracy:', test_acc)

# Make predictions
predictions = model.predict(test_images)

print(predictions[0]) # array of 10 numbers representing the confidence in each label

print(np.argmax(predictions[0])) # 9 -> most confident in label 9 (ankle boot), which we can check...
print(test_labels[0]) # also 9...

## Fancy prediction graphs
def plot_image(i, predictions_array, true_label, img):
    predictions_array, true_label, img = predictions_array[i], 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 = '#26b0ff'
    else:
        color = '#b7b0ff'

    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):
    predictions_array, true_label = predictions_array[i], true_label[i]
    plt.grid(False)
    plt.xticks([])
    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('#b7b0ff')
    thisplot[true_label].set_color('#26b0ff')

## The famous Ankle boot
i = 0
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions, test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions, test_labels)

# View our fancy Ankle boot graphic
# plt.show() # Editor's note: this seems to also render all previous plt graphs.

## The infamous ~bag~ sandal sneaker
i = 12 # This seems redundant, DRY it up?
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions, test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions, test_labels)

# View our terrible prediction
# plt.show()

## Once more, with feeling
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, test_labels, test_images)
    plt.subplot(num_rows, 2*num_cols, 2*i+2)
    plot_value_array(i, predictions, test_labels)

# View best graphic yet
#plt.show()

## Finally, predict a single image
# Grab an image from the test dataset
i = 0 # I heard you like useless assignments to `i`, so here's another on the house
img = test_images[i]

print(img.shape) # (28, 28)

# Convert image to batch of 1 (optimizations expect batch as params)
img = (np.expand_dims(img,0))

print(img.shape) # (1, 28, 28)

# Predict the image
predictions_single = model.predict(img)

print(predictions_single) # Array of prediction values for 10 labels...

plt.figure(figsize=(6,3))
plot_value_array(0, predictions_single, test_labels)
plt.xticks(range(10), class_names, rotation=45)
plt.show()

print(np.argmax(predictions_single[0])) # 9

# Fin.

# TensorFlow & Keras # Numpy & Matplot # Version >= 1.12.0 # Download fashion dataset # Classifications # Interesting print statements # (60000, 28, 28) # 60000 # array([9, 0, 0, ..., 3, 0, 5], dtype=uint8) # (10000, 28, 28) # 10000 # Preprocess data # View plot # plt.show() # Consistent preprocessing # View plot # plt.show() # Setup the layers # We deep learning now, dawg # Compile the model # Train the model # Evaluate accuracy # Make predictions # array of 10 numbers representing the confidence in each label # 9 -> most confident in label 9 (ankle boot), which we can check... # also 9... ## Fancy prediction graphs ## The famous Ankle boot # View our fancy Ankle boot graphic # plt.show() # Editor's note: this seems to also render all previous plt graphs. ## The infamous ~bag~ sandal sneaker # This seems redundant, DRY it up? # View our terrible prediction # plt.show() ## Once more, with feeling # View best graphic yet #plt.show() ## Finally, predict a single image # Grab an image from the test dataset # I heard you like useless assignments to `i`, so here's another on the house # (28, 28) # Convert image to batch of 1 (optimizations expect batch as params) # (1, 28, 28) # Predict the image # Array of prediction values for 10 labels... # 9 # Fin.