OpenMinedSwiftSyft 0.5

OpenMinedSwiftSyft 0.5

Mark Jimenez 维护。



 
依赖项
LibTorch~> 1.7
GoogleWebRTC~> 1.1.0
SyftProto= 0.4.9
 

  • 作者
  • OpenMined

SwiftSyft-logo CI Coverage License Contributors OpenCollective

All Contributors

SwiftSyft

SwiftSyft 使得您在 iOS 设备上对 PySyft 模型进行训练和推理变得容易。这允许您利用设备上直接存储的训练数据,从而无需将用户数据发送到中央服务器。这就是所谓的 联邦学习

  • ⚙️ 对 PyTorch 或 TensorFlow 编写的任何 PySyft 模型的 训练和推理
  • 👤允许所有数据留在用户设备上
  • 🔙支持委托给后台任务调度程序
  • 🔑支持 JWT 认证 以保护模型不受 Sybil 攻击
  • 👍内置的一套 最佳实践 以防止应用过度使用设备资源。
    • 🔌 检测设备是否连接充电器以允许在充电时仅在后台进行训练的 充电检测
    • 💤 检测用户开始使用设备时的 睡眠和唤醒 以确保应用在用户开始使用设备时不占用资源
    • 💸 检测 Wi-Fi 和计费网络 以确保模型更新不会使用所有可用的数据配额
    • 🔕所有这些智能默认设置都可以轻松 覆盖
  • :mortarboard: 支持反应式和回调模式,因此您有选择权 (_进行中_)
  • 🔒支持使用 点对点 WebRTC 连接的 安全多方计算安全聚合 协议 (_进行中_)

还可以应用各种额外的隐私保护措施,包括 差分隐私多方计算安全聚合

OpenMined 致力于构建 世界上第一个开源的 Web 和移动端联邦学习生态系统。SwiftSyft 是此生态系统的一部分,负责将安全联邦学习带到 iOS 设备上。您也可以使用 KotlinSyft 在 Android 设备上训练模型,或在网页浏览器中使用 syft.js

如果您想了解可扩展联邦系统的构建方法,请阅读一篇介绍性的文章 Towards Federated Learning at Scale

安装

Cocoapods

Cocoapods 是 Cocoa 项目的依赖管理器。只需将 OpenMinedSwiftSyft 添加到您的 Podfile 中,如下所示:

pod 'OpenMinedSwiftSyft', ~> 0.1.3-beta1

快速开始

作为开发者,根据 OpenMined 基础设施构建自己的安全联邦学习系统只需进行几个步骤。

  1. 🤖使用 PySyft 生成您的安全机器学习模型。PySyft 的设计是基于 PyTorch 和 TensorFlow,因此您无需学习新的 ML 框架。您还需要编写一个训练计划(工人运行的训练代码)和一个平均计划(PyGrid 运行的用于平均模型差的代码)。
  2. 🌎PyGrid 上托管您的模型和计划,它将处理您管道的所有联邦学习组件。您需要在某处设置一个 PyGrid 服务器,请参阅他们的安装说明以了解如何操作。
  3. 🎉在设备上开始训练!

📓有关完整的工作流程和过程,请参阅我们项目路线图的详细描述。

您可以使用 SwiftSyft 作为前端或作为后台服务。以下是一个快速开始示例用法

// This is a demonstration of how to use SwiftSyft with PyGrid to train a plan on local data on an iOS device

// Authentication token
let authToken = /* Get auth token from somewhere (if auth is required): */

// Create a client with a PyGrid server URL
if let syftClient = SyftClient(url: URL(string: "ws://127.0.0.1:5000")!, authToken: authToken) {

  // Store the client as a property so it doesn't get deallocated during training.
  self.syftClient = syftClient

  // Create a new federated learning job with the model name and version
  self.syftJob = syftClient.newJob(modelName: "mnist", version: "1.0.0")

  // This function is called when SwiftSyft has downloaded the plans and model parameters from PyGrid
  // You are ready to train your model on your data
  // plan - Use this to generate diffs using our training data
  // clientConfig - contains the configuration for the training cycle (batchSize, learning rate) and
  // metadata for the model (name, version)
  // modelReport - Used as a completion block and reports the diffs to PyGrid.
  self.syftJob?.onReady(execute: { plan, clientConfig, modelReport in

    do {

        // This returns a lazily evaluated sequence for each MNIST image and the corresponding label
        // It divides the training data and the label by batches
        let (mnistData, labels) = try MNISTLoader.load(setType: .train, batchSize: clientConfig.batchSize)

        // Iterate through each batch of MNIST data and label
        for case let (batchData, labels) in zip(mnistData, labels) {

            // We need to create an autorelease pool to release the training data from memory after each loop
            try autoreleasepool {

                // Preprocess MNIST data by flattening all of the MNIST batch data as a single array
                let flattenedBatch = MNISTLoader.flattenMNISTData(batchData)
                // Preprocess the label ( 0 to 9 ) by creating one-hot features and then flattening the entire thing
                let oneHotLabels = MNISTLoader.oneHotMNISTLabels(labels: labels).compactMap { Float($0)}

                // Since we don't have native tensor wrappers in Swift yet, we use
                // `TrainingData` and `ValidationData` classes to store the data and shape.
                let trainingData = try TrainingData(data: flattenedBatch, shape: [clientConfig.batchSize, 784])
                let validationData = try ValidationData(data: oneHotLabels, shape: [clientConfig.batchSize, 10])

                // Execute the plan with the training data and validation data. `plan.execute()`
                // returns the loss and you can use it if you want to (plan.execute()
                // has the @discardableResult attribute)
                let loss = plan.execute(trainingData: trainingData,
                                      validationData: validationData,
                                        clientConfig: clientConfig)

            }

        }

        // Generate diff data and report the final diffs as
        let diffStateData = try plan.generateDiffData()
        modelReport(diffStateData)

    } catch let error {
        // Handle any error from the training cycle
        debugPrint(error.localizedDescription)
    }

  })

  // This is the error handler for any job exeuction errors like connecting to PyGrid
  self.syftJob?.onError(execute: { error in
    print(error)
  })

  // This is the error handler for being rejected in a cycle. You can retry again
  // after the suggested timeout.
  self.syftJob?.onRejected(execute: { timeout in
      if let timeout = timeout {
          // Retry again after timeout
          print(timeout)
      }
  })

  // Start the job. You can set that the job should only execute if the device is being charge and there is
  // a WiFi connection. These options are on by default if you don't specify them.
  self.syftJob?.start(chargeDetection: true, wifiDetection: true)
}

API 文档

请参阅 API 文档 获取完整参考。

在后台运行

如何在iOS上使用后台任务调度器运行SwiftSyft的迷你教程可以在这里找到

运行示例应用

示例应用从本地托管的PyGrid服务器中获取计划、协议和模型权重。然后,使用libtorch反序列化并执行计划。

按照以下步骤设置运行示例应用的环境

  • 克隆仓库PyGrid并更改目录到其中
git clone https://github.com/OpenMined/PyGrid
cd PyGrid
  • 安装docker
  • 安装docker-compose。
  • 在命令行中执行docker-compose以启动pygrid服务器。
docker-compose up
  • 在虚拟环境中从源安装PySyft
virtualenv -p python3 venv
source venv/bin/activate
python setup.py install
  • 将虚拟环境设置为Jupyter Notebook可用
python -m ipykernel install --user --name=venv
  • 托管Jupyter Notebook
jupyter notebook
  • 打开浏览器并导航到localhost:8888。您应该能看到PySyft笔记本控制台。
  • 在Jupyter Notebook中,导航到examples/model-centric
  • 使用venv内核运行笔记本01-Create-plan。现在PyGrid已设置,模型通过它托管。
syft.base_url="<IP_address_from_step_16>:5000"
  • 使用Cocoapods设置示例项目
  • 安装Cocoapods
gem install cocoapods
  • 安装项目的依赖。
pod install # On the root directory of this project
  • 在Xcode中打开文件SwiftSyft.xcworkspace
  • 运行SwiftSyft项目。它自动使用127.0.0.1:5000作为PyGrid URL。

贡献

设置

您可以通过在根目录中运行pod install来工作于项目。然后,在Xcode中打开文件SwiftSyft.xcworkspace。当项目打开在Xcode时,您可以在Pods/Development Pods/SwiftSyft/Classes/*中工作于SwiftSyft pod本身。

工作流程

  1. 星星,为和克隆库
  2. 在Xcode中打开项目
  3. 查看当前问题和GitHub。对于新人,查看标记为“首次良好问题”的问题
  4. 做你的工作
  5. 推送你的分支
  6. 向OpenMined/SwiftSyft提交PR

阅读贡献指南作为良好起点。

支持

有关使用此库的支持,请加入#lib_swift_syft Slack频道。如果您想跟踪库中的任何代码更改,请加入#code_swiftsyft Slack频道。点击此处加入我们的Slack社区[加入Slack社区](https://slack.openmined.org)!

许可

Apache License 2.0

贡献者

向这些人表示感谢 (emoji key)


Mark Jimenez

💻 📖

Madalin Mamuleanu

💻

Rohith Pudari

💻

Sebastian Bischoff

📖

Luke Reichold

💻

此项目遵循all-contributors规范。欢迎任何类型的贡献!