Skip to content

Federated Learning Demo in Python using Socket Programming

Notifications You must be signed in to change notification settings

ahmedfgad/FederatedLearning

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Federated Learning Demo in Python using Socket Programming

This is a demo project for applying the concepts of federated learning (FL) in Python using socket programming by building and training machine learning (ML) models using FL. The ML model is trained using PyGAD which trains ML models using the genetic algorithm (GA). The problem used to demonstrate how things work is XOR.

The project builds GUI for the server and 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.

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: The 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

Citing PyGAD - Bibtex Formatted Citation

If you used PyGAD, please consider adding a citation to the following paper about PyGAD:

@misc{gad2021pygad,
      title={PyGAD: An Intuitive Genetic Algorithm Python Library}, 
      author={Ahmed Fawzy Gad},
      year={2021},
      eprint={2106.06158},
      archivePrefix={arXiv},
      primaryClass={cs.NE}
}

Contact Us