mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2025-10-05 16:48:03 +08:00
Add YOLOv5-cls Model (#335)
* add yolov5cls * fixed bugs * fixed bugs * fixed preprocess bug * add yolov5cls readme * deal with comments * Add YOLOv5Cls Note * add yolov5cls test Co-authored-by: Jason <jiangjiajun@baidu.com>
This commit is contained in:
1
fastdeploy/vision.h
Normal file → Executable file
1
fastdeploy/vision.h
Normal file → Executable file
@@ -15,6 +15,7 @@
|
||||
|
||||
#include "fastdeploy/core/config.h"
|
||||
#ifdef ENABLE_VISION
|
||||
#include "fastdeploy/vision/classification/contrib/yolov5cls.h"
|
||||
#include "fastdeploy/vision/classification/ppcls/model.h"
|
||||
#include "fastdeploy/vision/detection/contrib/nanodet_plus.h"
|
||||
#include "fastdeploy/vision/detection/contrib/scaledyolov4.h"
|
||||
|
@@ -16,11 +16,13 @@
|
||||
|
||||
namespace fastdeploy {
|
||||
|
||||
void BindYOLOv5Cls(pybind11::module& m);
|
||||
void BindPaddleClas(pybind11::module& m);
|
||||
|
||||
void BindClassification(pybind11::module& m) {
|
||||
auto classification_module =
|
||||
m.def_submodule("classification", "Image classification models.");
|
||||
BindYOLOv5Cls(classification_module);
|
||||
BindPaddleClas(classification_module);
|
||||
}
|
||||
} // namespace fastdeploy
|
||||
|
116
fastdeploy/vision/classification/contrib/yolov5cls.cc
Executable file
116
fastdeploy/vision/classification/contrib/yolov5cls.cc
Executable file
@@ -0,0 +1,116 @@
|
||||
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "fastdeploy/vision/classification/contrib/yolov5cls.h"
|
||||
|
||||
#include "fastdeploy/utils/perf.h"
|
||||
#include "fastdeploy/vision/utils/utils.h"
|
||||
|
||||
namespace fastdeploy {
|
||||
namespace vision {
|
||||
namespace classification {
|
||||
|
||||
YOLOv5Cls::YOLOv5Cls(const std::string& model_file,
|
||||
const std::string& params_file,
|
||||
const RuntimeOption& custom_option,
|
||||
const ModelFormat& model_format) {
|
||||
if (model_format == ModelFormat::ONNX) {
|
||||
valid_cpu_backends = {Backend::OPENVINO, Backend::ORT};
|
||||
valid_gpu_backends = {Backend::ORT, Backend::TRT};
|
||||
} else {
|
||||
valid_cpu_backends = {Backend::PDINFER, Backend::ORT};
|
||||
valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT};
|
||||
}
|
||||
runtime_option = custom_option;
|
||||
runtime_option.model_format = model_format;
|
||||
runtime_option.model_file = model_file;
|
||||
runtime_option.params_file = params_file;
|
||||
initialized = Initialize();
|
||||
}
|
||||
|
||||
bool YOLOv5Cls::Initialize() {
|
||||
// preprocess parameters
|
||||
size = {224, 224};
|
||||
if (!InitRuntime()) {
|
||||
FDERROR << "Failed to initialize fastdeploy backend." << std::endl;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool YOLOv5Cls::Preprocess(Mat* mat, FDTensor* output,
|
||||
const std::vector<int>& size) {
|
||||
// CenterCrop
|
||||
int crop_size = std::min(mat->Height(), mat->Width());
|
||||
CenterCrop::Run(mat, crop_size, crop_size);
|
||||
Resize::Run(mat, size[0], size[1], -1, -1, cv::INTER_LINEAR);
|
||||
// Normalize
|
||||
BGR2RGB::Run(mat);
|
||||
std::vector<float> alpha = {1.0f / 255.0f, 1.0f / 255.0f, 1.0f / 255.0f};
|
||||
std::vector<float> beta = {0.0f, 0.0f, 0.0f};
|
||||
Convert::Run(mat, alpha, beta);
|
||||
std::vector<float> mean = {0.485f, 0.456f, 0.406f};
|
||||
std::vector<float> std = {0.229f, 0.224f, 0.225f};
|
||||
Normalize::Run(mat, mean, std, false);
|
||||
HWC2CHW::Run(mat);
|
||||
Cast::Run(mat, "float");
|
||||
|
||||
mat->ShareWithTensor(output);
|
||||
output->shape.insert(output->shape.begin(), 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool YOLOv5Cls::Postprocess(const FDTensor& infer_result,
|
||||
ClassifyResult* result, int topk) {
|
||||
// Softmax
|
||||
FDTensor infer_result_softmax;
|
||||
Softmax(infer_result, &infer_result_softmax, 1);
|
||||
int num_classes = infer_result_softmax.shape[1];
|
||||
const float* infer_result_buffer =
|
||||
reinterpret_cast<const float*>(infer_result_softmax.Data());
|
||||
topk = std::min(num_classes, topk);
|
||||
result->label_ids =
|
||||
utils::TopKIndices(infer_result_buffer, num_classes, topk);
|
||||
result->scores.resize(topk);
|
||||
for (int i = 0; i < topk; ++i) {
|
||||
result->scores[i] = *(infer_result_buffer + result->label_ids[i]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool YOLOv5Cls::Predict(cv::Mat* im, ClassifyResult* result, int topk) {
|
||||
Mat mat(*im);
|
||||
std::vector<FDTensor> input_tensors(1);
|
||||
if (!Preprocess(&mat, &input_tensors[0], size)) {
|
||||
FDERROR << "Failed to preprocess input image." << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
input_tensors[0].name = InputInfoOfRuntime(0).name;
|
||||
std::vector<FDTensor> output_tensors(1);
|
||||
if (!Infer(input_tensors, &output_tensors)) {
|
||||
FDERROR << "Failed to inference." << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!Postprocess(output_tensors[0], result, topk)) {
|
||||
FDERROR << "Failed to post process." << std::endl;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace classification
|
||||
} // namespace vision
|
||||
} // namespace fastdeploy
|
70
fastdeploy/vision/classification/contrib/yolov5cls.h
Executable file
70
fastdeploy/vision/classification/contrib/yolov5cls.h
Executable file
@@ -0,0 +1,70 @@
|
||||
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#pragma once
|
||||
#include "fastdeploy/fastdeploy_model.h"
|
||||
#include "fastdeploy/vision/common/processors/transform.h"
|
||||
#include "fastdeploy/vision/common/result.h"
|
||||
|
||||
namespace fastdeploy {
|
||||
namespace vision {
|
||||
/** \brief All image classification model APIs are defined inside this namespace
|
||||
*
|
||||
*/
|
||||
namespace classification {
|
||||
|
||||
/*! @brief YOLOv5Cls model object used when to load a YOLOv5Cls model exported by YOLOv5
|
||||
*/
|
||||
class FASTDEPLOY_DECL YOLOv5Cls : public FastDeployModel {
|
||||
public:
|
||||
/** \brief Set path of model file and configuration file, and the configuration of runtime
|
||||
*
|
||||
* \param[in] model_file Path of model file, e.g yolov5cls/yolov5n-cls.onnx
|
||||
* \param[in] params_file Path of parameter file, if the model format is ONNX, this parameter will be ignored
|
||||
* \param[in] custom_option RuntimeOption for inference, the default will use cpu, and choose the backend defined in `valid_cpu_backends`
|
||||
* \param[in] model_format Model format of the loaded model, default is ONNX format
|
||||
*/
|
||||
YOLOv5Cls(const std::string& model_file, const std::string& params_file = "",
|
||||
const RuntimeOption& custom_option = RuntimeOption(),
|
||||
const ModelFormat& model_format = ModelFormat::ONNX);
|
||||
|
||||
/// Get model's name
|
||||
virtual std::string ModelName() const { return "yolov5cls"; }
|
||||
|
||||
/** \brief Predict the classification result for an input image
|
||||
*
|
||||
* \param[in] im The input image data, comes from cv::imread()
|
||||
* \param[in] result The output classification result will be writen to this structure
|
||||
* \param[in] topk Returns the topk classification result with the highest predicted probability, the default is 1
|
||||
* \return true if the prediction successed, otherwise false
|
||||
*/
|
||||
virtual bool Predict(cv::Mat* im, ClassifyResult* result, int topk = 1);
|
||||
|
||||
/// Preprocess image size, the default is (224, 224)
|
||||
std::vector<int> size;
|
||||
|
||||
private:
|
||||
bool Initialize();
|
||||
/// Preprocess an input image, and set the preprocessed results to `outputs`
|
||||
bool Preprocess(Mat* mat, FDTensor* output,
|
||||
const std::vector<int>& size = {224, 224});
|
||||
|
||||
/// Postprocess the inferenced results, and set the final result to `result`
|
||||
bool Postprocess(const FDTensor& infer_result, ClassifyResult* result,
|
||||
int topk = 1);
|
||||
};
|
||||
|
||||
} // namespace classification
|
||||
} // namespace vision
|
||||
} // namespace fastdeploy
|
32
fastdeploy/vision/classification/contrib/yolov5cls_pybind.cc
Executable file
32
fastdeploy/vision/classification/contrib/yolov5cls_pybind.cc
Executable file
@@ -0,0 +1,32 @@
|
||||
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#include "fastdeploy/pybind/main.h"
|
||||
|
||||
namespace fastdeploy {
|
||||
void BindYOLOv5Cls(pybind11::module& m) {
|
||||
pybind11::class_<vision::classification::YOLOv5Cls, FastDeployModel>(
|
||||
m, "YOLOv5Cls")
|
||||
.def(pybind11::init<std::string, std::string, RuntimeOption,
|
||||
ModelFormat>())
|
||||
.def("predict",
|
||||
[](vision::classification::YOLOv5Cls& self, pybind11::array& data,
|
||||
int topk = 1) {
|
||||
auto mat = PyArrayToCvMat(data);
|
||||
vision::ClassifyResult res;
|
||||
self.Predict(&mat, &res, topk);
|
||||
return res;
|
||||
})
|
||||
.def_readwrite("size", &vision::classification::YOLOv5Cls::size);
|
||||
}
|
||||
} // namespace fastdeploy
|
Reference in New Issue
Block a user