|
9 | 9 | "This tutorial shows how to convert the original Tensorflow Bert model to ONNX. \n",
|
10 | 10 | "In this example we fine tune Bert for squad-1.1 on top of [BERT-Base, Uncased](https://storage.googleapis.com/bert_models/2018_10_18/uncased_L-12_H-768_A-12.zip).\n",
|
11 | 11 | "\n",
|
12 |
| - "Since this tutorial cares mostly about the conversion process we reuse tokenizer and utilities defined in the Bert source tree as much as possible.\n", |
| 12 | + "Since this tutorial cares mostly about the conversion process, we reuse tokenizer and utilities defined in the Bert source tree as much as possible.\n", |
13 | 13 | "\n",
|
14 | 14 | "This should work with all versions supported by the [tensorflow-onnx converter](https://github.com/onnx/tensorflow-onnx), we used the following versions while writing the tutorial:\n",
|
15 | 15 | "```\n",
|
|
27 | 27 | "metadata": {},
|
28 | 28 | "source": [
|
29 | 29 | "## Step 1 - define some environment variables\n",
|
30 |
| - "Before we start, lets setup some variables where to find things." |
| 30 | + "Before we start, let's set up some variables for where to find things." |
31 | 31 | ]
|
32 | 32 | },
|
33 | 33 | {
|
|
96 | 96 | "outputs": [],
|
97 | 97 | "source": [
|
98 | 98 | "!wget -q https://storage.googleapis.com/bert_models/2018_10_18/uncased_L-12_H-768_A-12.zip\n",
|
99 |
| - "!unzip /uncased_L-12_H-768_A-12.zip\n", |
| 99 | + "!unzip uncased_L-12_H-768_A-12.zip\n", |
100 | 100 | "\n",
|
101 | 101 | "!mkdir squad-1.1 out\n",
|
102 | 102 | "\n",
|
103 | 103 | "!wget -O squad-1.1/train-v1.1.json https://rajpurkar.github.io/SQuAD-explorer/dataset/train-v1.1.json \n",
|
104 |
| - "!wget -O squad-1.1/dev-v1.1.json https://rajpurkar.github.io/SQuAD-explorer/dataset/dev-v1.1.json \n", |
105 |
| - "!wget -O squad-1.1/evaluate-v1.1.json https://rajpurkar.github.io/SQuAD-explorer/dataset/evaluate-v1.1.json " |
| 104 | + "!wget -O squad-1.1/dev-v1.1.json https://rajpurkar.github.io/SQuAD-explorer/dataset/dev-v1.1.json \n" |
106 | 105 | ]
|
107 | 106 | },
|
108 | 107 | {
|
109 | 108 | "cell_type": "markdown",
|
110 | 109 | "metadata": {},
|
111 | 110 | "source": [
|
112 | 111 | "## Step 4 - fine tune the Bert model for squad-1.1\n",
|
113 |
| - "This is the same as described in the [Bert repository](https://github.com/google-research/bert). You need to do this only once.\n" |
| 112 | + "This is the same as described in the [Bert repository](https://github.com/google-research/bert). This only needs to be done once.\n" |
114 | 113 | ]
|
115 | 114 | },
|
116 | 115 | {
|
|
121 | 120 | "source": [
|
122 | 121 | "#\n",
|
123 | 122 | "# finetune bert for squad-1.1\n",
|
124 |
| - "# this may take a bit\n", |
| 123 | + "# this will take around 3 hours to complete, and even longer if your device does not have a GPU \n", |
125 | 124 | "#\n",
|
126 | 125 | "\n",
|
127 | 126 | "!cd bert && \\\n",
|
|
146 | 145 | "metadata": {},
|
147 | 146 | "source": [
|
148 | 147 | "## Step 5 - create the inference graph and save it\n",
|
149 |
| - "With a fined tuned model in hands we want to create the inference graph for it and save it as saved_model format.\n", |
| 148 | + "With a fine-tuned model in hands we want to create the inference graph for it and save it as saved_model format.\n", |
150 | 149 | "\n",
|
151 |
| - "***We assune that after 2 epochs the checkpoint is model.ckpt-21899 - if the following code does not find it, check the $OUT directory for the higest checkpoint***." |
| 150 | + "***We assume that after 2 epochs the checkpoint is model.ckpt-21899 - if the following code does not find it, check the $OUT directory for the higest checkpoint***." |
152 | 151 | ]
|
153 | 152 | },
|
154 | 153 | {
|
|
202 | 201 | "cell_type": "markdown",
|
203 | 202 | "metadata": {},
|
204 | 203 | "source": [
|
205 |
| - "Create the model and run predictions on all data and save the results so we can compare them later to the onnxruntime version." |
| 204 | + "Create the model, run predictions on all data, and save the results to later compare them to the onnxruntime version." |
206 | 205 | ]
|
207 | 206 | },
|
208 | 207 | {
|
|
309 | 308 | "scrolled": true
|
310 | 309 | },
|
311 | 310 | "source": [
|
312 |
| - "Now lets create the inference graph and save it." |
| 311 | + "Now let's create the inference graph and save it." |
313 | 312 | ]
|
314 | 313 | },
|
315 | 314 | {
|
|
340 | 339 | "source": [
|
341 | 340 | "## Step 6 - convert to ONNX\n",
|
342 | 341 | "\n",
|
343 |
| - "Convert the model from tensorflow to onnx using https://github.com/onnx/tensorflow-onnx." |
| 342 | + "Convert the model from Tensorflow to ONNX using https://github.com/onnx/tensorflow-onnx." |
344 | 343 | ]
|
345 | 344 | },
|
346 | 345 | {
|
|
393 | 392 | "cell_type": "markdown",
|
394 | 393 | "metadata": {},
|
395 | 394 | "source": [
|
396 |
| - "Lets look at the inputs to the ONNX model. The input 'unique_ids' is special and creates some issue in ONNX: the input is passed directly to the output and in Tensorflow both have the same name. In ONNX that is not supported and the converter creates a new name for the input. We need to use that created name so we remember it." |
| 395 | + "Let's look at the inputs to the ONNX model. The input 'unique_ids' is special and creates an issue in ONNX: the input passed directly to the output and in Tensorflow both have the same name. Because that is not supported in ONNX, the converter creates a new name for the input. We need to use that created name as to remember it." |
397 | 396 | ]
|
398 | 397 | },
|
399 | 398 | {
|
|
552 | 551 | "source": [
|
553 | 552 | "## Summary\n",
|
554 | 553 | "\n",
|
555 |
| - "That was all it takes to convert a relativly complex model from Tensorflow to ONNX. \n", |
| 554 | + "That was all it takes to convert a relatively complex model from Tensorflow to ONNX. \n", |
556 | 555 | "\n",
|
557 |
| - "You find more documentation about tensorflow-onnx [here](https://github.com/onnx/tensorflow-onnx)." |
| 556 | + "You can find more documentation about tensorflow-onnx [here](https://github.com/onnx/tensorflow-onnx)." |
558 | 557 | ]
|
559 | 558 | },
|
560 | 559 | {
|
|
0 commit comments