YoloV5_infer_migraphx.py 8.21 KB
Newer Older
Your Name's avatar
Your Name committed
1
2
3
4
5
6
7
8
9
10
# -*- coding: utf-8 -*-
import cv2
import numpy as np
import os
import argparse
import time
import migraphx


class YOLOv5:
shizhm's avatar
shizhm committed
11
    def __init__(self, path, dynamic=False, obj_thres=0.5, conf_thres=0.25, iou_thres=0.5):
Your Name's avatar
Your Name committed
12
13
14
        self.objectThreshold = obj_thres
        self.confThreshold = conf_thres
        self.nmsThreshold = iou_thres
shizhm's avatar
shizhm committed
15
        self.isDynamic = dynamic
Your Name's avatar
Your Name committed
16
        # 获取模型检测的类别信息
liucong's avatar
liucong committed
17
        self.classNames = list(map(lambda x: x.strip(), open('../Resource/Models/coco.names', 'r').readlines()))
Your Name's avatar
Your Name committed
18
19

        # 解析推理模型
shizhm's avatar
shizhm committed
20
21
22
        if self.isDynamic:
            maxInput={"images":[1,3,800,800]}
            self.model = migraphx.parse_onnx(path, map_input_dims=maxInput)
liucong's avatar
liucong committed
23
24
25
26
27
28

            # 获取模型输入/输出节点信息
            inputs = self.model.get_inputs()
            outputs = self.model.get_outputs()

            # 获取模型的输入name
liucong's avatar
liucong committed
29
            self.inputName = self.model.get_parameter_names()[0]
shizhm's avatar
shizhm committed
30
            
liucong's avatar
liucong committed
31
32
33
34
35
            # 获取模型的输入尺寸
            inputShape = inputShape=inputs[self.inputName].lens()
            self.inputHeight = int(inputShape[2])
            self.inputWidth = int(inputShape[3])
            print("inputName:{0} \ninputShape:{1}".format(self.inputName, inputShape))
shizhm's avatar
shizhm committed
36
37
        else:
            self.model = migraphx.parse_onnx(path) 
liucong's avatar
liucong committed
38
39
40
41
42
            # 获取模型输入/输出节点信息
            inputs = self.model.get_inputs()
            outputs = self.model.get_outputs()

            # 获取模型的输入name
liucong's avatar
liucong committed
43
            self.inputName = self.model.get_parameter_names()[0]
liucong's avatar
liucong committed
44
45
46
47
48
49

            # 获取模型的输入尺寸
            inputShape = inputShape=inputs[self.inputName].lens()
            self.inputHeight = int(inputShape[2])
            self.inputWidth = int(inputShape[3])
            print("inputName:{0} \ninputShape:{1}".format(self.inputName, inputShape))
50
51
52
        
        # 模型编译
        self.model.compile(t=migraphx.get_target("gpu"), device_id=0)  # device_id: 设置GPU设备,默认为0号设备
Your Name's avatar
Your Name committed
53

shizhm's avatar
shizhm committed
54
55
56
57
    def detect(self, image, input_shape=None):
        if(self.isDynamic):
            self.inputWidth = input_shape[3]
            self.inputHeight = input_shape[2]
Your Name's avatar
Your Name committed
58
59
60
61
        # 输入图片预处理
        input_img = self.prepare_input(image)

        # 执行推理
liucong's avatar
liucong committed
62
        result = self.model.run({self.inputName: input_img})
liucong's avatar
liucong committed
63

Your Name's avatar
Your Name committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
        # 模型输出结果后处理
        boxes, scores, class_ids = self.process_output(result)

        return boxes, scores, class_ids

    def prepare_input(self, image):
        self.img_height, self.img_width = image.shape[:2]
        input_img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        input_img = cv2.resize(input_img, (self.inputWidth, self.inputHeight))
        input_img = input_img.transpose(2, 0, 1)
        input_img = np.expand_dims(input_img, 0)
        input_img = np.ascontiguousarray(input_img)
        input_img = input_img.astype(np.float32)
        input_img = input_img / 255

        return input_img

    def process_output(self, output):
        predictions = np.squeeze(output[0])

        # 筛选包含物体的anchor
        obj_conf = predictions[:, 4]
        predictions = predictions[obj_conf > self.objectThreshold]
        obj_conf = obj_conf[obj_conf > self.objectThreshold]

        # 筛选大于置信度阈值的anchor
        predictions[:, 5:] *= obj_conf[:, np.newaxis]
        scores = np.max(predictions[:, 5:], axis=1)
        valid_scores = scores > self.confThreshold
        predictions = predictions[valid_scores]
        scores = scores[valid_scores]

        # 获取最高置信度分数对应的类别ID
        class_ids = np.argmax(predictions[:, 5:], axis=1)

        # 获取每个物体对应的anchor
        boxes = self.extract_boxes(predictions)

        # 执行非极大值抑制消除冗余anchor
        indices = cv2.dnn.NMSBoxes(boxes.tolist(), scores.tolist(), self.confThreshold, self.nmsThreshold).flatten()

        return boxes[indices], scores[indices], class_ids[indices]

    def extract_boxes(self, predictions):
        boxes = predictions[:, :4]
        boxes = self.rescale_boxes(boxes)
        boxes_ = np.copy(boxes)
        boxes_[..., 0] = boxes[..., 0] - boxes[..., 2] * 0.5
        boxes_[..., 1] = boxes[..., 1] - boxes[..., 3] * 0.5
        return boxes_

    def rescale_boxes(self, boxes):
        # 对anchor尺寸进行变换
        input_shape = np.array([self.inputWidth, self.inputHeight, self.inputWidth, self.inputHeight])
        boxes = np.divide(boxes, input_shape, dtype=np.float32)
        boxes *= np.array([self.img_width, self.img_height, self.img_width, self.img_height])
        return boxes

    def draw_detections(self, image, boxes, scores, class_ids):
        for box, score, class_id in zip(boxes, scores, class_ids):
            cx, cy, w, h = box.astype(int)

            # 绘制检测物体框
            cv2.rectangle(image, (cx, cy), (cx + w, cy + h), (0, 255, 255), thickness=2)
            label = self.classNames[class_id]
            label = f'{label} {score:.2f}'
            labelSize, baseLine = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
            cv2.putText(image, label, (cx, cy - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), thickness=2)
        return image
133
134
135
136
137
138
139
140
141
    
def read_images(image_path):
    image_lists = []
    
    for image_name in os.listdir(image_path):
        image = cv2.imread(image_path +"/" + image_name, 1)
        image_lists.append(image)
        
    return image_lists
Your Name's avatar
Your Name committed
142

shizhm's avatar
shizhm committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
def yolov5_Static(imgpath, modelpath, objectThreshold, confThreshold, nmsThreshold):
    yolov5_detector = YOLOv5(modelpath, False, obj_thres=objectThreshold, conf_thres=confThreshold,
                             iou_thres=nmsThreshold)
    srcimg = cv2.imread(imgpath, 1)

    boxes, scores, class_ids = yolov5_detector.detect(srcimg)

    dstimg = yolov5_detector.draw_detections(srcimg, boxes, scores, class_ids)

    # 保存检测结果
    cv2.imwrite("./Result.jpg", dstimg)
    print("Success to save result")


def yolov5_dynamic(imgpath, modelpath, objectThreshold, confThreshold, nmsThreshold):
158
159
160
161
162
163
    # 设置动态输入shape
    input_shapes = []
    input_shapes.append([1,3,416,416])
    input_shapes.append([1,3,608,608])
    
    # 读取测试图像
shizhm's avatar
shizhm committed
164
    image_lists = read_images(imgpath)
165
166
    
    # 推理
shizhm's avatar
shizhm committed
167
168
    yolov5_detector = YOLOv5(modelpath, True, obj_thres=objectThreshold, 
                                    conf_thres=confThreshold, iou_thres=nmsThreshold)
169
170
171
172
173
174
175
176
177
178
    for i, image in enumerate(image_lists):
        print("Start to inference image{}".format(i))
        boxes, scores, class_ids = yolov5_detector.detect(image, input_shapes[i])
        dstimg = yolov5_detector.draw_detections(image, boxes, scores, class_ids)
        
        # 保存检测结果
        result_name = "Result{}.jpg".format(i)
        cv2.imwrite(result_name, dstimg)
    
    print("Success to save results")
shizhm's avatar
shizhm committed
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
    
    

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--imgPath', type=str, default='../Resource/Images/DynamicPics/image1.jpg', help="image path")
    parser.add_argument('--imgFolderPath', type=str, default='../Resource/Images/DynamicPics', help="image folder path")
    parser.add_argument('--staticModelPath', type=str, default='../Resource/Models/yolov5s.onnx', help="static onnx filepath")
    parser.add_argument('--dynamicModelPath', type=str, default='../Resource/Models/yolov5s_Nx3xNxN.onnx', help="static onnx filepath")
    parser.add_argument('--objectThreshold', default=0.5, type=float, help='class confidence')
    parser.add_argument('--confThreshold', default=0.25, type=float, help='class confidence')
    parser.add_argument('--nmsThreshold', default=0.5, type=float, help='nms iou thresh')
    parser.add_argument("--staticInfer",action="store_true",default=False,help="Performing static inference")
    parser.add_argument("--dynamicInfer",action="store_true",default=False,help="Performing static inference")
    args = parser.parse_args()
    
    # 静态推理
    if args.staticInfer:
        yolov5_Static(args.imgPath, args.staticModelPath, args.objectThreshold, args.confThreshold, args.nmsThreshold)
liucong's avatar
liucong committed
198
        
shizhm's avatar
shizhm committed
199
200
201
    # 动态推理
    if args.dynamicInfer:
        yolov5_dynamic(args.imgFolderPath, args.dynamicModelPath, args.objectThreshold, args.confThreshold, args.nmsThreshold)
202
203

    
Your Name's avatar
Your Name committed
204
205
206
207
208
209
210
211
212
213
214
215