TensorFlow2 — MobileNets: A Pre-trained Multiclass Classifier

MobileNets are lightweight deep neural networks that are suitable for mobile and embedded vision applications (Howard et al., 2017).

Figure 1: MobileNets in mobile devices. Photograph: iStock

Computer Vision (CV) engineers often utilize transfer learning techniques to perform CV tasks with pre-trained convolutional neural networks (CNN).

Keras Fashion-MNIST is a toy dataset which consists of images of clothing. The data has a training set of 60,000 28x28 grayscale images of 10 fashion categories, along with a testing set of 10,000 images. A dictionary key is defined below with the corresponding class name for each of the integer label values.

Data augmentation

In order to prevent model overfitting problem and help the model generalization. we use Keras “ImageDataGenerator” class to augment the training examples.

data augmentation

Figure 2 shows a set of augmented 128x128 grayscale images.

Figure 2: a set of augmented training examples

Modelling

We build our mobile_net model on MobileNets with pre-trained weights from ImageNet excluding the top layer. We develop a customized input layer so that our model will accept gray scale images of any size. After the base model’s last layer, we add a global_average_pooling2d layer and a Dense layer with 10 softmax functions as the output layer.

define mobile networks

Model training

We select Adam optimizer with a learning rate of 0.001 to minimize the “categorical_crossentropy” loss. Data from generator flow is directly fitted into the model during model training.

train the model
Results:

The best validation accuracy is 95.12%.

After 50 epochs training, the model achieves the best validation accuracy: 95% .

Making predictions

To make predictions on the test set, we can first load the best-trained model that has been automatically saved as the “mobile_net.h5” file by the “checkpointer” callback function (see the “train the model” code block). Then, we use the model to predict a batch of test images.

The notebook is accessible at this link.

References

Howard, A., Zhu, M., Chen, B., Kalenichenko, D., Wang, W., Weyand, T., Andreetto, M. and Adam, H., 2017. Mobilenets: Efficient Convolutional Neural Networks For Mobile Vision Applications. [online] arXiv.org. Available at: <https://arxiv.org/abs/1704.04861> [Accessed 31 May 2020].

--

--