Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jafermarq committed Jan 17, 2024
1 parent 05755d1 commit 370f90f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
7 changes: 4 additions & 3 deletions examples/advanced-tensorflow/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Advanced Flower Example (TensorFlow/Keras)

This example demonstrates an advanced federated learning setup using Flower with TensorFlow/Keras. It differs from the quickstart example in the following ways:
This example demonstrates an advanced federated learning setup using Flower with TensorFlow/Keras. This example uses [Flower Datasets](https://flower.dev/docs/datasets/) and it differs from the quickstart example in the following ways:

- 10 clients (instead of just 2)
- Each client holds a local dataset of 1/10 of the train datasets and 80% is training examples and 20% as test examples (note that by default only a small subset of this data is used when running the `run.sh` script)
Expand Down Expand Up @@ -57,10 +57,11 @@ pip install -r requirements.txt

## Run Federated Learning with TensorFlow/Keras and Flower

The included `run.sh` will call a script to generate certificates (which will be used by server and clients), start the Flower server (using `server.py`), sleep for 2 seconds to ensure the the server is up, and then start 10 Flower clients (using `client.py`). You can simply start everything in a terminal as follows:
The included `run.sh` will call a script to generate certificates (which will be used by server and clients), start the Flower server (using `server.py`), sleep for 10 seconds to ensure the the server is up, and then start 10 Flower clients (using `client.py`). You can simply start everything in a terminal as follows:

```shell
poetry run ./run.sh
# Once you have activated your environment
./run.sh
```

The `run.sh` script starts processes in the background so that you don't have to open eleven terminal windows. If you experiment with the code example and something goes wrong, simply using `CTRL + C` on Linux (or `CMD + C` on macOS) wouldn't normally kill all these processes, which is why the script ends with `trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT` and `wait`. This simply allows you to stop the experiment using `CTRL + C` (or `CMD + C`). If you change the script and anything goes wrong you can still use `killall python` (or `killall python3`) to kill all background processes (or a more specific command if you have other Python processes running that you don't want to kill).
Expand Down
8 changes: 3 additions & 5 deletions examples/advanced-tensorflow/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def main() -> None:
# Parse command line argument `partition`
parser = argparse.ArgumentParser(description="Flower")
parser.add_argument(
"--partition",
"--client-id",
type=int,
default=0,
choices=range(0, 10),
Expand All @@ -86,9 +86,7 @@ def main() -> None:
)
parser.add_argument(
"--toy",
type=bool,
default=False,
required=False,
action='store_true',
help="Set to true to quicky run the client using only 10 datasamples. "
"Useful for testing purposes. Default: False",
)
Expand All @@ -101,7 +99,7 @@ def main() -> None:
model.compile("adam", "sparse_categorical_crossentropy", metrics=["accuracy"])

# Load a subset of CIFAR-10 to simulate the local data partition
x_train, y_train, x_test, y_test = load_partition(args.partition)
x_train, y_train, x_test, y_test = load_partition(args.client_id)

if args.toy:
x_train, y_train = x_train[:10], y_train[:10]
Expand Down
1 change: 1 addition & 0 deletions examples/advanced-tensorflow/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
flwr>=1.0, <2.0
flwr-datasets = { extras = ["vision"], version = ">=0.0.2,<1.0.0" }
tensorflow-cpu>=2.9.1, != 2.11.1 ; platform_machine == "x86_64"
tensorflow-macos>=2.9.1, != 2.11.1 ; sys_platform == "darwin" and platform_machine == "arm64"
4 changes: 2 additions & 2 deletions examples/advanced-tensorflow/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
echo "Starting server"

python server.py &
sleep 3 # Sleep for 3s to give the server enough time to start
sleep 10 # Sleep for 10s to give the server enough time to start

for i in $(seq 0 9); do
echo "Starting client $i"
python client.py --partition=${i} --toy True &
python client.py --client-id=${i} --toy &
done

# This will allow you to use CTRL+C to stop all background processes
Expand Down

0 comments on commit 370f90f

Please sign in to comment.