(In-depth) Machine Learning Image Classification With TensorFlow 2.0

Original article can be found here (source): Deep Learning on Medium

TensorBoard provides visual insights into the events that occur during training at each epoch.

The training visualization provided by TensorBoard is stored in a ‘runs’ folder directory. We create a function to generate a folder directory and identify each log via a timestamp.

root_logdir = os.path.join(os.curdir, "runs")def get_run_logdir():
run_id = time.strftime("run_%Y_%m_%d-%H_%M_%S")
return os.path.join(root_logdir, run_id)
run_logdir = get_run_logdir()
tensorboard_cb = keras.callbacks.TensorBoard(run_logdir)

The Model’s functional API ‘fit’ method provides the tools to train the implemented network.

Passing specific arguments into the fit function:

  • we can specify the training data used for training
  • the number of epochs we are to train the network for
  • and also validation dataset to be used to validate the performance of the network during training to unseen data.

We’ll also utilize the ‘callbacks’ argument, which in this instance, calls the TensorBoard callback created.

The default batch size within Keras when training a neural network is 32. The network is trained for a total of 60 epochs. With the utilization of early stopping, a halt to training is made once no improvement in the validation loss is recorded after 3 epochs. Early stopping can save you hours, especially in the scenario where your network begins to overfit and stops converging.

In summary, we train the model for a maximum of 60 epochs, where we feed forward all our training data in batches of 32 (batch size) through the network at each epoch.

An update is made to our network’s weights parameters after it’s seen 32 training images and labels.

The ‘fit’ method takes additional arguments that are in the official Keras documentation.

early_stopping_cb = keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=3, verbose=0, mode='auto')
model.fit(train_images, train_labels, epochs=60, validation_data=(validation_images, validation_labels), callbacks=[tensorboard_cb, early_stopping_cb])

To run TensorBoard, place the command below in your terminal, and navigate to localhost:6006.

tensorboard --logdir=runs
Training snapshot from TensorBoard

Evaluation

To evaluate a model requires feed-forwarding through the network data that hasn’t been exposed to the network during training.

Evaluation of the model before actual utilization is a good indicator of observing how well the model can generalize to unseen data.

With the evaluation results, you can decide either to fine-tune the network hyperparameters or move forward to production after observing the accuracy of the evaluation over the test dataset.

model.evaluate(test_images, test_labels)
>> 10000/10000 [==============================] - 1s 74us/sample - loss: 0.3942 - accuracy: 0.8934
[0.3942159619651735, 0.8934]