|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +import os |
| 19 | +from tvm import relay |
| 20 | +from tvm.contrib.download import download_testdata |
| 21 | +import tflite.Model |
| 22 | + |
| 23 | + |
| 24 | +################################################ |
| 25 | +# Utils for downloading and extracting zip files |
| 26 | +# ---------------------------------------------- |
| 27 | +def extract(path): |
| 28 | + import tarfile |
| 29 | + if path.endswith("tgz") or path.endswith("gz"): |
| 30 | + dir_path = os.path.dirname(path) |
| 31 | + tar = tarfile.open(path) |
| 32 | + tar.extractall(path=dir_path) |
| 33 | + tar.close() |
| 34 | + else: |
| 35 | + raise RuntimeError('Could not decompress the file: ' + path) |
| 36 | + |
| 37 | + |
| 38 | +################################### |
| 39 | +# Download TFLite pre-trained model |
| 40 | +# --------------------------------- |
| 41 | + |
| 42 | +model_url = "https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_1.4_224.tgz" |
| 43 | +model_path = download_testdata(model_url, "mobilenet_v2_1.4_224.tgz", module=['tf', 'official']) |
| 44 | +model_dir = os.path.dirname(model_path) |
| 45 | +extract(model_path) |
| 46 | + |
| 47 | +# now we have mobilenet_v2_1.4_224.tflite on disk |
| 48 | +model_file = os.path.join(model_dir, "mobilenet_v2_1.4_224.tflite") |
| 49 | + |
| 50 | +# get TFLite model from buffer |
| 51 | +tflite_model_buf = open(model_file, "rb").read() |
| 52 | +tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0) |
| 53 | + |
| 54 | + |
| 55 | +############################## |
| 56 | +# Load Neural Network in Relay |
| 57 | +# ---------------------------- |
| 58 | + |
| 59 | +# TFLite input tensor name, shape and type |
| 60 | +input_tensor = "input" |
| 61 | +input_shape = (1, 224, 224, 3) |
| 62 | +input_dtype = "float32" |
| 63 | + |
| 64 | +# parse TFLite model and convert into Relay computation graph |
| 65 | +mod, params = relay.frontend.from_tflite(tflite_model, |
| 66 | + shape_dict={input_tensor: input_shape}, |
| 67 | + dtype_dict={input_tensor: input_dtype}) |
| 68 | + |
| 69 | +############# |
| 70 | +# Compilation |
| 71 | +# ----------- |
| 72 | + |
| 73 | +target = 'llvm' |
| 74 | + |
| 75 | +# Build with Relay |
| 76 | +with relay.build_config(opt_level=3): |
| 77 | + graph, lib, params = relay.build_module.build( |
| 78 | + mod, target, params=params) |
| 79 | + |
| 80 | +############################################### |
| 81 | +# Save the graph, lib and parameters into files |
| 82 | +# --------------------------------------------- |
| 83 | + |
| 84 | +lib.export_library("./mobilenet.so") |
| 85 | +print('lib export succeefully') |
| 86 | + |
| 87 | +with open("./mobilenet.json", "w") as fo: |
| 88 | + fo.write(graph) |
| 89 | + |
| 90 | +with open("./mobilenet.params", "wb") as fo: |
| 91 | + fo.write(relay.save_param_dict(params)) |
0 commit comments