"git@developer.sourcefind.cn:zhaoyu6/sglang.git" did not exist on "ca9291181df4b6a27c371eb71bf740263e5eb3ef"
Commit a5e2341e authored by yaoht's avatar yaoht
Browse files

fix cpp inputdata channel range problem

parent e80d6c72
This diff is collapsed.
#ifndef __DETECTOR_YOLOX_H__
#define __DETECTOR_YOLOX_H__
#include <migraphx/program.hpp>
#include <CommonDefinition.h>
#include <migraphx/program.hpp>
namespace migraphxSamples
{
namespace migraphxSamples {
typedef struct _YOLOXParameter
{
typedef struct _YOLOXParameter {
int numberOfClasses;
float confidenceThreshold;
float nmsThreshold;
float objectThreshold;
}YOLOXParameter;
} YOLOXParameter;
struct Object
{
struct Object {
cv::Rect_<float> rect;
int label;
float prob;
};
struct GridAndStride
{
struct GridAndStride {
int grid0;
int grid1;
int stride;
};
class DetectorYOLOX
{
public:
class DetectorYOLOX {
public:
DetectorYOLOX();
~DetectorYOLOX();
ErrorCode Initialize(InitializationParameterOfDetector initializationParameterOfDetector, bool dynamic);
void decode_outputs(float* prob, std::vector<Object>& objects, float scalew, float scaleh, const int img_w, const int img_h);
void generate_grids_and_stride(std::vector<int>& strides, std::vector<GridAndStride>& grid_strides);
void generate_yolox_proposals(std::vector<GridAndStride> grid_strides, float* feat_blob, float prob_threshold, std::vector<Object>& objects);
void qsort_descent_inplace(std::vector<Object>& faceobjects, int left, int right);
void qsort_descent_inplace(std::vector<Object>& objects);
void nms_sorted_bboxes(const std::vector<Object>& faceobjects, std::vector<int>& picked, float nms_threshold);
inline float intersection_area(const Object& a, const Object& b);
~DetectorYOLOX();
ErrorCode Detect(const cv::Mat &srcImage, std::vector<std::size_t> &relInputShape, std::vector<ResultOfDetection> &resultsOfDetection, bool dynamic);
ErrorCode Initialize(
InitializationParameterOfDetector initializationParameterOfDetector);
ErrorCode Detect(const cv::Mat &srcImage,
const std::vector<std::size_t> &relInputShape,
std::vector<ResultOfDetection> &resultsOfDetection);
private:
void decode_outputs(float *prob, std::vector<Object> &objects, float scalew,
float scaleh, const int img_w, const int img_h,
cv::Size inputSize);
void generate_grids_and_stride(std::vector<int> &strides,
std::vector<GridAndStride> &grid_strides,
cv::Size inputSize);
void generate_yolox_proposals(std::vector<GridAndStride> grid_strides,
float *feat_blob, float prob_threshold,
std::vector<Object> &objects);
void qsort_descent_inplace(std::vector<Object> &faceobjects, int left,
int right);
void qsort_descent_inplace(std::vector<Object> &objects);
void nms_sorted_bboxes(const std::vector<Object> &faceobjects,
std::vector<int> &picked, float nms_threshold);
inline float intersection_area(const Object &a, const Object &b);
private:
cv::FileStorage configurationFile;
migraphx::program net;
......@@ -57,15 +62,13 @@ private:
std::string inputName;
std::string modelPath;
migraphx::shape inputShape;
bool useFP16;
std::vector<std::string> classNames;
YOLOXParameter yoloxParameter;
};
}
} // namespace migraphxSamples
#endif
#include <Filesystem.h>
#include <SimpleLog.h>
#include <YOLOX.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SimpleLog.h>
#include <Filesystem.h>
#include <YOLOX.h>
void MIGraphXSamplesUsage(char* programName)
{
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"); 暂不支持
}
void Sample_YOLOX();
void Sample_YOLOX_Dynamic();
int main(int argc, char *argv[])
{
if (argc < 2 || argc > 2)
{
int main(int argc, char *argv[]) {
if (argc < 2 || argc > 2) {
MIGraphXSamplesUsage(argv[0]);
return -1;
}
if (!strncmp(argv[1], "-h", 2))
{
if (!strncmp(argv[1], "-h", 2)) {
MIGraphXSamplesUsage(argv[0]);
return 0;
}
switch (*argv[1])
{
case '0':
{
Sample_YOLOX();
break;
}
case '1':
{
// Sample_YOLOX_Dynamic(); 暂不支持
break;
}
default :
{
MIGraphXSamplesUsage(argv[0]);
break;
}
switch (*argv[1]) {
case '0': {
Sample_YOLOX();
break;
}
case '1': {
break;
}
default: {
MIGraphXSamplesUsage(argv[0]);
break;
}
}
return 0;
}
void Sample_YOLOX()
{
void Sample_YOLOX() {
// 创建YOLOX检测器
migraphxSamples::DetectorYOLOX detector;
migraphxSamples::InitializationParameterOfDetector initParamOfDetectorYOLOX;
initParamOfDetectorYOLOX.configFilePath = CONFIG_FILE;
migraphxSamples::ErrorCode errorCode=detector.Initialize(initParamOfDetectorYOLOX, false);
if(errorCode!=migraphxSamples::SUCCESS)
{
migraphxSamples::ErrorCode errorCode =
detector.Initialize(initParamOfDetectorYOLOX, false);
if (errorCode != migraphxSamples::SUCCESS) {
LOG_ERROR(stdout, "fail to initialize detector!\n");
exit(-1);
}
LOG_INFO(stdout, "succeed to initialize detector\n");
// 读取测试图片
cv::Mat srcImage = cv::imread("../Resource/Images/image_test.jpg",1);
cv::Mat srcImage = cv::imread("../Resource/Images/image_test.jpg", 1);
// 静态推理固定尺寸
std::vector<std::size_t> inputShape={1,3,640,640};
std::vector<std::size_t> inputShape = {1, 3, 640, 640};
// 推理
std::vector<migraphxSamples::ResultOfDetection> predictions;
double time1 = cv::getTickCount();
detector.Detect(srcImage,inputShape,predictions,false);
detector.Detect(srcImage, inputShape, predictions, false);
double time2 = cv::getTickCount();
double elapsedTime = (time2 - time1)*1000 / cv::getTickFrequency();
double elapsedTime = (time2 - time1) * 1000 / cv::getTickFrequency();
LOG_INFO(stdout, "inference time:%f ms\n", elapsedTime);
// 获取推理结果
LOG_INFO(stdout,"========== Detection Results ==========\n");
for(int i=0;i<predictions.size();++i)
{
migraphxSamples::ResultOfDetection result=predictions[i];
cv::rectangle(srcImage,result.boundingBox,cv::Scalar(0,255,255),2);
LOG_INFO(stdout, "========== Detection Results ==========\n");
for (int i = 0; i < predictions.size(); ++i) {
migraphxSamples::ResultOfDetection result = predictions[i];
cv::rectangle(srcImage, result.boundingBox, cv::Scalar(0, 255, 255), 2);
std::string label = cv::format("%.2f", result.confidence);
label = result.className + " " + label;
int left = predictions[i].boundingBox.x;
int top = predictions[i].boundingBox.y;
int baseLine;
cv::Size labelSize = cv::getTextSize(label, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
cv::Size labelSize =
cv::getTextSize(label, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
top = max(top, labelSize.height);
cv::putText(srcImage, label, cv::Point(left, top-10), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 255), 2);
LOG_INFO(stdout,"box:%d %d %d %d,label:%d,confidence:%f\n",predictions[i].boundingBox.x,
predictions[i].boundingBox.y,predictions[i].boundingBox.width,predictions[i].boundingBox.height,predictions[i].classID,predictions[i].confidence);
}
cv::imwrite("Result.jpg",srcImage);
LOG_INFO(stdout,"Detection results have been saved to ./Result.jpg\n");
}
void Sample_YOLOX_Dynamic()
{
// 创建YOLOX检测器
migraphxSamples::DetectorYOLOX detector;
migraphxSamples::InitializationParameterOfDetector initParamOfDetectorYOLOX;
initParamOfDetectorYOLOX.configFilePath = CONFIG_FILE;
migraphxSamples::ErrorCode errorCode=detector.Initialize(initParamOfDetectorYOLOX, true);
if(errorCode!=migraphxSamples::SUCCESS)
{
LOG_ERROR(stdout, "fail to initialize detector!\n");
exit(-1);
}
LOG_INFO(stdout, "succeed to initialize detector\n");
// 读取测试图像
std::vector<cv::Mat> srcImages;
cv::String folder = "../Resource/Images/DynamicPics";
std::vector<cv::String> imagePathList;
cv::glob(folder,imagePathList);
for (int i = 0; i < imagePathList.size(); ++i)
{
cv:: Mat srcImage=cv::imread(imagePathList[i], 1);
srcImages.push_back(srcImage);
}
// 设置动态推理shape
std::vector<std::vector<std::size_t>> inputShapes;
inputShapes.push_back({1,3,416,416});
inputShapes.push_back({1,3,608,608});
for (int i = 0; i < srcImages.size(); ++i)
{
// 推理
std::vector<migraphxSamples::ResultOfDetection> predictions;
double time1 = cv::getTickCount();
detector.Detect(srcImages[i], inputShapes[i], predictions, true);
double time2 = cv::getTickCount();
double elapsedTime = (time2 - time1)*1000 / cv::getTickFrequency();
LOG_INFO(stdout, "inference image%d time:%f ms\n", i, elapsedTime);
// 获取推理结果
LOG_INFO(stdout,"========== Detection Image%d Results ==========\n", i);
for(int j=0;j<predictions.size();++j)
{
migraphxSamples::ResultOfDetection result=predictions[j];
cv::rectangle(srcImages[i],result.boundingBox,cv::Scalar(0,255,255),2);
std::string label = cv::format("%.2f", result.confidence);
label = result.className + " " + label;
int left = predictions[j].boundingBox.x;
int top = predictions[j].boundingBox.y;
int baseLine;
cv::Size labelSize = cv::getTextSize(label, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
top = max(top, labelSize.height);
cv::putText(srcImages[i], label, cv::Point(left, top-10), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 255), 2);
LOG_INFO(stdout,"box:%d %d %d %d,label:%d,confidence:%f\n",predictions[j].boundingBox.x,
predictions[j].boundingBox.y,predictions[j].boundingBox.width,predictions[j].boundingBox.height,predictions[j].classID,predictions[j].confidence);
}
std::string imgName = cv::format("Result%d.jpg", i);
cv::imwrite(imgName, srcImages[i]);
LOG_INFO(stdout,"Detection results have been saved to ./Result%d.jpg\n", i);
cv::putText(srcImage, label, cv::Point(left, top - 10),
cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 255), 2);
LOG_INFO(stdout, "box:%d %d %d %d,label:%d,confidence:%f\n",
predictions[i].boundingBox.x, predictions[i].boundingBox.y,
predictions[i].boundingBox.width,
predictions[i].boundingBox.height, predictions[i].classID,
predictions[i].confidence);
}
cv::imwrite("Result.jpg", srcImage);
LOG_INFO(stdout, "Detection results have been saved to ./Result.jpg\n");
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment