Commit 0ed76f01 authored by liucong's avatar liucong
Browse files

重新进行Cpp代码格式化

parent 061ca3e8
...@@ -12,23 +12,17 @@ ...@@ -12,23 +12,17 @@
namespace migraphxSamples namespace migraphxSamples
{ {
Classifier::Classifier() Classifier::Classifier() {}
{
}
Classifier::~Classifier() Classifier::~Classifier() { configurationFile.release(); }
{
configurationFile.release();
}
cv::Mat Classifier::Preprocess(const std::vector<cv::Mat> &srcImages) cv::Mat Classifier::Preprocess(const std::vector<cv::Mat>& srcImages)
{ {
// 数据预处理 // 数据预处理
std::vector<cv::Mat> image; std::vector<cv::Mat> image;
for(int i =0;i<srcImages.size();++i) for(int i = 0; i < srcImages.size(); ++i)
{ {
//BGR转换为RGB // BGR转换为RGB
cv::Mat imgRGB; cv::Mat imgRGB;
cv::cvtColor(srcImages[i], imgRGB, cv::COLOR_BGR2RGB); cv::cvtColor(srcImages[i], imgRGB, cv::COLOR_BGR2RGB);
...@@ -45,8 +39,8 @@ cv::Mat Classifier::Preprocess(const std::vector<cv::Mat> &srcImages) ...@@ -45,8 +39,8 @@ cv::Mat Classifier::Preprocess(const std::vector<cv::Mat> &srcImages)
} }
// 裁剪中心窗口为224*224 // 裁剪中心窗口为224*224
int start_x = shrink.cols/2 - 224/2; int start_x = shrink.cols / 2 - 224 / 2;
int start_y = shrink.rows/2 - 224/2; int start_y = shrink.rows / 2 - 224 / 2;
cv::Rect rect(start_x, start_y, 224, 224); cv::Rect rect(start_x, start_y, 224, 224);
cv::Mat images = shrink(rect); cv::Mat images = shrink(rect);
image.push_back(images); image.push_back(images);
...@@ -55,17 +49,18 @@ cv::Mat Classifier::Preprocess(const std::vector<cv::Mat> &srcImages) ...@@ -55,17 +49,18 @@ cv::Mat Classifier::Preprocess(const std::vector<cv::Mat> &srcImages)
// normalize并转换为NCHW // normalize并转换为NCHW
cv::Mat inputBlob; cv::Mat inputBlob;
Image2BlobParams image2BlobParams; Image2BlobParams image2BlobParams;
image2BlobParams.scalefactor=cv::Scalar(1/58.395, 1/57.12, 1/57.375); image2BlobParams.scalefactor = cv::Scalar(1 / 58.395, 1 / 57.12, 1 / 57.375);
image2BlobParams.mean=cv::Scalar(123.675, 116.28, 103.53); image2BlobParams.mean = cv::Scalar(123.675, 116.28, 103.53);
image2BlobParams.swapRB=false; image2BlobParams.swapRB = false;
blobFromImagesWithParams(image,inputBlob,image2BlobParams); blobFromImagesWithParams(image, inputBlob, image2BlobParams);
return inputBlob; return inputBlob;
} }
ErrorCode Classifier::Initialize(InitializationParameterOfClassifier initializationParameterOfClassifier) ErrorCode
Classifier::Initialize(InitializationParameterOfClassifier initializationParameterOfClassifier)
{ {
// 读取配置文件 // 读取配置文件
std::string configFilePath=initializationParameterOfClassifier.configFilePath; std::string configFilePath = initializationParameterOfClassifier.configFilePath;
if(!Exists(configFilePath)) if(!Exists(configFilePath))
{ {
LOG_ERROR(stdout, "no configuration file!\n"); LOG_ERROR(stdout, "no configuration file!\n");
...@@ -73,39 +68,39 @@ ErrorCode Classifier::Initialize(InitializationParameterOfClassifier initializat ...@@ -73,39 +68,39 @@ ErrorCode Classifier::Initialize(InitializationParameterOfClassifier initializat
} }
if(!configurationFile.open(configFilePath, cv::FileStorage::READ)) if(!configurationFile.open(configFilePath, cv::FileStorage::READ))
{ {
LOG_ERROR(stdout, "fail to open configuration file\n"); LOG_ERROR(stdout, "fail to open configuration file\n");
return FAIL_TO_OPEN_CONFIG_FILE; return FAIL_TO_OPEN_CONFIG_FILE;
} }
LOG_INFO(stdout, "succeed to open configuration file\n"); LOG_INFO(stdout, "succeed to open configuration file\n");
// 获取配置文件参数 // 获取配置文件参数
cv::FileNode netNode = configurationFile["Classifier"]; cv::FileNode netNode = configurationFile["Classifier"];
std::string modelPath=(std::string)netNode["ModelPath"]; std::string modelPath = (std::string)netNode["ModelPath"];
useInt8=(bool)(int)netNode["UseInt8"]; useInt8 = (bool)(int)netNode["UseInt8"];
useFP16=(bool)(int)netNode["UseFP16"]; useFP16 = (bool)(int)netNode["UseFP16"];
useoffloadcopy=(bool)(int)netNode["Useoffloadcopy"]; useoffloadcopy = (bool)(int)netNode["Useoffloadcopy"];
// 加载模型 // 加载模型
if(!Exists(modelPath)) if(!Exists(modelPath))
{ {
LOG_ERROR(stdout,"%s not exist!\n",modelPath.c_str()); LOG_ERROR(stdout, "%s not exist!\n", modelPath.c_str());
return MODEL_NOT_EXIST; return MODEL_NOT_EXIST;
} }
net = migraphx::parse_onnx(modelPath); net = migraphx::parse_onnx(modelPath);
LOG_INFO(stdout,"succeed to load model: %s\n",GetFileName(modelPath).c_str()); LOG_INFO(stdout, "succeed to load model: %s\n", GetFileName(modelPath).c_str());
// 获取模型输入/输出节点信息 // 获取模型输入/输出节点信息
std::unordered_map<std::string, migraphx::shape> inputs=net.get_inputs(); std::unordered_map<std::string, migraphx::shape> inputs = net.get_inputs();
std::unordered_map<std::string, migraphx::shape> outputs=net.get_outputs(); std::unordered_map<std::string, migraphx::shape> outputs = net.get_outputs();
inputName=inputs.begin()->first; inputName = inputs.begin()->first;
inputShape=inputs.begin()->second; inputShape = inputs.begin()->second;
outputName=outputs.begin()->first; outputName = outputs.begin()->first;
outputShape=outputs.begin()->second; outputShape = outputs.begin()->second;
int N=inputShape.lens()[0]; int N = inputShape.lens()[0];
int C=inputShape.lens()[1]; int C = inputShape.lens()[1];
int H=inputShape.lens()[2]; int H = inputShape.lens()[2];
int W=inputShape.lens()[3]; int W = inputShape.lens()[3];
inputSize=cv::Size(W,H); inputSize = cv::Size(W, H);
// 设置模型为GPU模式 // 设置模型为GPU模式
migraphx::target gpuTarget = migraphx::gpu::target{}; migraphx::target gpuTarget = migraphx::gpu::target{};
...@@ -114,9 +109,9 @@ ErrorCode Classifier::Initialize(InitializationParameterOfClassifier initializat ...@@ -114,9 +109,9 @@ ErrorCode Classifier::Initialize(InitializationParameterOfClassifier initializat
if(useInt8) if(useInt8)
{ {
// 创建量化校准数据,建议使用测试集中的多张典型图像 // 创建量化校准数据,建议使用测试集中的多张典型图像
cv::Mat srcImage=cv::imread("../Resource/Images/ImageNet_test.jpg",1); cv::Mat srcImage = cv::imread("../Resource/Images/ImageNet_test.jpg", 1);
std::vector<cv::Mat> srcImages; std::vector<cv::Mat> srcImages;
for(int i=0;i<inputShape.lens()[0];++i) for(int i = 0; i < inputShape.lens()[0]; ++i)
{ {
srcImages.push_back(srcImage); srcImages.push_back(srcImage);
} }
...@@ -124,8 +119,9 @@ ErrorCode Classifier::Initialize(InitializationParameterOfClassifier initializat ...@@ -124,8 +119,9 @@ ErrorCode Classifier::Initialize(InitializationParameterOfClassifier initializat
// 数据预处理 // 数据预处理
cv::Mat inputBlob = Preprocess(srcImages); cv::Mat inputBlob = Preprocess(srcImages);
std::unordered_map<std::string, migraphx::argument> inputData; std::unordered_map<std::string, migraphx::argument> inputData;
inputData[inputName]= migraphx::argument{inputShape, (float*)inputBlob.data}; inputData[inputName] = migraphx::argument{inputShape, (float*)inputBlob.data};
std::vector<std::unordered_map<std::string, migraphx::argument>> calibrationData = {inputData}; std::vector<std::unordered_map<std::string, migraphx::argument>> calibrationData = {
inputData};
// INT8量化 // INT8量化
migraphx::quantize_int8(net, gpuTarget, calibrationData); migraphx::quantize_int8(net, gpuTarget, calibrationData);
...@@ -137,66 +133,67 @@ ErrorCode Classifier::Initialize(InitializationParameterOfClassifier initializat ...@@ -137,66 +133,67 @@ ErrorCode Classifier::Initialize(InitializationParameterOfClassifier initializat
// 编译模型 // 编译模型
migraphx::compile_options options; migraphx::compile_options options;
options.device_id=0; // 设置GPU设备,默认为0号设备 options.device_id = 0; // 设置GPU设备,默认为0号设备
if(useoffloadcopy) if(useoffloadcopy)
{ {
options.offload_copy=true; options.offload_copy = true;
} }
else else
{ {
options.offload_copy=false; options.offload_copy = false;
} }
net.compile(gpuTarget,options); net.compile(gpuTarget, options);
LOG_INFO(stdout,"succeed to compile model: %s\n",GetFileName(modelPath).c_str()); LOG_INFO(stdout, "succeed to compile model: %s\n", GetFileName(modelPath).c_str());
// offloadcopy为false的时候,分配输入和输出内存 // offloadcopy为false的时候,分配输入和输出内存
if(!useoffloadcopy) if(!useoffloadcopy)
{ {
// 分配device输入内存 // 分配device输入内存
inputBuffer_Device=nullptr; inputBuffer_Device = nullptr;
hipMalloc(&inputBuffer_Device, inputShape.bytes()); hipMalloc(&inputBuffer_Device, inputShape.bytes());
programParameters[inputName] = migraphx::argument{inputShape, inputBuffer_Device}; programParameters[inputName] = migraphx::argument{inputShape, inputBuffer_Device};
// 分配device和host输出内存 // 分配device和host输出内存
outputBuffer_Device=nullptr; outputBuffer_Device = nullptr;
hipMalloc(&outputBuffer_Device, outputShape.bytes()); hipMalloc(&outputBuffer_Device, outputShape.bytes());
programParameters[outputName] = migraphx::argument{outputShape, outputBuffer_Device}; programParameters[outputName] = migraphx::argument{outputShape, outputBuffer_Device};
outputBuffer_Host=nullptr; // host内存 outputBuffer_Host = nullptr; // host内存
outputBuffer_Host=malloc(outputShape.bytes()); outputBuffer_Host = malloc(outputShape.bytes());
} }
// warm up // warm up
if(useoffloadcopy) if(useoffloadcopy)
{ {
std::unordered_map<std::string, migraphx::argument> inputData; std::unordered_map<std::string, migraphx::argument> inputData;
inputData[inputName]=migraphx::argument{inputShape}; inputData[inputName] = migraphx::argument{inputShape};
net.eval(inputData); net.eval(inputData);
} }
else else
{ {
migraphx::argument inputData= migraphx::argument{inputShape}; migraphx::argument inputData = migraphx::argument{inputShape};
hipMemcpy(inputBuffer_Device, inputData.data(), inputShape.bytes(), hipMemcpyHostToDevice); hipMemcpy(inputBuffer_Device, inputData.data(), inputShape.bytes(), hipMemcpyHostToDevice);
net.eval(programParameters); net.eval(programParameters);
} }
// log // log
LOG_INFO(stdout,"InputSize:%dx%d\n",inputSize.width,inputSize.height); LOG_INFO(stdout, "InputSize:%dx%d\n", inputSize.width, inputSize.height);
LOG_INFO(stdout,"InputName:%s\n",inputName.c_str()); LOG_INFO(stdout, "InputName:%s\n", inputName.c_str());
LOG_INFO(stdout,"UseInt8:%d\n",(int)useInt8); LOG_INFO(stdout, "UseInt8:%d\n", (int)useInt8);
LOG_INFO(stdout,"UseFP16:%d\n",(int)useFP16); LOG_INFO(stdout, "UseFP16:%d\n", (int)useFP16);
LOG_INFO(stdout,"Useoffloadcopy:%d\n",(int)useoffloadcopy); LOG_INFO(stdout, "Useoffloadcopy:%d\n", (int)useoffloadcopy);
return SUCCESS; return SUCCESS;
} }
ErrorCode Classifier::Classify(const std::vector<cv::Mat> &srcImages,std::vector<std::vector<ResultOfPrediction>> &predictions) ErrorCode Classifier::Classify(const std::vector<cv::Mat>& srcImages,
std::vector<std::vector<ResultOfPrediction>>& predictions)
{ {
if(srcImages.size()==0||srcImages[0].empty()||srcImages[0].depth()!=CV_8U) if(srcImages.size() == 0 || srcImages[0].empty() || srcImages[0].depth() != CV_8U)
{ {
LOG_ERROR(stdout, "image error!\n"); LOG_ERROR(stdout, "image error!\n");
return IMAGE_ERROR; return IMAGE_ERROR;
} }
// 数据预处理 // 数据预处理
cv::Mat inputBlob = Preprocess(srcImages); cv::Mat inputBlob = Preprocess(srcImages);
...@@ -204,36 +201,37 @@ ErrorCode Classifier::Classify(const std::vector<cv::Mat> &srcImages,std::vector ...@@ -204,36 +201,37 @@ ErrorCode Classifier::Classify(const std::vector<cv::Mat> &srcImages,std::vector
if(useoffloadcopy) if(useoffloadcopy)
{ {
std::unordered_map<std::string, migraphx::argument> inputData; std::unordered_map<std::string, migraphx::argument> inputData;
inputData[inputName]= migraphx::argument{inputShape, (float*)inputBlob.data}; inputData[inputName] = migraphx::argument{inputShape, (float*)inputBlob.data};
// 推理 // 推理
std::vector<migraphx::argument> results = net.eval(inputData); std::vector<migraphx::argument> results = net.eval(inputData);
// 获取输出节点的属性 // 获取输出节点的属性
migraphx::argument result = results[0]; // 获取第一个输出节点的数据 migraphx::argument result = results[0]; // 获取第一个输出节点的数据
migraphx::shape outputShapes=result.get_shape(); // 输出节点的shape migraphx::shape outputShapes = result.get_shape(); // 输出节点的shape
std::vector<std::size_t> outputSize=outputShapes.lens(); // 每一维大小,维度顺序为(N,C,H,W) std::vector<std::size_t> outputSize =
int numberOfOutput=outputShapes.elements(); // 输出节点元素的个数 outputShapes.lens(); // 每一维大小,维度顺序为(N,C,H,W)
float *logits=(float *)result.data(); // 输出节点数据指针 int numberOfOutput = outputShapes.elements(); // 输出节点元素的个数
float* logits = (float*)result.data(); // 输出节点数据指针
// 获取每张图像的预测结果 // 获取每张图像的预测结果
int numberOfClasses=numberOfOutput/srcImages.size(); int numberOfClasses = numberOfOutput / srcImages.size();
for(int i=0;i<srcImages.size();++i) for(int i = 0; i < srcImages.size(); ++i)
{ {
int startIndex=numberOfClasses*i; int startIndex = numberOfClasses * i;
std::vector<float> logit; std::vector<float> logit;
for(int j=0;j<numberOfClasses;++j) for(int j = 0; j < numberOfClasses; ++j)
{ {
logit.push_back(logits[startIndex+j]); logit.push_back(logits[startIndex + j]);
} }
std::vector<ResultOfPrediction> resultOfPredictions; std::vector<ResultOfPrediction> resultOfPredictions;
for(int j=0;j<numberOfClasses;++j) for(int j = 0; j < numberOfClasses; ++j)
{ {
ResultOfPrediction prediction; ResultOfPrediction prediction;
prediction.label=j; prediction.label = j;
prediction.confidence=logit[j]; prediction.confidence = logit[j];
resultOfPredictions.push_back(prediction); resultOfPredictions.push_back(prediction);
} }
...@@ -241,7 +239,7 @@ ErrorCode Classifier::Classify(const std::vector<cv::Mat> &srcImages,std::vector ...@@ -241,7 +239,7 @@ ErrorCode Classifier::Classify(const std::vector<cv::Mat> &srcImages,std::vector
predictions.push_back(resultOfPredictions); predictions.push_back(resultOfPredictions);
} }
} }
else // 当offload为false时,需要内存拷贝 else // 当offload为false时,需要内存拷贝
{ {
migraphx::argument inputData = migraphx::argument{inputShape, (float*)inputBlob.data}; migraphx::argument inputData = migraphx::argument{inputShape, (float*)inputBlob.data};
...@@ -253,31 +251,35 @@ ErrorCode Classifier::Classify(const std::vector<cv::Mat> &srcImages,std::vector ...@@ -253,31 +251,35 @@ ErrorCode Classifier::Classify(const std::vector<cv::Mat> &srcImages,std::vector
std::vector<migraphx::argument> results = net.eval(programParameters); std::vector<migraphx::argument> results = net.eval(programParameters);
// 获取输出节点的属性 // 获取输出节点的属性
migraphx::argument result = results[0]; // 获取第一个输出节点的数据 migraphx::argument result = results[0]; // 获取第一个输出节点的数据
migraphx::shape outputShapes=result.get_shape(); // 输出节点的shape migraphx::shape outputShapes = result.get_shape(); // 输出节点的shape
std::vector<std::size_t> outputSize=outputShapes.lens(); // 每一维大小,维度顺序为(N,C,H,W) std::vector<std::size_t> outputSize =
int numberOfOutput=outputShapes.elements(); // 输出节点元素的个数 outputShapes.lens(); // 每一维大小,维度顺序为(N,C,H,W)
int numberOfOutput = outputShapes.elements(); // 输出节点元素的个数
// 将device输出数据拷贝到分配好的host输出内存 // 将device输出数据拷贝到分配好的host输出内存
hipMemcpy(outputBuffer_Host, outputBuffer_Device, outputShapes.bytes(), hipMemcpyDeviceToHost); // 直接使用事先分配好的输出内存拷贝 hipMemcpy(outputBuffer_Host,
outputBuffer_Device,
outputShapes.bytes(),
hipMemcpyDeviceToHost); // 直接使用事先分配好的输出内存拷贝
// 获取每张图像的预测结果 // 获取每张图像的预测结果
int numberOfClasses=numberOfOutput/srcImages.size(); int numberOfClasses = numberOfOutput / srcImages.size();
std::vector<float> logit; std::vector<float> logit;
for(int i=0;i<srcImages.size();++i) for(int i = 0; i < srcImages.size(); ++i)
{ {
int startIndex=numberOfClasses*i; int startIndex = numberOfClasses * i;
for(int j=0;j<numberOfClasses;++j) for(int j = 0; j < numberOfClasses; ++j)
{ {
logit.push_back(((float *)outputBuffer_Host)[startIndex+j]); logit.push_back(((float*)outputBuffer_Host)[startIndex + j]);
} }
std::vector<ResultOfPrediction> resultOfPredictions; std::vector<ResultOfPrediction> resultOfPredictions;
for(int j=0;j<numberOfClasses;++j) for(int j = 0; j < numberOfClasses; ++j)
{ {
ResultOfPrediction prediction; ResultOfPrediction prediction;
prediction.label=j; prediction.label = j;
prediction.confidence=logit[j]; prediction.confidence = logit[j];
resultOfPredictions.push_back(prediction); resultOfPredictions.push_back(prediction);
} }
...@@ -292,7 +294,6 @@ ErrorCode Classifier::Classify(const std::vector<cv::Mat> &srcImages,std::vector ...@@ -292,7 +294,6 @@ ErrorCode Classifier::Classify(const std::vector<cv::Mat> &srcImages,std::vector
} }
return SUCCESS; return SUCCESS;
} }
} } // namespace migraphxSamples
...@@ -8,22 +8,23 @@ ...@@ -8,22 +8,23 @@
namespace migraphxSamples namespace migraphxSamples
{ {
class Classifier class Classifier
{ {
public: public:
Classifier(); Classifier();
~Classifier(); ~Classifier();
ErrorCode Initialize(InitializationParameterOfClassifier initializationParameterOfClassifier); ErrorCode Initialize(InitializationParameterOfClassifier initializationParameterOfClassifier);
cv::Mat Preprocess(const std::vector<cv::Mat> &srcImages); cv::Mat Preprocess(const std::vector<cv::Mat>& srcImages);
ErrorCode Classify(const std::vector<cv::Mat> &srcImages,std::vector<std::vector<ResultOfPrediction>> &predictions); ErrorCode Classify(const std::vector<cv::Mat>& srcImages,
std::vector<std::vector<ResultOfPrediction>>& predictions);
private: private:
cv::FileStorage configurationFile; cv::FileStorage configurationFile;
migraphx::program net; migraphx::program net;
cv::Size inputSize; cv::Size inputSize;
std::string inputName; std::string inputName;
...@@ -32,18 +33,15 @@ private: ...@@ -32,18 +33,15 @@ private:
migraphx::shape outputShape; migraphx::shape outputShape;
std::unordered_map<std::string, migraphx::argument> programParameters; std::unordered_map<std::string, migraphx::argument> programParameters;
void *inputBuffer_Device; void* inputBuffer_Device;
void *outputBuffer_Device; void* outputBuffer_Device;
void *outputBuffer_Host; void* outputBuffer_Host;
bool useInt8; bool useInt8;
bool useFP16; bool useFP16;
bool useoffloadcopy; bool useoffloadcopy;
}; };
} } // namespace migraphxSamples
#endif #endif
...@@ -7,41 +7,40 @@ ...@@ -7,41 +7,40 @@
namespace migraphxSamples namespace migraphxSamples
{ {
// 路径分隔符(Linux:‘/’,Windows:’\\’) // 路径分隔符(Linux:‘/’,Windows:’\\’)
#ifdef _WIN32 #ifdef _WIN32
#define PATH_SEPARATOR '\\' #define PATH_SEPARATOR '\\'
#else #else
#define PATH_SEPARATOR '/' #define PATH_SEPARATOR '/'
#endif #endif
#define CONFIG_FILE "../Resource/Configuration.xml" #define CONFIG_FILE "../Resource/Configuration.xml"
typedef enum _ErrorCode typedef enum _ErrorCode
{ {
SUCCESS=0, // 0 SUCCESS = 0, // 0
MODEL_NOT_EXIST, // 模型不存在 MODEL_NOT_EXIST, // 模型不存在
CONFIG_FILE_NOT_EXIST, // 配置文件不存在 CONFIG_FILE_NOT_EXIST, // 配置文件不存在
FAIL_TO_LOAD_MODEL, // 加载模型失败 FAIL_TO_LOAD_MODEL, // 加载模型失败
FAIL_TO_OPEN_CONFIG_FILE, // 加载配置文件失败 FAIL_TO_OPEN_CONFIG_FILE, // 加载配置文件失败
IMAGE_ERROR, // 图像错误 IMAGE_ERROR, // 图像错误
}ErrorCode; } ErrorCode;
typedef struct _ResultOfPrediction typedef struct _ResultOfPrediction
{ {
float confidence; float confidence;
int label; int label;
_ResultOfPrediction():confidence(0.0f),label(0){} _ResultOfPrediction() : confidence(0.0f), label(0) {}
}ResultOfPrediction; } ResultOfPrediction;
typedef struct _InitializationParameterOfClassifier typedef struct _InitializationParameterOfClassifier
{ {
std::string parentPath; std::string parentPath;
std::string configFilePath; std::string configFilePath;
}InitializationParameterOfClassifier; } InitializationParameterOfClassifier;
} } // namespace migraphxSamples
#endif #endif
...@@ -6,37 +6,44 @@ using namespace cv::dnn; ...@@ -6,37 +6,44 @@ using namespace cv::dnn;
namespace migraphxSamples namespace migraphxSamples
{ {
void blobFromImagesWithParams(InputArrayOfArrays images_, OutputArray blob_, const Image2BlobParams& param) void blobFromImagesWithParams(InputArrayOfArrays images_,
OutputArray blob_,
const Image2BlobParams& param)
{ {
if (images_.kind() != _InputArray::STD_VECTOR_MAT && images_.kind() != _InputArray::STD_ARRAY_MAT && if(images_.kind() != _InputArray::STD_VECTOR_MAT &&
images_.kind() != _InputArray::STD_VECTOR_VECTOR) { images_.kind() != _InputArray::STD_ARRAY_MAT &&
images_.kind() != _InputArray::STD_VECTOR_VECTOR)
{
String error_message = "The data is expected as vectors of vectors or vectors of matrices."; String error_message = "The data is expected as vectors of vectors or vectors of matrices.";
CV_Error(Error::StsBadArg, error_message); CV_Error(Error::StsBadArg, error_message);
} }
CV_CheckType(param.ddepth, param.ddepth == CV_32F || param.ddepth == CV_8U, CV_CheckType(param.ddepth,
param.ddepth == CV_32F || param.ddepth == CV_8U,
"Blob depth should be CV_32F or CV_8U"); "Blob depth should be CV_32F or CV_8U");
Size size = param.size; Size size = param.size;
std::vector<Mat> images; std::vector<Mat> images;
images_.getMatVector(images); images_.getMatVector(images);
CV_Assert(!images.empty()); CV_Assert(!images.empty());
int nch = images[0].channels(); int nch = images[0].channels();
Scalar scalefactor = param.scalefactor; Scalar scalefactor = param.scalefactor;
if (param.ddepth == CV_8U) if(param.ddepth == CV_8U)
{ {
CV_Assert(scalefactor == Scalar::all(1.0) && "Scaling is not supported for CV_8U blob depth"); CV_Assert(scalefactor == Scalar::all(1.0) &&
CV_Assert(param.mean == Scalar() && "Mean subtraction is not supported for CV_8U blob depth"); "Scaling is not supported for CV_8U blob depth");
CV_Assert(param.mean == Scalar() &&
"Mean subtraction is not supported for CV_8U blob depth");
} }
for (size_t i = 0; i < images.size(); i++) for(size_t i = 0; i < images.size(); i++)
{ {
Size imgSize = images[i].size(); Size imgSize = images[i].size();
if (size == Size()) if(size == Size())
size = imgSize; size = imgSize;
if (size != imgSize) if(size != imgSize)
{ {
if (param.paddingmode == DNN_PMODE_CROP_CENTER) if(param.paddingmode == DNN_PMODE_CROP_CENTER)
{ {
float resizeFactor = std::max(size.width / (float)imgSize.width, float resizeFactor = std::max(size.width / (float)imgSize.width,
size.height / (float)imgSize.height); size.height / (float)imgSize.height);
...@@ -48,18 +55,18 @@ void blobFromImagesWithParams(InputArrayOfArrays images_, OutputArray blob_, con ...@@ -48,18 +55,18 @@ void blobFromImagesWithParams(InputArrayOfArrays images_, OutputArray blob_, con
} }
else else
{ {
if (param.paddingmode == DNN_PMODE_LETTERBOX) if(param.paddingmode == DNN_PMODE_LETTERBOX)
{ {
float resizeFactor = std::min(size.width / (float)imgSize.width, float resizeFactor = std::min(size.width / (float)imgSize.width,
size.height / (float)imgSize.height); size.height / (float)imgSize.height);
int rh = int(imgSize.height * resizeFactor); int rh = int(imgSize.height * resizeFactor);
int rw = int(imgSize.width * resizeFactor); int rw = int(imgSize.width * resizeFactor);
resize(images[i], images[i], Size(rw, rh), INTER_LINEAR); resize(images[i], images[i], Size(rw, rh), INTER_LINEAR);
int top = (size.height - rh)/2; int top = (size.height - rh) / 2;
int bottom = size.height - top - rh; int bottom = size.height - top - rh;
int left = (size.width - rw)/2; int left = (size.width - rw) / 2;
int right = size.width - left - rw; int right = size.width - left - rw;
copyMakeBorder(images[i], images[i], top, bottom, left, right, BORDER_CONSTANT); copyMakeBorder(images[i], images[i], top, bottom, left, right, BORDER_CONSTANT);
} }
else else
...@@ -68,13 +75,13 @@ void blobFromImagesWithParams(InputArrayOfArrays images_, OutputArray blob_, con ...@@ -68,13 +75,13 @@ void blobFromImagesWithParams(InputArrayOfArrays images_, OutputArray blob_, con
} }
Scalar mean = param.mean; Scalar mean = param.mean;
if (param.swapRB) if(param.swapRB)
{ {
std::swap(mean[0], mean[2]); std::swap(mean[0], mean[2]);
std::swap(scalefactor[0], scalefactor[2]); std::swap(scalefactor[0], scalefactor[2]);
} }
if (images[i].depth() == CV_8U && param.ddepth == CV_32F) if(images[i].depth() == CV_8U && param.ddepth == CV_32F)
images[i].convertTo(images[i], CV_32F); images[i].convertTo(images[i], CV_32F);
images[i] -= mean; images[i] -= mean;
...@@ -82,19 +89,19 @@ void blobFromImagesWithParams(InputArrayOfArrays images_, OutputArray blob_, con ...@@ -82,19 +89,19 @@ void blobFromImagesWithParams(InputArrayOfArrays images_, OutputArray blob_, con
} }
size_t nimages = images.size(); size_t nimages = images.size();
Mat image0 = images[0]; Mat image0 = images[0];
CV_Assert(image0.dims == 2); CV_Assert(image0.dims == 2);
if (param.datalayout == DNN_LAYOUT_NCHW) if(param.datalayout == DNN_LAYOUT_NCHW)
{ {
if (nch == 3 || nch == 4) if(nch == 3 || nch == 4)
{ {
int sz[] = { (int)nimages, nch, image0.rows, image0.cols }; int sz[] = {(int)nimages, nch, image0.rows, image0.cols};
blob_.create(4, sz, param.ddepth); blob_.create(4, sz, param.ddepth);
Mat blob = blob_.getMat(); Mat blob = blob_.getMat();
Mat ch[4]; Mat ch[4];
for (size_t i = 0; i < nimages; i++) for(size_t i = 0; i < nimages; i++)
{ {
const Mat& image = images[i]; const Mat& image = images[i];
CV_Assert(image.depth() == blob_.depth()); CV_Assert(image.depth() == blob_.depth());
...@@ -102,9 +109,9 @@ void blobFromImagesWithParams(InputArrayOfArrays images_, OutputArray blob_, con ...@@ -102,9 +109,9 @@ void blobFromImagesWithParams(InputArrayOfArrays images_, OutputArray blob_, con
CV_Assert(image.dims == 2 && (nch == 3 || nch == 4)); CV_Assert(image.dims == 2 && (nch == 3 || nch == 4));
CV_Assert(image.size() == image0.size()); CV_Assert(image.size() == image0.size());
for (int j = 0; j < nch; j++) for(int j = 0; j < nch; j++)
ch[j] = Mat(image.rows, image.cols, param.ddepth, blob.ptr((int)i, j)); ch[j] = Mat(image.rows, image.cols, param.ddepth, blob.ptr((int)i, j));
if (param.swapRB) if(param.swapRB)
std::swap(ch[0], ch[2]); std::swap(ch[0], ch[2]);
split(image, ch); split(image, ch);
} }
...@@ -112,11 +119,11 @@ void blobFromImagesWithParams(InputArrayOfArrays images_, OutputArray blob_, con ...@@ -112,11 +119,11 @@ void blobFromImagesWithParams(InputArrayOfArrays images_, OutputArray blob_, con
else else
{ {
CV_Assert(nch == 1); CV_Assert(nch == 1);
int sz[] = { (int)nimages, 1, image0.rows, image0.cols }; int sz[] = {(int)nimages, 1, image0.rows, image0.cols};
blob_.create(4, sz, param.ddepth); blob_.create(4, sz, param.ddepth);
Mat blob = blob_.getMat(); Mat blob = blob_.getMat();
for (size_t i = 0; i < nimages; i++) for(size_t i = 0; i < nimages; i++)
{ {
const Mat& image = images[i]; const Mat& image = images[i];
CV_Assert(image.depth() == blob_.depth()); CV_Assert(image.depth() == blob_.depth());
...@@ -128,19 +135,19 @@ void blobFromImagesWithParams(InputArrayOfArrays images_, OutputArray blob_, con ...@@ -128,19 +135,19 @@ void blobFromImagesWithParams(InputArrayOfArrays images_, OutputArray blob_, con
} }
} }
} }
else if (param.datalayout == DNN_LAYOUT_NHWC) else if(param.datalayout == DNN_LAYOUT_NHWC)
{ {
int sz[] = { (int)nimages, image0.rows, image0.cols, nch}; int sz[] = {(int)nimages, image0.rows, image0.cols, nch};
blob_.create(4, sz, param.ddepth); blob_.create(4, sz, param.ddepth);
Mat blob = blob_.getMat(); Mat blob = blob_.getMat();
int subMatType = CV_MAKETYPE(param.ddepth, nch); int subMatType = CV_MAKETYPE(param.ddepth, nch);
for (size_t i = 0; i < nimages; i++) for(size_t i = 0; i < nimages; i++)
{ {
const Mat& image = images[i]; const Mat& image = images[i];
CV_Assert(image.depth() == blob_.depth()); CV_Assert(image.depth() == blob_.depth());
CV_Assert(image.channels() == image0.channels()); CV_Assert(image.channels() == image0.channels());
CV_Assert(image.size() == image0.size()); CV_Assert(image.size() == image0.size());
if (param.swapRB) if(param.swapRB)
{ {
Mat tmpRB; Mat tmpRB;
cvtColor(image, tmpRB, COLOR_BGR2RGB); cvtColor(image, tmpRB, COLOR_BGR2RGB);
...@@ -151,7 +158,8 @@ void blobFromImagesWithParams(InputArrayOfArrays images_, OutputArray blob_, con ...@@ -151,7 +158,8 @@ void blobFromImagesWithParams(InputArrayOfArrays images_, OutputArray blob_, con
} }
} }
else else
CV_Error(Error::StsUnsupportedFormat, "Unsupported data layout in blobFromImagesWithParams function."); CV_Error(Error::StsUnsupportedFormat,
"Unsupported data layout in blobFromImagesWithParams function.");
} }
} } // namespace migraphxSamples
...@@ -12,46 +12,69 @@ namespace migraphxSamples ...@@ -12,46 +12,69 @@ namespace migraphxSamples
{ {
enum DataLayout enum DataLayout
{
DNN_LAYOUT_UNKNOWN = 0,
DNN_LAYOUT_ND = 1, //!< OpenCV data layout for 2D data.
DNN_LAYOUT_NCHW = 2, //!< OpenCV data layout for 4D data.
DNN_LAYOUT_NCDHW = 3, //!< OpenCV data layout for 5D data.
DNN_LAYOUT_NHWC = 4, //!< Tensorflow-like data layout for 4D data.
DNN_LAYOUT_NDHWC = 5, //!< Tensorflow-like data layout for 5D data.
DNN_LAYOUT_PLANAR =
6, //!< Tensorflow-like data layout, it should only be used at tf or tflite model parsing.
};
enum ImagePaddingMode
{
DNN_PMODE_NULL = 0, // !< Default. Resize to required input size without extra processing.
DNN_PMODE_CROP_CENTER = 1, // !< Image will be cropped after resize.
DNN_PMODE_LETTERBOX = 2, // !< Resize image to the desired size while preserving the aspect
// ratio of original image.
};
struct Image2BlobParams
{
Image2BlobParams()
: scalefactor(Scalar::all(1.0)),
size(Size()),
mean(Scalar()),
swapRB(false),
ddepth(CV_32F),
datalayout(DNN_LAYOUT_NCHW),
paddingmode(DNN_PMODE_NULL)
{ {
DNN_LAYOUT_UNKNOWN = 0, }
DNN_LAYOUT_ND = 1, //!< OpenCV data layout for 2D data.
DNN_LAYOUT_NCHW = 2, //!< OpenCV data layout for 4D data.
DNN_LAYOUT_NCDHW = 3, //!< OpenCV data layout for 5D data.
DNN_LAYOUT_NHWC = 4, //!< Tensorflow-like data layout for 4D data.
DNN_LAYOUT_NDHWC = 5, //!< Tensorflow-like data layout for 5D data.
DNN_LAYOUT_PLANAR = 6, //!< Tensorflow-like data layout, it should only be used at tf or tflite model parsing.
};
enum ImagePaddingMode
{
DNN_PMODE_NULL = 0, // !< Default. Resize to required input size without extra processing.
DNN_PMODE_CROP_CENTER = 1, // !< Image will be cropped after resize.
DNN_PMODE_LETTERBOX = 2, // !< Resize image to the desired size while preserving the aspect ratio of original image.
};
struct Image2BlobParams Image2BlobParams(const Scalar& scalefactor_,
const Size& size_,
const Scalar& mean_,
bool swapRB_,
int ddepth_,
DataLayout datalayout_,
ImagePaddingMode mode_)
: scalefactor(scalefactor_),
size(size_),
mean(mean_),
swapRB(swapRB_),
ddepth(ddepth_),
datalayout(datalayout_),
paddingmode(mode_)
{ {
Image2BlobParams():scalefactor(Scalar::all(1.0)), size(Size()), mean(Scalar()), swapRB(false), ddepth(CV_32F), }
datalayout(DNN_LAYOUT_NCHW), paddingmode(DNN_PMODE_NULL)
{} Scalar scalefactor; //!< scalefactor multiplier for input image values.
Size size; //!< Spatial size for output image.
Image2BlobParams(const Scalar& scalefactor_, const Size& size_, const Scalar& mean_, bool swapRB_, Scalar mean; //!< Scalar with mean values which are subtracted from channels.
int ddepth_, DataLayout datalayout_, ImagePaddingMode mode_): bool swapRB; //!< Flag which indicates that swap first and last channels
scalefactor(scalefactor_), size(size_), mean(mean_), swapRB(swapRB_), ddepth(ddepth_), int ddepth; //!< Depth of output blob. Choose CV_32F or CV_8U.
datalayout(datalayout_), paddingmode(mode_) DataLayout
{} datalayout; //!< Order of output dimensions. Choose DNN_LAYOUT_NCHW or DNN_LAYOUT_NHWC.
ImagePaddingMode paddingmode; //!< Image padding mode. @see ImagePaddingMode.
Scalar scalefactor; //!< scalefactor multiplier for input image values. };
Size size; //!< Spatial size for output image.
Scalar mean; //!< Scalar with mean values which are subtracted from channels. void blobFromImagesWithParams(InputArrayOfArrays images_,
bool swapRB; //!< Flag which indicates that swap first and last channels OutputArray blob_,
int ddepth; //!< Depth of output blob. Choose CV_32F or CV_8U. const Image2BlobParams& param);
DataLayout datalayout; //!< Order of output dimensions. Choose DNN_LAYOUT_NCHW or DNN_LAYOUT_NHWC.
ImagePaddingMode paddingmode; //!< Image padding mode. @see ImagePaddingMode. } // namespace migraphxSamples
};
void blobFromImagesWithParams(InputArrayOfArrays images_, OutputArray blob_, const Image2BlobParams& param);
}
#endif #endif
...@@ -14,9 +14,9 @@ ...@@ -14,9 +14,9 @@
// 路径分隔符(Linux:‘/’,Windows:’\\’) // 路径分隔符(Linux:‘/’,Windows:’\\’)
#ifdef _WIN32 #ifdef _WIN32
#define PATH_SEPARATOR '\\' #define PATH_SEPARATOR '\\'
#else #else
#define PATH_SEPARATOR '/' #define PATH_SEPARATOR '/'
#endif #endif
using namespace std; using namespace std;
...@@ -28,666 +28,672 @@ static std::vector<std::string> SplitString(std::string str, std::string separat ...@@ -28,666 +28,672 @@ static std::vector<std::string> SplitString(std::string str, std::string separat
{ {
std::string::size_type pos; std::string::size_type pos;
std::vector<std::string> result; std::vector<std::string> result;
str+=separator;//扩展字符串以方便操作 str += separator; // 扩展字符串以方便操作
int size=str.size(); int size = str.size();
for(int i=0; i<size; i++) for(int i = 0; i < size; i++)
{ {
pos=str.find(separator,i); pos = str.find(separator, i);
if(pos<size) if(pos < size)
{ {
std::string s=str.substr(i,pos-i); std::string s = str.substr(i, pos - i);
result.push_back(s); result.push_back(s);
i=pos+separator.size()-1; i = pos + separator.size() - 1;
} }
} }
return result; return result;
} }
#if defined _WIN32 || defined WINCE #if defined _WIN32 || defined WINCE
const char dir_separators[] = "/\\"; const char dir_separators[] = "/\\";
struct dirent struct dirent
{ {
const char* d_name; const char* d_name;
}; };
struct DIR struct DIR
{ {
#ifdef WINRT #ifdef WINRT
WIN32_FIND_DATAW data; WIN32_FIND_DATAW data;
#else #else
WIN32_FIND_DATAA data; WIN32_FIND_DATAA data;
#endif #endif
HANDLE handle; HANDLE handle;
dirent ent; dirent ent;
#ifdef WINRT #ifdef WINRT
DIR() { } DIR() {}
~DIR() ~DIR()
{ {
if (ent.d_name) if(ent.d_name)
delete[] ent.d_name; delete[] ent.d_name;
} }
#endif #endif
}; };
DIR* opendir(const char* path) DIR* opendir(const char* path)
{ {
DIR* dir = new DIR; DIR* dir = new DIR;
dir->ent.d_name = 0; dir->ent.d_name = 0;
#ifdef WINRT #ifdef WINRT
string full_path = string(path) + "\\*"; string full_path = string(path) + "\\*";
wchar_t wfull_path[MAX_PATH]; wchar_t wfull_path[MAX_PATH];
size_t copied = mbstowcs(wfull_path, full_path.c_str(), MAX_PATH); size_t copied = mbstowcs(wfull_path, full_path.c_str(), MAX_PATH);
CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1)); CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1));
dir->handle = ::FindFirstFileExW(wfull_path, FindExInfoStandard, dir->handle = ::FindFirstFileExW(
&dir->data, FindExSearchNameMatch, NULL, 0); wfull_path, FindExInfoStandard, &dir->data, FindExSearchNameMatch, NULL, 0);
#else #else
dir->handle = ::FindFirstFileExA((string(path) + "\\*").c_str(), dir->handle = ::FindFirstFileExA((string(path) + "\\*").c_str(),
FindExInfoStandard, &dir->data, FindExSearchNameMatch, NULL, 0); FindExInfoStandard,
&dir->data,
FindExSearchNameMatch,
NULL,
0);
#endif #endif
if (dir->handle == INVALID_HANDLE_VALUE) if(dir->handle == INVALID_HANDLE_VALUE)
{ {
/*closedir will do all cleanup*/ /*closedir will do all cleanup*/
delete dir; delete dir;
return 0; return 0;
} }
return dir; return dir;
} }
dirent* readdir(DIR* dir) dirent* readdir(DIR* dir)
{ {
#ifdef WINRT #ifdef WINRT
if (dir->ent.d_name != 0) if(dir->ent.d_name != 0)
{ {
if (::FindNextFileW(dir->handle, &dir->data) != TRUE) if(::FindNextFileW(dir->handle, &dir->data) != TRUE)
return 0; return 0;
} }
size_t asize = wcstombs(NULL, dir->data.cFileName, 0); size_t asize = wcstombs(NULL, dir->data.cFileName, 0);
CV_Assert((asize != 0) && (asize != (size_t)-1)); CV_Assert((asize != 0) && (asize != (size_t)-1));
char* aname = new char[asize + 1]; char* aname = new char[asize + 1];
aname[asize] = 0; aname[asize] = 0;
wcstombs(aname, dir->data.cFileName, asize); wcstombs(aname, dir->data.cFileName, asize);
dir->ent.d_name = aname; dir->ent.d_name = aname;
#else #else
if (dir->ent.d_name != 0) if(dir->ent.d_name != 0)
{ {
if (::FindNextFileA(dir->handle, &dir->data) != TRUE) if(::FindNextFileA(dir->handle, &dir->data) != TRUE)
return 0; return 0;
} }
dir->ent.d_name = dir->data.cFileName; dir->ent.d_name = dir->data.cFileName;
#endif #endif
return &dir->ent; return &dir->ent;
} }
void closedir(DIR* dir) void closedir(DIR* dir)
{ {
::FindClose(dir->handle); ::FindClose(dir->handle);
delete dir; delete dir;
} }
#else #else
# include <dirent.h> #include <dirent.h>
# include <sys/stat.h> #include <sys/stat.h>
const char dir_separators[] = "/"; const char dir_separators[] = "/";
#endif #endif
static bool isDir(const string &path, DIR* dir) static bool isDir(const string& path, DIR* dir)
{ {
#if defined _WIN32 || defined WINCE #if defined _WIN32 || defined WINCE
DWORD attributes; DWORD attributes;
BOOL status = TRUE; BOOL status = TRUE;
if (dir) if(dir)
attributes = dir->data.dwFileAttributes; attributes = dir->data.dwFileAttributes;
else else
{ {
WIN32_FILE_ATTRIBUTE_DATA all_attrs; WIN32_FILE_ATTRIBUTE_DATA all_attrs;
#ifdef WINRT #ifdef WINRT
wchar_t wpath[MAX_PATH]; wchar_t wpath[MAX_PATH];
size_t copied = mbstowcs(wpath, path.c_str(), MAX_PATH); size_t copied = mbstowcs(wpath, path.c_str(), MAX_PATH);
CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1)); CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1));
status = ::GetFileAttributesExW(wpath, GetFileExInfoStandard, &all_attrs); status = ::GetFileAttributesExW(wpath, GetFileExInfoStandard, &all_attrs);
#else #else
status = ::GetFileAttributesExA(path.c_str(), GetFileExInfoStandard, &all_attrs); status = ::GetFileAttributesExA(path.c_str(), GetFileExInfoStandard, &all_attrs);
#endif #endif
attributes = all_attrs.dwFileAttributes; attributes = all_attrs.dwFileAttributes;
} }
return status && ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0); return status && ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
#else #else
(void)dir; (void)dir;
struct stat stat_buf; struct stat stat_buf;
if (0 != stat(path.c_str(), &stat_buf)) if(0 != stat(path.c_str(), &stat_buf))
return false; return false;
int is_dir = S_ISDIR(stat_buf.st_mode); int is_dir = S_ISDIR(stat_buf.st_mode);
return is_dir != 0; return is_dir != 0;
#endif #endif
} }
bool IsDirectory(const string &path) bool IsDirectory(const string& path) { return isDir(path, NULL); }
{
return isDir(path, NULL);
}
bool Exists(const string& path) bool Exists(const string& path)
{ {
#if defined _WIN32 || defined WINCE #if defined _WIN32 || defined WINCE
BOOL status = TRUE; BOOL status = TRUE;
{ {
WIN32_FILE_ATTRIBUTE_DATA all_attrs; WIN32_FILE_ATTRIBUTE_DATA all_attrs;
#ifdef WINRT #ifdef WINRT
wchar_t wpath[MAX_PATH]; wchar_t wpath[MAX_PATH];
size_t copied = mbstowcs(wpath, path.c_str(), MAX_PATH); size_t copied = mbstowcs(wpath, path.c_str(), MAX_PATH);
CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1)); CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1));
status = ::GetFileAttributesExW(wpath, GetFileExInfoStandard, &all_attrs); status = ::GetFileAttributesExW(wpath, GetFileExInfoStandard, &all_attrs);
#else #else
status = ::GetFileAttributesExA(path.c_str(), GetFileExInfoStandard, &all_attrs); status = ::GetFileAttributesExA(path.c_str(), GetFileExInfoStandard, &all_attrs);
#endif #endif
} }
return !!status; return !!status;
#else #else
struct stat stat_buf; struct stat stat_buf;
return (0 == stat(path.c_str(), &stat_buf)); return (0 == stat(path.c_str(), &stat_buf));
#endif #endif
} }
bool IsPathSeparator(char c) bool IsPathSeparator(char c) { return c == '/' || c == '\\'; }
{
return c == '/' || c == '\\'; string JoinPath(const string& base, const string& path)
} {
if(base.empty())
string JoinPath(const string& base, const string& path) return path;
{ if(path.empty())
if (base.empty()) return base;
return path;
if (path.empty()) bool baseSep = IsPathSeparator(base[base.size() - 1]);
return base; bool pathSep = IsPathSeparator(path[0]);
string result;
bool baseSep = IsPathSeparator(base[base.size() - 1]); if(baseSep && pathSep)
bool pathSep = IsPathSeparator(path[0]); {
string result; result = base + path.substr(1);
if (baseSep && pathSep) }
{ else if(!baseSep && !pathSep)
result = base + path.substr(1); {
} result = base + PATH_SEPARATOR + path;
else if (!baseSep && !pathSep) }
{ else
result = base + PATH_SEPARATOR + path; {
} result = base + path;
else }
{ return result;
result = base + path; }
}
return result; static bool wildcmp(const char* string, const char* wild)
} {
const char *cp = 0, *mp = 0;
static bool wildcmp(const char *string, const char *wild)
{ while((*string) && (*wild != '*'))
const char *cp = 0, *mp = 0; {
if((*wild != *string) && (*wild != '?'))
while ((*string) && (*wild != '*'))
{
if ((*wild != *string) && (*wild != '?'))
{
return false;
}
wild++;
string++;
}
while (*string)
{
if (*wild == '*')
{
if (!*++wild)
{
return true;
}
mp = wild;
cp = string + 1;
}
else if ((*wild == *string) || (*wild == '?'))
{
wild++;
string++;
}
else
{
wild = mp;
string = cp++;
}
}
while (*wild == '*')
{
wild++;
}
return *wild == 0;
}
static void glob_rec(const string &directory, const string& wildchart, std::vector<string>& result,
bool recursive, bool includeDirectories, const string& pathPrefix)
{
DIR *dir;
if ((dir = opendir(directory.c_str())) != 0)
{
/* find all the files and directories within directory */
try
{
struct dirent *ent;
while ((ent = readdir(dir)) != 0)
{
const char* name = ent->d_name;
if ((name[0] == 0) || (name[0] == '.' && name[1] == 0) || (name[0] == '.' && name[1] == '.' && name[2] == 0))
continue;
string path = JoinPath(directory, name);
string entry = JoinPath(pathPrefix, name);
if (isDir(path, dir))
{
if (recursive)
glob_rec(path, wildchart, result, recursive, includeDirectories, entry);
if (!includeDirectories)
continue;
}
if (wildchart.empty() || wildcmp(name, wildchart.c_str()))
result.push_back(entry);
}
}
catch (...)
{
closedir(dir);
throw;
}
closedir(dir);
}
else
{
printf("could not open directory: %s", directory.c_str());
}
}
void GetFileNameList(const string &directory, const string &pattern, std::vector<string>& result, bool recursive, bool addPath)
{
// split pattern
vector<string> patterns=SplitString(pattern,",");
result.clear();
for(int i=0;i<patterns.size();++i)
{ {
string eachPattern=patterns[i]; return false;
std::vector<string> eachResult; }
glob_rec(directory, eachPattern, eachResult, recursive, true, directory);
for(int j=0;j<eachResult.size();++j) wild++;
string++;
}
while(*string)
{
if(*wild == '*')
{
if(!*++wild)
{ {
if (IsDirectory(eachResult[j])) return true;
continue; }
if(addPath)
mp = wild;
cp = string + 1;
}
else if((*wild == *string) || (*wild == '?'))
{
wild++;
string++;
}
else
{
wild = mp;
string = cp++;
}
}
while(*wild == '*')
{
wild++;
}
return *wild == 0;
}
static void glob_rec(const string& directory,
const string& wildchart,
std::vector<string>& result,
bool recursive,
bool includeDirectories,
const string& pathPrefix)
{
DIR* dir;
if((dir = opendir(directory.c_str())) != 0)
{
/* find all the files and directories within directory */
try
{
struct dirent* ent;
while((ent = readdir(dir)) != 0)
{
const char* name = ent->d_name;
if((name[0] == 0) || (name[0] == '.' && name[1] == 0) ||
(name[0] == '.' && name[1] == '.' && name[2] == 0))
continue;
string path = JoinPath(directory, name);
string entry = JoinPath(pathPrefix, name);
if(isDir(path, dir))
{
if(recursive)
glob_rec(path, wildchart, result, recursive, includeDirectories, entry);
if(!includeDirectories)
continue;
}
if(wildchart.empty() || wildcmp(name, wildchart.c_str()))
result.push_back(entry);
}
}
catch(...)
{
closedir(dir);
throw;
}
closedir(dir);
}
else
{
printf("could not open directory: %s", directory.c_str());
}
}
void GetFileNameList(const string& directory,
const string& pattern,
std::vector<string>& result,
bool recursive,
bool addPath)
{
// split pattern
vector<string> patterns = SplitString(pattern, ",");
result.clear();
for(int i = 0; i < patterns.size(); ++i)
{
string eachPattern = patterns[i];
std::vector<string> eachResult;
glob_rec(directory, eachPattern, eachResult, recursive, true, directory);
for(int j = 0; j < eachResult.size(); ++j)
{
if(IsDirectory(eachResult[j]))
continue;
if(addPath)
{
result.push_back(eachResult[j]);
}
else
{
result.push_back(GetFileName(eachResult[j]));
}
}
}
std::sort(result.begin(), result.end());
}
void GetFileNameList2(const string& directory,
const string& pattern,
std::vector<string>& result,
bool recursive,
bool addPath)
{
// split pattern
vector<string> patterns = SplitString(pattern, ",");
result.clear();
for(int i = 0; i < patterns.size(); ++i)
{
string eachPattern = patterns[i];
std::vector<string> eachResult;
glob_rec(directory, eachPattern, eachResult, recursive, true, directory);
for(int j = 0; j < eachResult.size(); ++j)
{
string filePath = eachResult[j];
if(IsDirectory(filePath))
{
filePath = filePath + "/";
for(int k = 0; k < filePath.size(); ++k)
{ {
result.push_back(eachResult[j]); if(IsPathSeparator(filePath[k]))
{
filePath[k] = '/';
}
} }
else }
if(addPath)
{
result.push_back(filePath);
}
else
{
if(!IsDirectory(filePath))
{ {
result.push_back(GetFileName(eachResult[j])); result.push_back(GetFileName(filePath));
} }
} }
} }
std::sort(result.begin(), result.end()); }
} std::sort(result.begin(), result.end());
}
void GetFileNameList2(const string &directory, const string &pattern, std::vector<string>& result, bool recursive, bool addPath)
{ void RemoveAll(const string& path)
// split pattern {
vector<string> patterns = SplitString(pattern, ",");
if(!Exists(path))
result.clear(); return;
for (int i = 0; i<patterns.size(); ++i) if(IsDirectory(path))
{ {
string eachPattern = patterns[i]; std::vector<string> entries;
std::vector<string> eachResult; GetFileNameList2(path, string(), entries, false, true);
glob_rec(directory, eachPattern, eachResult, recursive, true, directory); for(size_t i = 0; i < entries.size(); i++)
for (int j = 0; j<eachResult.size(); ++j) {
{ const string& e = entries[i];
string filePath = eachResult[j]; RemoveAll(e);
if (IsDirectory(filePath)) }
{
filePath = filePath + "/";
for (int k = 0; k < filePath.size(); ++k)
{
if (IsPathSeparator(filePath[k]))
{
filePath[k] = '/';
}
}
}
if (addPath)
{
result.push_back(filePath);
}
else
{
if (!IsDirectory(filePath))
{
result.push_back(GetFileName(filePath));
}
}
}
}
std::sort(result.begin(), result.end());
}
void RemoveAll(const string& path)
{
if (!Exists(path))
return;
if (IsDirectory(path))
{
std::vector<string> entries;
GetFileNameList2(path, string(), entries, false, true);
for (size_t i = 0; i < entries.size(); i++)
{
const string& e = entries[i];
RemoveAll(e);
}
#ifdef _MSC_VER #ifdef _MSC_VER
bool result = _rmdir(path.c_str()) == 0; bool result = _rmdir(path.c_str()) == 0;
#else #else
bool result = rmdir(path.c_str()) == 0; bool result = rmdir(path.c_str()) == 0;
#endif #endif
if (!result) if(!result)
{ {
printf("can't remove directory: %s\n", path.c_str()); printf("can't remove directory: %s\n", path.c_str());
} }
} }
else else
{ {
#ifdef _MSC_VER #ifdef _MSC_VER
bool result = _unlink(path.c_str()) == 0; bool result = _unlink(path.c_str()) == 0;
#else #else
bool result = unlink(path.c_str()) == 0; bool result = unlink(path.c_str()) == 0;
#endif #endif
if (!result) if(!result)
{
printf("can't remove file: %s\n", path.c_str());
}
}
}
void Remove(const string &directory, const string &extension)
{
DIR *dir;
static int numberOfFiles = 0;
if ((dir = opendir(directory.c_str())) != 0)
{
/* find all the files and directories within directory */
try
{
struct dirent *ent;
while ((ent = readdir(dir)) != 0)
{
const char* name = ent->d_name;
if ((name[0] == 0) || (name[0] == '.' && name[1] == 0) || (name[0] == '.' && name[1] == '.' && name[2] == 0))
continue;
string path = JoinPath(directory, name);
if (isDir(path, dir))
{
Remove(path, extension);
}
// �ж���չ��
if (extension.empty() || wildcmp(name, extension.c_str()))
{
RemoveAll(path);
++numberOfFiles;
printf("%s deleted! number of deleted files:%d\n", path.c_str(), numberOfFiles);
}
}
}
catch (...)
{
closedir(dir);
throw;
}
closedir(dir);
}
else
{
printf("could not open directory: %s", directory.c_str());
}
// ����RemoveAllɾ��Ŀ¼
RemoveAll(directory);
}
string GetFileName(const string &path)
{
string fileName;
int indexOfPathSeparator = -1;
for (int i = path.size() - 1; i >= 0; --i)
{
if (IsPathSeparator(path[i]))
{
fileName = path.substr(i + 1, path.size() - i - 1);
indexOfPathSeparator = i;
break;
}
}
if (indexOfPathSeparator == -1)
{ {
fileName = path; printf("can't remove file: %s\n", path.c_str());
} }
}
}
void Remove(const string& directory, const string& extension)
{
return fileName; DIR* dir;
}
string GetFileName_NoExtension(const string &path) static int numberOfFiles = 0;
if((dir = opendir(directory.c_str())) != 0)
{ {
string fileName=GetFileName(path); /* find all the files and directories within directory */
string fileName_NoExtension; try
for(int i=fileName.size()-1;i>0;--i)
{ {
if(fileName[i]=='.') struct dirent* ent;
while((ent = readdir(dir)) != 0)
{ {
fileName_NoExtension=fileName.substr(0,i); const char* name = ent->d_name;
break; if((name[0] == 0) || (name[0] == '.' && name[1] == 0) ||
} (name[0] == '.' && name[1] == '.' && name[2] == 0))
} continue;
return fileName_NoExtension; string path = JoinPath(directory, name);
}
if(isDir(path, dir))
string GetExtension(const string &path) {
{ Remove(path, extension);
string fileName; }
for (int i = path.size() - 1; i >= 0; --i)
{ // �ж���չ��
if (path[i]=='.') if(extension.empty() || wildcmp(name, extension.c_str()))
{ {
fileName = path.substr(i, path.size() - i); RemoveAll(path);
break; ++numberOfFiles;
} printf("%s deleted! number of deleted files:%d\n", path.c_str(), numberOfFiles);
} }
return fileName;
}
string GetParentPath(const string &path)
{
string fileName;
for (int i = path.size() - 1; i >= 0; --i)
{
if (IsPathSeparator(path[i]))
{
fileName = path.substr(0, i+1);
break;
}
}
return fileName;
}
static bool CreateDirectory(const string &path)
{
#if defined WIN32 || defined _WIN32 || defined WINCE
#ifdef WINRT
wchar_t wpath[MAX_PATH];
size_t copied = mbstowcs(wpath, path.c_str(), MAX_PATH);
CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1));
int result = CreateDirectoryA(wpath, NULL) ? 0 : -1;
#else
int result = _mkdir(path.c_str());
#endif
#elif defined __linux__ || defined __APPLE__
int result = mkdir(path.c_str(), 0777);
#else
int result = -1;
#endif
if (result == -1)
{
return IsDirectory(path);
} }
return true; }
} catch(...)
bool CreateDirectories(const string &directoryPath)
{
string path = directoryPath;
for (;;)
{
char last_char = path.empty() ? 0 : path[path.length() - 1];
if (IsPathSeparator(last_char))
{
path = path.substr(0, path.length() - 1);
continue;
}
break;
}
if (path.empty() || path == "./" || path == ".\\" || path == ".")
return true;
if (IsDirectory(path))
return true;
size_t pos = path.rfind('/');
if (pos == string::npos)
pos = path.rfind('\\');
if (pos != string::npos)
{
string parent_directory = path.substr(0, pos);
if (!parent_directory.empty())
{
if (!CreateDirectories(parent_directory))
return false;
}
}
return CreateDirectory(path);
}
bool CopyFile(const string srcPath, const string dstPath)
{
std::ifstream srcFile(srcPath,ios::binary);
std::ofstream dstFile(dstPath,ios::binary);
if(!srcFile.is_open())
{ {
printf("can not open %s\n",srcPath.c_str()); closedir(dir);
return false; throw;
} }
if(!dstFile.is_open()) closedir(dir);
}
else
{
printf("could not open directory: %s", directory.c_str());
}
// ����RemoveAllɾ��Ŀ¼
RemoveAll(directory);
}
string GetFileName(const string& path)
{
string fileName;
int indexOfPathSeparator = -1;
for(int i = path.size() - 1; i >= 0; --i)
{
if(IsPathSeparator(path[i]))
{ {
printf("can not open %s\n", dstPath.c_str()); fileName = path.substr(i + 1, path.size() - i - 1);
return false; indexOfPathSeparator = i;
break;
} }
if(srcPath==dstPath) }
if(indexOfPathSeparator == -1)
{
fileName = path;
}
return fileName;
}
string GetFileName_NoExtension(const string& path)
{
string fileName = GetFileName(path);
string fileName_NoExtension;
for(int i = fileName.size() - 1; i > 0; --i)
{
if(fileName[i] == '.')
{ {
printf("src can not be same with dst\n"); fileName_NoExtension = fileName.substr(0, i);
return false; break;
} }
char buffer[2048]; }
unsigned int numberOfBytes=0;
while(srcFile) return fileName_NoExtension;
}
string GetExtension(const string& path)
{
string fileName;
for(int i = path.size() - 1; i >= 0; --i)
{
if(path[i] == '.')
{ {
srcFile.read(buffer,2048); fileName = path.substr(i, path.size() - i);
dstFile.write(buffer,srcFile.gcount()); break;
numberOfBytes+=srcFile.gcount();
} }
srcFile.close();
dstFile.close();
return true;
} }
bool CopyDirectories(string srcPath, const string dstPath) return fileName;
}
string GetParentPath(const string& path)
{
string fileName;
for(int i = path.size() - 1; i >= 0; --i)
{ {
if(srcPath==dstPath) if(IsPathSeparator(path[i]))
{ {
printf("src can not be same with dst\n"); fileName = path.substr(0, i + 1);
return false; break;
} }
}
// ȥ������·���ָ��� return fileName;
srcPath = srcPath.substr(0, srcPath.size() - 1); }
vector<string> fileNameList; static bool CreateDirectory(const string& path)
GetFileNameList2(srcPath, "", fileNameList, true, true); {
#if defined WIN32 || defined _WIN32 || defined WINCE
#ifdef WINRT
wchar_t wpath[MAX_PATH];
size_t copied = mbstowcs(wpath, path.c_str(), MAX_PATH);
CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1));
int result = CreateDirectoryA(wpath, NULL) ? 0 : -1;
#else
int result = _mkdir(path.c_str());
#endif
#elif defined __linux__ || defined __APPLE__
int result = mkdir(path.c_str(), 0777);
#else
int result = -1;
#endif
string parentPathOfSrc=GetParentPath(srcPath); if(result == -1)
int length=parentPathOfSrc.size(); {
return IsDirectory(path);
}
return true;
}
// create all directories bool CreateDirectories(const string& directoryPath)
for(int i=0;i<fileNameList.size();++i) {
string path = directoryPath;
for(;;)
{
char last_char = path.empty() ? 0 : path[path.length() - 1];
if(IsPathSeparator(last_char))
{ {
// create directory path = path.substr(0, path.length() - 1);
string srcFilePath=fileNameList[i]; continue;
string subStr=srcFilePath.substr(length,srcFilePath.size()-length);
string dstFilePath=dstPath+subStr;
string parentPathOfDst=GetParentPath(dstFilePath);
CreateDirectories(parentPathOfDst);
} }
break;
}
// copy file if(path.empty() || path == "./" || path == ".\\" || path == ".")
for(int i=0;i<fileNameList.size();++i) return true;
if(IsDirectory(path))
return true;
size_t pos = path.rfind('/');
if(pos == string::npos)
pos = path.rfind('\\');
if(pos != string::npos)
{
string parent_directory = path.substr(0, pos);
if(!parent_directory.empty())
{ {
string srcFilePath=fileNameList[i]; if(!CreateDirectories(parent_directory))
if (IsDirectory(srcFilePath)) return false;
{
continue;
}
string subStr=srcFilePath.substr(length,srcFilePath.size()-length);
string dstFilePath=dstPath+subStr;
// copy file
CopyFile(srcFilePath,dstFilePath);
// process
double process = (1.0*(i + 1) / fileNameList.size()) * 100;
printf("%s done! %f% \n", GetFileName(fileNameList[i]).c_str(), process);
} }
printf("all done!(the number of files:%d)\n", fileNameList.size()); }
return true; return CreateDirectory(path);
}
bool CopyFile(const string srcPath, const string dstPath)
{
std::ifstream srcFile(srcPath, ios::binary);
std::ofstream dstFile(dstPath, ios::binary);
if(!srcFile.is_open())
{
printf("can not open %s\n", srcPath.c_str());
return false;
}
if(!dstFile.is_open())
{
printf("can not open %s\n", dstPath.c_str());
return false;
}
if(srcPath == dstPath)
{
printf("src can not be same with dst\n");
return false;
}
char buffer[2048];
unsigned int numberOfBytes = 0;
while(srcFile)
{
srcFile.read(buffer, 2048);
dstFile.write(buffer, srcFile.gcount());
numberOfBytes += srcFile.gcount();
}
srcFile.close();
dstFile.close();
return true;
}
bool CopyDirectories(string srcPath, const string dstPath)
{
if(srcPath == dstPath)
{
printf("src can not be same with dst\n");
return false;
} }
} // ȥ������·���ָ���
srcPath = srcPath.substr(0, srcPath.size() - 1);
vector<string> fileNameList;
GetFileNameList2(srcPath, "", fileNameList, true, true);
string parentPathOfSrc = GetParentPath(srcPath);
int length = parentPathOfSrc.size();
// create all directories
for(int i = 0; i < fileNameList.size(); ++i)
{
// create directory
string srcFilePath = fileNameList[i];
string subStr = srcFilePath.substr(length, srcFilePath.size() - length);
string dstFilePath = dstPath + subStr;
string parentPathOfDst = GetParentPath(dstFilePath);
CreateDirectories(parentPathOfDst);
}
// copy file
for(int i = 0; i < fileNameList.size(); ++i)
{
string srcFilePath = fileNameList[i];
if(IsDirectory(srcFilePath))
{
continue;
}
string subStr = srcFilePath.substr(length, srcFilePath.size() - length);
string dstFilePath = dstPath + subStr;
// copy file
CopyFile(srcFilePath, dstFilePath);
// process
double process = (1.0 * (i + 1) / fileNameList.size()) * 100;
printf("%s done! %f% \n", GetFileName(fileNameList[i]).c_str(), process);
}
printf("all done!(the number of files:%d)\n", fileNameList.size());
return true;
}
} // namespace migraphxSamples
...@@ -5,27 +5,27 @@ ...@@ -5,27 +5,27 @@
#include <string> #include <string>
#include <vector> #include <vector>
namespace migraphxSamples namespace migraphxSamples
{ {
// 路径是否存在 // 路径是否存在
bool Exists(const std::string &path); bool Exists(const std::string& path);
// 路径是否为目录 // 路径是否为目录
bool IsDirectory(const std::string &path); bool IsDirectory(const std::string& path);
// 是否是路径分隔符(Linux:‘/’,Windows:’\\’) // 是否是路径分隔符(Linux:‘/’,Windows:’\\’)
bool IsPathSeparator(char c); bool IsPathSeparator(char c);
// 路径拼接 // 路径拼接
std::string JoinPath(const std::string &base, const std::string &path); std::string JoinPath(const std::string& base, const std::string& path);
// 创建多级目录,注意:创建多级目录的时候,目标目录是不能有文件存在的 // 创建多级目录,注意:创建多级目录的时候,目标目录是不能有文件存在的
bool CreateDirectories(const std::string &directoryPath); bool CreateDirectories(const std::string& directoryPath);
/** 生成符合指定模式的文件名列表(支持递归遍历) /** 生成符合指定模式的文件名列表(支持递归遍历)
* *
* pattern: 模式,比如"*.jpg","*.png","*.jpg,*.png" * pattern: 模式,比如"*.jpg","*.png","*.jpg,*.png"
* addPath:是否包含父路径 * addPath:是否包含父路径
* 注意: * 注意:
...@@ -36,35 +36,43 @@ bool CreateDirectories(const std::string &directoryPath); ...@@ -36,35 +36,43 @@ bool CreateDirectories(const std::string &directoryPath);
5. 不能返回子目录名 5. 不能返回子目录名
* *
*/ */
void GetFileNameList(const std::string &directory, const std::string &pattern, std::vector<std::string> &result, bool recursive, bool addPath); void GetFileNameList(const std::string& directory,
const std::string& pattern,
std::vector<std::string>& result,
bool recursive,
bool addPath);
// 与GetFileNameList的区别在于如果有子目录,在addPath为true的时候会返回子目录路径(目录名最后有"/") // 与GetFileNameList的区别在于如果有子目录,在addPath为true的时候会返回子目录路径(目录名最后有"/")
void GetFileNameList2(const std::string &directory, const std::string &pattern, std::vector<std::string> &result, bool recursive, bool addPath); void GetFileNameList2(const std::string& directory,
const std::string& pattern,
std::vector<std::string>& result,
bool recursive,
bool addPath);
// 删除文件或者目录,支持递归删除 // 删除文件或者目录,支持递归删除
void Remove(const std::string &directory, const std::string &extension=""); void Remove(const std::string& directory, const std::string& extension = "");
/** 获取路径的文件名和扩展名 /** 获取路径的文件名和扩展名
* *
* 示例:path为D:/1/1.txt,则GetFileName()为1.txt,GetFileName_NoExtension()为1,GetExtension()为.txt,GetParentPath()为D:/1/ * 示例:path为D:/1/1.txt,则GetFileName()为1.txt,GetFileName_NoExtension()为1,GetExtension()为.txt,GetParentPath()为D:/1/
*/ */
std::string GetFileName(const std::string &path); std::string GetFileName(const std::string& path);
std::string GetFileName_NoExtension(const std::string &path); std::string GetFileName_NoExtension(const std::string& path);
std::string GetExtension(const std::string &path); std::string GetExtension(const std::string& path);
std::string GetParentPath(const std::string &path); std::string GetParentPath(const std::string& path);
// 拷贝文件 // 拷贝文件
bool CopyFile(const std::string srcPath,const std::string dstPath); bool CopyFile(const std::string srcPath, const std::string dstPath);
/** 拷贝目录 /** 拷贝目录
* *
* 示例:CopyDirectories("D:/0/1/2/","E:/3/");实现把D:/0/1/2/目录拷贝到E:/3/目录中(即拷贝完成后的目录结构为E:/3/2/) * 示例:CopyDirectories("D:/0/1/2/","E:/3/");实现把D:/0/1/2/目录拷贝到E:/3/目录中(即拷贝完成后的目录结构为E:/3/2/)
* 注意: * 注意:
1.第一个参数的最后不能加”/” 1.第一个参数的最后不能加”/”
2.不能拷贝隐藏文件 2.不能拷贝隐藏文件
*/ */
bool CopyDirectories(std::string srcPath,const std::string dstPath); bool CopyDirectories(std::string srcPath, const std::string dstPath);
} } // namespace migraphxSamples
#endif #endif
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
#include <map> #include <map>
#include <thread> #include <thread>
#include <mutex> #include <mutex>
#if (defined WIN32 || defined _WIN32) #if(defined WIN32 || defined _WIN32)
#include <Windows.h> #include <Windows.h>
#else #else
#include <sys/time.h> #include <sys/time.h>
...@@ -16,13 +16,13 @@ ...@@ -16,13 +16,13 @@
using namespace std; using namespace std;
/** 简易日志 /** 简易日志
* *
* 不依赖于其他第三方库,只需要包含一个头文件就可以使用。提供了4种日志级别,包括INFO,DEBUG,WARN和ERROR。 * 不依赖于其他第三方库,只需要包含一个头文件就可以使用。提供了4种日志级别,包括INFO,DEBUG,WARN和ERROR。
* *
* 示例1: * 示例1:
// 初始化日志,在./Log/目录下创建两个日志文件log1.log和log2.log(注意:目录./Log/需要存在,否则日志创建失败) //
初始化日志,在./Log/目录下创建两个日志文件log1.log和log2.log(注意:目录./Log/需要存在,否则日志创建失败)
LogManager::GetInstance()->Initialize("./Log/","log1"); LogManager::GetInstance()->Initialize("./Log/","log1");
LogManager::GetInstance()->Initialize("./Log/","log2"); LogManager::GetInstance()->Initialize("./Log/","log2");
...@@ -34,11 +34,11 @@ using namespace std; ...@@ -34,11 +34,11 @@ using namespace std;
// 关闭日志 // 关闭日志
LogManager::GetInstance()->Close("log1"); LogManager::GetInstance()->Close("log1");
LogManager::GetInstance()->Close("log2"); LogManager::GetInstance()->Close("log2");
* 示例2: * 示例2:
// 将日志输出到控制台 // 将日志输出到控制台
string log = "Hello World"; string log = "Hello World";
LOG_INFO(stdout, "%s\n", log.c_str()); LOG_INFO(stdout, "%s\n", log.c_str());
* 注意: * 注意:
1. 需要C++11 1. 需要C++11
...@@ -50,44 +50,43 @@ using namespace std; ...@@ -50,44 +50,43 @@ using namespace std;
class LogManager class LogManager
{ {
private: private:
LogManager(){} LogManager() {}
public: public:
~LogManager(){} ~LogManager() {}
inline void Initialize(const string &parentPath,const string &logName) inline void Initialize(const string& parentPath, const string& logName)
{ {
// 日志名为空表示输出到控制台 // 日志名为空表示输出到控制台
if(logName.size()==0) if(logName.size() == 0)
return; return;
// 查找该日志文件,如果没有则创建 // 查找该日志文件,如果没有则创建
std::map<string, FILE*>::const_iterator iter = logMap.find(logName); std::map<string, FILE*>::const_iterator iter = logMap.find(logName);
if (iter == logMap.end()) if(iter == logMap.end())
{ {
string pathOfLog = parentPath+ logName + ".log"; string pathOfLog = parentPath + logName + ".log";
FILE *logFile = fopen(pathOfLog.c_str(), "a"); // w:覆盖原有文件,a:追加 FILE* logFile = fopen(pathOfLog.c_str(), "a"); // w:覆盖原有文件,a:追加
if(logFile!=NULL) if(logFile != NULL)
{ {
logMap.insert(std::make_pair(logName, logFile)); logMap.insert(std::make_pair(logName, logFile));
} }
} }
} }
inline FILE* GetLogFile(const string &logName) inline FILE* GetLogFile(const string& logName)
{ {
std::map<string, FILE*>::const_iterator iter=logMap.find(logName); std::map<string, FILE*>::const_iterator iter = logMap.find(logName);
if(iter==logMap.end()) if(iter == logMap.end())
{ {
return NULL; return NULL;
} }
return (*iter).second; return (*iter).second;
} }
inline void Close(const string &logName) inline void Close(const string& logName)
{ {
std::map<string, FILE*>::const_iterator iter=logMap.find(logName); std::map<string, FILE*>::const_iterator iter = logMap.find(logName);
if(iter==logMap.end()) if(iter == logMap.end())
{ {
return; return;
} }
...@@ -95,10 +94,7 @@ public: ...@@ -95,10 +94,7 @@ public:
fclose((*iter).second); fclose((*iter).second);
logMap.erase(iter); logMap.erase(iter);
} }
inline std::mutex &GetLogMutex() inline std::mutex& GetLogMutex() { return logMutex; }
{
return logMutex;
}
// Singleton // Singleton
static LogManager* GetInstance() static LogManager* GetInstance()
...@@ -106,21 +102,22 @@ public: ...@@ -106,21 +102,22 @@ public:
static LogManager logManager; static LogManager logManager;
return &logManager; return &logManager;
} }
private:
private:
std::map<string, FILE*> logMap; std::map<string, FILE*> logMap;
std::mutex logMutex; std::mutex logMutex;
}; };
#ifdef LOG_MUTEX #ifdef LOG_MUTEX
#define LOCK LogManager::GetInstance()->GetLogMutex().lock() #define LOCK LogManager::GetInstance()->GetLogMutex().lock()
#define UNLOCK LogManager::GetInstance()->GetLogMutex().unlock() #define UNLOCK LogManager::GetInstance()->GetLogMutex().unlock()
#else #else
#define LOCK #define LOCK
#define UNLOCK #define UNLOCK
#endif #endif
// log time // log time
typedef struct _LogTime typedef struct _LogTime
{ {
string year; string year;
string month; string month;
...@@ -131,53 +128,53 @@ typedef struct _LogTime ...@@ -131,53 +128,53 @@ typedef struct _LogTime
string millisecond; // ms string millisecond; // ms
string microsecond; // us string microsecond; // us
string weekDay; string weekDay;
}LogTime; } LogTime;
inline LogTime GetTime() inline LogTime GetTime()
{ {
LogTime currentTime; LogTime currentTime;
#if (defined WIN32 || defined _WIN32) #if(defined WIN32 || defined _WIN32)
SYSTEMTIME systemTime; SYSTEMTIME systemTime;
GetLocalTime(&systemTime); GetLocalTime(&systemTime);
char temp[8] = { 0 }; char temp[8] = {0};
sprintf(temp, "%04d", systemTime.wYear); sprintf(temp, "%04d", systemTime.wYear);
currentTime.year=string(temp); currentTime.year = string(temp);
sprintf(temp, "%02d", systemTime.wMonth); sprintf(temp, "%02d", systemTime.wMonth);
currentTime.month=string(temp); currentTime.month = string(temp);
sprintf(temp, "%02d", systemTime.wDay); sprintf(temp, "%02d", systemTime.wDay);
currentTime.day=string(temp); currentTime.day = string(temp);
sprintf(temp, "%02d", systemTime.wHour); sprintf(temp, "%02d", systemTime.wHour);
currentTime.hour=string(temp); currentTime.hour = string(temp);
sprintf(temp, "%02d", systemTime.wMinute); sprintf(temp, "%02d", systemTime.wMinute);
currentTime.minute=string(temp); currentTime.minute = string(temp);
sprintf(temp, "%02d", systemTime.wSecond); sprintf(temp, "%02d", systemTime.wSecond);
currentTime.second=string(temp); currentTime.second = string(temp);
sprintf(temp, "%03d", systemTime.wMilliseconds); sprintf(temp, "%03d", systemTime.wMilliseconds);
currentTime.millisecond=string(temp); currentTime.millisecond = string(temp);
sprintf(temp, "%d", systemTime.wDayOfWeek); sprintf(temp, "%d", systemTime.wDayOfWeek);
currentTime.weekDay=string(temp); currentTime.weekDay = string(temp);
#else #else
struct timeval tv; struct timeval tv;
struct tm *p; struct tm* p;
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
p = localtime(&tv.tv_sec); p = localtime(&tv.tv_sec);
char temp[8]={0}; char temp[8] = {0};
sprintf(temp,"%04d",1900+p->tm_year); sprintf(temp, "%04d", 1900 + p->tm_year);
currentTime.year=string(temp); currentTime.year = string(temp);
sprintf(temp,"%02d",1+p->tm_mon); sprintf(temp, "%02d", 1 + p->tm_mon);
currentTime.month=string(temp); currentTime.month = string(temp);
sprintf(temp,"%02d",p->tm_mday); sprintf(temp, "%02d", p->tm_mday);
currentTime.day=string(temp); currentTime.day = string(temp);
sprintf(temp,"%02d",p->tm_hour); sprintf(temp, "%02d", p->tm_hour);
currentTime.hour=string(temp); currentTime.hour = string(temp);
sprintf(temp,"%02d",p->tm_min); sprintf(temp, "%02d", p->tm_min);
currentTime.minute=string(temp); currentTime.minute = string(temp);
sprintf(temp,"%02d",p->tm_sec); sprintf(temp, "%02d", p->tm_sec);
currentTime.second=string(temp); currentTime.second = string(temp);
sprintf(temp,"%03d",(int)(tv.tv_usec/1000)); sprintf(temp, "%03d", (int)(tv.tv_usec / 1000));
currentTime.millisecond = string(temp); currentTime.millisecond = string(temp);
sprintf(temp, "%03d", (int)(tv.tv_usec % 1000)); sprintf(temp, "%03d", (int)(tv.tv_usec % 1000));
currentTime.microsecond = string(temp); currentTime.microsecond = string(temp);
...@@ -187,61 +184,83 @@ inline LogTime GetTime() ...@@ -187,61 +184,83 @@ inline LogTime GetTime()
return currentTime; return currentTime;
} }
#define LOG_TIME(logFile) \ #define LOG_TIME(logFile) \
do\ do \
{\ { \
LogTime currentTime=GetTime(); \ LogTime currentTime = GetTime(); \
fprintf(((logFile == NULL) ? stdout : logFile), "%s-%s-%s %s:%s:%s.%s\t",currentTime.year.c_str(),currentTime.month.c_str(),currentTime.day.c_str(),currentTime.hour.c_str(),currentTime.minute.c_str(),currentTime.second.c_str(),currentTime.millisecond.c_str()); \ fprintf(((logFile == NULL) ? stdout : logFile), \
}while (0) "%s-%s-%s %s:%s:%s.%s\t", \
currentTime.year.c_str(), \
currentTime.month.c_str(), \
#define LOG_INFO(logFile,logInfo, ...) \ currentTime.day.c_str(), \
do\ currentTime.hour.c_str(), \
{\ currentTime.minute.c_str(), \
LOCK; \ currentTime.second.c_str(), \
LOG_TIME(logFile); \ currentTime.millisecond.c_str()); \
fprintf(((logFile == NULL) ? stdout : logFile), "INFO\t"); \ } while(0)
fprintf(((logFile == NULL) ? stdout : logFile), "[%s:%d (%s) ]: ", __FILE__, __LINE__, __FUNCTION__); \
fprintf(((logFile == NULL) ? stdout : logFile), logInfo, ## __VA_ARGS__); \
fflush(logFile); \
UNLOCK; \
} while (0)
#define LOG_DEBUG(logFile,logInfo, ...) \
do\
{\
LOCK; \
LOG_TIME(logFile);\
fprintf(((logFile==NULL)?stdout:logFile), "DEBUG\t"); \
fprintf(((logFile==NULL)?stdout:logFile), "[%s:%d (%s) ]: ", __FILE__, __LINE__, __FUNCTION__); \
fprintf(((logFile==NULL)?stdout:logFile),logInfo, ## __VA_ARGS__); \
fflush(logFile); \
UNLOCK; \
} while (0)
#define LOG_ERROR(logFile,logInfo, ...) \
do\
{\
LOCK; \
LOG_TIME(logFile);\
fprintf(((logFile==NULL)?stdout:logFile), "ERROR\t"); \
fprintf(((logFile==NULL)?stdout:logFile), "[%s:%d (%s) ]: ", __FILE__, __LINE__, __FUNCTION__); \
fprintf(((logFile==NULL)?stdout:logFile),logInfo, ## __VA_ARGS__); \
fflush(logFile); \
UNLOCK; \
} while (0)
#define LOG_WARN(logFile,logInfo, ...) \
do\
{\
LOCK; \
LOG_TIME(logFile);\
fprintf(((logFile==NULL)?stdout:logFile), "WARN\t"); \
fprintf(((logFile==NULL)?stdout:logFile), "[%s:%d (%s) ]: ", __FILE__, __LINE__, __FUNCTION__); \
fprintf(((logFile==NULL)?stdout:logFile),logInfo, ## __VA_ARGS__); \
fflush(logFile); \
UNLOCK; \
} while (0)
#endif // __SIMPLE_LOG_H__ #define LOG_INFO(logFile, logInfo, ...) \
do \
{ \
LOCK; \
LOG_TIME(logFile); \
fprintf(((logFile == NULL) ? stdout : logFile), "INFO\t"); \
fprintf(((logFile == NULL) ? stdout : logFile), \
"[%s:%d (%s) ]: ", \
__FILE__, \
__LINE__, \
__FUNCTION__); \
fprintf(((logFile == NULL) ? stdout : logFile), logInfo, ##__VA_ARGS__); \
fflush(logFile); \
UNLOCK; \
} while(0)
#define LOG_DEBUG(logFile, logInfo, ...) \
do \
{ \
LOCK; \
LOG_TIME(logFile); \
fprintf(((logFile == NULL) ? stdout : logFile), "DEBUG\t"); \
fprintf(((logFile == NULL) ? stdout : logFile), \
"[%s:%d (%s) ]: ", \
__FILE__, \
__LINE__, \
__FUNCTION__); \
fprintf(((logFile == NULL) ? stdout : logFile), logInfo, ##__VA_ARGS__); \
fflush(logFile); \
UNLOCK; \
} while(0)
#define LOG_ERROR(logFile, logInfo, ...) \
do \
{ \
LOCK; \
LOG_TIME(logFile); \
fprintf(((logFile == NULL) ? stdout : logFile), "ERROR\t"); \
fprintf(((logFile == NULL) ? stdout : logFile), \
"[%s:%d (%s) ]: ", \
__FILE__, \
__LINE__, \
__FUNCTION__); \
fprintf(((logFile == NULL) ? stdout : logFile), logInfo, ##__VA_ARGS__); \
fflush(logFile); \
UNLOCK; \
} while(0)
#define LOG_WARN(logFile, logInfo, ...) \
do \
{ \
LOCK; \
LOG_TIME(logFile); \
fprintf(((logFile == NULL) ? stdout : logFile), "WARN\t"); \
fprintf(((logFile == NULL) ? stdout : logFile), \
"[%s:%d (%s) ]: ", \
__FILE__, \
__LINE__, \
__FUNCTION__); \
fprintf(((logFile == NULL) ? stdout : logFile), logInfo, ##__VA_ARGS__); \
fflush(logFile); \
UNLOCK; \
} while(0)
#endif // __SIMPLE_LOG_H__
...@@ -10,9 +10,9 @@ int main() ...@@ -10,9 +10,9 @@ int main()
// 创建分类器 // 创建分类器
migraphxSamples::Classifier classifier; migraphxSamples::Classifier classifier;
migraphxSamples::InitializationParameterOfClassifier initParamOfClassifier; migraphxSamples::InitializationParameterOfClassifier initParamOfClassifier;
initParamOfClassifier.configFilePath=CONFIG_FILE; initParamOfClassifier.configFilePath = CONFIG_FILE;
migraphxSamples::ErrorCode errorCode=classifier.Initialize(initParamOfClassifier); migraphxSamples::ErrorCode errorCode = classifier.Initialize(initParamOfClassifier);
if(errorCode!=migraphxSamples::SUCCESS) if(errorCode != migraphxSamples::SUCCESS)
{ {
LOG_ERROR(stdout, "fail to initialize ResNet50!\n"); LOG_ERROR(stdout, "fail to initialize ResNet50!\n");
exit(-1); exit(-1);
...@@ -20,33 +20,32 @@ int main() ...@@ -20,33 +20,32 @@ int main()
LOG_INFO(stdout, "succeed to initialize ResNet50\n"); LOG_INFO(stdout, "succeed to initialize ResNet50\n");
// 读取测试图片 // 读取测试图片
cv::Mat srcImage=cv::imread("../Resource/Images/ImageNet_01.jpg",1); cv::Mat srcImage = cv::imread("../Resource/Images/ImageNet_01.jpg", 1);
// 设置batchsize // 设置batchsize
int batchsize=1; int batchsize = 1;
std::vector<cv::Mat> srcImages; std::vector<cv::Mat> srcImages;
for(int i=0;i<batchsize;++i) for(int i = 0; i < batchsize; ++i)
{ {
srcImages.push_back(srcImage); srcImages.push_back(srcImage);
} }
// 推理 // 推理
std::vector<std::vector<migraphxSamples::ResultOfPrediction>> predictions; std::vector<std::vector<migraphxSamples::ResultOfPrediction>> predictions;
classifier.Classify(srcImages,predictions); classifier.Classify(srcImages, predictions);
// 获取推理结果 // 获取推理结果
LOG_INFO(stdout,"========== Classification Results ==========\n"); LOG_INFO(stdout, "========== Classification Results ==========\n");
for(int i=0;i<predictions.size();++i) for(int i = 0; i < predictions.size(); ++i)
{ {
// 一个batch中第i幅图像的结果 // 一个batch中第i幅图像的结果
LOG_INFO(stdout,"========== %d result ==========\n",i); LOG_INFO(stdout, "========== %d result ==========\n", i);
std::vector<migraphxSamples::ResultOfPrediction> resultOfPredictions=predictions[i]; std::vector<migraphxSamples::ResultOfPrediction> resultOfPredictions = predictions[i];
for(int j=0;j<resultOfPredictions.size();++j) for(int j = 0; j < resultOfPredictions.size(); ++j)
{ {
migraphxSamples::ResultOfPrediction prediction=resultOfPredictions[j]; migraphxSamples::ResultOfPrediction prediction = resultOfPredictions[j];
LOG_INFO(stdout,"label:%d,confidence:%f\n",prediction.label,prediction.confidence); LOG_INFO(stdout, "label:%d,confidence:%f\n", prediction.label, prediction.confidence);
} }
} }
return 0; return 0;
......
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