fully_connected

Fully connected NN

Open In Colab

In [1]:
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
In [2]:
fashion_mnist = 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']
In [3]:
train_images.shape
Out[3]:
(60000, 28, 28)
In [4]:
test_images.shape
Out[4]:
(10000, 28, 28)
In [5]:
train_images = train_images / 255.0 -0.5

test_images = test_images / 255.0 -0.5
In [6]:
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()

Build a NN model

keras.Sequential: the simplest "one-after-the-other" layer architecture

In [7]:
model = keras.Sequential([
    # keras.layers.Input(shape=(28,28)),
    keras.layers.Flatten(input_shape=(28,28)),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dense(10, activation='softmax')
])
WARNING:tensorflow:From c:\users\chech\appdata\local\programs\python\cv_course_venv\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py:435: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.

model.compile()

  • Loss function —This measures how accurate the model is during training. You want to minimize this function to "steer" the model in the right direction.
  • Optimizer —This is how the model is updated based on the data it sees and its loss function.
  • Metrics —Used to monitor the training and testing steps. The following example uses accuracy, the fraction of the images that are correctly classified.
In [8]:
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
In [9]:
model.fit(train_images, train_labels, epochs=5, batch_size=32)
Epoch 1/5
60000/60000 [==============================] - 4s 73us/sample - loss: 0.4839 - acc: 0.8262
Epoch 2/5
60000/60000 [==============================] - 4s 68us/sample - loss: 0.3775 - acc: 0.8622
Epoch 3/5
60000/60000 [==============================] - 5s 79us/sample - loss: 0.3380 - acc: 0.8759
Epoch 4/5
60000/60000 [==============================] - 4s 71us/sample - loss: 0.3142 - acc: 0.8850
Epoch 5/5
60000/60000 [==============================] - 4s 72us/sample - loss: 0.2952 - acc: 0.8901
Out[9]:
<tensorflow.python.keras.callbacks.History at 0x27ac9360eb8>
In [10]:
test_loss, test_acc = model.evaluate(test_images,  test_labels)
              
print('\nTest accuracy:', test_acc)
10000/10000 [==============================] - 0s 40us/sample - loss: 0.3574 - acc: 0.8740

Test accuracy: 0.874
In [11]:
predictions = model.predict(test_images)
In [12]:
predictions[0]
Out[12]:
array([1.7985272e-07, 4.6142952e-09, 1.5989988e-08, 1.3690409e-07,
       7.0981878e-07, 4.4175517e-02, 5.1331915e-07, 2.7903259e-02,
       8.2744409e-05, 9.2783695e-01], dtype=float32)
In [ ]:
 
In [13]:
def plot_image(i, predictions_array, true_label, img):
    predictions_array, true_label, img = predictions_array, 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):
    predictions_array, true_label = predictions_array, 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')
In [14]:
# 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()
In [15]:
img = test_images[1]

print(img.shape)
try:
    predictions_single = model.predict(img)
except:
    print("not working...")
(28, 28)
not working...
In [16]:
# Add the image to a batch where it's the only member.
img = (np.expand_dims(img,0))

print(img.shape)

predictions_single = model.predict(img)

print(predictions_single)
(1, 28, 28)
[[1.6265686e-04 5.4133618e-11 9.9831414e-01 6.2368560e-10 1.4762873e-03
  6.3300407e-12 4.6898636e-05 3.8099099e-18 1.0440819e-10 7.3795787e-16]]
In [ ]: