树莓派实现联邦学习

树莓派联邦学习部署

安装系统依赖(十分重要,建议所有任务开始之前必须安装)

设置代理

1
2
export http_proxy=http://192.168.10.4:7890
export https_proxy=http://192.168.10.4:7890

更新库

1
sudo apt-get update

安装依赖

1
sudo apt-get install libopenblas-dev m4 libblas-dev cmake libatlas-base-dev gfortran libffi-dev libavformat-dev libavdevice-dev

对于Raspberry os lite而言,不自带git,因此需要提前下载

1
sudo apt install git

树莓派4B

Rasp4(树莓派系统 Raspberry Pi OS Lite 2023-12-11 64 bit system) ,下载地址:http://share.ninglang.fun/rasp4b/image/2023-12-11-raspios-bookworm-arm64-lite.img.xz

Miniforge安装

1
2
#Raspberry OS
Linux raspberrypi 6.1.0-rpi7-rpi-v8 #1 SMP PREEMPT Debian 1:6.1.63-1+rpt1 (2023-11-24) aarch64 GNU/Linux

下载 Miniforge

1
wget http://share.ninglang.fun/rasp4b/Miniforge3-Linux-aarch64.sh

安装Miniforge

1
bash ./Miniforge3-Linux-aarch64.sh

安装树莓派系统

创建Python3.9的环境

1
conda create --name=fl python=3.9

安装必要的wheel包(可以不安装,但是为了减少编译时间,可以先安装已经编译好的)

1
wget http://share.ninglang.fun/rasp4b/wheel/gevent-23.9.1-cp39-cp39-linux_aarch64.whl
1
wget http://share.ninglang.fun/rasp4b/wheel/psutil-5.9.6-cp39-abi3-linux_aarch64.whl

安装syft==0.8.4,默认安装numpy==1.24.4,但是无torch

1
pip install syft==0.8.4

安装torch,参考torch官网PyTorch,conda无法正常安装,采用pip

1
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu

pip安装正常,所有的依赖包为

1
wget http://share.ninglang.fun/rasp4b/Federal_learning/requirements.txt

打包syft的环境(conda同平台迁移)

安装打包软件包

1
pip install conda-pack

打包环境fl

1
conda pack -n fl

得到fl.tar.gz,已上传至服务器,下载命令

1
wget http://share.ninglang.fun/rasp4b/Federal_learning/syft0.8.4.tar.gz

新建项目文件夹

1
mkdir -p syft

解压项目

1
tar -xzf syft0.8.4.tar.gz -C syft/

激活环境

1
source ~/syft/bin/activate

树莓派5

Rasp5/Rasp4B(Ubuntu 23.10 (GNU/Linux 6.5.0-1011-raspi aarch64)) ,下载地址:http://share.ninglang.fun/rasp5/image/ubuntu-23.10-preinstalled-server-arm64+raspi.img.xz

MiniConda 安装( ubuntu/raspberry OS系统)

树莓派系统(如果已经设置代理,则下面步骤中无需设置源)

1
2
3
4
5
#树莓派系统(和上面树莓派4B一样)
Linux raspberrypi 6.1.0-rpi7-rpi-2712 #1 SMP PREEMPT Debian 1:6.1.63-1+rpt1 (2023-11-24) aarch64 GNU/Linux

#ubuntu系统
Linux ubuntu 6.5.0-1011-raspi #14-Ubuntu SMP PREEMPT_DYNAMIC Fri Feb 9 14:06:28 UTC 2024 aarch64 aarch64 aarch64 GNU/Linux

进入miniconda下载地址,,使用wget下载Miniconda,对于树莓派4B而言

1
sudo wget http://share.ninglang.fun/rasp5/Miniconda3-py38_23.11.0-2-Linux-aarch64.sh

创建Python3.9环境

1
conda create --name=fl python=3.9

安装syft==0.8.4,默认安装numpy==1.24.4,但是无torch

1
pip install syft==0.8.4

安装torch,参考torch官网PyTorch,conda无法正常安装,采用pip

1
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu

正常安装略过......

设置conda为环境变量

1
2
#conda安装路径
/home/ninglang/minisconda3/bin

编辑~/.bashrc,追加

1
export PATH="/home/ninglang/miniconda3/bin:$PATH"

保存,输入

1
source ./.bashrc

conda换源,修改~/.condarc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
channels:
- defaults
show_channel_urls: true
default_channels:
- http://mirrors.aliyun.com/anaconda/pkgs/main
- http://mirrors.aliyun.com/anaconda/pkgs/r
- http://mirrors.aliyun.com/anaconda/pkgs/msys2
custom_channels:
conda-forge: http://mirrors.aliyun.com/anaconda/cloud
msys2: http://mirrors.aliyun.com/anaconda/cloud
bioconda: http://mirrors.aliyun.com/anaconda/cloud
menpo: http://mirrors.aliyun.com/anaconda/cloud
pytorch: http://mirrors.aliyun.com/anaconda/cloud
simpleitk: http://mirrors.aliyun.com/anaconda/cloud

如果需要安装torch,设置

1
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/

新建conda环境

1
conda create --name fl python=3.9

conda查看环境

1
conda env list

conda激活环境

1
conda activate fl

pip换源(如果设置代理则无需更换源)

1
pip config set global.index-url http://mirrors.aliyun.com/pypi/simple/

Torch的安装

对于树莓派(使用arrch64的ubuntu或者raspberry os),先到torch-wheel下载文件,再手动安装

  • 对于没有GPU的板子,直接安装
1
2
#注意:torch和torchvision一一对应,请安装对应的版本
pip install torch==1.10.0+cpu torchvision==0.11.1+cpu -f https://download.pytorch.org/whl/torch_stable.html

设置代理

1
2
export http_proxy=http://192.168.10.4:7890
export https_proxy=http://192.168.10.4:7890

jupyter配置

  • 先生成jupyter的配置文件夹
1
jupyter notebook --generate-config

可以看到生成了/home/ninglang/.jupyter/jupyter_notebook_config.py.

  • 设置jupyter的密码,运行下面命令之后输入密码即可
1
jupyter notebook password
  • 修改配置文件(端口,ip,外部访问)修改/home/ninglang/.jupyter/jupyter_notebook_config.py文件
1
2
3
4
5
c.NotebookApp.ip='*'       #在所有的网卡接口上开启服务

c.NotebookApp.port =8888 #可自行指定一个端口, 访问时使用该端口7777

c.NotebookApp.allow_remote_access = True #允许远程