将autokeras模型转换为移动设备

时间:2020-09-23 13:08:05

标签: keras mobile

我正在尝试使用autokeras制作模型,并将模型部署到移动设备上。 我创建了一个简单的模型,并尝试转换移动模型。

reg = ak.StructuredDataRegressor(max_trials=1, overwrite=True)
reg.fit(X_train, y_train, epochs=2)
model = reg.export_model()
tf.keras.models.save_model(model, "model_test/ak_pb", save_format="tf")
(I also tried with converted Sequential model using Sequential(model.layers))

然后,我尝试将模型转换为移动模型(据我所知),但是在任何方法上都失败了。

  1. 将模型转换为tflite模型
  2. 有人说tflite在某些情况下受到限制(不支持操作员)。因此,请使用原型格式。
  3. 使用tensorflowjs_converter

错误消息大约有3种类型。

  1. InvalidArgumentError:无法将dtype资源的张量转换为NumPy数组。
  2. ValueError:优化StringToNumber之前模型中不支持的操作....
  3. 等 关于错误消息2,负责团队表示他们将尽快实施不受支持的操作...

我不是该领域的专家,所以我无法完全理解错误消息。 我想知道错误是否是因为:

1. coding error?
2. convert routine(converter.convert()) is not perfect?
3. autokeras generated model is different from native keras model?    
  1. 将模型转换为tflite模型
import tensorflow as tf
import autokeras as ak
from tensorflow.keras.models import load_model
model_tf = load_model("model_test/ak_pb")
# model = load_model("model_test/ak_pb", custom_objects=ak.CUSTOM_OBJECTS)
model_tf.summary()
converter = tf.lite.TFLiteConverter.from_keras_model(model_tf)

输出为:(省略了一些警告消息...)

Model: "functional_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         [(None, 132)]             0         
_________________________________________________________________
multi_category_encoding (Mul (None, 132)               0         
_________________________________________________________________
dense (Dense)                (None, 32)                4256      
_________________________________________________________________
re_lu (ReLU)                 (None, 32)                0         
_________________________________________________________________
dense_1 (Dense)              (None, 16)                528       
_________________________________________________________________
re_lu_1 (ReLU)               (None, 16)                0         
_________________________________________________________________
regression_head_1 (Dense)    (None, 1)                 17        
=================================================================
Total params: 4,801
Trainable params: 4,801
Non-trainable params: 0
_________________________________________________________________
WARNING:tensorflow:From /home/gb/miniconda3/envs/tfconv/lib/python3.6/site-packages/tensorflow/python/training/tracking/tracking.py:111: Model.state_updates (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version.
Instructions for updating:
This property should not be used in TensorFlow 2.0, as updates are applied automatically.
WARNING:tensorflow:From /home/gb/miniconda3/envs/tfconv/lib/python3.6/site-packages/tensorflow/python/training/tracking/tracking.py:111: Layer.updates (from tensorflow.python.keras.engine.base_layer) is deprecated and will be removed in a future version.
Instructions for updating:
This property should not be used in TensorFlow 2.0, as updates are applied automatically.
INFO:tensorflow:Assets written to: /tmp/tmphq0lh3_e/assets
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-3-f4dca08044b1> in <module>
     12 converter.allow_custom_ops=False
     13 
---> 14 tflite_model = converter.convert()

~/miniconda3/envs/tfconv/lib/python3.6/site-packages/tensorflow/lite/python/lite.py in convert(self)
    807     frozen_func, graph_def = (
    808         _convert_to_constants.convert_variables_to_constants_v2_as_graph(
--> 809             self._funcs[0], lower_control_flow=False))
    810 
    811     input_tensors = [

~/miniconda3/envs/tfconv/lib/python3.6/site-packages/tensorflow/python/framework/convert_to_constants.py in convert_variables_to_constants_v2_as_graph(func, lower_control_flow, aggressive_inlining)
   1101       func=func,
   1102       lower_control_flow=lower_control_flow,
-> 1103       aggressive_inlining=aggressive_inlining)
   1104 
   1105   output_graph_def, converted_input_indices = _replace_variables_by_constants(

~/miniconda3/envs/tfconv/lib/python3.6/site-packages/tensorflow/python/framework/convert_to_constants.py in __init__(self, func, lower_control_flow, aggressive_inlining, variable_names_whitelist, variable_names_blacklist)
    802         variable_names_whitelist=variable_names_whitelist,
    803         variable_names_blacklist=variable_names_blacklist)
--> 804     self._build_tensor_data()
    805 
    806   def _build_tensor_data(self):

~/miniconda3/envs/tfconv/lib/python3.6/site-packages/tensorflow/python/framework/convert_to_constants.py in _build_tensor_data(self)
    821         data = map_index_to_variable[idx].numpy()
    822       else:
--> 823         data = val_tensor.numpy()
    824       self._tensor_data[tensor_name] = _TensorData(
    825           numpy=data,

~/miniconda3/envs/tfconv/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in numpy(self)
   1061     """
   1062     # TODO(slebedev): Consider avoiding a copy for non-CPU or remote tensors.
-> 1063     maybe_arr = self._numpy()  # pylint: disable=protected-access
   1064     return maybe_arr.copy() if isinstance(maybe_arr, np.ndarray) else maybe_arr
   1065 

~/miniconda3/envs/tfconv/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in _numpy(self)
   1029       return self._numpy_internal()
   1030     except core._NotOkStatusException as e:  # pylint: disable=protected-access
-> 1031       six.raise_from(core._status_to_exception(e.code, e.message), None)  # pylint: disable=protected-access
   1032 
   1033   @property

~/.local/lib/python3.6/site-packages/six.py in raise_from(value, from_value)

InvalidArgumentError: Cannot convert a Tensor of dtype resource to a NumPy array.
  1. 尝试原型格式。
from tensorflow.compat.v1 import graph_util
import tensorflow.keras.backend as K
from tensorflow.python.framework.convert_to_constants import     convert_variables_to_constants_v2
import numpy as np
print (f'eager execution mode should be True == {tf.executing_eagerly()}\n')

from tensorflow.keras.models import load_model
# model_tf = load_model("model_test/ak_pb")
model_tf = load_model("model_test/ak_pb", custom_objects=ak.CUSTOM_OBJECTS)

full_model = tf.function(lambda x: model_tf(x))
full_model = full_model.get_concrete_function(
        tf.TensorSpec(model_tf.inputs[0].shape, model_tf.inputs[0].dtype))

# Get frozen ConcreteFunction
frozen_func = convert_variables_to_constants_v2(full_model)
# frozen_func.graph.as_graph_def()

layers = [op.name for op in frozen_func.graph.get_operations()]

# Save frozen graph from frozen ConcreteFunction to hard drive
tf.io.write_graph(graph_or_graph_def=frozen_func.graph,
                  logdir="frozen_models", name="frozen_graph.pb", as_text=False)

它产生,

InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-4-8a2e5ff939d8> in <module>
     16 
     17 # Get frozen ConcreteFunction
---> 18 frozen_func = convert_variables_to_constants_v2(full_model)
     19 # frozen_func.graph.as_graph_def()
     20 

~/miniconda3/envs/tfconv/lib/python3.6/site-packages/tensorflow/python/framework/convert_to_constants.py in convert_variables_to_constants_v2(func, lower_control_flow, aggressive_inlining)
   1067       func=func,
   1068       lower_control_flow=lower_control_flow,
-> 1069       aggressive_inlining=aggressive_inlining)
   1070 
   1071   output_graph_def, converted_input_indices = _replace_variables_by_constants(

~/miniconda3/envs/tfconv/lib/python3.6/site-packages/tensorflow/python/framework/convert_to_constants.py in __init__(self, func, lower_control_flow, aggressive_inlining, variable_names_whitelist, variable_names_blacklist)
    802         variable_names_whitelist=variable_names_whitelist,
    803         variable_names_blacklist=variable_names_blacklist)
--> 804     self._build_tensor_data()
    805 
    806   def _build_tensor_data(self):

~/miniconda3/envs/tfconv/lib/python3.6/site-packages/tensorflow/python/framework/convert_to_constants.py in _build_tensor_data(self)
    821         data = map_index_to_variable[idx].numpy()
    822       else:
--> 823         data = val_tensor.numpy()
    824       self._tensor_data[tensor_name] = _TensorData(
    825           numpy=data,

~/miniconda3/envs/tfconv/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in numpy(self)
   1061     """
   1062     # TODO(slebedev): Consider avoiding a copy for non-CPU or remote tensors.
-> 1063     maybe_arr = self._numpy()  # pylint: disable=protected-access
   1064     return maybe_arr.copy() if isinstance(maybe_arr, np.ndarray) else maybe_arr
   1065 

~/miniconda3/envs/tfconv/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in _numpy(self)
   1029       return self._numpy_internal()
   1030     except core._NotOkStatusException as e:  # pylint: disable=protected-access
-> 1031       six.raise_from(core._status_to_exception(e.code, e.message), None)  # pylint: disable=protected-access
   1032 
   1033   @property

~/.local/lib/python3.6/site-packages/six.py in raise_from(value, from_value)

InvalidArgumentError: Cannot convert a Tensor of dtype resource to a NumPy array.
  1. 使用tensorflowjs_converter

<< input_format = tf_saved_model >>

(tfjsv) D:\0.Projects\autokeras_test\tfjs>tensorflowjs_converter --input_format tf_saved_model --output_format tfjs_graph_model D:\0.Projects\autokeras_test\model_test\ak_pb D:\0.Projects\autokeras_test\model_test\res\

2020-09-18 21:44:20.651344: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x2d1bea1ea00 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-09-18 21:44:20.651688: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
2020-09-18 21:44:23.029133: I tensorflow/core/grappler/devices.cc:78] Number of eligible GPUs (core count >= 8, compute capability >= 0.0): 0 (Note: TensorFlow was not compiled with CUDA or ROCm support)
2020-09-18 21:44:23.029870: I tensorflow/core/grappler/clusters/single_machine.cc:356] Starting new session
2020-09-18 21:44:23.338069: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: graph_to_optimize
2020-09-18 21:44:23.340454: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818]   function_optimizer: Graph size after: 537 nodes (528), 910 edges (901), time = 147.066ms.
2020-09-18 21:44:23.340631: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818]   function_optimizer: function_optimizer did nothing. time = 0.629ms.
Traceback (most recent call last):
  File "D:\0.Projects\autokeras_test\tfjs\pyenv-win\versions\3.6.8\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "D:\0.Projects\autokeras_test\tfjs\pyenv-win\versions\3.6.8\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "D:\0.Projects\autokeras_test\tfjs\tfjsv\Scripts\tensorflowjs_converter.exe\__main__.py", line 9, in <module>
  File "d:\0.projects\autokeras_test\tfjs\tfjsv\lib\site-packages\tensorflowjs\converters\converter.py", line 757, in pip_main
    main([' '.join(sys.argv[1:])])
  File "d:\0.projects\autokeras_test\tfjs\tfjsv\lib\site-packages\tensorflowjs\converters\converter.py", line 761, in main
    convert(argv[0].split(' '))
  File "d:\0.projects\autokeras_test\tfjs\tfjsv\lib\site-packages\tensorflowjs\converters\converter.py", line 699, in convert
    experiments=args.experiments)
  File "d:\0.projects\autokeras_test\tfjs\tfjsv\lib\site-packages\tensorflowjs\converters\tf_saved_model_conversion_v2.py", line 552, in convert_tf_saved_model
    experiments=experiments)
  File "d:\0.projects\autokeras_test\tfjs\tfjsv\lib\site-packages\tensorflowjs\converters\tf_saved_model_conversion_v2.py", line 143, in optimize_graph
    ', '.join(unsupported))
ValueError: Unsupported Ops in the model before optimization
StringToNumber, IsNan

<< --input_format = tf_frozen_model >>

(tfjsv) D:\0.Projects\autokeras_test\tfjs>tensorflowjs_converter --input_format tf_frozen_model --output_format tfjs_graph_model --output_node_names=functional_1/regression_head_1/BiasAdd D:\0.Projects\autokeras_test\model_test\frozen_graph.pb D:\0.Projects\autokeras_test\model_test\res\

Traceback (most recent call last):
  File "D:\0.Projects\autokeras_test\tfjs\pyenv-win\versions\3.6.8\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "D:\0.Projects\autokeras_test\tfjs\pyenv-win\versions\3.6.8\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "D:\0.Projects\autokeras_test\tfjs\tfjsv\Scripts\tensorflowjs_converter.exe\__main__.py", line 9, in <module>
  File "d:\0.projects\autokeras_test\tfjs\tfjsv\lib\site-packages\tensorflowjs\converters\converter.py", line 757, in pip_main
    main([' '.join(sys.argv[1:])])
  File "d:\0.projects\autokeras_test\tfjs\tfjsv\lib\site-packages\tensorflowjs\converters\converter.py", line 761, in main
    convert(argv[0].split(' '))
  File "d:\0.projects\autokeras_test\tfjs\tfjsv\lib\site-packages\tensorflowjs\converters\converter.py", line 744, in convert
    experiments=args.experiments)
  File "d:\0.projects\autokeras_test\tfjs\tfjsv\lib\site-packages\tensorflowjs\converters\tf_saved_model_conversion_v2.py", line 433, in convert_tf_frozen_model
    experiments=experiments)
  File "d:\0.projects\autokeras_test\tfjs\tfjsv\lib\site-packages\tensorflowjs\converters\tf_saved_model_conversion_v2.py", line 143, in optimize_graph
    ', '.join(unsupported))
ValueError: Unsupported Ops in the model before optimization
IsNan, StringToNumber

0 个答案:

没有答案