-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d309a0
commit 975ba1f
Showing
8 changed files
with
4,924 additions
and
91 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"cells": [], | ||
"metadata": {}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
255 changes: 255 additions & 0 deletions
255
fastinference/.ipynb_checkpoints/02 ONNX-checkpoint.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,255 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": { | ||
"scrolled": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"!pip install git+https://github.com/muellerzr/fastinference.git --quiet" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# ONNX\n", | ||
"\n", | ||
"Commonly code is run on ONNX, and trying to utilize `fastai` as best you can can be a challenge. The beneifits of ONNX is it uses C++ so it can be a faster runtime, and recently ONNX came out with `CUDA` support as well! How hard is it to integrate? Let's make a `tabular` problem again, this time importing `ONNX`:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Name: fastinference\r\n", | ||
"Version: 0.0.13\r\n", | ||
"Summary: A collection of inference modules\r\n", | ||
"Home-page: https://github.com/muellerzr/fastinference/tree/master/\r\n", | ||
"Author: Zachary Mueller\r\n", | ||
"Author-email: [email protected]\r\n", | ||
"License: Apache Software License 2.0\r\n", | ||
"Location: /home/ml1/anaconda3/envs/fastai2/lib/python3.7/site-packages\r\n", | ||
"Requires: onnxruntime-gpu, fastai2, shap\r\n", | ||
"Required-by: \r\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"!pip show fastinference" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from fastai2.tabular.all import *\n", | ||
"from fastinference.onnx import *" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"path = untar_data(URLs.ADULT_SAMPLE)\n", | ||
"df = pd.read_csv(path/'adult.csv')\n", | ||
"splits = RandomSplitter()(range_of(df))\n", | ||
"cat_names = ['workclass', 'education', 'marital-status', 'occupation', 'relationship', 'race']\n", | ||
"cont_names = ['age', 'fnlwgt', 'education-num']\n", | ||
"procs = [Categorify, FillMissing, Normalize]\n", | ||
"y_names = 'salary'" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"to = TabularPandas(df, procs=procs, cat_names=cat_names, cont_names=cont_names,\n", | ||
" y_names=y_names, splits=splits)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"dls = to.dataloaders()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"learn = tabular_learner(dls, layers=[200,100])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"I've made a special `fastONNX` wrapper, which will take your `Learner` and export both your model and the `DataLoaders` so ONNX can use them via `learn.to_onnx()`" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"learn.to_onnx('tabular')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Now let's load it in:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"onnx_learn = fastONNX('tabular')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"What all can we do here? We can still do everything exactly the same minus one change: `predict`\n", | ||
"\n", | ||
"`predict` requires the raw inputs, so instead *always* build a `test_dl` and pass to `get_preds` (just made it simpler to code for me). Let's run a few examples also testing the times:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 10, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"single_dl = onnx_learn.test_dl(df.iloc[:1])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 11, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"3.46 ms ± 5.44 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"%%timeit\n", | ||
"preds = onnx_learn.get_preds(dl=single_dl)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Wow! 3.46 ms even beats our previous `predict` with 25 ms! what is it's output?" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 12, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"name, preds = onnx_learn.get_preds(dl=single_dl)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 13, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"(['<50k'], array([[[0.5036924, 0.4963076]]], dtype=float32))" | ||
] | ||
}, | ||
"execution_count": 13, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"name, preds" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Currently it doesn't support returning the raw inputs, I need to work on that some more" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.7.7" | ||
}, | ||
"toc": { | ||
"base_numbering": 1, | ||
"nav_menu": {}, | ||
"number_sections": false, | ||
"sideBar": true, | ||
"skip_h1_title": true, | ||
"title_cell": "Table of Contents", | ||
"title_sidebar": "Contents", | ||
"toc_cell": false, | ||
"toc_position": {}, | ||
"toc_section_display": true, | ||
"toc_window_display": true | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
Oops, something went wrong.