TensorFlow Lite-批量推理



我有一个用Python3和TensorFlow2编码的CIFAR-10数据集的修剪和聚类VGG-18 CNN模型。我在计算其准确性的教程中找到的代码一次一个地提供输入,因此所有10000个验证图像都需要很长时间。我考虑将验证图像作为一批输入,并编码如下:

train_dataset = tf.data.Dataset.from_tensor_slices((X_train, y_train))
train_dataset = train_dataset.shuffle(buffer_size = 10000).batch(batch_size)
representative_dataset = tf.data.Dataset.from_tensor_slices(X_test.astype('float32'))
representative_dataset = representative_dataset.shuffle(buffer_size = 10000).batch(batch_size = batch_size)
converter = tf.lite.TFLiteConverter.from_keras_model(clustered_model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
# batch_size = 64
def representative_dataset_gen():
# for i, samples in enumerate(representative_dataset.take(1)):
for i, samples in enumerate(representative_dataset.take(batch_size)):
yield[samples]
converter.representative_dataset = representative_dataset_gen
# Restrict supported target op specification to INT8-
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
tflite_model_int = converter.convert()
with tf.io.gfile.GFile("VGG18_Pruned_Clustered_Trained_Quantized.tflite", 'wb') as file:
file.write(tflite_model_int)
# Load TF Lite file and allocate input & output tensors-
tflite_model_file = 'VGG18_Pruned_Clustered_Trained_Quantized.tflite'
interpreter = tf.lite.Interpreter(model_path = tflite_model_file)
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
interpreter.resize_tensor_input(input_details[0]['index'], (batch_size, 32, 32, 3))
interpreter.resize_tensor_input(output_details[0]['index'], (batch_size, num_classes))
interpreter.allocate_tensors()
input_index = interpreter.get_input_details()[0]["index"]
output_index = interpreter.get_output_details()[0]["index"]
# Prepare validation dataset while generating only one sample at a time-
test_dataset = tf.data.Dataset.from_tensor_slices((X_test, y_test))
test_dataset = test_dataset.batch(batch_size = batch_size)

# Make predictions using Pruned, Trained and Quantized VGG-18 TF Lite model-
predictions = []
test_labels, test_imgs = [], []
data_sample_count = 0
# for img, label in tqdm(test_batches.take(100)):
for img, label in test_dataset.take(10000):
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)
data_sample_count += 1
print(data_sample_count)

这将打印156批64号的产品。我有以下两个问题:

1.(最后一批中只有16个验证图像。我该如何处理,因为对于一批16个图像,代码:

interpreter.set_tensor(input_index, img)

给出错误:

ValueError:无法设置张量:维度不匹配。得了16分,但预期64表示输入50的维度0。

2.(存储在"预测"列表中的"解释器"所做的预测的维度错误,因为:

test_imgs[0].shape
# TensorShape([64, 32, 32, 3])
test_labels[0]
# array([0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], dtype=float32)

这意味着"test_imgs"列表中的每个元素都包含一批64个图像。而"test_label"只包含一个预测标签,而不是相应的64个标签。

如何修复这些错误?

感谢

为了使图在输入大小上灵活,TensorFlow图应该以这样的方式设计。例如,使图形中的批次大小应为None,而不是64。之后,在使用转换后的TFLite模型进行推理时,在设置张量数据之前,应调用intereter.resize_tensor_input方法,用更新后的批量大小更新新的形状信息。

为了获得所有批次的所有测试标签,TF图应该有这样的输出。请查看您的TF图,并使该图生成所有批次的测试标签,以满足您的需求。

最新更新