Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Federated Learning Demo in Python using Socket Programming: Part 4

This is Part 4 of the federated learning (FL) demo project in Python using socket programming. In this part, a GUI is created for the server and client apps built in Part 3.

In Part 3, the user can only interact with the server and the client apps using the terminal. In Part 4, the major change is building a GUI for the client using Kivy. This has a number of benefits.

  • Easy way to manage the client application.
  • Ability to make the client available in mobile devices because Kivy supports deploying its desktop apps into mobile apps. As a result, machine learning models could be trained using federated learning by the massive private data available in mobile devices.

Similar to building a GUI for the client app, a GUI can be built for the server app using Kivy.

Project Files

The project has the following files:

  • server.py: The server Kivy app. It creates a model that is trained on the clients' devices using FL.
  • client1.py: A client Kivy app which trains the model sent by the server using just 2 samples of the XOR problem.
  • client2.py: Another client Kivy app that trains the server's model using the other 2 samples in the XOR problem.

Install PyGAD

Before running the project, the PyGAD library must be installed.

pip install pygad

For Linux and Mac, use pip3:

pip3 install pygad

Running the Project

Start the project by running the server.py file. The GUI of the server Kivy app is shown below. Follow these steps to make sure the server is running and listening for connections.

  • Click on the Create Socket button to create a socket.

  • Enter the IPv4 address and port number of the server's socket. localhost is used if both the server and the clients are running on the same machine. This is just for testing purposes. Practically, they run on different machines. Thus, the user need to specify the IPv4 address (e.g. 192.168.1.4).

  • Click on the Bind Socket button to bind the create socket to the entered IPv4 address and port number.

  • Click on the Listen to Connections button to start listening and accepting incoming connections. Each connected device receives the current model to be trained by its local data. Once the model is trained, then no more models will be sent to the connected devices.

Fig01

After running the server, next is to run one or more clients. The project creates 2 clients but you can add more. The only expected change among the different clients is the data being used for training the model sent by the server.

For client1.py, here is the training data (2 samples of the XOR problem):

# Preparing the NumPy array of the inputs.
data_inputs = numpy.array([[0, 1],
                           [0, 0]])

# Preparing the NumPy array of the outputs.
data_outputs = numpy.array([1, 
                            0])

Here is the training data (other 2 samples of the XOR problem) for the other client (client2.py):

# Preparing the NumPy array of the inputs.
data_inputs = numpy.array([[1, 0],
                           [1, 1]])

# Preparing the NumPy array of the outputs.
data_outputs = numpy.array([1, 
                            0])

Just run any client and a GUI will appear like that. You can either run the client at a desktop or a mobile device.

Follow these steps to run the client:

  • Click on the Create Socket button to create a socket.

  • Enter the IPv4 address and port number of the server's socket. If both the client and the server are running on the same machine, just use localhost for the IPv4 address. Otherwise, specify the IPv4 address (e.g. 192.168.1.4).s

  • Click on the Connect to Server button to create a TCP connection with the server.

  • Click on the Receive & Train Model button to ask the server to send its current ML model. The model will be trained by the client's local private data. The updated model will be sent back to the server. Once the model is trained, the message Model is Trained will appear.

Fig03

Download APKs

The links for the APK files of both the server and the client Android apps are given below:

For More Information

There are a number of resources to get started with federated learning and Kivy.

This tutorial describes the pipeline of training a machine learning model using federated learning.

Even that federated learning does not disclose the private user data, there are some cases in which the privacy of federated learning can be broken.

This tutorial titled Python for Android: Start Building Kivy Cross-Platform Applications covers the steps for creating an Android app out of the Kivy app.

Kivy-Tutorial

To get started with Kivy app development and how to built Android apps out of the Kivy app, check the book titled Building Android Apps in Python Using Kivy with Android Studio

kivy-book

Contact Us