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
Convert PyTorch models into JavaScript models via ONNX.js or TensorFlow.js.
1
+
## Run PyTorch models in the browser using ONNX.js
2
+
3
+
Run PyTorch models in the browser with JavaScript by first converting your PyTorch model into the ONNX format and then loading that ONNX model in your website or app using ONNX.js. In the video tutorial below, I take you through this process using the demo example of a handwritten digit recognition model trained on the MNIST dataset.
### The files in this repo (and a description of what they do)
17
+
```
18
+
├── demo
19
+
│ ├── test.html (a minimal test for making sure the generated ONNX model works, uses ONNX.js to load and run the generated ONNX model)
20
+
│ └── onnx_model.onnx (a copy of the generated ONNX model, used by the test code)
21
+
├── convert_to_onnx.py (converts a trained PyTorch model into an ONNX model)
22
+
├── inference_mnist_model.py (the PyTorch model description (without the trained parameters) used by convert_to_onnx.py to generate the ONNX model)
23
+
├── inputs_batch_preview.png (a preview of a batch of augmented input data, generated by preview_mnist_dataset.py)
24
+
├── onnx_model.py (the ONNX model generated by convert_to_onnx.py)
25
+
├── preview_dataset.py (for testing out different types of data augmentation)
26
+
├── pytorch_model.pt (the trained PyTorch model parameters, generated by train_mnist.model.py, and used by convert_to_onnx.py to generate the ONNX model)
27
+
└── train_mnist_model.pt (trains the PyTorch model and saves the trained parameters as pytorch_model.pt)
28
+
```
29
+
30
+
### The benefits of running a model in the browser:
31
+
* Faster inference times with smaller models.
32
+
* Easy to host and scale (only static files).
33
+
* Offline support.
34
+
* User privacy (can keep the data on the device).
35
+
36
+
### The benefits of using a backend server:
37
+
* Faster load times (don't have to download the model).
38
+
* Faster and consistent inference times with larger models (can take advantage of GPUs or other accelerators).
39
+
* Model privacy (don't have to share your model if you want to keep it private).
0 commit comments