Skip to content

Commit 7a0c522

Browse files
committed
update:convert_model_to_edge_tpu_tflite_format_for_google_coral
1 parent 9119e0f commit 7a0c522

File tree

1 file changed

+22
-63
lines changed

1 file changed

+22
-63
lines changed

docs/Edge/Raspberry_Pi_Devices/reComputer_R1000/Applications/Computer-Vision/convert_model_to_edge_tpu_tflite_format_for_google_coral.md

Lines changed: 22 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,16 @@ optional arguments:
120120
```
121121
### Convert Tensorflow Model to TFlite Model
122122

123-
:::note
124-
If you want to optimize your model please check the [Optimize Tensorflow Model](https://www.tensorflow.org/lite/performance/model_optimization)
125-
:::
123+
126124
```
127125
tflite_convert --saved_model_dir=YOUR_MODEL_PATH --output_file=YOUR_MODEL_NAME.tflite
128126
```
129127
### Convert TFlite Model to Edge TPU Model
130128

129+
:::note
130+
You should optimize your model before you convert tflite model to edge tup model, please check the [Optimize Tensorflow Model](https://www.tensorflow.org/lite/performance/model_optimization)
131+
:::
132+
131133
#### Install edgetpu compiler
132134

133135
```
@@ -155,74 +157,38 @@ And then you should get a new file named `YOUR_MODEL_NAME_edgetpu.tflite`
155157
We do not recommend this approach because there are many conflicting packages in the actual process. And TensorFlow Lite supports a limited set of operations, some PyTorch operations may not be supported.
156158
:::
157159

158-
### Convert Pytorch model to ONNX model
160+
### Convert Pytorch model to tflite model
159161

160-
#### Install Pytorch
162+
#### Install dependencies
161163

162164
```
163-
pip3 install torch torchvision torchaudio
164-
```
165-
#### Install ONNX
166-
167-
```
168-
pip3 install onnx
165+
pip install -r https://github.com/google-ai-edge/ai-edge-torch/releases/download/v0.1.1/requirements.txt
166+
pip install ai-edge-torch==0.1.1
169167
```
170168

171169
#### Convert
172-
173-
Here is the function to convert PyTorch model to ONNX model:
174-
```
175-
torch.onnx.export(
176-
model, # PyTorch Model
177-
sample_input, # Input tensor
178-
onnx_model_path, # Output file (eg. 'output_model.onnx')
179-
opset_version=12, # Operator support version
180-
input_names=['input'] # Input tensor name (arbitary)
181-
output_names=['output'] # Output tensor name (arbitary)
182-
)
183170
```
171+
import ai_edge_torch
172+
import numpy
173+
import torch
174+
import torchvision
184175
185-
Here is the code to convert PyTorch model to ONNX model:
186-
```
187-
import torch
188-
import onnx
189-
import torchvision
190-
191-
# Load PyTorch model
192-
model = torchvision.models.resnet18(pretrained=True)
193-
194-
# Set input shape of the model
195-
input_shape = (1, 3, 224, 224)
196-
197-
# Export PyTorch model to ONNX format
198-
torch.onnx.export(model, torch.randn(input_shape), 'resnet18.onnx', opset_version=11)
199-
```
200176
201-
### Convert ONNX model to TF model
177+
resnet18 = torchvision.models.resnet18(torchvision.models.ResNet18_Weights.IMAGENET1K_V1).eval()
178+
sample_inputs = (torch.randn(1, 3, 224, 224),)
179+
torch_output = resnet18(*sample_inputs)
202180
203-
#### Install Tensorflow and onnx-tf
181+
edge_model = ai_edge_torch.convert(resnet18.eval(), sample_inputs)
204182
183+
edge_model.export('resnet.tflite')
205184
```
206-
pip3 install tensorflow
207-
pip3 install onnx-tf
208-
pip install -U tensorflow-probability
209-
```
210-
#### convert
211-
212-
```
213-
import onnx
214-
import onnx_tf
215185

216-
# Load ONNX model
217-
onnx_model = onnx.load('resnet18.onnx')
218-
# Convert ONNX model to TensorFlow format
219-
tf_model = onnx_tf.backend.prepare(onnx_model)
220-
# Export TensorFlow model
221-
tf_model.export_graph("resnet18.tf")
222-
```
186+
You will get ```resnet.tflite```
223187

224188
### Check tflite_converter
225-
189+
:::note
190+
You should optimize your model before you convert tflite model to edge tup model, please check the [Optimize Tensorflow Model](https://www.tensorflow.org/lite/performance/model_optimization)
191+
:::
226192
```
227193
tflite_convert -h
228194
```
@@ -258,14 +224,7 @@ optional arguments:
258224
Experimental flag, subject to change. Enables MLIR-based quantizer instead of flatbuffer conversion. (default True)
259225
260226
```
261-
### Convert Tensorflow Model to TFlite Model
262227

263-
:::note
264-
If you want to optimize your model please check the [Optimize Tensorflow Model](https://www.tensorflow.org/lite/performance/model_optimization)
265-
:::
266-
```
267-
tflite_convert --saved_model_dir=resnet18 --output_file=resnet18.tflite
268-
```
269228
### Convert TFlite Model to Edge TPU Model
270229

271230
#### Install edgetpu compiler

0 commit comments

Comments
 (0)