How these work?different from github version

jmjjmj: <p>export<br>
torch.onnx.export(model, im, f, verbose=False, opset_version=opset,<br>
training=torch.onnx.TrainingMode.TRAINING if train else torch.onnx.TrainingMode.EVAL,<br>
do_constant_folding=not train,<br>
input_names=[‘images’],<br>
output_names=[‘output’],<br>
dynamic_axes={‘images’: {0: ‘batch’, 2: ‘height’, 3: ‘width’},  # shape(1,3,640,640)<br>
‘output’: {0: ‘batch’, 1: ‘anchors’}  # shape(1,25200,85)<br>
} if dynamic else None)<br>
objdet</p>
<h1><a name="coding-utf-8-1" class="anchor" href="#coding-utf-8-1"></a>-<em>- coding: utf-8 -</em>-</h1>
<p>import cv2<br>
import torch<br>
import numpy as np<br>
import time<br>
from onnxruntime import InferenceSession</p>
<p>from utils import load_classes, preprocess, postprocess, plot_results</p>
<p>if <strong>name</strong> == “<strong>main</strong>”:</p>
<pre><code># get input data
input_size = 640
img_file = "./data/street.jpg"
img = cv2.imread(img_file)
img_h = img.shape[0]
img_w = img.shape[1]
input_data, offset = preprocess(img, input_size, input_size)

# get classes
class_name_file = "./data/coco.names"
classes = load_classes(class_name_file)

start_time = time.time()
# inference
output_names = ["output0"]
onnx_file = "./yolov5s-fp16.onnx"
session = InferenceSession(onnx_file)
outputs = session.run(output_names=output_names, input_feed={"images": input_data})

# postprocess
pred_boxes, pred_cls, pred_scores = postprocess(outputs, 
                                                img_w, 
                                                img_h, 
                                                input_size, 
                                                input_size, 
                                                offset, 
                                                conf_thresh=0.25, 
                                                iou_thresh=0.45)
end_time = time.time()
delta_time = end_time-start_time
print(delta_time)
# show result
save_path = "./output/pred_result_onnx.jpg"
plot_results(img, pred_boxes, pred_cls, pred_scores, class_names=classes, color=None, save_path=save_path)
</code></pre>
<p><span class="hashtag-raw">#utils</span></p>
<h1><a name="coding-utf-8-2" class="anchor" href="#coding-utf-8-2"></a>-<em>- coding: utf-8 -</em>-</h1>
<p>import cv2<br>
import torch<br>
import numpy as np</p>
<h1><a name="colors-for-visualization-3" class="anchor" href="#colors-for-visualization-3"></a>colors for visualization</h1>
<p>COLORS = [[0, 0, 255], [80, 127, 255], [0, 255, 0],<br>
[255, 255, 0], [48, 64, 33], [205, 224, 64],<br>
[255, 0, 0], [0, 255, 255], [226, 43, 138]]</p>
<p>def load_classes(namesfile):<br>
fp = open(namesfile, “r”)<br>
names = fp.read().split(“\n”)[:-1]<br>
return names</p>
<p>def preprocess(img, height, width):<br>
h_ratio = height / img.shape[0]<br>
w_ratio = width/ img.shape[1]<br>
ratio = min(w_ratio, h_ratio)<br>
border_h = int(img.shape[0] * ratio)<br>
border_w = int(img.shape[1] * ratio)<br>
x_offset = int((width - border_w) / 2)<br>
y_offset = int((height - border_h) / 2)<br>
offset = [x_offset, y_offset, ratio]</p>
<pre><code>input_data = cv2.resize(img, (border_w, border_h))
input_data = cv2.copyMakeBorder(input_data, y_offset, y_offset, x_offset, x_offset, cv2.BORDER_CONSTANT, value=(114, 114, 114))
input_data = cv2.cvtColor(input_data, cv2.COLOR_BGR2RGB)
input_data = np.array(input_data, dtype=np.float32)
input_data = input_data / 255.0
input_data = input_data.transpose((2, 0, 1))
input_data = input_data[np.newaxis, :, :, :]

return input_data, offset
</code></pre>
<p>def box_cxcywh_to_xyxy(cbox, offset):<br>
x_c = cbox[0]<br>
y_c = cbox[1]<br>
w = cbox[2]<br>
h = cbox[3]<br>
x_offset = offset[0]<br>
y_offset = offset[1]<br>
ratio = offset[2]<br>
scale = 1.0 / ratio<br>
xmin = (x_c - 0.5 * w - x_offset) * scale<br>
ymin = (y_c - 0.5 * h - y_offset) * scale<br>
xmax = (x_c + 0.5 * w - x_offset) * scale<br>
ymax = (y_c + 0.5 * h - y_offset) * scale</p>
<pre><code>box_coord = np.array([xmin, ymin, xmax, ymax])
return box_coord
</code></pre>
<p>def clip_box(pred_box, img_w, img_h):<br>
pred_box[0] = np.clip(pred_box[0], 0, img_w)<br>
pred_box[1] = np.clip(pred_box[1], 0, img_h)<br>
pred_box[2] = np.clip(pred_box[2], 0, img_w)<br>
pred_box[3] = np.clip(pred_box[3], 0, img_h)</p>
<p>def rescale_bbox(pred_box, img_w, img_h, input_w, input_h):<br>
w_ratio = input_w / img_w<br>
h_ratio = input_h / img_h<br>
ratio = min(w_ratio, h_ratio)<br>
new_w = np.round(input_w * ratio)<br>
new_h = np.round(input_h * ratio)<br>
offset_w = (input_w - new_w) / 2<br>
offset_h = (input_h - new_h) / 2<br>
scale = 1.0 / ratio</p>
<pre><code>scale_box = np.array([0, 0, 0, 0])
scale_box[0] = (pred_box[0] - offset_w) * scale
scale_box[1] = (pred_box[1] - offset_h) * scale
scale_box[2] = (pred_box[2] - offset_w) * scale
scale_box[3] = (pred_box[3] - offset_h) * scale

clip_box(scale_box, img_w, img_h)
return scale_box
</code></pre>
<p>def box_iou(box1, box2):<br>
xmin1 = box1[0]<br>
ymin1 = box1[1]<br>
xmax1 = box1[2]<br>
ymax1 = box1[3]</p>
<pre><code>xmin2 = box2[0]
ymin2 = box2[1]
xmax2 = box2[2]
ymax2 = box2[3]

# calculate intersection area
xx1 = np.max([xmin1, xmin2])
yy1 = np.max([ymin1, ymin2])
xx2 = np.min([xmax1, xmax2])
yy2 = np.min([ymax1, ymax2])

w = np.max([0.0, xx2 - xx1 + 1])
h = np.max([0.0, yy2 - yy1 + 1])
intersection_area = w * h

# calculate union area
area1 = (xmax1 - xmin1 + 1) * (ymax1 - ymin1 + 1)
area2 = (xmax2 - xmin2 + 1) * (ymax2 - ymin2 + 1)
union_area = area1 + area2 - intersection_area

# calculate iou
eps = 1e-8
iou = intersection_area / (union_area + eps)
return iou
</code></pre>
<p>def non_max_suppression(prediction,<br>
pred_boxes,<br>
pred_cls,<br>
pred_scores,<br>
offset,<br>
conf_thres=0.25,<br>
iou_thres=0.45,<br>
max_obj_num=100):</p>
<pre><code>det_boxes = []
det_cls_idx = []
det_cls_scores = []
det_conf = []

for i in range(len(prediction)):
    pred_item = prediction[i]
    obj_conf = pred_item[4]
    if obj_conf &lt; conf_thres:
        continue

    cls_idx = np.argmax(pred_item[5:])
    cls_conf = pred_item[5 + cls_idx]
    conf = obj_conf * cls_conf
    if conf &gt; conf_thres:
        cbox = pred_item[:4] 
        box = box_cxcywh_to_xyxy(cbox, offset)
        det_boxes.append(box)
        det_cls_idx.append(cls_idx)
        det_cls_scores.append(cls_conf)
        det_conf.append(conf)

det_boxes = np.array(det_boxes)
det_conf = np.array(det_conf)
order = np.argsort(det_conf)[::-1]

keep = []
while len(order) &gt; 0:

    if len(keep) &gt; max_obj_num:
        break

    top1_idx = order[0]
    top1_box = det_boxes[top1_idx]
    keep.append(top1_idx)
    order = np.delete(order, 0)

    idx = 0
    inds = []
    for k in range(len(order)):
        topk_idx = order[k]
        topk_box = det_boxes[topk_idx]
        iou = box_iou(top1_box, topk_box)
        if iou &lt;= iou_thres:
            inds.append(k)
    order = order[inds]

for i in range(len(keep)):
    idx = keep[i]
    pred_boxes.append(det_boxes[idx])
    pred_cls.append(det_cls_idx[idx])
    pred_scores.append(det_cls_scores[idx])

return
</code></pre>
<p>def postprocess(prediction,<br>
img_w,<br>
img_h,<br>
input_w,<br>
input_h,<br>
offset,<br>
conf_thresh=0.25,<br>
iou_thresh=0.45,<br>
max_det=300):</p>
<pre><code>prediction = prediction[0][0]
pred_boxes = []
pred_cls = []
pred_scores = []
rescale_boxes = []
max_obj_num = max_det
non_max_suppression(prediction, 
                    pred_boxes, 
                    pred_cls, 
                    pred_scores, 
                    offset, 
                    conf_thres=conf_thresh, 
                    iou_thres=iou_thresh,  
                    max_obj_num=max_obj_num)
# for i in range(len(pred_boxes)):
#     box = rescale_bbox(pred_boxes[i], img_w, img_h, input_w, input_h)
#     rescale_boxes.append(box)

return pred_boxes, pred_cls, pred_scores
</code></pre>
<p>def plot_results(img, boxes, classes, scores, class_names=None, color=None, save_path=‘’):<br>
img = np.copy(img)<br>
for i in range(len(boxes)):<br>
box = boxes[i]<br>
xmin = int(box[0])<br>
ymin = int(box[1])<br>
xmax = int(box[2])<br>
ymax = int(box[3])</p>
<pre><code>    score = scores[i]
    class_id = classes[i]
    text = f'{class_names[class_id]}: {score:0.2f}'
    color_id = class_id % len(COLORS)
    color_array = COLORS[color_id]
    img = cv2.putText(img, text, (xmin, ymin - 5),
                      cv2.FONT_HERSHEY_SIMPLEX, 0.6, color_array, 2)
    img = cv2.rectangle(img, (xmin, ymin), (xmax, ymax), color_array, 2)
    cv2.imwrite(save_path, img)

return
</code></pre>
<p><span class="hashtag-raw">#generate</span></p>
<h1><a name="coding-utf-8-4" class="anchor" href="#coding-utf-8-4"></a>-<em>- coding: utf-8 -</em>-</h1>
<p>import tensorrt as trt</p>
<p>if <strong>name</strong> == “<strong>main</strong>”:</p>
<pre><code># set batch size to 1
EXPLICIT_BATCH = 1 &lt;&lt; int(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH)

# create logger instance
logger = trt.Logger()
trt.init_libnvinfer_plugins(logger,"")

# generate engine
builder = trt.Builder(logger)
network = builder.create_network(EXPLICIT_BATCH)
parser = trt.OnnxParser(network, logger)
runtime = trt.Runtime(logger)
config = builder.create_builder_config()

config.max_workspace_size = 1 &lt;&lt; 32  # 4GB
builder.max_batch_size = 1

onnx_file = "/root/AI/deep_learning/yolov5/demo/tensorRT/yolov5m.onnx"
success = parser.parse_from_file(onnx_file)

serialize_engine = builder.build_serialized_network(network, config)

engine_path = "/root/AI/deep_learning/yolov5/demo/tensorRT/yolov5m.engine"
with open(engine_path, "wb") as f:
    f.write(serialize_engine)
    print("generate engine successfully")
</code></pre>

jmjjmj: <p><span class="hashtag-raw">#linux</span> code<br>
/root/nvidia/TensorRT-8.6.1.6/bin/trtexec --onnx=/root/AI/deep_learning/yolov5/demo/tensorRT/yolov5m.onnx – saveEngine=./yolov5m.trt<br>
<span class="hashtag-raw">#objdet_image_trt_test</span></p>
<h1><a name="coding-utf-8-1" class="anchor" href="#coding-utf-8-1"></a>-<em>- coding: utf-8 -</em>-</h1>
<p>import cv2<br>
import torch<br>
import numpy as np<br>
import tensorrt as trt<br>
import pycuda.autoinit<br>
import pycuda.driver as cuda<br>
import time</p>
<p>from utils import load_classes, preprocess, postprocess, plot_results</p>
<p>if <strong>name</strong> == “<strong>main</strong>”:</p>
<pre><code># get input data
input_size = 640
img_file = "/root/AI/deep_learning/yolov5/demo/tensorRT/data/dog.jpg"
img = cv2.imread(img_file)
img_h = img.shape[0]
img_w = img.shape[1]
start_time = time.time()
input_data, offset = preprocess(img, input_size, input_size)
end_time = time.time()
preprocess_time = (end_time - start_time) * 1000
print("preprocess time (ms): ", preprocess_time)
input_dim = input_data.shape

# get classes
class_name_file = "/root/AI/deep_learning/yolov5/demo/tensorRT/data/coco.names"
classes = load_classes(class_name_file)


# deserialize model engine
logger = trt.Logger(trt.Logger.WARNING)
engine_path = "/root/AI/deep_learning/yolov5/demo/tensorRT/yolov5m-fp16.engine"
with open(engine_path, "rb") as f, trt.Runtime(logger) as runtime:
    engine = runtime.deserialize_cuda_engine(f.read())


# inference
start_time = time.time()
with engine.create_execution_context() as context:
    context.set_binding_shape(engine.get_binding_index("input_data"), input_dim)
    inputs = []
    outputs = []
    bindings = []
    stream = cuda.Stream()

    for binding in engine:
        binding_idx = engine.get_binding_index(binding)

        size = trt.volume(engine.get_binding_shape(binding_idx))
        dtype = trt.nptype(engine.get_binding_dtype(binding_idx))

        host_mem = cuda.pagelocked_empty(size, dtype)
        device_mem = cuda.mem_alloc(host_mem.nbytes)
        bindings.append(int(device_mem))

        if engine.binding_is_input(binding):
            host_mem = np.ascontiguousarray(input_data)
            inputs.append([host_mem, device_mem])
        else:
            outputs.append([host_mem, device_mem])
    
    for i in range(len(inputs)):
        cuda.memcpy_htod_async(inputs[i][1], inputs[i][0], stream)

    context.execute_async_v2(bindings=bindings, stream_handle=stream.handle)

    for i in range(len(outputs)):
        cuda.memcpy_dtoh_async(outputs[i][0], outputs[i][1], stream)
    stream.synchronize()
    end_time = time.time()
    process_time = (end_time - start_time) * 1000
    print("process_time (ms): ", process_time)
  
    # postprocess
    start_time = time.time()
    output = outputs[i][0]
    output_dim = [25200, 85]
    output = output.reshape(output_dim)
    pred_boxes, pred_cls, pred_scores = postprocess(output, offset)
    end_time = time.time()
    postprocess_time = (end_time - start_time) * 1000
    print("postprocess_time (ms): ", postprocess_time)

    # show result
    save_path = "./pred_tensorRT.jpg"
    plot_results(img, pred_boxes, pred_cls, pred_scores, class_names=classes, color=None, save_path=save_path)
</code></pre>

jmjjmj: <p>简化ONNX</p>
<p>import onnxoptimizer</p>
<p>onnx_file = “yolov5m.onnx” model = onnx.load(onnx_file)</p>
<p>optim_model = onnxoptimizer.optimize(model)</p>
<p>simp_model, check = simplify(optim_model)</p>
<p>save_file = “./yolov5m-simp.onnx” onnx.save(simp_model, save_file)<br>
<span class="hashtag-raw">#jiegoujiexi</span><br>
inputs = model.graph.input<br>
outputs = model.graph.output<br>
print(inputs)<br>
print(outputs)<br>
<span class="hashtag-raw">#jiedianliebiao</span><br>
idx = 0<br>
for node in model.graph.node:<br>
idx+=1<br>
print(“node idx:”,idx)<br>
print(“node name”,node.name)<br>
print(“node of type:”,node.op_type)</p>

ptrblck: <p>It’s unclear to me what the actual question is since you just posted unformatted code snippets without explaining anything about what fails or where you are seeing an issue.</p>