Skip to content

Commit c7d8b94

Browse files
Updates
1 parent 538698f commit c7d8b94

File tree

1 file changed

+14
-13
lines changed
  • content/learning-paths/embedded-and-microcontrollers/training-inference-pytorch

1 file changed

+14
-13
lines changed

content/learning-paths/embedded-and-microcontrollers/training-inference-pytorch/fine-tune-2.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ weight: 3
66
layout: learningpathall
77
---
88

9-
## Build the Model
9+
## Build the model
1010

11-
Navigate to the Arm examples directory in the ExecuTorch repository.
11+
Navigate to the Arm examples directory in the ExecuTorch repository:
1212

1313
```bash
1414
cd $HOME/executorch/examples/arm
1515
```
1616

17-
Using a file editor of your choice, create a file named `rps_tiny.py`, copy and paste the code shown below:
17+
Create a file named `rps_tiny.py` and paste the following code:
1818

1919
```python
2020
#!/usr/bin/env python3
@@ -369,24 +369,24 @@ if __name__ == "__main__":
369369
```
370370

371371

372-
### About the Script
372+
### About the script
373373
The script handles the entire workflow: data generation, model training, and a simple command-line game.
374374

375-
- **Synthetic Data Generation:** The script includes a function `render_rps()` that generates 28x28 grayscale images of the letters 'R', 'P', and 'S' with random rotations, blurs, and noise. This creates a diverse dataset that's used to train the model.
376-
- **Model Architecture:** The model, a TinyRPS class, is a simple Convolutional Neural Network (CNN). It uses a series of 2D convolutional layers, followed by pooling layers to reduce spatial dimensions, and finally, fully connected linear layers to produce a final prediction. This architecture is efficient and well-suited for edge devices.
377-
- **Training:** The script generates synthetic training and validation datasets. It then trains the CNN model using the **Adam optimizer** and **Cross-Entropy Loss**. It tracks validation accuracy and saves the best-performing model to `rps_best.pt`.
378-
- **ExecuTorch Export:** A key part of the script is the `export_to_pte()` function. This function uses the `torch.export module` (or a fallback) to trace the trained PyTorch model and convert it into an ExecuTorch program (`.pte`). This compiled program is highly optimized for deployment on any target hardware, for example Cortex-M or Cortex-A CPUs for embedded devices.
379-
- **CLI Mini-Game**: After training, you can play an interactive game. The script generates an image of your move and a random opponent's move. It then uses the trained model to classify both images and determines the winner based on the model's predictions.
375+
- Synthetic Data Generation: the script includes a function `render_rps()` that generates 28x28 grayscale images of the letters 'R', 'P', and 'S' with random rotations, blurs, and noise. This creates a diverse dataset that's used to train the model.
376+
- Model Architecture: the model, a TinyRPS class, is a simple Convolutional Neural Network (CNN). It uses a series of 2D convolutional layers, followed by pooling layers to reduce spatial dimensions, and finally, fully connected linear layers to produce a final prediction. This architecture is efficient and well-suited for edge devices.
377+
- Training: the script generates synthetic training and validation datasets. It then trains the CNN model using the **Adam optimizer** and **Cross-Entropy Loss**. It tracks validation accuracy and saves the best-performing model to `rps_best.pt`.
378+
- ExecuTorch Export: a key part of the script is the `export_to_pte()` function. This function uses the `torch.export module` (or a fallback) to trace the trained PyTorch model and convert it into an ExecuTorch program (`.pte`). This compiled program is highly optimized for deployment on any target hardware, for example Cortex-M or Cortex-A CPUs for embedded devices.
379+
- CLI Mini-Game: after training, you can play an interactive game. The script generates an image of your move and a random opponent's move. It then uses the trained model to classify both images and determines the winner based on the model's predictions.
380380

381-
### Running the Script:
381+
## Running the Script:
382382

383-
To train the model, export it, and play the game, run the following command:
383+
Train the model, export it, and play the game:
384384

385385
```bash
386386
python rps_tiny.py --epochs 8 --export --play
387387
```
388388

389-
You'll see the training progress, where the model's accuracy rapidly improves on the synthetic data.
389+
Youll see training progress similar to:
390390

391391
```output
392392
== Building synthetic datasets ==
@@ -402,7 +402,8 @@ Training done.
402402
Loaded weights from rps_best.pt
403403
[export] wrote rps_tiny.pte
404404
```
405-
After training and export, the game will start. Type rock, paper, or scissors and see the model's predictions and what your opponent played.
405+
406+
After training and export, the game starts. Type rock, paper, or scissors, and review the model’s predictions for you and a random opponent:
406407

407408
```output
408409
=== Rock–Paper–Scissors: Play vs Tiny CNN ===

0 commit comments

Comments
 (0)