|
| 1 | +# CIFAR 10 End-to-End Fine-Tuning Tutorial |
| 2 | + |
| 3 | +## Objective: |
| 4 | + |
| 5 | +This tutorial guides the users through the training process of a simple PyTorch CNN model on the server and subsequently fine-tune the model on their edge devices. |
| 6 | + |
| 7 | +### Key Objectives |
| 8 | + |
| 9 | +1. **Server-Side Training**: Users can leverage the computational resource of the server to perform initial model training using PyTorch. |
| 10 | +2. **Edge Device Fine-Tuning**: Pre-trained models are lowered and deployed on mobile devices through ExecuTorch where they undergo fine-tuning. |
| 11 | +3. **Performance Benchmarking**: To track comprehensive performance metrics for on-device fine-tuning operations, measuring factors such as training speed, memory usage, and model accuracy to evaluate ExecuTorch's effectiveness in the edge environment. |
| 12 | + |
| 13 | +## ExecuTorch Environment Setup |
| 14 | + |
| 15 | +For easier management of Python environments and packages, we recommended using a Python environment management tool such as `conda`, `venv`, or `uv`. In this demonstration, we will use `uv` to set up the Python environment. |
| 16 | + |
| 17 | +To install ExecuTorch in a [`uv`](https://docs.astral.sh/uv/getting-started/installation/) Python environment use the following commands: |
| 18 | + |
| 19 | +```bash |
| 20 | +$ git clone https://github.com/pytorch/executorch.git --recurse-submodules |
| 21 | +$ cd executorch |
| 22 | +$ uv venv --seed --prompt et --python 3.10 |
| 23 | +$ source .venv/bin/activate |
| 24 | +$ git fetch origin |
| 25 | +$ git submodule sync --recursive |
| 26 | +$ git submodule update --init --recursive |
| 27 | +$ ./install_executorch.sh |
| 28 | +``` |
| 29 | + |
| 30 | +## Data Preparation |
| 31 | + |
| 32 | +We can download the CIFAR-10 dataset from the [official website](https://www.cs.toronto.edu/~kriz/cifar.html) and extract it to the desired location. Alternatively, we can also use the following command to download, extract, and create a balanced dataset: |
| 33 | + |
| 34 | +```bash |
| 35 | +python data_utils.py --train-data-batch-path ./data/cifar-10/cifar-10-batches-py/data_batch_1 --train-output-path ./data/cifar-10/extracted_data/train_data.bin --test-data-batch-path ./data/cifar-10/cifar-10-batches-py/test_batch --test-output-path ./data/cifar-10/extracted_data/test_data.bin --train-images-per-class 100 |
| 36 | +``` |
| 37 | + |
| 38 | +## Model Export |
| 39 | + |
| 40 | +Alternatively, if the users have a pre-trained pytorch model, they can export the standalone `pte`file using the following command: |
| 41 | + |
| 42 | +```bash |
| 43 | +python export.py --train-model-path cifar10_model.pth --pte-only-model-path cifar10_model.pte |
| 44 | +``` |
| 45 | + |
| 46 | +For getting the `pte` and `ptd` files, they can use the following command: |
| 47 | + |
| 48 | +```bash |
| 49 | +python export.py --train-model-path cifar10_model.pth --with-ptd --pte-model-path cifar10_model.pte --ptd-model-path . |
| 50 | +``` |
| 51 | + |
| 52 | +## Model Training and Fine-Tuning |
| 53 | + |
| 54 | +To run the end-to-end example, the users can use the following command: |
| 55 | + |
| 56 | +```bash |
| 57 | +python main.py --data-dir ./data --model-path cifar10_model.pth --pte-model-path cifar10_model.pte --split-pte-model-path cifar10_model_pte_only.pte --save-pt-json cifar10_pt.json --save-et-json cifar10_et.json --ptd-model-dir . --epochs 1 --fine-tune-epochs 1 |
| 58 | +``` |
0 commit comments