【发布时间】:2020-01-03 21:13:04
【问题描述】:
我目前正在尝试将 RNN 模型转换为 TF lite。在多次尝试失败后,我尝试运行在找到here 的存储库中给出的示例。由于图层定义位置的更改,这也会引发错误。一旦固定到下面的代码
import os
os.environ['TF_ENABLE_CONTROL_FLOW_V2'] = '1'
import tensorflow as tf
import numpy as np
from tensorflow_core.lite.experimental.examples.lstm.rnn_cell import TFLiteLSTMCell
from tensorflow_core.lite.experimental.examples.lstm.rnn import dynamic_rnn
# Step 1: Build the MNIST LSTM model.
def buildLstmLayer(inputs, num_layers, num_units):
"""Build the lstm layer.
Args:
inputs: The input data.
num_layers: How many LSTM layers do we want.
num_units: The unmber of hidden units in the LSTM cell.
"""
lstm_cells = []
for i in range(num_layers):
lstm_cells.append(
# tf.lite.experimental.nn.TFLiteLSTMCell(
# num_units, forget_bias=0, name='rnn{}'.format(i)))
TFLiteLSTMCell(
num_units, forget_bias=0, name='rnn{}'.format(i)))
lstm_layers = tf.keras.layers.StackedRNNCells(lstm_cells)
# Assume the input is sized as [batch, time, input_size], then we're going
# to transpose to be time-majored.
transposed_inputs = tf.transpose(
inputs, perm=[1, 0, 2])
# outputs, _ = tf.lite.experimental.nn.dynamic_rnn(
outputs, _ = dynamic_rnn(
lstm_layers,
transposed_inputs,
dtype='float32',
time_major=True)
unstacked_outputs = tf.unstack(outputs, axis=0)
return unstacked_outputs[-1]
# tf.reset_default_graph()
model = tf.keras.models.Sequential([
tf.keras.layers.Input(shape=(28, 28), name='input'),
tf.keras.layers.Lambda(buildLstmLayer, arguments={'num_layers' : 2, 'num_units' : 64}),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(10, activation=tf.nn.softmax, name='output')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.summary()
# Step 2: Train & Evaluate the model.
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
# Cast x_train & x_test to float32.
x_train = x_train.astype(np.float32)
x_test = x_test.astype(np.float32)
model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test)
# Step 3: Convert the Keras model to TensorFlow Lite model.
sess = tf.keras.backend.get_session()
input_tensor = sess.graph.get_tensor_by_name('input:0')
output_tensor = sess.graph.get_tensor_by_name('output/Softmax:0')
converter = tf.lite.TFLiteConverter.from_session(
sess, [input_tensor], [output_tensor])
tflite = converter.convert()
print('Model converted successfully!')
# Step 4: Check the converted TensorFlow Lite model.
interpreter = tf.lite.Interpreter(model_content=tflite)
try:
interpreter.allocate_tensors()
except ValueError:
assert False
MINI_BATCH_SIZE = 1
correct_case = 0
for i in range(len(x_test)):
input_index = (interpreter.get_input_details()[0]['index'])
interpreter.set_tensor(input_index, x_test[i * MINI_BATCH_SIZE: (i + 1) * MINI_BATCH_SIZE])
interpreter.invoke()
output_index = (interpreter.get_output_details()[0]['index'])
result = interpreter.get_tensor(output_index)
# Reset all variables so it will not pollute other inferences.
interpreter.reset_all_variables()
# Evaluate.
prediction = np.argmax(result)
if prediction == y_test[i]:
correct_case += 1
print('TensorFlow Lite Evaluation result is {}'.format(correct_case * 1.0 / len(x_test)))
我继续收到错误
Traceback (most recent call last):
File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/ops/variable_scope.py", line 2346, in _enter_scope_uncached
entered_pure_variable_scope = pure_variable_scope.__enter__()
File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/ops/variable_scope.py", line 1912, in __enter__
constraint=self._constraint)
File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/ops/variable_scope.py", line 1073, in __init__
raise NotImplementedError("Caching devices is not yet supported "
NotImplementedError: Caching devices is not yet supported when eager execution is enabled.
在使用 tf.compat.v1.disable_eager_execution() 禁用急切执行后,我得到了错误
Traceback (most recent call last):
File "/home/xyz/genesis-dnn-se/TFLite_example.py", line 46, in <module>
tf.keras.layers.Dense(10, activation=tf.nn.softmax, name='output')
File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/training/tracking/base.py", line 457, in _method_wrapper
result = method(self, *args, **kwargs)
File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/sequential.py", line 114, in __init__
self.add(layer)
File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/training/tracking/base.py", line 457, in _method_wrapper
result = method(self, *args, **kwargs)
File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/sequential.py", line 196, in add
output_tensor = layer(self.outputs[0])
File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py", line 847, in __call__
outputs = call_fn(cast_inputs, *args, **kwargs)
File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/keras/layers/core.py", line 795, in call
return self.function(inputs, **arguments)
File "/home/xyz/genesis-dnn-se/TFLite_example.py", line 37, in buildLstmLayer
time_major=True)
File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/lite/experimental/examples/lstm/rnn.py", line 266, in dynamic_rnn
dtype=dtype)
File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/ops/rnn.py", line 916, in _dynamic_rnn_loop
swap_memory=swap_memory)
File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/ops/control_flow_ops.py", line 2675, in while_loop
back_prop=back_prop)
File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/ops/while_v2.py", line 198, in while_loop
add_control_dependencies=add_control_dependencies)
File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/framework/func_graph.py", line 915, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/ops/while_v2.py", line 176, in wrapped_body
outputs = body(*_pack_sequence_as(orig_loop_vars, args))
File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/ops/rnn.py", line 884, in _time_step
(output, new_state) = call_cell()
File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/ops/rnn.py", line 870, in <lambda>
call_cell = lambda: cell(input_t, state)
File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py", line 847, in __call__
outputs = call_fn(cast_inputs, *args, **kwargs)
File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/python/keras/layers/recurrent.py", line 137, in call
inputs, states = cell.call(inputs, states, **kwargs)
File "/home/xyz/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/lite/experimental/examples/lstm/rnn_cell.py", line 440, in call
if input_size.value is None:
AttributeError: 'int' object has no attribute 'value'
谁有在 TensorFlow 2.0 中将 RNN(LSTM、GRU、CustomRNN)转换为 TFLite 的工作示例。我使用的是 TF 版本 2.0.0。
【问题讨论】:
-
同样的问题....真是令人沮丧
-
2020 年 4 月谷歌发布了一个新的转换器来支持 TFLite 中的 RNN colab.research.google.com/github/tensorflow/tensorflow/blob/…
标签: python recurrent-neural-network tensorflow2.0 tensorflow-lite tf.keras