Computer Vision — Petals to the Metal🌻🌸🌹

Umar Farooq
8 min readSep 28, 2021

--

How to Build a Flower Species Classification MobileNetV2 Model using TPU?

https://www.kaggleusercontent.com/kf/74221952/

What’s the species of Flowers? How can we identify the nature of different flowers? How can we recognize which type of flower most people relish? Apparently, the Deep Learning Classification models help us accurately classify the flower species from different 104 flowers type. Testify How TPU (Tensor Processing Unit) accelerates the distribution strategy of Tensorflow?

https://gesneriads.info/genera/primulina-bipinnatifida/

This species has similar foliage to that of P. Maciejewski, and the two species can be difficult to distinguish out of flower. The inflorescences and flowers, while similar, have several points of difference, including color and pedicel length, as can be seen in this comparison photo.

In Petals to the Metal — Flower Classification on TPU, Kaggle competition, I completed a challenge to build a machine learning model to classify 104 types of flowers based on their images. In this tutorial notebook, you’ll learn how to build an image classifier in Keras and train it on a Tensor Processing Unit (TPU). Although, you’ll have a complete project you can build off of with ideas of your own.

Expected Output from TPU Flower Classification

Essential Points to Improve Classification Model Accuracy

To improve the classification accuracy of the model on the test dataset, the following are explored:

  1. Input image size
  2. Pretrained model and number of trainable parameters of the final model
  3. Data augmentation
  4. Regularization techniques
  5. Use of learning rate schedule

Steps To Be Followed:

Step 0: Import Libraries

We begin this notebook by importing useful analytics libraries, in which we import statistical, data visualization, and militating overfitting libraries along with Tensorflow and Keras.

Importing Libraries

Step 1: Distribution Strategy

A TPU has eight different cores, and each of these cores acts as its own accelerator. (A TPU is a sort of like having eight GPUs in one machine.) We tell TensorFlow how to make use of all these cores at once through a distribution strategy. Would you please run the following cell to create the distribution strategy that we’ll later apply to our model?

What TPUClusterResolver() does?

TPUs are network-connected accelerators, and you must first locate them on the network. In TPUStrategy, the main objective is to contain the necessary distributed training code that will work on TPUs with their 8 compute cores. Whenever you use the TPUStrategy by instantiating your model in the scope of the strategy, this creates the model on the TPU. The model size is constrained by the TPU RAM only, not by the amount of memory available on the VM running your Python code. Model creation and model training use the usual Keras APIs. Further, read about TPUClusterResolver() and Kaggle TPU Doc.

Appropriate distribution strategy snippets

We’ll use the distribution strategy when we create our neural network model. Then, TensorFlow will distribute the training among the eight TPU cores by creating eight different replicas of the model, one for each core.

Step 2: Loading The Competition Data

Get GCS Path

When used with TPUs, datasets need to be stored in a Google Cloud Storage bucket. You can use data from any public GCS bucket by giving its path just like you would data from ‘/Kaggle/input.’ The following will retrieve the GCS path for this competition’s dataset.

You can use data from any public dataset here on Kaggle in just the same way. If you’d like to use data from one of your private datasets, see here.

Step 3: Loading Data (Setting up the parameters)

When used with TPUs, datasets are often serialized into TFRecords. This is a format convenient for distributing data to each of the TPUs cores. We’ve hidden the cell that reads the TFRecords for our dataset since the process is a bit long. You could come back to it later for some guidance on using your own datasets with TPUs.

TPU’s is basically used to allocate the larger models having huge training inputs and batches, equipped with up to 128GB of high-speed memory allocation. In this notebook, we used an images dataset with a pixel size of 512 x 512px and saw how TPU v3–8 handles it.

  • num_parallel_reads=AUTO is used to read multiple files automatically.
  • experimental_deterministic = False, we used “experimental_deterministic” to maintain the order of the data. Here, we disable the enforcement order to shuffle the data anyway.

Tuning the Additional Flower Data

I have to use the external flower dataset with the helping material from Dmitry’s and Araik’s notebook to increase data proficiency. To visit the notebook to a better understanding of the Ensemble learning and augmentation of the external dataset.

Data Augmentation

This tutorial demonstrates data augmentation: a technique to increase the diversity of your training set by applying random (but realistic) transformations such as image rotation. Read more

TensorFlow Addons is a repository of contributions that conform to well-established API patterns but implement new functionality not available in core TensorFlow. TensorFlow natively supports a large number of operators, layers, metrics, losses, and optimizers. Readout more

Step4: Data Pipelines

To make TPU faster, increase the Batch Size

These datasets are tf.data.Dataset objects. You can think about a dataset in TensorFlow as a stream of data records. The training and validation sets are streams of (image, label) pairs.

Step5: Data Exploration

Image Analysis with or without Augmentation

Original versus w/ Random Augmentation

Original versus w/ Random Augmentation

UnBatch the Training Data

Training Dataset

Step6: Data Augmentation Sample

The snippet of Data Augmentation
Data Augmentation sample of a single image

Data Augmentation Sample V2 (Implementing Image Processing)

The snippet of mapping the Data Augmentation image

Data Augmentation Sample V3 (Implementing Image Processing)

Step7: Defining The Model

Now we’re ready to create a neural network for classifying images! We’ll use what’s known as transfer learning. You reuse part of a pre-trained model with transfer learning to get a head-start on a new dataset.

For this tutorial, we’ll use a model called VGG16 pre-trained on ImageNet). Later, you might want to experiment with other models included with Keras. Xception wouldn’t be a bad choice.

The distribution strategy we created earlier contains a context manager, strategy.scope. This context manager tells TensorFlow how to divide the work of training among the eight TPU cores. When using TensorFlow with a TPU, it’s important to define your model in a strategy.scope() context.

To keep track of the model performance and find out the best suitable model through model-monitoring instance.

Important: How to track learning rate during model training?

Note: Stochastic gradient descent is an optimization algorithm that estimates the error gradient for the model's current state using examples from the training dataset, then updates the model's weights using the back-propagation of errors algorithm, referred to as simply backpropagation. The weights' amount is updated during training is referred to as the step size or the “learning rate.” Specifically, the learning rate is a configurable hyperparameter used in neural network training with a small positive value, often in the range between 0.0 and 1.0. For more information, review the article of Jason Brownlee, “How to Configure the Learning Rate When Training Deep Learning Neural Networks.”

Track learning rate during Training NotFoundError: Container worker does not exist. (Could not find a resource: worker/_AnonymousVar8064) Encountered when executing an operation using EagerExecutor. This error cancels all future operations and poisons their output tensors.

Tuning Custom Callbacks

Calculate the Weight of each Flower Class

Ensemble Learning Sample Code

Model Summary

model.summary()

tf.keras.utils.plot_model(model, show_shapes=True)

Step8: Model Training

Customize learning rate scheduler

we have to train this model using a custom learning rate schedule.

if not ensemble_learning_models:
history = model.fit(
ds_train,
validation_data=ds_valid,
epochs=EPOCHS,
steps_per_epoch=STEPS_PER_EPOCH,
callbacks=[lr_callback, checkpoint],
class_weight = weight_per_class #tuning11
)

Visualizing Model Performance

Functions used to known the training model performances:

  1. Loss
  2. Metrics
if not ensemble_learning_models:
display_training_curves_v2(
history.history['loss'],
history.history['val_loss'],
history.history['lr'],
'loss',
211,
)

display_training_curves_v2(
history.history['sparse_categorical_accuracy'],
history.history['val_sparse_categorical_accuracy'],
history.history['lr'],
'accuracy',
212,
)

Deploy the model on mobile and IoT

To deploy the models into IoT and mobile devices, we need to convert the .h5 into Tensorflow lite.

#if ensemble_learning_models:
test_ds = get_test_dataset(ordered=True)
#best_alpha = 0.35
print('Computing predictions...')
test_images_ds = test_ds.map(lambda image, idnum: image)
probabilities = model.predict(test_images_ds)
predictions = np.argmax(probabilities, axis=-1)
print(predictions)


print('Generating submission.csv file...')
# Get image ids from test set and convert to unicode
test_ids_ds = test_ds.map(lambda image, idnum: idnum).unbatch()
test_ids = next(iter(test_ids_ds.batch(NUM_TEST_IMAGES))).numpy().astype('U')

# Write the submission file
np.savetxt('submission.csv', np.rec.fromarrays([test_ids, predictions]), fmt=['%s', '%d'], delimiter=',', header='id,label', comments='')
# Look at the first few predictions

Model Performance Report

Models’ Prediction

Submission

!head submission.csv

For further details, please review my Kaggle Notebook, Github, and contact me through social contacts for discussion.

If you want to keep working to improve your performance, select the blue Edit button in the top right of the screen on Kaggle Notebook. Then you can change your code and repeat the process. There’s a lot of room to improve, and you will climb up the leaderboard as you work.

How to Build a Classification model of Flower Species through Ensemble Learning?

How to improve the model performance by using different Deep Learning models and reduce the Gradient Descent & Loss?

--

--

Umar Farooq

Passionate about learning how machines predict the future | Computer Vision Enthusiast | Healthcare Analyst