Code Monkey home page Code Monkey logo

python-sdk's Introduction

Modalic Logo


Python SDK

Python SDK library for using the Modalic Federated Learning Operations Platform.

The SDK library serves as convenient interface for performing Federated Learning with the most common Machine Learning frameworks like Pytorch or Tensorflow written in the Python programming language. As an additional software layer within a Machine Learning pipeline, the SKD enables an individual client application to take part within a Federated Learning setup. The coordination of a distributed Machine Learning process solving a particular problem, is done by a central server or service provider which can be started via Python script using the SDK.

As the main entrypoint to a production ready FLOps Platform, this software package aims for all developers and ML practitioners that want to run ML use cases in distributed fashion.

Usage

In order to run a Federated Learning procedure two main entities have to instantiated. The client logic and the aggregation server application. Both can be started via the SDK. Currently Pytorch & Tensorflow are supported as framework to construct the ML architecture.

Run the Aggregation Server

# (1) Run the aggregation server with configuration using .toml
cfg = toml.load("${configPATH}.toml")
modalic.run_server(cfg)

The .toml file can be used to control hyperparameters for the aggregation server.

# -c configs/config.toml

# REST API settings.
[api]
# The address to which the REST API of the server
# will be bound. All requests should be sent to this address.
server_address = "127.0.0.1:8080"

# Hyperparameter controlling the Federated Learning training process.
[protocol]
# Defines the number of training rounds (global epochs) 
# that will be performed.
training_rounds = 10
# Sets the number of participants & local models 
# one global epoch should at least contain.
participants = 2

For implementing the client logic a framework of choice can be used.

Pytorch

# (2) Construct the client logic.
import modalic

# Define a FLClient object that implements all the ML logic and will
# used as an input to an internal modalic client which enables the 
# program to connect to the server an perform training in distributed fashion.
class FLClient(modalic.Client):

  def __init__(self, dataset, ...):
    self.model = Net()
    self.dataset = torch.utils.data.DataLoader(dataset, batch_size=32)
    ...

  def train(self):
    for epoch in range(0, self.epochs):
        for images, labels in self.trainloader:
            ...

    return self.model

  def serialize_local_model(self, model):
      return modalic.serialize_torch_model(model)

  def deserialize_global_model(self, global_model):
      self.model = modalic.deserialize_torch_model(
          self.model, global_model, self._get_model_shape()
      )

  def get_model_shape(self):
      return modalic.get_torch_model_shape(self.model)

  def get_model_dtype(self):
      ...

# Construct the client layer..
client = FLClient(...)

# (3) Run training for single client.
modalic.run_client(client)

Tensorflow

# (2) Construct the client logic.

class FLClient(modalic.Client):

  def __init__(self, dataset, ...):
    # Initialize & compile the MobileNetV2 model.
    self.model = tf.keras.applications.MobileNetV2((32, 32, 3), classes=10, weights=None)
    # Load the CIFAR-10 dataset using tf.keras.
    (self.x_train, self.y_train), (_, _) = tf.keras.datasets.cifar10.load_data()
    ...

  def train(self):
    ...
    self.model.fit(self.x_train, self.y_train, batch_size=self.batch_size, epochs=self.epochs)
    ...
    return self.model

  def serialize_local_model(self, model):
      return modalic.serialize_tf_keras_model(model)

  def deserialize_global_model(self, global_model):
      self.model = modalic.deserialize_tf_keras_model(
          self.model, global_model, self._get_model_shape()
      )

  def get_model_shape(self):
      return modalic.get_tf_keras_model_shape(self.model)

  def get_model_dtype(self):
      ...

# Construct the client layer..
client = FLClient(...)

# (3) Run training for single client.
modalic.run_client(client)

Please keep in mind that this code snippet shows only the logic and the general idea. For more details, check out the /examples folder that contains more in-depth and complete instruction sets and examples that are actually actionable.

Installation

The latest release of Modalic Python SDK can be installed via pip:

pip install modalic

Documentation

See the Python SDK docs for more information. Additionally, some examples for starting with Modalic are provided in this repository under the examples folder. Any Questions? Reach out to us on modalic.ai.

Development

Find more information on contributing to the open source stack and the development process in general here.

License

The Modalic Python SDK is distributed under the terms of the Apache License Version 2.0. A complete version of the license is available in LICENSE.

python-sdk's People

Contributors

dxlnr avatar a-doering 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.