Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
yaoht
YOLOX_MiGraphX
Commits
b19fd646
Commit
b19fd646
authored
Jan 03, 2024
by
yaoht
Browse files
comment dynamic part
parent
04e4e763
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
6 additions
and
170 deletions
+6
-170
Python/demo_output/image_test.jpg
Python/demo_output/image_test.jpg
+0
-0
Python/yolox_infer_migraphx.py
Python/yolox_infer_migraphx.py
+4
-168
Src/main.cpp
Src/main.cpp
+2
-2
No files found.
Python/demo_output/image_test.jpg
0 → 100644
View file @
b19fd646
123 KB
Python/yolox_infer_migraphx.py
View file @
b19fd646
...
...
@@ -275,151 +275,6 @@ class YOLOX:
# Generate a color palette for the classes
self
.
color_palette
=
np
.
random
.
uniform
(
0
,
255
,
size
=
(
len
(
self
.
classNames
),
3
))
# def draw_detections(self, img, box, score, class_id):
# """
# Draws bounding boxes and labels on the input image based on the detected objects.
# Args:
# img: The input image to draw detections on.
# box: Detected bounding box.
# score: Corresponding detection score.
# class_id: Class ID for the detected object.
# Returns:
# None
# """
# # Extract the coordinates of the bounding box
# x1, y1, w, h = box
# # Retrieve the color for the class ID
# color = self.color_palette[class_id]
# # Draw the bounding box on the image
# cv2.rectangle(img, (int(x1), int(y1)), (int(x1 + w), int(y1 + h)), color, 2)
# # Create the label text with class name and score
# label = f'{self.classNames[class_id]}: {score:.2f}'
# # Calculate the dimensions of the label text
# (label_width, label_height), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
# # Calculate the position of the label text
# label_x = x1
# label_y = y1 - 10 if y1 - 10 > label_height else y1 + 10
# # Draw a filled rectangle as the background for the label text
# cv2.rectangle(img, (label_x, label_y - label_height), (label_x + label_width, label_y + label_height), color,
# cv2.FILLED)
# # Draw the label text on the image
# cv2.putText(img, label, (label_x, label_y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1, cv2.LINE_AA)
# def preprocess(self, image):
# """
# Preprocesses the input image before performing inference.
# Returns:
# image_data: Preprocessed image data ready for inference.
# """
# # Read the input image using OpenCV
# # self.img = cv2.imread(self.input_image)
# self.img = image
# # Get the height and width of the input image
# self.img_height, self.img_width = self.img.shape[:2]
# # Convert the image color space from BGR to RGB
# img = cv2.cvtColor(self.img, cv2.COLOR_BGR2RGB)
# # Resize the image to match the input shape
# img = cv2.resize(img, (self.inputWidth, self.inputHeight))
# # Normalize the image data by dividing it by 255.0
# image_data = np.array(img) / 255.0
# # Transpose the image to have the channel dimension as the first dimension
# image_data = np.transpose(image_data, (2, 0, 1)) # Channel first
# # Expand the dimensions of the image data to match the expected input shape
# image_data = np.expand_dims(image_data, axis=0).astype(np.float32)
# # Make array memery contiguous
# image_data = np.ascontiguousarray(image_data)
# # Return the preprocessed image data
# return image_data
# def postprocess(self, input_image, output):
# """
# Performs post-processing on the model's output to extract bounding boxes, scores, and class IDs.
# Args:
# input_image (numpy.ndarray): The input image.
# output (numpy.ndarray): The output of the model.
# Returns:
# numpy.ndarray: The input image with detections drawn on it.
# """
# # Transpose and squeeze the output to match the expected shape
# outputs = np.transpose(np.squeeze(output[0]))
# # Get the number of rows in the outputs array
# rows = outputs.shape[0]
# # Lists to store the bounding boxes, scores, and class IDs of the detections
# boxes = []
# scores = []
# class_ids = []
# # Calculate the scaling factors for the bounding box coordinates
# x_factor = self.img_width / self.inputWidth
# y_factor = self.img_height / self.inputHeight
# # Iterate over each row in the outputs array
# for i in range(rows):
# # Extract the class scores from the current row
# classes_scores = outputs[i][4:]
# # Find the maximum score among the class scores
# max_score = np.amax(classes_scores)
# # If the maximum score is above the confidence threshold
# if max_score >= self.confThreshold:
# # Get the class ID with the highest score
# class_id = np.argmax(classes_scores)
# # Extract the bounding box coordinates from the current row
# x, y, w, h = outputs[i][0], outputs[i][1], outputs[i][2], outputs[i][3]
# # Calculate the scaled coordinates of the bounding box
# left = int((x - w / 2) * x_factor)
# top = int((y - h / 2) * y_factor)
# width = int(w * x_factor)
# height = int(h * y_factor)
# # Add the class ID, score, and box coordinates to the respective lists
# class_ids.append(class_id)
# scores.append(max_score)
# boxes.append([left, top, width, height])
# # Apply non-maximum suppression to filter out overlapping bounding boxes
# indices = cv2.dnn.NMSBoxes(boxes, scores, self.confThreshold, self.nmsThreshold)
# # Iterate over the selected indices after non-maximum suppression
# for i in indices:
# # Get the box, score, and class ID corresponding to the index
# box = boxes[i]
# score = scores[i]
# class_id = class_ids[i]
# # Draw the detection on the input image
# self.draw_detections(input_image, box, score, class_id)
# # Return the modified input image
# return input_image
def
preproc
(
self
,
img
,
input_size
,
swap
=
(
2
,
0
,
1
)):
if
len
(
img
.
shape
)
==
3
:
padded_img
=
np
.
ones
((
input_size
[
0
],
input_size
[
1
],
3
),
dtype
=
np
.
uint8
)
*
114
...
...
@@ -466,27 +321,16 @@ class YOLOX:
# self.inputWidth = input_shape[3]
# self.inputHeight = input_shape[2]
# 输入图片预处理
# input_img = self.preprocess(image)
img
,
ratio
=
self
.
preproc
(
image
,
input_shape
)
flat_predictions
=
np
.
array
(
img
).
ravel
()
for
i
in
range
(
min
(
100
,
len
(
flat_predictions
))):
print
(
flat_predictions
[
i
])
# 执行推理
start
=
time
.
time
()
result
=
self
.
model
.
run
({
self
.
inputName
:
img
})
print
(
'net forward time: {:.4f}'
.
format
(
time
.
time
()
-
start
))
# 模型输出结果后处理
# dstimg = self.postprocess(image, result)
predictions
=
self
.
demo_postprocess
(
np
.
array
(
result
[
0
]),
input_shape
)[
0
]
# flat_predictions = np.array(result[0]).ravel()
# for i in range(min(100, len(flat_predictions))):
# print(flat_predictions[i])
boxes
=
predictions
[:,
:
4
]
scores
=
predictions
[:,
4
:
5
]
*
predictions
[:,
5
:]
print
(
"max(predictions[:, 4:5]):{}"
.
format
(
np
.
amax
(
predictions
[:,
4
:
5
])))
print
(
"max(predictions[:, 5:]):{}"
.
format
(
np
.
amax
(
predictions
[:,
5
:])))
print
(
"max(scores):{}"
.
format
(
np
.
amax
(
scores
)))
boxes_xyxy
=
np
.
ones_like
(
boxes
)
boxes_xyxy
[:,
0
]
=
boxes
[:,
0
]
-
boxes
[:,
2
]
/
2.
boxes_xyxy
[:,
1
]
=
boxes
[:,
1
]
-
boxes
[:,
3
]
/
2.
...
...
@@ -524,9 +368,6 @@ def yoloX_Static(imgpath, modelpath, confThreshold, nmsThreshold, output_dir, in
srcimg
=
cv2
.
imread
(
imgpath
,
1
)
dstimg
=
yoloX_detector
.
detect
(
srcimg
,
output_dir
,
imgpath
,
input_shape
)
# 保存检测结果
# cv2.imwrite("./Result.jpg", dstimg)
print
(
"Success to save result"
)
...
...
@@ -545,11 +386,6 @@ def yoloX_dynamic(imgpath, modelpath, confThreshold, nmsThreshold, output_dir, i
for
i
,
image
in
enumerate
(
image_lists
):
print
(
"Start to inference image{}"
.
format
(
i
))
dstimg
=
yoloX_detector
.
detect
(
image
,
output_dir
,
image_name_lists
[
i
],
input_shape
)
# 保存检测结果
# result_name = "Result{}.jpg".format(i)
# cv2.imwrite(result_name, dstimg)
print
(
"Success to save results"
)
if
__name__
==
'__main__'
:
...
...
@@ -562,7 +398,7 @@ if __name__ == '__main__':
parser
.
add_argument
(
'--confThreshold'
,
default
=
0.5
,
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 dynamic inference"
)
#
parser.add_argument("--dynamicInfer",action="store_true",default=False,help="Performing dynamic inference")
parser
.
add_argument
(
"-o"
,
"--output_dir"
,
...
...
@@ -582,6 +418,6 @@ if __name__ == '__main__':
# 静态推理
if
args
.
staticInfer
:
yoloX_Static
(
args
.
imgPath
,
args
.
staticModelPath
,
args
.
confThreshold
,
args
.
nmsThreshold
,
args
.
output_dir
,
input_shape
)
# 动态推理
if
args
.
dynamicInfer
:
yoloX_dynamic
(
args
.
imgFolderPath
,
args
.
dynamicModelPath
,
args
.
confThreshold
,
args
.
nmsThreshold
,
args
.
output_dir
,
input_shape
)
# 动态推理
暂不支持
#
if args.dynamicInfer:
#
yoloX_dynamic(args.imgFolderPath, args.dynamicModelPath, args.confThreshold, args.nmsThreshold, args.output_dir, input_shape)
Src/main.cpp
View file @
b19fd646
...
...
@@ -10,7 +10,7 @@ void MIGraphXSamplesUsage(char* programName)
printf
(
"Usage : %s <index>
\n
"
,
programName
);
printf
(
"index:
\n
"
);
printf
(
"
\t
0) YOLOX sample.
\n
"
);
printf
(
"
\t
1) YOLOX Dynamic sample.
\n
"
);
//
printf("\t 1) YOLOX Dynamic sample.\n");
暂不支持
}
void
Sample_YOLOX
();
...
...
@@ -37,7 +37,7 @@ int main(int argc, char *argv[])
}
case
'1'
:
{
Sample_YOLOX_Dynamic
();
//
Sample_YOLOX_Dynamic();
暂不支持
break
;
}
default
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment