我正在尝试使用tensorflow TOCO和tf_convert
工具将基于tensorflow(LSTM)的模型转换为tensorflow lite ,但是转换后的tensorflow lite模型约为245MB,其中原始tensorflow mobile约为1MB。
以下是我使用的命令
bazel run --config=opt //tensorflow/contrib/lite/toco:toco -- \
--input_file= <inputfile> \
--output_file= <outputfile> \
--input_format=TENSORFLOW_GRAPHDEF \
--output_format=TFLITE \
--input_shape=1,1\
--input_array=input\
--output_array=output\
--inference_type=QUANTIZED_UINT8 \
--inference_type=FLOAT \
--input_data_type=FLOAT \
--allow_custom_ops
Run Code Online (Sandbox Code Playgroud)
我尝试使用,--allow_custom_ops
并且转换模型的大小与是否使用TOCO工具中的自定义操作相同。
2018-08-27 18:09:18.538279:我tensorflow / contrib / lite / toco / graph_transformations / graph_transformations.cc:39]删除未使用的操作之前:4210个运算符,6021个数组(0量化)2018-08-27 18:09 :19.255416:I tensorflow / contrib / lite / toco / graph_transformations / graph_transformations.cc:39]在通用图变换之前:4210个运算符,6021个数组(0量化)2018-08-27 18:09:20.504422:I tensorflow / contrib / lite / toco / graph_transformations / graph_transformations.cc:39]在通用图形转换通过1:3604运算符之后,6008个数组(0量化)2018-08-27 18:09:21.335526:I tensorflow / contrib / …
我尝试将冻结的SSD mobilenet v2模型转换为TFLITE格式以供Android使用。这是我所有的步骤:
我使用模型动物园的ssd_mobilenet_v2_coco_2018_03_29模型对TF Object Detection API的train.py文件进行了重新训练。(好)
使用TF Object Detection API也提供的export_inference_graph.p y 将训练后的model.ckpt导出到冻结的模型文件。(好)
使用GPU和仅允许CPU在python中测试冻结的图形。有用。(好)
不利之处在于,我尝试使用以下代码:
import tensorflow as tf
tf.enable_eager_execution()
saved_model_dir = 'inference_graph/saved_model/'
converter = tf.contrib.lite.TFLiteConverter.from_saved_model(saved_model_dir,input_arrays=input_arrays,output_arrays=output_arrays,input_shapes={"image_tensor": [1, 832, 832, 3]})
converter.post_training_quantize = True
Run Code Online (Sandbox Code Playgroud)
首先,我尝试不向函数添加输入shapes参数,但是没有用。从那时起,我读到您可以在这里写任何无关紧要的内容。
直到这一行的输出:
INFO:tensorflow:Saver not created because there are no variables in the graph to restore
INFO:tensorflow:The specified SavedModel has no variables; no checkpoints were restored.
INFO:tensorflow:The given SavedModel MetaGraphDef contains SignatureDefs with the following keys: {'serving_default'}
INFO:tensorflow:input …
Run Code Online (Sandbox Code Playgroud) 我正在开发一个带有回归预测的深度学习模型。我创建了一个 tflite 模型,但它的预测与原始模型不同,而且完全错误。这是我的过程:
我用 keras 训练了我的模型
model = Sequential()
model.add(Dense(100, input_dim=x.shape[1], activation='relu')) # Hidden 1
model.add(Dense(50, activation='relu')) # Hidden 2
model.add(Dense(1)) # Output
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(x,y,verbose=0,epochs=500)
Run Code Online (Sandbox Code Playgroud)
并将我的模型保存为 h5 文件
model.save("keras_model.h5")
Run Code Online (Sandbox Code Playgroud)
然后通过 TocoConverter 将 h5 文件转换为 tflile 格式
converter = tf.contrib.lite.TocoConverter.from_keras_model_file("keras_model.h5")
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)
Run Code Online (Sandbox Code Playgroud)
当我用相同的输入测试两个文件时,原始 keras 模型给出了合理的输出,但转换后的模型给出了不合理的输出。
# Load TFLite model and allocate tensors.
interpreter = tf.contrib.lite.Interpreter(model_path="converted_model.tflite")
interpreter.allocate_tensors()
# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
# Test model on random input data.
input_shape = …
Run Code Online (Sandbox Code Playgroud)