我正在使用Tensorflow Lite进行一个TinyML项目,该项目包括量化模型和浮点模型。在我的管道中,我使用tf.keras API训练我的模型,然后将模型转换成TFLite模型。最后,将TFLite模型量化为int8模型。
我可以使用API model.save和tf.keras.model.load_model保存和加载“正常”的tensorflow模型
对转换后的TFLite模型也可以这样做吗?每次都要经历量化过程,这是相当耗时的。
发布于 2021-06-25 14:33:13
您可以使用tflite解释器直接在笔记本中从TFLite模型中获得推断。
这是一个图像分类模型的例子。假设我们有一个tflite模型:
tflite_model_file = 'converted_model.tflite'然后我们可以像这样加载和测试它:
# Load TFLite model and allocate tensors.
with open(tflite_model_file, 'rb') as fid:
tflite_model = fid.read()
interpreter = tf.lite.Interpreter(model_content=tflite_model)
interpreter.allocate_tensors()
input_index = interpreter.get_input_details()[0]["index"]
output_index = interpreter.get_output_details()[0]["index"]
# Gather results for the randomly sampled test images
predictions = []
test_labels, test_imgs = [], []
for img, label in tqdm(test_batches.take(10)):
interpreter.set_tensor(input_index, img)
interpreter.invoke()
predictions.append(interpreter.get_tensor(output_index))
test_labels.append(label.numpy()[0])
test_imgs.append(img)请注意,您只需从tflite模型推断。您无法在体系结构和层中进行更改,比如重新加载Keras模型。如果您想要更改架构,您应该保存Keras模型,并对其进行测试,直到得到满意的结果,然后将其转换为tflite。
https://stackoverflow.com/questions/68132431
复制相似问题