How to detect a lung infection (Pneumonia, COVID-19) with Google Teachable Machine?

Google Teachable machine

Teachable Machine is an online tool, created by Google Creative Labs, that makes creating machine learning models fast, easy and accessible to everyone.

Teachable Machine is based on Transfer Learning. This technique takes an already trained model and specializes it for a new task. This allows the model to be trained much faster and with less data than if it had to be trained from scratch. The new task can be to classify more or less mature bananas (Bananameter) as well as to detect a type of lung infection.

Teachable Machine is based on the already trained MobileNet model on the ImageNet dataset. MobileNets are low latency, low power convolutional neural networks (CNNs) that can be used for classification. To make a quick comparison in terms of size, the size of the full VGG16 network on disk is about 553 megabytes. The size of one of the largest current MobileNets is about 17 megabytes. Because of their small size, they are considered excellent deep learning models for use on mobile devices.

Detecting a lung infection with Google Teachable Machine

Step 1: Load the images that will allow to train the model. Creation of 4 categories: viral pneumonia, bacterial pneumonia, covid-19, no pathology. For each category, the number of images is 133.

The images are uploaded from the computer files by drag and drop
Creation of 4 categories: viral pneumonia, bacterial pneumonia, covid-19, no pathology

Step 2: Choose the parameters in the “Advanced” section that will optimize the training of the model and click on “Train Model”.

Epoch — Like humans, Artificial Intelligence learns over time and its intelligence increases as researchers continually feed the model training data. An epoch means that the model has been fed once with all the data.

Learning rate —In machine learning, the learning rate is a tuning parameter that determines the step size at each iteration while moving towards a minimum of a loss function.

  • Batch size — The batch size defines the number of samples that will be propagated through the network. For example, let’s say you have 1050 training samples and you want to set the batch size equal to 100. The algorithm takes the first 100 samples (1st to 100th) from the training data set and trains the network. Then it takes the other 100 samples (101st to 200th) and trains the network again.
Adjustment of parameters

Step 3: Once the model is trained, ‘model trained’ is displayed and by clicking on the “Under the hood” button, you can evaluate the performance of the trained model. Accuracy per epoch, Loss per epoch, Confusion Matrix, Accuracy per class.

We note that for the trained model the accuracy for viral pneumonia is lower than for the other classes.

Accuracy et Loss per epoch
Accuracy per class et Confusion Matrix

The model seems to have inferior performances for the classification of this pathology. Indeed, in 6 cases out of 20, the model will predict a viral pneumonia instead of a bacterial pneumonia. It might be necessary to improve the training of this model by increasing the number of images of each class and by changing the training parameters of the model.

Step 4: Make predictions using new images that have not been used to train or validate model performance.

To make a prediction, you just have to load by drag and drop the radio that will allow the model to make the prediction.

  • A first prediction is made exactly (output covid-19 = 100%) by the model from a covid-19 x-ray.
  • A second prediction is made accurately (output healthy patient = 98%) by the model from an x-ray without lung infection.
  • A third prediction is made incorrectly (healthy patient output = 86%) by the model from an X-ray of a viral pneumonia.
  • A fourth prediction is made erroneously by the model (output covid-19 = 60%) by the model from an X-ray of a viral pneumonia. The predictions made from the X-rays of viral pneumonia confirm the 65% accuracy on this class of data.
  • At last prediction shows that the model is still able to predict a viral pneumonia from an X-ray of a patient with viral pneumonia.
Prediction made from a covid-19 x-ray
input = covid-19 radio / model prediction = 100% covid-19
input = X-ray of a patient without pathology / model prediction = 98% healthy patient, 2% viral pneumonia
input = X-ray of a patient with viral pneumonia / model prediction = 86% healthy patient, 14% viral pneumonia
input = X-ray of a patient with viral pneumonia / model prediction = patient covid at 60%, viral pneumonia 39%
input = radio of viral pneumonia / model prediction = 100% viral pneumonia

Step 5: Once improved in an iterative way (more images, change of training parameters) this model can be exported in a tensorflow.js, tensorflow or tensorflow lite format in order to be integrated in a website (see the Coding Train videos in appendix), in a mobile interface or to be integrated with Raspberry Pi (see the Teachable Sorter project in appendix).

How to use Teachable Machine Tutorials

Teachable Machine on the Coding Train : Image and sound classification

Diplodocus interested in the applications of artificial intelligence to healthcare. Twitter : @