# TRT\_object\_detection using my model

**URL:** <https://forums.developer.nvidia.com/t/trt-object-detection-using-my-model/119128>\
**Category:** Jetson TX2\
**Tags:** tensorrt\
**Created:** [April 9, 2020, 5:45am UTC](https://forums.developer.nvidia.com/t/trt-object-detection-using-my-model/119128 "2020-04-09T05:45:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gpqls7669](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@gpqls7669](https://forums.developer.nvidia.com/u/gpqls7669)\
**Post date:** [April 9, 2020, 5:45am UTC](https://forums.developer.nvidia.com/t/trt-object-detection-using-my-model/119128/1 "2020-04-09T05:45:02Z")

</div>

I was copying TRT\_object\_detection.  
How do I change main.py to use a model I retrained, not a coco model here?

import os  
import sys  
import cv2  
import time  
import ctypes  
import numpy as np  
import pycuda.autoinit  
import pycuda.driver as cuda

import coco  
import uff  
import tensorrt as trt  
import graphsurgeon as gs  
#from config import model\_ssd\_inception\_v2\_coco\_2017\_11\_17 as model  
#from config import model\_ssd\_mobilenet\_v1\_coco\_2018\_01\_28 as model  
from config import model\_ssd\_mobilenet\_v2\_coco\_2018\_03\_29 as model

ctypes.CDLL(“lib/libflattenconcat.so”)  
COCO\_LABELS = coco.COCO\_CLASSES\_LIST

# initialize

TRT\_LOGGER = trt.Logger(trt.Logger.INFO)  
trt.init\_libnvinfer\_plugins(TRT\_LOGGER, ‘’)  
runtime = trt.Runtime(TRT\_LOGGER)

# compile model into TensorRT

if not os.path.isfile(model.TRTbin):  
dynamic\_graph = model.add\_plugin(gs.DynamicGraph(model.path))  
uff\_model = uff.from\_tensorflow(dynamic\_graph.as\_graph\_def(), model.output\_name, output\_filename=‘tmp.uff’)

```
with trt.Builder(TRT_LOGGER) as builder, builder.create_network() as network, trt.UffParser() as parser:
    builder.max_workspace_size = 1 << 28
    builder.max_batch_size = 1
    builder.fp16_mode = True

    parser.register_input('Input', model.dims)
    parser.register_output('MarkOutput_0')
    parser.parse('tmp.uff', network)
    engine = builder.build_cuda_engine(network)

    buf = engine.serialize()
    with open(model.TRTbin, 'wb') as f:
        f.write(buf)

```

# create engine

with open(model.TRTbin, ‘rb’) as f:  
buf = f.read()  
engine = runtime.deserialize\_cuda\_engine(buf)

# create buffer

host\_inputs =   
cuda\_inputs =   
host\_outputs =   
cuda\_outputs =   
bindings =   
stream = cuda.Stream()

for binding in engine:  
size = trt.volume(engine.get\_binding\_shape(binding)) \* engine.max\_batch\_size  
host\_mem = cuda.pagelocked\_empty(size, np.float32)  
cuda\_mem = cuda.mem\_alloc(host\_mem.nbytes)

```
bindings.append(int(cuda_mem))
if engine.binding_is_input(binding):
    host_inputs.append(host_mem)
    cuda_inputs.append(cuda_mem)
else:
    host_outputs.append(host_mem)
    cuda_outputs.append(cuda_mem)

```

context = engine.create\_execution\_context()

# inference

#TODO enable video pipeline  
#TODO using pyCUDA for preprocess  
ori = cv2.imread(sys.argv[1])  
image = cv2.cvtColor(ori, cv2.COLOR\_BGR2RGB)  
image = cv2.resize(image, (model.dims[2],model.dims[1]))  
image = (2.0/255.0) \* image - 1.0  
image = image.transpose((2, 0, 1))  
np.copyto(host\_inputs[0], image.ravel())

start\_time = time.time()  
cuda.memcpy\_htod\_async(cuda\_inputs[0], host\_inputs[0], stream)  
context.execute\_async(bindings=bindings, stream\_handle=stream.handle)  
cuda.memcpy\_dtoh\_async(host\_outputs[1], cuda\_outputs[1], stream)  
cuda.memcpy\_dtoh\_async(host\_outputs[0], cuda\_outputs[0], stream)  
stream.synchronize()  
print("execute times "+str(time.time()-start\_time))

output = host\_outputs[0]  
height, width, channels = ori.shape  
for i in range(int(len(output)/model.layout)):  
prefix = i\*model.layout  
index = int(output[prefix+0])  
label = int(output[prefix+1])  
conf = output[prefix+2]  
xmin = int(output[prefix+3]\*width)  
ymin = int(output[prefix+4]\*height)  
xmax = int(output[prefix+5]\*width)  
ymax = int(output[prefix+6]\*height)

```
if conf > 0.7:
    print("Detected {} with confidence {}".format(COCO_LABELS[label], "{0:.0%}".format(conf)))
    cv2.rectangle(ori, (xmin,ymin), (xmax, ymax), (0,0,255),3)
    cv2.putText(ori, COCO_LABELS[label],(xmin+10,ymin+10), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2, cv2.LINE_AA)

```

cv2.imwrite(“result.jpg”, ori)  
cv2.imshow(“result”, ori)  
cv2.waitKey(0)

---

<div class="post-metadata">

**Author:** ![AastaLLL](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/aastalll/32/14043_2.png) [@AastaLLL](https://forums.developer.nvidia.com/u/AastaLLL)\
**Post date:** [April 9, 2020, 8:30am UTC](https://forums.developer.nvidia.com/t/trt-object-detection-using-my-model/119128/3 "2020-04-09T08:30:54Z")

</div>

Hi,

What kind of model do you use?  
Could you share your model with us for further checking?

Thanks.

---

<div class="post-metadata">

**Author:** ![gpqls7669](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@gpqls7669](https://forums.developer.nvidia.com/u/gpqls7669)\
**Post date:** [April 13, 2020, 6:53am UTC](https://forums.developer.nvidia.com/t/trt-object-detection-using-my-model/119128/4 "2020-04-13T06:53:01Z")

</div>

I retrain using my data using the ssd\_mobilenet\_v2\_coco model.  
There is a ssd\_mobilenet\_v2\_coco model in that example, but it is a model already out there, and I want you to know that I retrain with my data.  
And is there any way to do real-time detection using that model?

---

<div class="post-metadata">

**Author:** ![AastaLLL](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/aastalll/32/14043_2.png) [@AastaLLL](https://forums.developer.nvidia.com/u/AastaLLL)\
**Post date:** [April 20, 2020, 7:19am UTC](https://forums.developer.nvidia.com/t/trt-object-detection-using-my-model/119128/5 "2020-04-20T07:19:22Z")

</div>

Hi,

If the model architecture doesn’t change, you should just need to update the label file.  
[https://github.com/AastaNV/TRT\_object\_detection/blob/master/coco.py](https://github.com/AastaNV/TRT_object_detection/blob/master/coco.py)

Thanks.

---

<div class="post-metadata">

**Author:** ![nadeemm](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/nadeemm/32/367020_2.png) [@nadeemm](https://forums.developer.nvidia.com/u/nadeemm)\
**Post date:** [October 18, 2021, 6:18pm UTC](https://forums.developer.nvidia.com/t/trt-object-detection-using-my-model/119128/7 "2021-10-18T18:18:26Z")

</div>

Closed on October 18, 2021, 6:18pm UTC


