Skip to content

Commit

Permalink
Update trt inference
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitroprobachay committed Jun 1, 2022
1 parent d3169ca commit f20b45d
Show file tree
Hide file tree
Showing 11 changed files with 264 additions and 353 deletions.
5 changes: 2 additions & 3 deletions docker/tensorrt/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# nVidia TensorRT Base Image
ARG TRT_CONTAINER_VERSION=21.07
FROM nvcr.io/nvidia/tensorrt:${TRT_CONTAINER_VERSION}-py3
ARG TRT_CONTAINER_VERSION=21.12
FROM nvcr.io/nvidia/pytorch:${TRT_CONTAINER_VERSION}-py3

ENV DEBIAN_FRONTEND noninteractive
ENV TZ=Europe/Kiev
Expand All @@ -22,7 +22,6 @@ RUN apt-get install -y libturbojpeg

RUN pip install --upgrade pip
RUN pip install pillow==8.0.1
RUN pip install torch==1.11.0+cu115 torchvision==0.12.0+cu115 -f https://download.pytorch.org/whl/torch_stable.html
RUN pip install setuptools
RUN pip install "PyYAML>=5.3"
RUN pip install "numpy>=1.16.*"
Expand Down
264 changes: 109 additions & 155 deletions examples/ju/benchmark/runtime-test-tensorrt.ipynb

Large diffs are not rendered by default.

323 changes: 135 additions & 188 deletions examples/ju/inference/number-plate-recognition-tensorrt.ipynb

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions examples/py/benchmark/runtime-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

from _paths import nomeroff_net_dir
from nomeroff_net import pipeline
import faulthandler


faulthandler.enable()

warnings.filterwarnings("ignore")

Expand Down
10 changes: 8 additions & 2 deletions examples/py/model_convertors/convert_yolo_to_tensorrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""
import sys
import os
import pathlib
import argparse
import subprocess

Expand All @@ -16,7 +17,7 @@ def parse_args():
ap = argparse.ArgumentParser()
ap.add_argument("-f", "--filepath",
default=os.path.join(os.path.abspath(os.getcwd()),
"../../../data/model_repository/numberplate_options/1/model.onnx"),
"../../../data/model_repository/yolov5s/1/model.engine"),
required=False,
type=str,
help="Result onnx model filepath")
Expand All @@ -28,15 +29,20 @@ def main():
args = parse_args()
model_filepath = args["filepath"]

# make dirs
p = pathlib.Path(os.path.dirname(model_filepath))
p.mkdir(parents=True, exist_ok=True)

# download and append to path yolo repo
info = modelhub.download_repo_for_model("yolov5")
repo_path = info["repo_path"]
model_info = modelhub.download_model_by_name("yolov5")
path_to_model = model_info["path"]
print(f'python3 ./export.py --weights={path_to_model} --include=engine --device 0 --dynamic;')
res = path_to_model.replace(".pt", ".engine")
# python3 ./export.py --weights yolov5s-2021-12-14.pt --include engine --device 0 --dinamic
subprocess.call([f'cd {repo_path}; '
f'python3 ./export.py --weights={path_to_model} --include=engine --device 0 --dinamic;'
f'python3 ./export.py --weights={path_to_model} --include=engine --device 0;'
f'cp {res} {model_filepath}'], shell=True)


Expand Down
2 changes: 1 addition & 1 deletion nomeroff_net/pipelines/number_plate_classification_trt.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ def forward(self, inputs: Any, **forward_parameters: Dict) -> Any:
model_outputs = []
for inp in inputs:
model_output = self.detector.forward([inp])
model_output = unzip(model_output)
model_output = unzip(model_output)[0]
model_outputs.append(model_output)
return model_outputs
2 changes: 1 addition & 1 deletion nomeroff_net/pipelines/number_plate_localization.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def preprocess(self, inputs: Any, **preprocess_parameters: Dict) -> Any:
@no_grad()
def forward(self, images: Any, **forward_parameters: Dict) -> Any:
model_outputs = self.detector.predict(images)
return unzip([model_outputs, images])
return unzip([images, model_outputs])

def postprocess(self, inputs: Any, **postprocess_parameters: Dict) -> Any:
return inputs
2 changes: 1 addition & 1 deletion nomeroff_net/pipelines/number_plate_localization_trt.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def preprocess(self, inputs: Any, **preprocess_parameters: Dict) -> Any:
@no_grad()
def forward(self, images: Any, **forward_parameters: Dict) -> Any:
detected_images_bboxs = self.detector.predict(images)
return unzip([images, detected_images_bboxs])
return unzip([detected_images_bboxs, images])

def postprocess(self, inputs: Any, **postprocess_parameters: Dict) -> Any:
return inputs
2 changes: 1 addition & 1 deletion nomeroff_net/pipelines/number_plate_text_reading_trt.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ def forward(self, inputs: Any, **forward_parameters: Dict) -> Any:
model_inputs = self.detector.preprocess([image], [label], [line])
model_output = self.detector.forward(model_inputs)
model_output = self.detector.postprocess(model_output)
model_outputs.append(model_output)
model_outputs.append(model_output[0])
return unzip([images, model_outputs, labels])
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def run_engine(self, input_image):
self.height,
self.width
))
input_image = np.array(input_image)
# Allocate host and device buffers
bindings = []
outputs = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ def predict(self, imgs: List[np.ndarray], min_accuracy: float = 0.5) -> List:
for item in img_item.to_dict(orient="records")
if item["confidence"] > min_accuracy]
for img_item in model_outputs.pandas().xyxy]
return model_outputs
return np.array(model_outputs)

0 comments on commit f20b45d

Please sign in to comment.