federated-learning-framework


Namefederated-learning-framework JSON
Version 0.0.61 PyPI version JSON
download
home_pagehttps://github.com/mehrdaddjavadi/federated_learning_framework
SummaryA modular and extensible framework for federated learning applications.
upload_time2024-07-31 14:10:30
maintainerNone
docs_urlNone
authorMehrdad Javadi
requires_python>=3.6
licenseNone
keywords federated learning machine learning deep learning active learning encryption homomorphic encryption
VCS
bugtrack_url
requirements numpy tensorflow websockets pytest tenseal
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Federated Learning Framework

## Overview

Welcome to the Federated Learning Framework, a modular and extensible solution for implementing federated learning across various applications. Harness the power of collective intelligence, ensure data privacy with homomorphic encryption, and apply it to domains like NLP, autonomous vehicles, drones, and more.

## Features

- **Modular and Extensible**: Easily customizable for different machine learning and deep learning applications.
- **Secure**: Utilizes homomorphic encryption to ensure data privacy.
- **Active Learning**: Incorporates active learning strategies to improve model performance.
- **Flexible Communication**: Supports various connection methods including socket programming.
- **Customizable**: Users can edit and control every part of the framework with various functions.

## Potential Applications

### Healthcare

Federated learning can be used to train models on patient data from multiple hospitals without sharing sensitive information. This approach can improve medical diagnostics and treatment recommendations while preserving patient privacy.

### Autonomous Vehicles

By collecting and learning from data across multiple autonomous vehicles, the framework can help improve the safety and performance of self-driving cars without exposing individual vehicle data.

### Drones

Drones can use federated learning to share and learn from data collected during their operations, enhancing their navigation, object detection, and other capabilities while ensuring data security.

### Natural Language Processing (NLP)

Federated learning can be applied to train NLP models on data from multiple sources, such as user devices, to improve language understanding and generation without compromising user privacy.

### Finance

Financial institutions can use federated learning to develop fraud detection and risk management models by leveraging data from multiple sources while keeping customer data secure.

### Smart Homes and IoT Devices

IoT devices in smart homes can collaboratively learn from user interactions to optimize performance and provide better services without sharing raw data.

## Detailed Component Description

### Central Server

**File:** `central_server.py`

The central server orchestrates the federated learning process by coordinating the communication and aggregation of model weights from various client devices.

**Key Functions:**

- `run_server`: Starts the server to handle client connections.
- `handle_client`: Manages incoming messages from clients.
- `transmit_weights`: Broadcasts the aggregated weights to clients.
- `send_data_to_client`: Sends specific data to a client.
- `get_data_from_client`: Requests and receives data from a client.
- `query_active_learning`: Implements active learning strategies to select data for labeling.

### Client Device

**File:** `client_device.py`

Client devices perform local training on their datasets and communicate with the central server.

**Key Functions:**

- `connect_to_central_server`: Connects to the central server.
- `federated_learning`: Coordinates local training and communication with the server.
- `receive_weights`: Receives model weights from the central server.
- `send_weights`: Sends model weights to the central server.
- `receive_data`: Receives data from the central server.

### Encryption

**File:** `encryption.py`

Provides functions for creating encryption contexts and encrypting/decrypting model weights.

**Key Functions:**

- `create_context`: Sets up the encryption context using TenSEAL.
- `encrypt_weights`: Encrypts model weights.
- `decrypt_weights`: Decrypts encrypted model weights.

### Active Learning

**File:** `active_learning.py`

Implements active learning strategies to enhance the training process by selectively querying informative data points.

**Key Functions:**

- `select_informative_samples`: Selects samples for labeling based on uncertainty.

### Connection

**File:** `connection.py`

Manages the connection types and protocols (e.g., WebSocket) for communication between the central server and client devices.

**Key Functions:**

- `run_server`: Starts a WebSocket server.
- `connect_to_server`: Establishes a WebSocket connection to the server.

### Decorators

**File:** `decorators.py`

Provides decorators for adding federated learning and encryption functionalities to functions.

**Key Functions:**

- `federated_learning_decorator`: Wraps a function to enable federated learning.
- `encryption_decorator`: Wraps a function to enable homomorphic encryption.

### Utilities

**File:** `utils.py`

Includes utility functions used throughout the framework.

## Installation

Clone the repository:

```sh
git clone https://github.com/mehrdaddjavadi/federated_learning_framework.git
```

Navigate to the directory:

```sh
cd federated_learning_framework
```

Install the dependencies:

```sh
pip install -r requirements.txt
```

## Usage

### Setting Up the Central Server

```python
import asyncio
from federated_learning_framework.central_server import CentralServer

async def main():
    server = CentralServer()
    await server.run_server()

asyncio.run(main())
```

### Setting Up the Central Server On Interactive Environment Like Jupyter Notebook

```python
import nest_asyncio
import asyncio
from federated_learning_framework.central_server import CentralServer

nest_asyncio.apply()

async def main():
    server = CentralServer()
    await server.run_server()

# If running in an environment with an existing event loop
if __name__ == "__main__":
    asyncio.run(main())
```

### Setting Up a Client Device

```python
import asyncio
import tensorflow as tf
from federated_learning_framework.client_device import ClientDevice
from federated_learning_framework.encryption import create_context

# Define your model
model = tf.keras.Sequential([
    tf.keras.layers.Dense(4, activation='relu', input_shape=(3072,)),
    tf.keras.layers.Dense(10, activation='softmax')
])

# Create context for encryption
context = create_context()

# Initialize the client device
client = ClientDevice(client_id=1, model=model, context=context)

async def main():
    uri = "ws://localhost:8089"
    await client.connect_to_central_server(uri)
    x_train, y_train = ...  # Load your training data
    await client.federated_learning(uri, x_train, y_train)
    # Optionally receive data from central server
    data = await client.receive_data()
    print(f"Received data: {data}")

asyncio.run(main())
```

### Sample Execution Script Using Decorators For Interactive Environments Like Colab And Jupyter Notebook

```python
import asyncio
import tensorflow as tf
import numpy as np
from federated_learning_framework.client_device import ClientDevice
from federated_learning_framework.central_server import CentralServer
from federated_learning_framework.encryption import create_context
from federated_learning_framework.models.tensorflow_model import TensorFlowModel

# Setup logging
import logging
logging.basicConfig(level=logging.INFO)

# Define a simple TensorFlow model
model = tf.keras.Sequential([
    tf.keras.layers.Dense(4, activation='relu', input_shape=(3072,)),
    tf.keras.layers.Dense(10, activation='softmax')
])
wrapped_model = TensorFlowModel(model)

# Create encryption context
context = create_context()

# Initialize server and clients
central_server = CentralServer(context=context)
client1 = ClientDevice(client_id=1, model=wrapped_model, context=context)
client2 = ClientDevice(client_id=2, model=wrapped_model, context=context)

# Dummy training data
x_train = np.random.rand(10, 3072)
y_train = np.random.randint(0, 10, 10)

async def main():
    await asyncio.gather(
        central_server.run_server(),
        client1.connect_to_central_server("ws://localhost:8089"),
        client2.connect_to_central_server("ws://localhost:8089"),
        client1.federated_learning(x_train, y_train),
        client2.federated_learning(x_train, y_train)
    )

asyncio.run(main())

```

### Using Decorators

```python
import asyncio
import tensorflow as tf
from federated_learning_framework.decorators import federated_learning_decorator, encryption_decorator
from federated_learning_framework.client_device import ClientDevice
from federated_learning_framework.encryption import create_context

# Create context for encryption
context = create_context()

# Define your model
model = tf.keras.Sequential([
    tf.keras.layers.Dense(4, activation='relu', input_shape=(3072,)),
    tf.keras.layers.Dense(10, activation='softmax')
])

@federated_learning_decorator(uri="ws://localhost:8089")
@encryption_decorator(context=context)
async def main():
    client = ClientDevice(client_id=1, model=model, context=context)
    await client.connect_to_central_server('ws://localhost:8089')
    x_train, y_train = ...  # Load your training data
    await client.federated_learning('ws://localhost:8089', x_train, y_train)

asyncio.run(main())
```

## Running Tests

To run the tests, execute the following command in the root directory:

```sh
python -m unittest discover -s tests
```

## License

The usage of this library is free for academic work with proper referencing. For business, governmental, and any other types of usage, please contact me directly. All rights are reserved.

**Contact:** mehrdaddjavadi@gmail.com

## Contributing

Feel free to contribute by submitting a pull request or opening an issue.

```

Copy and paste this into your README.md file. This format provides a clear, organized structure and includes all necessary details and instructions for potential users and contributors.
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/mehrdaddjavadi/federated_learning_framework",
    "name": "federated-learning-framework",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": null,
    "keywords": "federated learning, machine learning, deep learning, active learning, encryption, homomorphic encryption",
    "author": "Mehrdad Javadi",
    "author_email": "mehrdaddjavadi@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/44/e1/9dd1f2b0f90deec71828accaaa43e718a8393ee2a36b1ddb2a6b3bdd9234/federated_learning_framework-0.0.61.tar.gz",
    "platform": null,
    "description": "# Federated Learning Framework\r\n\r\n## Overview\r\n\r\nWelcome to the Federated Learning Framework, a modular and extensible solution for implementing federated learning across various applications. Harness the power of collective intelligence, ensure data privacy with homomorphic encryption, and apply it to domains like NLP, autonomous vehicles, drones, and more.\r\n\r\n## Features\r\n\r\n- **Modular and Extensible**: Easily customizable for different machine learning and deep learning applications.\r\n- **Secure**: Utilizes homomorphic encryption to ensure data privacy.\r\n- **Active Learning**: Incorporates active learning strategies to improve model performance.\r\n- **Flexible Communication**: Supports various connection methods including socket programming.\r\n- **Customizable**: Users can edit and control every part of the framework with various functions.\r\n\r\n## Potential Applications\r\n\r\n### Healthcare\r\n\r\nFederated learning can be used to train models on patient data from multiple hospitals without sharing sensitive information. This approach can improve medical diagnostics and treatment recommendations while preserving patient privacy.\r\n\r\n### Autonomous Vehicles\r\n\r\nBy collecting and learning from data across multiple autonomous vehicles, the framework can help improve the safety and performance of self-driving cars without exposing individual vehicle data.\r\n\r\n### Drones\r\n\r\nDrones can use federated learning to share and learn from data collected during their operations, enhancing their navigation, object detection, and other capabilities while ensuring data security.\r\n\r\n### Natural Language Processing (NLP)\r\n\r\nFederated learning can be applied to train NLP models on data from multiple sources, such as user devices, to improve language understanding and generation without compromising user privacy.\r\n\r\n### Finance\r\n\r\nFinancial institutions can use federated learning to develop fraud detection and risk management models by leveraging data from multiple sources while keeping customer data secure.\r\n\r\n### Smart Homes and IoT Devices\r\n\r\nIoT devices in smart homes can collaboratively learn from user interactions to optimize performance and provide better services without sharing raw data.\r\n\r\n## Detailed Component Description\r\n\r\n### Central Server\r\n\r\n**File:** `central_server.py`\r\n\r\nThe central server orchestrates the federated learning process by coordinating the communication and aggregation of model weights from various client devices.\r\n\r\n**Key Functions:**\r\n\r\n- `run_server`: Starts the server to handle client connections.\r\n- `handle_client`: Manages incoming messages from clients.\r\n- `transmit_weights`: Broadcasts the aggregated weights to clients.\r\n- `send_data_to_client`: Sends specific data to a client.\r\n- `get_data_from_client`: Requests and receives data from a client.\r\n- `query_active_learning`: Implements active learning strategies to select data for labeling.\r\n\r\n### Client Device\r\n\r\n**File:** `client_device.py`\r\n\r\nClient devices perform local training on their datasets and communicate with the central server.\r\n\r\n**Key Functions:**\r\n\r\n- `connect_to_central_server`: Connects to the central server.\r\n- `federated_learning`: Coordinates local training and communication with the server.\r\n- `receive_weights`: Receives model weights from the central server.\r\n- `send_weights`: Sends model weights to the central server.\r\n- `receive_data`: Receives data from the central server.\r\n\r\n### Encryption\r\n\r\n**File:** `encryption.py`\r\n\r\nProvides functions for creating encryption contexts and encrypting/decrypting model weights.\r\n\r\n**Key Functions:**\r\n\r\n- `create_context`: Sets up the encryption context using TenSEAL.\r\n- `encrypt_weights`: Encrypts model weights.\r\n- `decrypt_weights`: Decrypts encrypted model weights.\r\n\r\n### Active Learning\r\n\r\n**File:** `active_learning.py`\r\n\r\nImplements active learning strategies to enhance the training process by selectively querying informative data points.\r\n\r\n**Key Functions:**\r\n\r\n- `select_informative_samples`: Selects samples for labeling based on uncertainty.\r\n\r\n### Connection\r\n\r\n**File:** `connection.py`\r\n\r\nManages the connection types and protocols (e.g., WebSocket) for communication between the central server and client devices.\r\n\r\n**Key Functions:**\r\n\r\n- `run_server`: Starts a WebSocket server.\r\n- `connect_to_server`: Establishes a WebSocket connection to the server.\r\n\r\n### Decorators\r\n\r\n**File:** `decorators.py`\r\n\r\nProvides decorators for adding federated learning and encryption functionalities to functions.\r\n\r\n**Key Functions:**\r\n\r\n- `federated_learning_decorator`: Wraps a function to enable federated learning.\r\n- `encryption_decorator`: Wraps a function to enable homomorphic encryption.\r\n\r\n### Utilities\r\n\r\n**File:** `utils.py`\r\n\r\nIncludes utility functions used throughout the framework.\r\n\r\n## Installation\r\n\r\nClone the repository:\r\n\r\n```sh\r\ngit clone https://github.com/mehrdaddjavadi/federated_learning_framework.git\r\n```\r\n\r\nNavigate to the directory:\r\n\r\n```sh\r\ncd federated_learning_framework\r\n```\r\n\r\nInstall the dependencies:\r\n\r\n```sh\r\npip install -r requirements.txt\r\n```\r\n\r\n## Usage\r\n\r\n### Setting Up the Central Server\r\n\r\n```python\r\nimport asyncio\r\nfrom federated_learning_framework.central_server import CentralServer\r\n\r\nasync def main():\r\n    server = CentralServer()\r\n    await server.run_server()\r\n\r\nasyncio.run(main())\r\n```\r\n\r\n### Setting Up the Central Server On Interactive Environment Like Jupyter Notebook\r\n\r\n```python\r\nimport nest_asyncio\r\nimport asyncio\r\nfrom federated_learning_framework.central_server import CentralServer\r\n\r\nnest_asyncio.apply()\r\n\r\nasync def main():\r\n    server = CentralServer()\r\n    await server.run_server()\r\n\r\n# If running in an environment with an existing event loop\r\nif __name__ == \"__main__\":\r\n    asyncio.run(main())\r\n```\r\n\r\n### Setting Up a Client Device\r\n\r\n```python\r\nimport asyncio\r\nimport tensorflow as tf\r\nfrom federated_learning_framework.client_device import ClientDevice\r\nfrom federated_learning_framework.encryption import create_context\r\n\r\n# Define your model\r\nmodel = tf.keras.Sequential([\r\n    tf.keras.layers.Dense(4, activation='relu', input_shape=(3072,)),\r\n    tf.keras.layers.Dense(10, activation='softmax')\r\n])\r\n\r\n# Create context for encryption\r\ncontext = create_context()\r\n\r\n# Initialize the client device\r\nclient = ClientDevice(client_id=1, model=model, context=context)\r\n\r\nasync def main():\r\n    uri = \"ws://localhost:8089\"\r\n    await client.connect_to_central_server(uri)\r\n    x_train, y_train = ...  # Load your training data\r\n    await client.federated_learning(uri, x_train, y_train)\r\n    # Optionally receive data from central server\r\n    data = await client.receive_data()\r\n    print(f\"Received data: {data}\")\r\n\r\nasyncio.run(main())\r\n```\r\n\r\n### Sample Execution Script Using Decorators For Interactive Environments Like Colab And Jupyter Notebook\r\n\r\n```python\r\nimport asyncio\r\nimport tensorflow as tf\r\nimport numpy as np\r\nfrom federated_learning_framework.client_device import ClientDevice\r\nfrom federated_learning_framework.central_server import CentralServer\r\nfrom federated_learning_framework.encryption import create_context\r\nfrom federated_learning_framework.models.tensorflow_model import TensorFlowModel\r\n\r\n# Setup logging\r\nimport logging\r\nlogging.basicConfig(level=logging.INFO)\r\n\r\n# Define a simple TensorFlow model\r\nmodel = tf.keras.Sequential([\r\n    tf.keras.layers.Dense(4, activation='relu', input_shape=(3072,)),\r\n    tf.keras.layers.Dense(10, activation='softmax')\r\n])\r\nwrapped_model = TensorFlowModel(model)\r\n\r\n# Create encryption context\r\ncontext = create_context()\r\n\r\n# Initialize server and clients\r\ncentral_server = CentralServer(context=context)\r\nclient1 = ClientDevice(client_id=1, model=wrapped_model, context=context)\r\nclient2 = ClientDevice(client_id=2, model=wrapped_model, context=context)\r\n\r\n# Dummy training data\r\nx_train = np.random.rand(10, 3072)\r\ny_train = np.random.randint(0, 10, 10)\r\n\r\nasync def main():\r\n    await asyncio.gather(\r\n        central_server.run_server(),\r\n        client1.connect_to_central_server(\"ws://localhost:8089\"),\r\n        client2.connect_to_central_server(\"ws://localhost:8089\"),\r\n        client1.federated_learning(x_train, y_train),\r\n        client2.federated_learning(x_train, y_train)\r\n    )\r\n\r\nasyncio.run(main())\r\n\r\n```\r\n\r\n### Using Decorators\r\n\r\n```python\r\nimport asyncio\r\nimport tensorflow as tf\r\nfrom federated_learning_framework.decorators import federated_learning_decorator, encryption_decorator\r\nfrom federated_learning_framework.client_device import ClientDevice\r\nfrom federated_learning_framework.encryption import create_context\r\n\r\n# Create context for encryption\r\ncontext = create_context()\r\n\r\n# Define your model\r\nmodel = tf.keras.Sequential([\r\n    tf.keras.layers.Dense(4, activation='relu', input_shape=(3072,)),\r\n    tf.keras.layers.Dense(10, activation='softmax')\r\n])\r\n\r\n@federated_learning_decorator(uri=\"ws://localhost:8089\")\r\n@encryption_decorator(context=context)\r\nasync def main():\r\n    client = ClientDevice(client_id=1, model=model, context=context)\r\n    await client.connect_to_central_server('ws://localhost:8089')\r\n    x_train, y_train = ...  # Load your training data\r\n    await client.federated_learning('ws://localhost:8089', x_train, y_train)\r\n\r\nasyncio.run(main())\r\n```\r\n\r\n## Running Tests\r\n\r\nTo run the tests, execute the following command in the root directory:\r\n\r\n```sh\r\npython -m unittest discover -s tests\r\n```\r\n\r\n## License\r\n\r\nThe usage of this library is free for academic work with proper referencing. For business, governmental, and any other types of usage, please contact me directly. All rights are reserved.\r\n\r\n**Contact:** mehrdaddjavadi@gmail.com\r\n\r\n## Contributing\r\n\r\nFeel free to contribute by submitting a pull request or opening an issue.\r\n\r\n```\r\n\r\nCopy and paste this into your README.md file. This format provides a clear, organized structure and includes all necessary details and instructions for potential users and contributors.\r\n```\r\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A modular and extensible framework for federated learning applications.",
    "version": "0.0.61",
    "project_urls": {
        "Homepage": "https://github.com/mehrdaddjavadi/federated_learning_framework"
    },
    "split_keywords": [
        "federated learning",
        " machine learning",
        " deep learning",
        " active learning",
        " encryption",
        " homomorphic encryption"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f9a31a58c70cef865a8b7b75088682781bb8637eb36fbef81b3d80298d0a3ba5",
                "md5": "022d87eb94c1f2e281c3b13d03fb3c30",
                "sha256": "b1e74454420ffcfaf234f0821339526a8b359cd6583b02f2f45cb71bad0603f6"
            },
            "downloads": -1,
            "filename": "federated_learning_framework-0.0.61-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "022d87eb94c1f2e281c3b13d03fb3c30",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 14998,
            "upload_time": "2024-07-31T14:10:28",
            "upload_time_iso_8601": "2024-07-31T14:10:28.434215Z",
            "url": "https://files.pythonhosted.org/packages/f9/a3/1a58c70cef865a8b7b75088682781bb8637eb36fbef81b3d80298d0a3ba5/federated_learning_framework-0.0.61-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "44e19dd1f2b0f90deec71828accaaa43e718a8393ee2a36b1ddb2a6b3bdd9234",
                "md5": "9e8e8bdc46e365cb9af95523cc8b97dd",
                "sha256": "7b77cdbc3774456817a1cb8b00b8e20a0dfb2f7f09021f6208d62a1bde70b2a1"
            },
            "downloads": -1,
            "filename": "federated_learning_framework-0.0.61.tar.gz",
            "has_sig": false,
            "md5_digest": "9e8e8bdc46e365cb9af95523cc8b97dd",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 13003,
            "upload_time": "2024-07-31T14:10:30",
            "upload_time_iso_8601": "2024-07-31T14:10:30.502927Z",
            "url": "https://files.pythonhosted.org/packages/44/e1/9dd1f2b0f90deec71828accaaa43e718a8393ee2a36b1ddb2a6b3bdd9234/federated_learning_framework-0.0.61.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-07-31 14:10:30",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "mehrdaddjavadi",
    "github_project": "federated_learning_framework",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [
        {
            "name": "numpy",
            "specs": []
        },
        {
            "name": "tensorflow",
            "specs": []
        },
        {
            "name": "websockets",
            "specs": []
        },
        {
            "name": "pytest",
            "specs": []
        },
        {
            "name": "tenseal",
            "specs": []
        }
    ],
    "lcname": "federated-learning-framework"
}
        
Elapsed time: 0.78593s