You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/learning-paths/embedded-and-microcontrollers/training-inference-pytorch/fine-tune-2.md
+14-13Lines changed: 14 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,15 +6,15 @@ weight: 3
6
6
layout: learningpathall
7
7
---
8
8
9
-
## Build the Model
9
+
## Build the model
10
10
11
-
Navigate to the Arm examples directory in the ExecuTorch repository.
11
+
Navigate to the Arm examples directory in the ExecuTorch repository:
12
12
13
13
```bash
14
14
cd$HOME/executorch/examples/arm
15
15
```
16
16
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:
18
18
19
19
```python
20
20
#!/usr/bin/env python3
@@ -369,24 +369,24 @@ if __name__ == "__main__":
369
369
```
370
370
371
371
372
-
### About the Script
372
+
### About the script
373
373
The script handles the entire workflow: data generation, model training, and a simple command-line game.
374
374
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.
380
380
381
-
###Running the Script:
381
+
## Running the Script:
382
382
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:
384
384
385
385
```bash
386
386
python rps_tiny.py --epochs 8 --export --play
387
387
```
388
388
389
-
You'll see the training progress, where the model's accuracy rapidly improves on the synthetic data.
389
+
You’ll see training progress similar to:
390
390
391
391
```output
392
392
== Building synthetic datasets ==
@@ -402,7 +402,8 @@ Training done.
402
402
Loaded weights from rps_best.pt
403
403
[export] wrote rps_tiny.pte
404
404
```
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:
0 commit comments