Skip to content

Commit

Permalink
welding defect example fix
Browse files Browse the repository at this point in the history
  • Loading branch information
KatHellg committed Nov 4, 2024
1 parent 27cf9f4 commit 279ec29
Show file tree
Hide file tree
Showing 12 changed files with 22 additions and 456 deletions.
113 changes: 22 additions & 91 deletions examples/welding-defect-detection/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@

**Note: If you are new to FEDn, we recommend that you start with the MNIST-Pytorch example instead: https://github.com/scaleoutsystems/fedn/examples/mnist-pytorch**

# Welding Defect Object Detection Example

This is an example FEDn project that trains a YOLOv8n model on images of welds to classify them as "good", "bad", or "defected". The dataset is pre-labeled and can be accessed for free from Kaggle https://www.kaggle.com/datasets/sukmaadhiwijaya/welding-defect-object-detection. See a few examples below,
Expand All @@ -15,105 +12,39 @@ This is an example FEDn project that trains a YOLOv8n model on images of welds t
This example is generalizable to many manufacturing and operations use cases, such as automatic optical inspection. The federated setup enables the organization to make use of available data in different factories and in different parts of the manufacturing process, without having to centralize the data.


## How to run the example

To run the example, follow the steps below. For a more detailed explanation, follow the Quickstart Tutorial: https://fedn.readthedocs.io/en/stable/quickstart.html

**Note: To be able to run this example, you need to have GPU access.**

## Step 1: Downloading the data

### 1. Prerequisites
Download the dataset from the following link and extract it to the `datasets` directory:
https://www.kaggle.com/datasets/sukmaadhiwijaya/welding-defect-object-detection

- `Python >=3.8, <=3.12 <https://www.python.org/downloads>`__
- `A project in FEDn Studio <https://fedn.scaleoutsystems.com/signup>`__
- `A Kaggle account <https://www.kaggle.com/account/login?phase=startSignInTab&returnUrl=%2Fsignup>`__
- GPU access

## Step 2: Partitioning the data

### 2. Install FEDn and clone GitHub repo
To partition the data for each client, run the following command:

Install fedn:

```
pip install fedn
```

Clone this repository, then locate into this directory:

```
git clone https://github.com/scaleoutsystems/fedn-ultralytics-tutorial.git
cd fedn-ultralytics-tutorial/examples/welding-defect-detection
```bash
python3 partition_data.py welding-defect-detection <num_splits>
```
Replace `<num_splits>` with the number of clients you want to partition the data for.

This generates the dataset partitions in the 'datasets' directory. These partitions needs to be distributed to the respective clients and renamed to 'fed_dataset' instead of 'welding_defect_detection_split_X.

### 3. Creating the compute package and seed model
## Step 3: Setting up the global_config.yaml

Create the compute package:
Inside the 'client' folder configure the 'global_config.yaml' in the following way:

```
fedn package create --path client
```

This creates a file 'package.tgz' in the project folder.
```bash
# Configuration for YOLOv8 Model and Dataset Paths
# Adjust settings here to define model size, class details, and dataset paths

Next, generate the seed model:
model_size: nano # Options: nano, small, medium, large, extra-large
num_classes: 3 # Number of classes
class_names: ['Bad Weld', 'Good Weld', 'Defect'] # A list of class names

train: fed_dataset/train/images # Configure paths (usually not needed to be configured)
val: fed_dataset/valid/images
test: fed_dataset/test/images
```
fedn run build --path client
```

This will create a model file 'seed.npz' in the root of the project. This step will take a few minutes, depending on hardware and internet connection (builds a virtualenv).

### 4. Running the project on FEDn

To learn how to set up your FEDn Studio project and connect clients, take the quickstart tutorial: https://fedn.readthedocs.io/en/stable/quickstart.html. When activating the first client, you will be asked to provide your login credentials to Kaggle to download the welding defect dataset and split it into separate client folders.


## Experiments with results

Below are a few examples of experiments which have been run using this example. A centralized setup has been used as baseline to compare against. Two clients have been used in the federated setup and a few different epoch-to-round ratios have been tested.


### Experimental setup

Aggregator:
- FedAvg

Hyperparameters:
- batch size: 16
- learning rate: 0.01
- imgsz: 640

Approach: The number of epochs and rounds in each experiment are divided such that rounds * epochs = 250.

#### Centralized setup

| Experiment ID| # clients | epochs | rounds |
| ----------- | ---------- | -------- | ------ |
| 0 | 1 | 250 | 1 |

#### Federated setup

| Experiment ID| # clients | epochs | rounds |
| ----------- | ---------- | -------- | ------ |
| 1 | 2 | 5 | 50 |
| 2 | 2 | 10 | 25 |
| 3 | 2 | 25 | 10 |



### Results

Centralized:

<img src="figs/CentralizedmAP50.png" width=50% height=50%>


Federated:

<img src="figs/2clients_5epochs_50rounds.png" width=50% height=50%>

<img src="figs/2clients_10epochs_25rounds.png" width=50% height=50%>

<img src="figs/2clients_25epochs_10rounds.png" width=50% height=50%>

## Step 4: Return to the root guide and follow the instructions from there
Now your dataset is ready and you have configured the global settings for the YOLOv8 model. Return to the root guide and follow the instructions from there to continue with the federated learning process.
46 changes: 0 additions & 46 deletions examples/welding-defect-detection/client/custom.yaml

This file was deleted.

131 changes: 0 additions & 131 deletions examples/welding-defect-detection/client/data.py

This file was deleted.

11 changes: 0 additions & 11 deletions examples/welding-defect-detection/client/fedn.yaml

This file was deleted.

65 changes: 0 additions & 65 deletions examples/welding-defect-detection/client/model.py

This file was deleted.

Loading

0 comments on commit 279ec29

Please sign in to comment.