Repository of the conceptual work around Federated Learning to train the object detection model YOLOv5.
Here you can find the developed code, tests, concept experiments and demos.
The current repository implements the logic regarding the tasks that the server and clients do.
git clone -b develop https://github.com/JQ-MC/object-detection-fedlearn.git fedlearn
cd fedlearn
pip3 install -r requirements.txt
In order to activate venv
environment, execute the following command from repository root:
source venv/bin/activate
.vscode
: vscode configuration.data
: datasets and configuration files about the datasets.models
: implemented models and configuration files about the models.src
: source code.
client
: implements the logic of the client in FLserver
: implements the logic of the server in FLstrategy
: implements the strategies that guide the server and the parameters aggregationutils
: custom helper functionsdata_splitter.py
: Splits a folder of images that follow YOLOv5 guidelines into partitions to try a federated approach to train a modelyolo_train_solo.py
: trains yolo in a centralized wayrunner_client.py
: sets up a client to train the netrunner_server.py
: sets up the server to handle the clientsutils
: yolo helper functions.detect.py
: used to try the model in the webcam or imagestrain.py
: implements the train pipeline of the model. Adapted to be used through the clientsval.py
: implements the validation of the model. Adapted to be used through the server.Set up a Federated Learning process!
In order to start a Federated Learning training, it is necessary to start the server:
python3 runner_server.py --server_address [::]:8080 --rounds 2 --load_params
- server_address: ip of the server
- rounds: number of federated rounds
- loads_params: controls if the server provides the initial parameters or it gets them from a random client (`--no_load_params`)
Default useful parameters:
- weights: initial weights path. `default=ROOT / "yolov5s.pt"`
- cfg: path to the specifications about the structure of the model to be trained. default="models/yolov5s.yaml"
- hyp: path to the specifications about the hyperparameters of the model. default=ROOT / "data/camacuc2.yaml"
- data: path to the specifications about the data to be trained. default=ROOT / "data/hyps/hyp.scratch.yaml"
Control the number of clients that participate in a Federated training
- min_num_clients: Minimum number of available clients required for sampling (default: 2)
- min_sample_size: Minimum number of clients used for fit/evaluate (default: 2)
- sample_fraction: Fraction of available clients used for fit/evaluate (default: 1.0
Clients must be created after the server. There must be at least --min_num_clients
clients, a parameter from the server, to start the training. Defaults to 2.
python3 runner_client.py --server_address [::]:8080 --cid 1 --epochs 100
- server_address: ip of the server. if executed in the same machine keep default. If not, check ip address of the server: XXX.XX.X.XX:8080
- cid: client id
- epochs: number of epochs per round
Default useful parameters:
- weights: initial weights path. `default=ROOT / "yolov5s.pt"`
- cfg: path to the specifications about the structure of the model to be trained. default="models/yolov5s.yaml"
- hyp: path to the specifications about the hyperparameters of the model. default=ROOT / "data/camacuc2.yaml"
- data: path to the specifications about the data to be trained. default=ROOT / "data/hyps/hyp.scratch.yaml"
Memory Handling:
In order to reduce the amount of memory in RAM and specially for embedded devices, lowering these parameters may help
- img: train, val image size (pixels)
- batch-size: number of images to pass through each batch of an epoch
This repository is able to run on edge devices such as Raspberry Pi 4 or Pi 3B.
In order to flash the raspberries follow these tutorials:
Then clone the repository and install requirements with pip3.
It is very important to handle RAM memory.
You can run YOLOv5 as the original repository:
$ python yolo_train_solo.py --data data/camacuc2.yaml --cfg yolov5s.yaml --weights '' --batch-size 64
yolov5m 40
24
16