Federated Learning:Bringing Machine Learning to the edge with Kotlin and Android (Reading Notes)

3 minute read


With the promulgation of the General Data Protection Regulation, users are becoming more aware of their data values and privacy concerns. While anonymous technology can greatly solve the problem of privacy security, the way in which all data is sent to the central processor to train the machine learning model is always the cause of data security concerns.

Selected from the Medium Blog
Author: Jose Corbacho


🔗Android Application:https://github.com/mccorby/PhotoLabeller
🔗The Server:https://github.com/mccorby/PhotoLabellerServer


The project consists of three main components:

  • The server, written by Kotlin, uses DL4J to generate a model based on the Cifar-10 dataset.
  • An Android app that uses this model to classify camera images, written by Kotlin and also uses DL4J.
  • The federated learning environment enables the Android app to use local data to train the model, and its server can update the shared model using edge updates.


The model is based on the Cifar-10 dataset, which classifies images from ten different categories.

The model chosen is a shallow convolutional neural network with a CNN layer and a dense layer. We used 50 neurons and 10,000 samples to get good performance. The code for the server-side training model is located in the model of PhotoLabellerServerproject.

When connected to a server that uses the latest version of the shared model, the app allows for basic categorization of photos taken by the user using the camera using models embedded in the app itself.

The app is built by modules, including Android-specific categories and categories related to the Deeplearning4j trainer. The basic module includes interactors and domain objects. The purpose of the trainer application includes making predictions and training with DL4J, and calling the prediction function to obtain image classification.

Federated Learning

Federated learning reverses the update of machine learning models by allowing edge devices to participate in training. Instead of sending data from the client to a centralized location, federated learning sends the model parameters to the participating devices in an encrypted manner. Then use the local data to retrain the model user’s data without leaving the device including mobile phones, laptops, IoT gadgets, and more. The server opens “loop training” during which the client can send updates to the parameters to the server.

Client - edge training

The Android app will decide when to participate in the training of the shared model. The updated parameters of the model completion are then sent to the server.

Server - aggregation and update model

Once the loop training is over, the server updates the shared model via the Federated Averaging algorithm, as shown in the following figure:

When using the Android app for any image processing operation, the calculation amount of the device is required to be large enough. Training images with images will require them to increase the number of calculations. This also means that the process of migrating learning in an Android application will be fast. Most apps are able to complete training before the memory is exhausted. The total amount of parameters is about 450k, which is very sufficient for the app.