# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + id="JoD8Onofbspl" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 143} outputId="d4ec550e-d3f7-45fc-a506-b11f7d9bb148" executionInfo={"status": "ok", "timestamp": 1576429067918, "user_tz": -120, "elapsed": 42338, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "16586829010922884016"}}
# Install and import TF2
# !pip install -q tensorflow==2.0.0
import tensorflow as tf
print(tf.__version__)

# + id="fuaTssW-b9gO" colab_type="code" colab={}
# Additional Imports
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.layers import Input,Conv2D,Dense,Flatten,Dropout
from tensorflow.keras.models import Model

# + id="gnlW_uCCcUHm" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 179} outputId="d343f922-b306-44f3-9167-3d6705939f10" executionInfo={"status": "ok", "timestamp": 1576429073009, "user_tz": -120, "elapsed": 1921, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "16586829010922884016"}}
# Load in the data
fashion_mnist = tf.keras.datasets.fashion_mnist

(x_train,y_train),(x_test,y_test) = fashion_mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
print('x_train.shape:', x_train.shape)

# + id="fvkzM14fdHkb" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 35} outputId="5fb1c029-47a3-4ec0-dc55-a33365af2054" executionInfo={"status": "ok", "timestamp": 1576429076481, "user_tz": -120, "elapsed": 668, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "16586829010922884016"}}
# the data is only 2d
# convolutional layer expects HxWxC
x_train = np.expand_dims(x_train, -1)
x_test = np.expand_dims(x_test, -1)
print(x_train.shape)

# + id="bFfVfAGndlBX" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 35} outputId="367648d8-8cd0-4f76-c034-24446c4e7eb5" executionInfo={"status": "ok", "timestamp": 1576429078441, "user_tz": -120, "elapsed": 680, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "16586829010922884016"}}
# number of classes (find number of unique elements using set)
K = len(set(y_train))
print('number of classes: ',K)

# + id="PWIb5Dydd0sJ" colab_type="code" colab={}
# build the model using the functional API
i = Input(shape=x_train[0].shape)
x = Conv2D(32, (3,3), strides=2, activation='relu')(i)
x = Conv2D(64, (3,3), strides=2, activation='relu')(x)
x = Conv2D(128, (3,3), strides=2, activation='relu')(x)
x = Flatten()(x)
x = Dropout(0.2)(x)
x = Dense(512, activation='relu')(x)
x = Dropout(0.2)(x)
x = Dense(K, activation='softmax')(x)

model = Model(i,x)


# + id="eubC4Sc-ezSD" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 595} outputId="6c1951ee-5792-454f-be8e-3ee30ce41b08" executionInfo={"status": "ok", "timestamp": 1576429736210, "user_tz": -120, "elapsed": 610022, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "16586829010922884016"}}
# Compile and fit
# note: make sure we are using a GPU enabled notebook for this
model.compile(optimizer='adam',
  loss='sparse_categorical_crossentropy',
  metrics=['accuracy'])
r = model.fit(x_train,y_train, validation_data=(x_test,y_test), epochs=15)

# + id="r7p3O3HIgJNb" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 283} outputId="5b7f49f3-0df0-4219-aade-fe82cc8d19b1" executionInfo={"status": "ok", "timestamp": 1576429775807, "user_tz": -120, "elapsed": 848, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "16586829010922884016"}}
# Plot loss per iteration
plt.plot(r.history['loss'], label='loss')
plt.plot(r.history['val_loss'], label='val_loss')
plt.legend()

# + id="xgDvLTyZgakH" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 283} outputId="e8df473f-4848-4ff4-ad26-280acc409c20" executionInfo={"status": "ok", "timestamp": 1576429784588, "user_tz": -120, "elapsed": 791, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "16586829010922884016"}}
# Plot accuracy per iteration
plt.plot(r.history['accuracy'], label='accuracy')
plt.plot(r.history['val_accuracy'], label='val_accuracy')
plt.legend()

# + id="Rv2DNRAUhGkl" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 509} outputId="c874bf5d-3b03-4e8c-c732-db4e7a077b8c" executionInfo={"status": "ok", "timestamp": 1576429790800, "user_tz": -120, "elapsed": 3675, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "16586829010922884016"}}
# Plot confusion matrix
from sklearn.metrics import confusion_matrix
import numpy as np
import itertools

def plot_confusion_matrix(cm,classes,normalize=False,title='Confustion matrix',cmap=plt.cm.Blues):
  ###
  # This function prints and plots the confustion matrix
  # Normalization can be applied by setting 'normalize=True' 
  ###
  if normalize:
    cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
    print('Normalized confusion matrix')
  else:
    print('Confustion matrix, without normalization')
    print(cm)
    plt.imshow(cm, interpolation='nearest',cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks,classes,rotation=45)
    plt.yticks(tick_marks,classes)

    fmt = '.2f' if normalize else 'd'
    thresh = cm.max()  / 2.
    for i, j in itertools.product(range(cm.shape[0]),range(cm.shape[1])):
      plt.text(j,i, format(cm[i,j],fmt),
        horizontalalignment='center',
        color='white' if cm[i,j] > thresh else 'black')
    
    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
    plt.show()

p_test = model.predict(x_test).argmax(axis=1)
cm = confusion_matrix(y_test,p_test)
plot_confusion_matrix(cm,list(range(10)))

# + id="fTYAICJThSiO" colab_type="code" colab={}
# label mapping
labels = '''T-shirt/top
Trouser
Pullover
Dress
Coat
Sandal
Shirt
Sneaker
Bag
Ankle boot'''.split()

# + id="37jIe5LShlni" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 281} outputId="df419b04-0ea3-42d3-eebb-6f8bf8cd9e80" executionInfo={"status": "ok", "timestamp": 1576429890740, "user_tz": -120, "elapsed": 519, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "16586829010922884016"}}
# Show some misclassified examples

misclassified_idx = np.where(p_test != y_test)[0]
i = np.random.choice(misclassified_idx)
plt.imshow(x_test[i].reshape(28,28),cmap='gray')
plt.title('True label: %s Predicted: %s' % (labels[y_test[i]],labels[p_test[i]]));

# + id="YT1pN4OEicca" colab_type="code" colab={}


StarcoderdataJupyterScriptsDedupFiltered