python - 在 VGG16 模型和 Tensorflow lite 中使用 converter.optimization 时预测时间长

标签 python tensorflow machine-learning tensorflow-lite

我写了一个基于 VGG16 的模型,我只添加了两个额外的卷积层。输出是一个大小为 16x16x1 的数组,这只是简单二进制分类的结果。我使用了 TensorFlow-lite,代码基于可用的文档。问题是,当我使用模型进行预测时,需要很长时间(将近 5 分钟)才能得到结果。 我在 GPU、Python 3.7 上使用 Tensorflow 2.4,我的显卡是 GTX 1660Ti(移动版),CPU 是 intel i7 9750H。
代码如下。

import tensorflow as tf
import os
import time
import numpy as np
import cv2
import keras
import pathlib

saved_model_dir= 'model/'
saved_modelh5 = 'model.h5'
dataset_path = 'bound box dataset/img'
out_path = 'converted_model.tflite'
num_calibration_steps = 10


#-----------------------------------------------------------
images = []
for file in os.listdir(dataset_path):
    img = cv2.imread( os.path.join(dataset_path,file) )
    images.append(img)
images = np.array( images )

imgs_tensor = tf.cast( images, dtype = tf.float32)/255.0
ds = tf.data.Dataset.from_tensor_slices((imgs_tensor)).batch(1)
print('data loaded')

#-----------------------------------------------------------
def representative_dataset_gen():
  for input_value in ds.take(num_calibration_steps):
    yield [input_value]




#converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
converter = tf.lite.TFLiteConverter.from_keras_model(keras.models.load_model(saved_modelh5))  
converter.optimizations = [tf.lite.Optimize.DEFAULT ]
#converter.representative_dataset = tf.lite.RepresentativeDataset( representative_dataset_gen )
#converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
tflite_model = converter.convert()


#------------------------------------------------------------
#with open(out_path, "wb")as f:
#    f.write(tflite_model)
print('converted')
tflite_model_file = pathlib.Path(out_path)
tflite_model_file.write_bytes(tflite_model)
print('Saved')



img = cv2.imread('bound box dataset/img/1.png')
input_data = img.reshape(1,512,512,3).astype(np.float32)/255.0

interpreter = tf.lite.Interpreter( model_content = tflite_model)
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.
t = time.time()
input_shape = input_details[0]['shape']
#input_data = np.array(np.random.random_sample(input_shape), dtype=np.float32)
interpreter.set_tensor(input_details[0]['index'], input_data)

interpreter.invoke()

# The function `get_tensor()` returns a copy of the tensor data.
# Use `tensor()` in order to get a pointer to the tensor.
output_data = interpreter.get_tensor(output_details[0]['index'])

t = time.time() - t
print('predict time:',t)

最佳答案

首先,您的 GPU 并未计算此预测。您必须使用 cuda 将数据传输到 gpu,但这不是必需的。

  1. Reshape 您的图像为 (256,256) 或更小,尺寸为 (512, 512) 图像对于 VGG 输入来说是非常大的倍。这就是您的计算需要这么长时间的原因。

  2. 我的下一个建议是改用更新的架构,例如 ResNet50。

关于python - 在 VGG16 模型和 Tensorflow lite 中使用 converter.optimization 时预测时间长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66086579/

相关文章:

python - DataFrame 的元组列表。元素列,元组长度列

tensorflow - 如何使用Tensorflow在Keras中禁用GPU?

python - Macro VS Micro VS Weighted VS Samples F1 分数

python - 在二进制网格中扩展 block ( dilation )

python - 将 MySql 数据库值拉入列表存储 - Python3 和 Mysql.connector

javascript - tensorflow : Uncaught (in promise) TypeError: Cannot read property 'length' of undefined

python - Python Tensorflow 中的人脸识别系统

machine-learning - 股市预测或有关该主题的书籍中使用的模型示例有哪些?

python - python sqlite3中的递归(级联)选择

azure - CUDA 在 Azure 数据科学虚拟机上不可用