Skip to content

Repository files navigation

Federated-Training-TFG

This project is designed to train a YOLOv5 model in a federated approach using embedded systems for coffee plant detection.

The embedded systems used for the development of this project were three 4GB Jetson Nano Developer Kits.

To perform the complete installation of the necessary libraries on a Jetson Nano, visit the tutorial here

For server library installation instructions, see here

Execution of federated training:

Generation of certifications on the server

Warning

Ensure OpenSSL is installed on the server. To allow clients to connect securely, check the server's IP address in gen_certs.sh and then run:

chmod +x gen_certs.sh
./gen_certs.sh

This will generate the OpenSSL certificates; ca.crt must be copied to each node.

Secure the connection on each node

This ensures that only the server can connect to the nodes. If UFW is not installed, run this command twice.

chmod +x ufw_setup.sh
sudo ./ufw_setup.sh

Dividing the Dataset:

If you only have the base dataset, run:

python3 divide_dataset.py `--num_nodes` N `--input_dir` <Directory where the dataset is located> `--output_dir` <Directory where each subset is saved>

This divides your dataset into the number of nodes N you have randomly, so you must send each folder to the nodes.

Training:

  • First, run the server
python3 server.py

Tip

The server has several options.

  • --no_defenses: Disables Byzantine defenses.
  • --output: The directory where the models for each round will be stored in .npy format. (If the output folder already has a .npy model, its treated as a checkpoint).
  • Then run the clients.
python3 client.py

Tip

You can run the clients first and then the server because they wait for the server to become available (no more than 30 seconds).

  • If you want to run a malicious client, choose the type of attack to perform in the .env file (sign_flip, gaussian_noise, scale_attack, zero_weights) and run:
python3 malicious_client.py

Graphing the Results

The results are saved in ~/logs within each client. Use the filename plus the file extension to graph the results.

To view the loss during training, run:

python plot_loss.py --csv <path_to_file.csv> --out <output_format>

To view the metrics, run:

python plot_loss.py -csv <path_to_file.csv> --out <output_format> --hw_metrics

Extracting the Final Model

Once federated training is complete, simply run:

python3 export_model.py --round N --params_path <path_to_model.npy> --output <model_name>
  • N is the round from which you want to extract the model

Testing the Model

To explicitly test the trained model, a series of steps must be performed.

First, uninstall grpcio, since yolo uses version 1.70.0.

pip uninstall grpcio

Next, install the default version, which is installed as Python 3.8 (it must be 1.70.0).

pip install grpcio

Next, navigate to the yolo folder.

cd yolov5/

Here we will use the inference script detect.py included in the repository as follows:

python.exe detect.py --weights <model_path.pt> --source <image_path_to_use.jpg>

This script will save the inference in this directory:

~/yolov5/runs/detect/exp*

About

Final graduation project based on Federated Training on Embedded Systems

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages