-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix the bug of compile model to tensorrt
Closes #491 Signed-off-by: zhangkaili <zhang.kaili@zte.com.cn>
- Loading branch information
1 parent
01b8e7f
commit 9a0b266
Showing
83 changed files
with
6,044 additions
and
5,897 deletions.
There are no files selected for viewing
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
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
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
54 changes: 27 additions & 27 deletions
54
model_compiler/src/model_compiler/compilers/keras_model_file_to_keras_model.py
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 |
---|---|---|
@@ -1,27 +1,27 @@ | ||
# Copyright 2019 ZTE corporation. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import tensorflow as tf | ||
from tensorflow import keras | ||
|
||
from . import repository | ||
from .. import utilities | ||
from .. import keras_util | ||
from ..models.irs.keras_model import KerasModel | ||
from ..models.sources.keras_model_file import KerasModelFile | ||
|
||
|
||
@repository.REPOSITORY.register(source_type=KerasModelFile, target_type=KerasModel) | ||
def compile_source(source: KerasModelFile) -> KerasModel: | ||
with tf.Graph().as_default(): | ||
if source.script_path: | ||
with tf.compat.v1.Session(graph=tf.Graph(), config=utilities.get_tf_cpu_only_config()): | ||
custom_objects = keras_util.get_custom_objects(source.script_path) | ||
else: | ||
custom_objects = None | ||
|
||
with tf.compat.v1.Session(config=utilities.get_tf_cpu_only_config()).as_default() as session: | ||
keras.backend.set_learning_phase(0) | ||
model = keras.models.load_model(source.model_path, custom_objects=custom_objects, compile=False) | ||
|
||
return KerasModel(model=model, session=session) | ||
# # Copyright 2019 ZTE corporation. All Rights Reserved. | ||
# # SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# import tensorflow as tf | ||
# from tensorflow import keras | ||
# | ||
# from . import repository | ||
# from .. import utilities | ||
# from .. import keras_util | ||
# from ..models.irs.keras_model import KerasModel | ||
# from ..models.sources.keras_model_file import KerasModelFile | ||
# | ||
# | ||
# @repository.REPOSITORY.register(source_type=KerasModelFile, target_type=KerasModel) | ||
# def compile_source(source: KerasModelFile) -> KerasModel: | ||
# with tf.Graph().as_default(): | ||
# if source.script_path: | ||
# with tf.compat.v1.Session(graph=tf.Graph(), config=utilities.get_tf_cpu_only_config()): | ||
# custom_objects = keras_util.get_custom_objects(source.script_path) | ||
# else: | ||
# custom_objects = None | ||
# | ||
# with tf.compat.v1.Session(config=utilities.get_tf_cpu_only_config()).as_default() as session: | ||
# keras.backend.set_learning_phase(0) | ||
# model = keras.models.load_model(source.model_path, custom_objects=custom_objects, compile=False) | ||
# | ||
# return KerasModel(model=model, session=session) |
48 changes: 24 additions & 24 deletions
48
model_compiler/src/model_compiler/compilers/keras_model_file_to_tflite_model.py
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 |
---|---|---|
@@ -1,24 +1,24 @@ | ||
# Copyright 2019 ZTE corporation. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import tensorflow as tf | ||
|
||
from . import repository | ||
from ..models.sources.keras_model_file import KerasModelFile | ||
|
||
from ..models.targets.tflite_model import TfLiteModel | ||
from .. import tflite_util | ||
from .. import keras_util | ||
|
||
|
||
@repository.REPOSITORY.register(source_type=KerasModelFile, target_type=TfLiteModel, config_type=tflite_util.Config) | ||
def compile_source(source: KerasModelFile, config: tflite_util.Config) -> TfLiteModel: | ||
if source.script_path: | ||
custom_objects = keras_util.get_custom_objects(source.script_path) | ||
else: | ||
custom_objects = None | ||
|
||
model = tf.keras.models.load_model(filepath=source.model_path, custom_objects=custom_objects, compile=False) | ||
converter = tf.lite.TFLiteConverter.from_keras_model(model) | ||
tflite_model = tflite_util.get_tflite_model(converter, config) | ||
return TfLiteModel(tflite_model, config.input_formats) | ||
# # Copyright 2019 ZTE corporation. All Rights Reserved. | ||
# # SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# import tensorflow as tf | ||
# | ||
# from . import repository | ||
# from ..models.sources.keras_model_file import KerasModelFile | ||
# | ||
# from ..models.targets.tflite_model import TfLiteModel | ||
# from .. import tflite_util | ||
# from .. import keras_util | ||
# | ||
# | ||
# @repository.REPOSITORY.register(source_type=KerasModelFile, target_type=TfLiteModel, config_type=tflite_util.Config) | ||
# def compile_source(source: KerasModelFile, config: tflite_util.Config) -> TfLiteModel: | ||
# if source.script_path: | ||
# custom_objects = keras_util.get_custom_objects(source.script_path) | ||
# else: | ||
# custom_objects = None | ||
# | ||
# model = tf.keras.models.load_model(filepath=source.model_path, custom_objects=custom_objects, compile=False) | ||
# converter = tf.lite.TFLiteConverter.from_keras_model(model) | ||
# tflite_model = tflite_util.get_tflite_model(converter, config) | ||
# return TfLiteModel(tflite_model, config.input_formats) |
86 changes: 43 additions & 43 deletions
86
model_compiler/src/model_compiler/compilers/keras_model_file_to_tvm_model.py
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 |
---|---|---|
@@ -1,43 +1,43 @@ | ||
# Copyright 2019 ZTE corporation. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import tensorflow as tf | ||
import tvm | ||
import tvm.relay as relay | ||
|
||
from . import repository | ||
from ..models.sources.keras_model_file import KerasModelFile | ||
from ..models.targets.tvm_model import TvmModel, Input, Output | ||
from ..keras_util import Config, get_inputs, get_outputs, DataFormat | ||
|
||
|
||
def _get_shape_dict(model_inputs, max_batch_size): | ||
shape_dict = {} | ||
for input_tensor, data_format in model_inputs: | ||
tensor_shape = list(input_tensor.shape) | ||
tensor_shape.pop(0) | ||
tensor_shape.insert(0, max_batch_size) | ||
if data_format == DataFormat.CHANNELS_LAST: | ||
tensor_shape[1], tensor_shape[3] = tensor_shape[3], tensor_shape[1] | ||
shape_dict[input_tensor.name] = tensor_shape | ||
return shape_dict | ||
|
||
|
||
@repository.REPOSITORY.register(source_type=KerasModelFile, target_type=TvmModel, config_type=Config) | ||
def compile_source(source: KerasModelFile, config: Config) -> TvmModel: | ||
tf.keras.backend.set_learning_phase(0) | ||
source_model = tf.keras.models.load_model(source.model_path, compile=False) | ||
model_inputs = get_inputs(source_model, config.input_nodes) | ||
|
||
shape_dict = _get_shape_dict(model_inputs, config.max_batch_size) | ||
model, params = relay.frontend.from_keras(source_model, shape_dict) | ||
compiled_lib = relay.build(model, tvm.target.create("llvm"), params=params) | ||
return TvmModel(tvm_model=compiled_lib, | ||
model_inputs=[Input(name=tensor.name, | ||
shape=shape_dict[tensor.name], | ||
data_type=tensor.dtype.as_datatype_enum, | ||
data_format=DataFormat.CHANNELS_FIRST) for tensor, _ in model_inputs], | ||
model_outputs=[Output(name=tensor.name, | ||
shape=list(tensor.shape), | ||
data_type=tensor.dtype.as_datatype_enum) | ||
for tensor in get_outputs(source_model, config.output_nodes)]) | ||
# # Copyright 2019 ZTE corporation. All Rights Reserved. | ||
# # SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# import tensorflow as tf | ||
# import tvm | ||
# import tvm.relay as relay | ||
# | ||
# from . import repository | ||
# from ..models.sources.keras_model_file import KerasModelFile | ||
# from ..models.targets.tvm_model import TvmModel, Input, Output | ||
# from ..keras_util import Config, get_inputs, get_outputs, DataFormat | ||
# | ||
# | ||
# def _get_shape_dict(model_inputs, max_batch_size): | ||
# shape_dict = {} | ||
# for input_tensor, data_format in model_inputs: | ||
# tensor_shape = list(input_tensor.shape) | ||
# tensor_shape.pop(0) | ||
# tensor_shape.insert(0, max_batch_size) | ||
# if data_format == DataFormat.CHANNELS_LAST: | ||
# tensor_shape[1], tensor_shape[3] = tensor_shape[3], tensor_shape[1] | ||
# shape_dict[input_tensor.name] = tensor_shape | ||
# return shape_dict | ||
# | ||
# | ||
# @repository.REPOSITORY.register(source_type=KerasModelFile, target_type=TvmModel, config_type=Config) | ||
# def compile_source(source: KerasModelFile, config: Config) -> TvmModel: | ||
# tf.keras.backend.set_learning_phase(0) | ||
# source_model = tf.keras.models.load_model(source.model_path, compile=False) | ||
# model_inputs = get_inputs(source_model, config.input_nodes) | ||
# | ||
# shape_dict = _get_shape_dict(model_inputs, config.max_batch_size) | ||
# model, params = relay.frontend.from_keras(source_model, shape_dict) | ||
# compiled_lib = relay.build(model, tvm.target.create("llvm"), params=params) | ||
# return TvmModel(tvm_model=compiled_lib, | ||
# model_inputs=[Input(name=tensor.name, | ||
# shape=shape_dict[tensor.name], | ||
# data_type=tensor.dtype.as_datatype_enum, | ||
# data_format=DataFormat.CHANNELS_FIRST) for tensor, _ in model_inputs], | ||
# model_outputs=[Output(name=tensor.name, | ||
# shape=list(tensor.shape), | ||
# data_type=tensor.dtype.as_datatype_enum) | ||
# for tensor in get_outputs(source_model, config.output_nodes)]) |
38 changes: 19 additions & 19 deletions
38
model_compiler/src/model_compiler/compilers/keras_model_to_tf_model.py
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 |
---|---|---|
@@ -1,19 +1,19 @@ | ||
# Copyright 2019 ZTE corporation. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from . import repository | ||
from .. import utilities | ||
from ..models.irs.keras_model import KerasModel | ||
from ..models.irs.tf_model import Input, TensorFlowModel | ||
from ..keras_util import Config, get_inputs, get_outputs | ||
|
||
|
||
@repository.REPOSITORY.register(source_type=KerasModel, target_type=TensorFlowModel, config_type=Config) | ||
def compile_source(source: KerasModel, config: Config) -> TensorFlowModel: | ||
inputs = [Input(tensor=tensor, data_format=data_format) | ||
for tensor, data_format in get_inputs(source.model, config.input_nodes)] | ||
outputs = get_outputs(source.model, config.output_nodes) | ||
utilities.judge_batch_size([model_input.tensor.shape for model_input in inputs], | ||
[model_output.shape for model_output in outputs]) | ||
|
||
return TensorFlowModel(inputs=inputs, outputs=outputs, session=source.session) | ||
# # Copyright 2019 ZTE corporation. All Rights Reserved. | ||
# # SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# from . import repository | ||
# from .. import utilities | ||
# from ..models.irs.keras_model import KerasModel | ||
# from ..models.irs.tf_model import Input, TensorFlowModel | ||
# from ..keras_util import Config, get_inputs, get_outputs | ||
# | ||
# | ||
# @repository.REPOSITORY.register(source_type=KerasModel, target_type=TensorFlowModel, config_type=Config) | ||
# def compile_source(source: KerasModel, config: Config) -> TensorFlowModel: | ||
# inputs = [Input(tensor=tensor, data_format=data_format) | ||
# for tensor, data_format in get_inputs(source.model, config.input_nodes)] | ||
# outputs = get_outputs(source.model, config.output_nodes) | ||
# utilities.judge_batch_size([model_input.tensor.shape for model_input in inputs], | ||
# [model_output.shape for model_output in outputs]) | ||
# | ||
# return TensorFlowModel(inputs=inputs, outputs=outputs, session=source.session) |
Oops, something went wrong.