GithubHelp home page GithubHelp logo

replomancer / kotlinsyft Goto Github PK

View Code? Open in Web Editor NEW

This project forked from openmined/kotlinsyft

0.0 1.0 0.0 7.31 MB

The official Syft worker for secure on-device machine learning

Home Page: https://www.openmined.org

License: Apache License 2.0

Java 0.27% Kotlin 99.73%

kotlinsyft's Introduction

KotlinSyft-logo

Tests Coverage build License OpenCollective

All Contributors

KotlinSyft

KotlinSyft makes it easy for you to train and inference PySyft models on Android devices. This allows you to utilize training data located directly on the device itself, bypassing the need to send a user's data to a central server. This is known as federated learning.

  • โš™๏ธ Training and inference of any PySyft model written in PyTorch or TensorFlow
  • ๐Ÿ‘ค Allows all data to stay on the user's device
  • โšก Support for full multi-threading / background service execution
  • ๐Ÿ”‘ Support for JWT authentication to protect models from Sybil attacks
  • ๐Ÿ‘ A set of inbuilt best practices to prevent apps from over using device resources.
    • ๐Ÿ”Œ Charge detection to allow background training only when device is connected to charger
    • ๐Ÿ’ค Sleep and wake detection so that the app does not occupy resource when user starts using the device
    • ๐Ÿ’ธ Wifi and metered network detection to ensure the model updates do not use all the available data quota
    • ๐Ÿ”• All of these smart defaults are easily are overridable
  • ๐ŸŽ“ Support for both reactive and callback patterns so you have your freedom of choice (in progress)
  • ๐Ÿ”’ Support for secure multi-party computation and secure aggregation protocols using peer-to-peer WebRTC connections (in progress).

There are a variety of additional privacy-preserving protections that may be applied, including differential privacy, muliti-party computation, and secure aggregation.

OpenMined set out to build the world's first open-source ecosystem for federated learning on web and mobile. KotlinSyft is a part of this ecosystem, responsible for bringing secure federated learning to Android devices. You may also train models on iOS devices using SwiftSyft or in web browsers using syft.js.

If you want to know how scalable federated systems are built, Towards Federated Learning at Scale is a fantastic introduction!

Installation

We have not currently made our initial release. KotlinSyft would soon be available on JCenter and Maven.

Quick Start

As a developer, there are few steps to building your own secure federated learning system upon the OpenMined infrastructure:

  1. ๐Ÿค– Generate your secure ML model using PySyft. By design, PySyft is built upon PyTorch and TensorFlow so you don't need to learn a new ML framework. You will also need to write a training plan (training code the worker runs) and an averaging plan (code that PyGrid runs to average the model diff).
  2. ๐ŸŒŽ Host your model and plans on PyGrid which will deal with all the federated learning components of your pipeline. You will need to set up a PyGrid server somewhere, please see their installation instructions on how to do this.
  3. ๐ŸŽ‰ Start training on the device!

๐Ÿ““ The entire workflow and process is described in greater detail in our project roadmap.

You can use KotlinSyft as a front-end or as a background service. The following is a quick start example usage:

    val userId = "my Id"
    
    // Optional: Make an http request to your server to get an authentication token
    val authToken = apiClient.requestToken("https://www.mywebsite.com/request-token/$userId")

    // The config defines all the adjustable properties of the syft worker
    // The url entered here cannot define connection protocol like https/wss since the worker allots them by its own
    // `this` supplies the context. It can be an activity context, a service context, or an application context.
    val config = SyftConfiguration.builder(this, "www.mypygrid-url.com").build()
    
    // Initiate Syft worker to handle all your jobs
    val syftWorker = Syft.getInstance(authToken, configuration)
    
    // Create a new Job
    val newJob = syftWorker.newJob("mnist", "1.0.0")
    
    // Define training procedure for the job
    val jobStatusSubscriber = object : JobStatusSubscriber() {
        override fun onReady(
            model: SyftModel,
            plans: ConcurrentHashMap<String, Plan>,
            clientConfig: ClientConfig
        ) {
            // This function is called when KotlinSyft has downloaded the plans and protocols from PyGrid
            // You are ready to train your model on your data
            // param model stores the model weights given by PyGrid
            // param plans is a HashMap of all the planIDs and their plans. 
            // ClientConfig has hyper parameters like batchsize, learning rate, number of steps, etc
            
            // Plans are accessible by their plan Id used while hosting it on PyGrid.
            // eventually you would be able to use plan name here 
            val plan = plans["plan id"]

            repeat(clientConfig.maxUpdates) { step ->
                // your custom implementation to read a databatch from your data
                val batchData = dataRepository.loadDataBatch(clientConfig.batchSize)
                // plan.execute runs a single gradient step and returns the output as PyTorch IValue
                val output = plan.execute(
                    model,
                    batchData,
                    clientConfig
                )?.toTuple()
                // The output is a tuple with outputs defined by the pysyft plan along with all the model params
                output?.let { outputResult ->
                    val paramSize = model.modelState!!.syftTensors.size
                    // The model params are always appended at the end of the output tuple
                    val beginIndex = outputResult.size - paramSize
                    val updatedParams =
                            outputResult.slice(beginIndex until outputResult.size - 1)
                    // update your model. You can perform any arbitrary computation and checkpoint creation with these model weights
                    model.updateModel(updatedParams.map { it.toTensor() })
                    // get the required loss, accuracy, etc values just like you do in Pytorch Android
                    val accuracy = outputResult[1].toTensor().dataAsFloatArray.last()
                } ?: return // this will happen when plan execution fails. 
                // Most probably due to device state not fulfilling syft config constraints 
                // You should not handle any error here and simply return to close the subscriber. 
                // Failing to return from onReady will crash the application.
                // All error handling must be done with `onError` Listener
            }
        }

        override fun onRejected(timeout: String) {
        // Implement this function to define what your worker will do when your worker is rejected from the cycle
        // timeout tells you after how much time you should try again for the cycle at PyGrid
        }

        override fun onError(throwable: Throwable) {
        // Implement this function to handle error during job execution 
        }
    }
    
    // Start your job
    newJob.start(jobStatusSubscriber) 
    
    // Voila! You are done.

Running the Demo App

The demo app fetches the plans, protocols and model weights from pygrid server hosted locally. The plans are then deserialized and executed using libtorch.

Follow these steps to setup an environment to run the demo app:

  • Clone the repo PyGrid and change directory to it
git clone https://github.com/OpenMined/PyGrid
cd PyGrid
  • Install docker
  • Install docker-compose.
  • Execute docker-compose in the command line to start pygrid server.
docker-compose up
  • Install PySyft v0.2.5 in the virtual environment.
virtualenv -p python3 venv
source venv/bin/activate
pip install syft==0.2.5
pip install jupyter==1.0.0
pip install  notebook==5.7.8
git clone https://github.com/OpenMined/PySyft
git checkout tags/v0.2.5 -b v0.2.5
  • Host Jupyter Notebook
jupyter notebook
  • Open a browser and navigate to localhost:8888. You should be able to see the pysyft notebook console.
  • In the Jupyter Notebook, navigate to examples/experimental/FL Training Plan
  • Run the notebooks Create Plan. It should save three files in the FL Training Plan folder
  • Run the notebook Host Plan. Now PyGrid is setup and the model is hosted over it.
  • The android app connects to your PC's localhost via router (easier approach)
  • Get the IP address of your computer by running ip address show | grep "inet " | grep -v 127.0.0.1 if using Linux/Mac. For windows there are different steps. Alternatively, if you want to run the demo app in the emulator, use 10.0.2.2 as the IP address.
  • Use this IP address in your login screen to supply the PyGrid server url

Training Dataset

The demo app has a smaller randomly sampled subset of MNIST Training Data. You can replace the demo-app/src/main/res/raw/mnist_train.csv with the 100MB file from here

Download the above file and replace it with train_mnist.csv to train on complete mnist data.

Contributing

  1. Star, fork, and clone the repo
  2. Open Android Studio and import project
  3. Do your work.
  4. Push to your fork
  5. Submit a PR to OpenMined/KotlinSyft

Read the contribution guide as a good starting place. Additionally, we welcome you to the slack for queries related to the library and contribution in general. The Slack channel #lib_kotlin_syft is specific to KotlinSyft development, the Slack channel #lib_syft_mobile is meant for both Android and iOS teams. See you there!

Contributors

These people were integral part of the efforts to bring KotlinSyft to fruition and in its active development.


varun khare

๐Ÿ’ป โš ๏ธ ๐Ÿ“–

Jose A. Corbacho

๐Ÿ’ป โš ๏ธ

Ravikant Singh

๐Ÿ’ป ๐Ÿ“–

Saksham Rastogi

๐Ÿ“–

Patrick Cason

๐Ÿ“– ๐Ÿ’ผ

Mohammed Galalen

๐Ÿ“–

Erik Ziegler

๐Ÿ›

License

Apache License 2.0

kotlinsyft's People

Contributors

allcontributors[bot] avatar codeboy5 avatar galalen avatar hdodenhof avatar mccorby avatar rav1kantsingh avatar vkkhare avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.