|
| 1 | +from torch import Tensor |
| 2 | +from wanwu.core.backends.trt import TensorRTInferencer |
| 3 | +import os |
| 4 | +import cv2 |
| 5 | +import argparse |
| 6 | +import numpy as np |
| 7 | +import onnxruntime |
| 8 | +from alfred.vis.image.det import visualize_det_cv2_part |
| 9 | +from alfred.vis.image.mask import vis_bitmasks_with_classes |
| 10 | +from alfred.utils.file_io import ImageSourceIter |
| 11 | + |
| 12 | + |
| 13 | +def vis_res_fast(img, boxes, masks, scores, labels): |
| 14 | + if masks is not None: |
| 15 | + # masks shape, might not same as img, resize contours if so |
| 16 | + img = vis_bitmasks_with_classes( |
| 17 | + img, |
| 18 | + labels, |
| 19 | + masks, |
| 20 | + force_colors=None, |
| 21 | + draw_contours=True, |
| 22 | + mask_border_color=[255, 255, 255], |
| 23 | + ) |
| 24 | + thickness = 1 if masks is None else 2 |
| 25 | + font_scale = 0.3 if masks is None else 0.4 |
| 26 | + if boxes: |
| 27 | + img = visualize_det_cv2_part( |
| 28 | + img, |
| 29 | + scores, |
| 30 | + labels, |
| 31 | + boxes, |
| 32 | + line_thickness=thickness, |
| 33 | + font_scale=font_scale, |
| 34 | + ) |
| 35 | + return img |
| 36 | + |
| 37 | + |
| 38 | +def load_test_image(f, h, w): |
| 39 | + a = cv2.imread(f) |
| 40 | + a = cv2.resize(a, (w, h)) |
| 41 | + a_t = np.expand_dims(np.array(a).astype(np.float32), axis=0) |
| 42 | + return a_t, a |
| 43 | + |
| 44 | + |
| 45 | +def preprocess_image(img, h, w): |
| 46 | + a = cv2.resize(img, (w, h)) |
| 47 | + a_t = np.expand_dims(np.array(a).astype(np.float32), axis=0) |
| 48 | + return a_t, img |
| 49 | + |
| 50 | + |
| 51 | +def make_parser(): |
| 52 | + parser = argparse.ArgumentParser("onnxruntime inference sample") |
| 53 | + parser.add_argument( |
| 54 | + "-m", |
| 55 | + "--model", |
| 56 | + type=str, |
| 57 | + default="yolox.onnx", |
| 58 | + help="Input your onnx model.", |
| 59 | + ) |
| 60 | + parser.add_argument( |
| 61 | + "-i", |
| 62 | + "--image_path", |
| 63 | + type=str, |
| 64 | + default="test_image.png", |
| 65 | + help="Path to your input image.", |
| 66 | + ) |
| 67 | + parser.add_argument( |
| 68 | + "-o", |
| 69 | + "--output_dir", |
| 70 | + type=str, |
| 71 | + default="demo_output", |
| 72 | + help="Path to your output directory.", |
| 73 | + ) |
| 74 | + parser.add_argument( |
| 75 | + "-s", |
| 76 | + "--score_thr", |
| 77 | + type=float, |
| 78 | + default=0.3, |
| 79 | + help="Score threshould to filter the result.", |
| 80 | + ) |
| 81 | + parser.add_argument( |
| 82 | + "-t", |
| 83 | + "--type", |
| 84 | + default='sparseinst', |
| 85 | + help="model type.", |
| 86 | + ) |
| 87 | + return parser |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + args = make_parser().parse_args() |
| 92 | + |
| 93 | + engine_f = args.model |
| 94 | + trt_model = TensorRTInferencer(engine_f) |
| 95 | + input_shape = trt_model.ori_input_shape |
| 96 | + print('input shape: ', input_shape) |
| 97 | + |
| 98 | + iter = ImageSourceIter(args.image_path) |
| 99 | + while True: |
| 100 | + im = next(iter) |
| 101 | + if isinstance(im, str): |
| 102 | + im = cv2.imread(im) |
| 103 | + |
| 104 | + inp, ori_img = preprocess_image(im, h=input_shape[0], w=input_shape[1]) |
| 105 | + output = trt_model.infer(inp) |
| 106 | + |
| 107 | + print(output) |
| 108 | + |
| 109 | + if "sparse" in args.type: |
| 110 | + masks, scores, labels = None, None, None |
| 111 | + for o in output: |
| 112 | + if o.dtype == np.float32: |
| 113 | + scores = o |
| 114 | + if o.dtype == np.int32 or o.dtype == np.int64: |
| 115 | + labels = o |
| 116 | + if o.dtype == bool: |
| 117 | + masks = o |
| 118 | + masks = masks[0] |
| 119 | + print(masks.shape) |
| 120 | + if len(masks.shape) > 3: |
| 121 | + masks = np.squeeze(masks, axis=1) |
| 122 | + scores = scores[0] |
| 123 | + labels = labels[0] |
| 124 | + # keep = scores > 0.15 |
| 125 | + keep = scores > 0.06 |
| 126 | + scores = scores[keep] |
| 127 | + labels = labels[keep] |
| 128 | + masks = masks[keep] |
| 129 | + print(scores) |
| 130 | + print(labels) |
| 131 | + print(masks.shape) |
| 132 | + img = vis_res_fast(im, None, masks, scores, labels) |
| 133 | + else: |
| 134 | + predictions = demo_postprocess(output[0], input_shape, p6=args.with_p6)[0] |
| 135 | + boxes = predictions[:, :4] |
| 136 | + scores = predictions[:, 4:5] * predictions[:, 5:] |
| 137 | + |
| 138 | + boxes_xyxy = np.ones_like(boxes) |
| 139 | + boxes_xyxy[:, 0] = boxes[:, 0] - boxes[:, 2] / 2.0 |
| 140 | + boxes_xyxy[:, 1] = boxes[:, 1] - boxes[:, 3] / 2.0 |
| 141 | + boxes_xyxy[:, 2] = boxes[:, 0] + boxes[:, 2] / 2.0 |
| 142 | + boxes_xyxy[:, 3] = boxes[:, 1] + boxes[:, 3] / 2.0 |
| 143 | + # boxes_xyxy /= ratio |
| 144 | + dets = multiclass_nms(boxes_xyxy, scores, nms_thr=0.65, score_thr=0.1) |
| 145 | + final_boxes, final_scores, final_cls_inds = ( |
| 146 | + dets[:, :4], |
| 147 | + dets[:, 4], |
| 148 | + dets[:, 5], |
| 149 | + ) |
| 150 | + img = visualize_det_cv2_part( |
| 151 | + ori_img, final_scores, final_cls_inds, final_boxes |
| 152 | + ) |
| 153 | + cv2.imshow("aa", img) |
| 154 | + cv2.waitKey(0) |
| 155 | + |
| 156 | + cv2.imshow("YOLOv7 SparseInst CPU int8", img) |
| 157 | + if iter.video_mode: |
| 158 | + if cv2.waitKey(1) & 0xFF == ord("q"): |
| 159 | + break |
| 160 | + else: |
| 161 | + cv2.waitKey(0) |
0 commit comments