Hi !
I am trying to run the latest models from Tensorflow Detection Zoo 2 on a Jetson Xavier NX models/tf2_detection_zoo.md at master · tensorflow/models · GitHub
I tried adapt from this blog: Accelerating Inference In TF-TRT User Guide :: NVIDIA Deep Learning Frameworks Documentation to run a MobilenetV2
I have 2 issues:
- it takes about 25min to get the model ready to run in the inference script, I’d like to have this load time lower than 5min max
- the FPS is really low (around 6FPS)
I downloaded ssd_mobilenet_v2_320x320_coco17_tpu-8
from models/tf2_detection_zoo.md at master · tensorflow/models · GitHub, and unzipped it in ./coco_models
Here is my script to convert from TF to TF-TRT:
from pathlib import Path
from numpy import uint8
from numpy.random.mtrand import normal
from tensorflow.python.compiler.tensorrt.test.tf_trt_integration_test_base import FP16
from tensorflow.python.compiler.tensorrt.trt_convert import TrtConversionParams, TrtGraphConverterV2
def convert(model_directory: Path):
converter = TrtGraphConverterV2(
input_saved_model_dir=str(model_directory / "saved_model"),
conversion_params=TrtConversionParams(precision_mode=FP16, max_workspace_size_bytes=1 << 32),
)
converter.convert()
def fake_inputs():
yield normal(size=(1, 1_280, 720, 3)).astype(uint8),
converter.build(input_fn=fake_inputs)
converter.save(str(model_directory / "trt"))
if __name__ == "__main__":
convert(Path("coco_models/ssd_mobilenet_v2_320x320_coco17_tpu-8"))
And the script to infer from the model:
from time import time
import cv2
from numpy import expand_dims
from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2
from tensorflow.python.framework.ops import convert_to_tensor
from tensorflow.python.saved_model import saved_model
from tensorflow.python.saved_model.signature_constants import DEFAULT_SERVING_SIGNATURE_DEF_KEY
from tensorflow.python.saved_model.tag_constants import SERVING
saved_model_loaded = saved_model.load("coco_models/ssd_mobilenet_v2_320x320_coco17_tpu-8/trt", tags=[SERVING])
graph_func = saved_model_loaded.signatures[DEFAULT_SERVING_SIGNATURE_DEF_KEY]
frozen_func = convert_variables_to_constants_v2(graph_func)
def demo_object_detection():
cap = cv2.VideoCapture(
"nvarguscamerasrc ! "
"video/x-raw(memory:NVMM), "
f"width=(int)1280, height=(int)720, "
"format=(string)NV12, framerate=(fraction)60/1 ! "
"nvvidconv flip-method=0 ! "
f"video/x-raw, width=(int)1280, height=(int)720, "
"format=(string)BGRx ! "
"videoconvert ! appsink",
cv2.CAP_GSTREAMER,
)
while True:
ret, image = cap.read()
if ret is None:
break
t = time()
# Detection
_, [boxes], [ids], _, [scores], *_ = (
x.numpy() for x in frozen_func(convert_to_tensor(expand_dims(image, axis=0)))
)
print(f"FPS: {1 / (time() - t):.1f}")
# display
for box, class_id, score in zip(boxes, ids, scores):
if score > 0.5:
cv2.rectangle(
image,
(
int(box[1] * image.shape[1]),
int(box[0] * image.shape[0]),
),
(
int(box[3] * image.shape[1]),
int(box[2] * image.shape[0]),
),
(1.0, 1.0, 1.0),
2,
)
cv2.imshow("object detection", image)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
demo_object_detection()
Is there something that I’m doing wrongly ?
Many thanks !