【发布时间】:2020-05-28 11:37:38
【问题描述】:
我正在尝试用视频测试我导出的Mobilenet v2 SSDLitemodel(https://drive.google.com/open?id=1htyBE6R62yVCV8v-9muEJ_lGmoPxQMmJ)。然后我找到了答案here,我在某处修改以适应我的模型:
import cv2
from PIL import Image
import numpy as np
import tensorflow as tf
def read_tensor_from_readed_frame(frame, input_height=300, input_width=300,
input_mean=128, input_std=128):
output_name = "normalized"
# float_caster = tf.cast(frame, tf.float32)
float_caster = tf.cast(frame, tf.uint8)
dims_expander = tf.expand_dims(float_caster, 0);
resized = tf.image.resize_bilinear(dims_expander, [input_height, input_width])
normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std])
sess = tf.Session()
result = sess.run(normalized)
return result
def load_labels(label_file):
label = []
proto_as_ascii_lines = tf.gfile.GFile(label_file).readlines()
for l in proto_as_ascii_lines:
label.append(l.rstrip())
return label
def VideoSrcInit(paath):
cap = cv2.VideoCapture(paath)
flag, image = cap.read()
if flag:
print("Valid Video Path. Lets move to detection!")
else:
raise ValueError("Video Initialization Failed. Please make sure video path is valid.")
return cap
def main():
Labels_Path = "C:/MachineLearning/CV/coco-labelmap.txt"
Model_Path = "C:/MachineLearning/CV/previous_float_model_converted_from_ssd_mobilenet_v2_quantized_300x300_coco_2019_01_03.tflite"
input_path = "C:/MachineLearning/CV/Object_Tracking/video2.mp4"
##Loading labels
labels = load_labels(Labels_Path)
##Load tflite model and allocate tensors
interpreter = tf.lite.Interpreter(model_path=Model_Path)
interpreter.allocate_tensors()
# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
input_shape = input_details[0]['shape']
##Read video
cap = VideoSrcInit(input_path)
while True:
ok, cv_image = cap.read()
if not ok:
break
##Converting the readed frame to RGB as opencv reads frame in BGR
image = Image.fromarray(cv_image).convert('RGB')
##Converting image into tensor
image_tensor = read_tensor_from_readed_frame(image ,300, 300)
##Test model
interpreter.set_tensor(input_details[0]['index'], image_tensor)
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])
## You need to check the output of the output_data variable and
## map it on the frame in order to draw the bounding boxes.
cv2.namedWindow("cv_image", cv2.WINDOW_NORMAL)
cv2.imshow("cv_image",cv_image)
##Use p to pause the video and use q to termiate the program
key = cv2.waitKey(10) & 0xFF
if key == ord("q"):
break
elif key == ord("p"):
cv2.waitKey(0)
continue
cap.release()
if __name__ == '__main__':
main()
当我在我的 tflite 模型上运行这个脚本时,FPS 几乎仍然非常非常慢,那么脚本有什么问题?
【问题讨论】:
-
1) 你在什么平台上运行它? 2)你用的是什么型号? 3) 使用模型处理单帧时的延迟是多少?
-
感谢您的提醒!我的操作系统是window10,我想在视频输入上测试Mobilenet v2 SSDLite TFLite模型,现在我有python脚本来测试单图像模型,推理时间约为0.12秒,但现在我想测试模型与视频。我在描述中找到了一个脚本,但是当我在我身边使用它时,出现了一些问题,所以我想知道如何更正那个 python 脚本。
标签: tensorflow-lite