Posted on:

15 Nov 2022

0

Numbers not showing in the tensorboard image

Dear 365 Data Science Team,



When I use tensorboard for some reason I dont see the CM numbers on the image "Check below picture" before and after I tried to decrease my accuracy.
However, it is still showing the error even if I entered the commands in CMD but its printing.
Also, I have more than 3 check boxes in addition to train, val, and CM. What is the issue here

image




import io
import itertools
import sklearn
from sklearn import metrics
import tensorflow as tf
import tensorflow_datasets as tfds
import matplotlib.pyplot as plt
import numpy as np
import datetime








Downloading the data and preprocessing the data

In [448]:







BUFFER_SIZE = 100
BATCH_SIZE = 128
NUM_EPOCHS = 20







In [449]:








mnist_dataset, mnist_info = tfds.load(name='mnist', with_info = True, as_supervised=True)







In [450]:








mnist_train, mnist_test = mnist_dataset['train'], mnist_dataset['test']







In [451]:








def scale(image, label):
    image=tf.cast(image, tf.float32)
    image /= 255.

    return image,label







In [452]:








train_and_validation_data = mnist_train.map(scale)
test_data = mnist_test.map(scale)







In [453]:








num_validation_samples = 0.1 * mnist_info.splits['train'].num_examples
num_validation_samples = tf.cast(num_validation_samples, tf.int64)







In [454]:








num_test_samples = mnist_info.splits['test'].num_examples
num_test_samples = tf.cast(num_test_samples, tf.int64)







In [455]:








train_and_validation_data = train_and_validation_data.shuffle(BUFFER_SIZE)







In [456]:

train_data = train_and_validation_data.skip(num_validation_samples)
validation_data = train_and_validation_data.take(num_validation_samples)



In [457]:

train_data = train_data.batch(BATCH_SIZE)
validation_data = validation_data.batch(num_validation_samples)
test_data = test_data.batch(num_test_samples)





In [458]:


for images, labels in validation_data:
    images_val = images.numpy()
    labels_val = labels.numpy()






Creating the model and training it

In [459]:

model = tf.keras.Sequential([
    tf.keras.layers.Conv2D(50, 5, activation='relu', input_shape=(28,28,1)),
    tf.keras.layers.MaxPooling2D(pool_size=(2,2)),
    tf.keras.layers.Conv2D(50, 3, activation='relu'),
    tf.keras.layers.MaxPooling2D(pool_size=(2,2)),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(10)
##Why no Softmax function? because in some cases we might have mess up our calculations
    #So its better to incorporate the softmax into the loss function itself
])



In [460]:

model.summary(line_length=75)






Model: "sequential_18"
___________________________________________________________________________
 Layer (type)                    Output Shape                  Param #
===========================================================================
 conv2d_35 (Conv2D)              (None, 24, 24, 50)            1300

 max_pooling2d_33 (MaxPooling2D)  (None, 12, 12, 50)           0

 conv2d_36 (Conv2D)              (None, 10, 10, 50)            22550

 max_pooling2d_34 (MaxPooling2D)  (None, 5, 5, 50)             0

 flatten_17 (Flatten)            (None, 1250)                  0

 dense_18 (Dense)                (None, 10)                    12510

===========================================================================
Total params: 36,360
Trainable params: 36,360
Non-trainable params: 0
___________________________________________________________________________



In [461]:








loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)







In [462]:

model.compile(optimizer='Adadelta', loss=loss_fn, metrics=['accuracy'])



In [463]:


log_dir = 'logs\\fit\\'+ "run-1"





In [464]:


def plot_confusion_matrix(cm, class_names):
    figure = plt.figure(figsize=(12,12))
    plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)
    plt.title("Confusion matrix")
    plt.colorbar()
    tick_marks = np.arange(len(class_names))
    plt.xticks(tick_marks, class_names, rotation=45)
    plt.yticks(tick_marks, class_names)
    #Normalize the confusion matrix
    cm=np.around(cm.astype('float') / cm.sum(axis=1)[:, np.newaxis], decimals=2)
    #Use white text if squares are dark; otherwise black
    threshold = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        color = 'white' if cm[i,j] > threshold else "black"
        plt.text(j, i, cm[i,j], horizontalalignment='center', color=color)
        plt.tight_layout()
        plt.ylabel('True label')
        plt.xlabel('Predicted label')
        return figure





In [465]:




def plot_to_image(figure):
    """Converts the matplotlib plot specified by 'figre' to a PNG image and returns it
    The supplied figure is closed and inaccessible after this call"""

    #Save the plot to a PNG in memory
    buf = io.BytesIO()
    plt.savefig(buf, format='png')

    #Closing figure prevents it from being displayed directly inside the notebook
    plt.close(figure)

    buf.seek(0)

    #Convert PNG buffer to TF image
    image = tf.image.decode_png(buf.getvalue(), channels=4)

    #Add the batch dimension
    image= tf.expand_dims(image,0)

    return image







In [466]:








#Define a file writer variable for logging purposes
file_writer_cm = tf.summary.create_file_writer(log_dir + '/cm')

def log_confusion_matrix(epoch, logs):
    #Use the model to predcit the values from validation dataset
    test_pred_raw = model.predict(images_val)
    test_pred = np.argmax(test_pred_raw, axis=1)

    #Calculate the confusion matrix
    cm = sklearn.metrics.confusion_matrix(labels_val, test_pred)

    #Log the confusion matrix as an image summary
    figure = plot_confusion_matrix(cm, class_names=['0','1','2','3','4','5','6','7','8','9'])
    cm_image = plot_to_image(figure)

    #Log the confusion matrix as an image summary
    with file_writer_cm.as_default():
        tf.summary.image("Confusion Matrix", cm_image, step=epoch)







In [467]:


cm_callback = tf.keras.callbacks.LambdaCallback(on_epoch_end=log_confusion_matrix)
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1, profile_batch=0)







In [468]:








early_stopping= tf.keras.callbacks.EarlyStopping(
    monitor='val_loss',
    mode = 'auto',
    min_delta = 0,
    patience = 2,
    verbose = 0,
    restore_best_weights = True
)







In [469]:








model.fit(
    train_data,
    epochs= NUM_EPOCHS,
    callbacks = [tensorboard_callback, cm_callback, early_stopping],
    validation_data = validation_data,
    verbose = 2
)






Epoch 1/20
188/188 [==============================] - 1s 4ms/step
422/422 - 21s - loss: 2.3151 - accuracy: 0.0837 - val_loss: 2.3088 - val_accuracy: 0.0893 - 21s/epoch - 50ms/step
Epoch 2/20
188/188 [==============================] - 1s 4ms/step
422/422 - 20s - loss: 2.2996 - accuracy: 0.0991 - val_loss: 2.2932 - val_accuracy: 0.1018 - 20s/epoch - 47ms/step
Epoch 3/20
188/188 [==============================] - 1s 4ms/step
422/422 - 20s - loss: 2.2840 - accuracy: 0.1186 - val_loss: 2.2776 - val_accuracy: 0.1205 - 20s/epoch - 47ms/step
Epoch 4/20
188/188 [==============================] - 1s 4ms/step
422/422 - 20s - loss: 2.2681 - accuracy: 0.1490 - val_loss: 2.2613 - val_accuracy: 0.1617 - 20s/epoch - 48ms/step
Epoch 5/20
188/188 [==============================] - 1s 4ms/step
422/422 - 20s - loss: 2.2513 - accuracy: 0.1976 - val_loss: 2.2441 - val_accuracy: 0.2220 - 20s/epoch - 47ms/step
Epoch 6/20
188/188 [==============================] - 1s 4ms/step
422/422 - 20s - loss: 2.2335 - accuracy: 0.2607 - val_loss: 2.2254 - val_accuracy: 0.2837 - 20s/epoch - 48ms/step
Epoch 7/20
188/188 [==============================] - 1s 4ms/step
422/422 - 20s - loss: 2.2140 - accuracy: 0.3336 - val_loss: 2.2049 - val_accuracy: 0.3633 - 20s/epoch - 47ms/step
Epoch 8/20
188/188 [==============================] - 1s 4ms/step
422/422 - 20s - loss: 2.1923 - accuracy: 0.4089 - val_loss: 2.1820 - val_accuracy: 0.4372 - 20s/epoch - 48ms/step
Epoch 9/20
188/188 [==============================] - 1s 4ms/step
422/422 - 20s - loss: 2.1680 - accuracy: 0.4753 - val_loss: 2.1563 - val_accuracy: 0.4988 - 20s/epoch - 47ms/step
Epoch 10/20
188/188 [==============================] - 1s 4ms/step
422/422 - 20s - loss: 2.1405 - accuracy: 0.5290 - val_loss: 2.1268 - val_accuracy: 0.5503 - 20s/epoch - 47ms/step
Epoch 11/20
188/188 [==============================] - 1s 4ms/step
422/422 - 20s - loss: 2.1091 - accuracy: 0.5700 - val_loss: 2.0931 - val_accuracy: 0.5852 - 20s/epoch - 48ms/step
Epoch 12/20
188/188 [==============================] - 1s 4ms/step
422/422 - 20s - loss: 2.0733 - accuracy: 0.6020 - val_loss: 2.0551 - val_accuracy: 0.6112 - 20s/epoch - 48ms/step
Epoch 13/20
188/188 [==============================] - 1s 4ms/step
422/422 - 20s - loss: 2.0330 - accuracy: 0.6267 - val_loss: 2.0124 - val_accuracy: 0.6345 - 20s/epoch - 48ms/step
Epoch 14/20
188/188 [==============================] - 1s 4ms/step
422/422 - 20s - loss: 1.9874 - accuracy: 0.6484 - val_loss: 1.9640 - val_accuracy: 0.6507 - 20s/epoch - 48ms/step
Epoch 15/20
188/188 [==============================] - 1s 4ms/step
422/422 - 20s - loss: 1.9365 - accuracy: 0.6654 - val_loss: 1.9099 - val_accuracy: 0.6657 - 20s/epoch - 47ms/step
Epoch 16/20
188/188 [==============================] - 1s 4ms/step
422/422 - 20s - loss: 1.8799 - accuracy: 0.6782 - val_loss: 1.8506 - val_accuracy: 0.6817 - 20s/epoch - 48ms/step
Epoch 17/20
188/188 [==============================] - 1s 4ms/step
422/422 - 20s - loss: 1.8178 - accuracy: 0.6891 - val_loss: 1.7855 - val_accuracy: 0.6940 - 20s/epoch - 48ms/step
Epoch 18/20
188/188 [==============================] - 1s 4ms/step
422/422 - 20s - loss: 1.7502 - accuracy: 0.6999 - val_loss: 1.7152 - val_accuracy: 0.7020 - 20s/epoch - 48ms/step
Epoch 19/20
188/188 [==============================] - 1s 4ms/step
422/422 - 21s - loss: 1.6783 - accuracy: 0.7096 - val_loss: 1.6412 - val_accuracy: 0.7085 - 21s/epoch - 49ms/step
Epoch 20/20
188/188 [==============================] - 1s 4ms/step
422/422 - 20s - loss: 1.6039 - accuracy: 0.7182 - val_loss: 1.5674 - val_accuracy: 0.7203 - 20s/epoch - 48ms/step

Out[469]:

<keras.callbacks.History at 0x23b39bb21f0>


Visualizing in Tensorboard

In [470]:


%load_ext tensorboard
%tensorboard --logdir "logs\fit"
0 answers ( 0 marked as helpful)

Submit an answer