Skip to content

Commit

Permalink
[Model] Refactoring code of YOLOv5Cls with new model type (PaddlePadd…
Browse files Browse the repository at this point in the history
…le#1237)

* Refactoring code of YOLOv5Cls with new model type

* fix reviewed problem

* Normalize&HWC2CHW -> NormalizeAndPermute

* remove cast()
  • Loading branch information
GodIsBoom authored Feb 8, 2023
1 parent c5b414a commit 9cd00ad
Show file tree
Hide file tree
Showing 14 changed files with 593 additions and 248 deletions.
9 changes: 3 additions & 6 deletions examples/vision/classification/yolov5cls/cpp/infer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ void CpuInfer(const std::string& model_file, const std::string& image_file) {
}

auto im = cv::imread(image_file);
auto im_bak = im.clone();

fastdeploy::vision::ClassifyResult res;
if (!model.Predict(&im, &res)) {
if (!model.Predict(im, &res)) {
std::cerr << "Failed to predict." << std::endl;
return;
}
Expand All @@ -48,10 +47,9 @@ void GpuInfer(const std::string& model_file, const std::string& image_file) {
}

auto im = cv::imread(image_file);
auto im_bak = im.clone();

fastdeploy::vision::ClassifyResult res;
if (!model.Predict(&im, &res)) {
if (!model.Predict(im, &res)) {
std::cerr << "Failed to predict." << std::endl;
return;
}
Expand All @@ -71,10 +69,9 @@ void TrtInfer(const std::string& model_file, const std::string& image_file) {
}

auto im = cv::imread(image_file);
auto im_bak = im.clone();

fastdeploy::vision::ClassifyResult res;
if (!model.Predict(&im, &res)) {
if (!model.Predict(im, &res)) {
std::cerr << "Failed to predict." << std::endl;
return;
}
Expand Down
3 changes: 2 additions & 1 deletion examples/vision/classification/yolov5cls/python/infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ def build_option(args):
runtime_option = build_option(args)
model = fd.vision.classification.YOLOv5Cls(
args.model, runtime_option=runtime_option)
model.postprocessor.topk = args.topk

# 预测图片分类结果
im = cv2.imread(args.image)
result = model.predict(im, args.topk)
result = model.predict(im)
print(result)
2 changes: 1 addition & 1 deletion fastdeploy/vision.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "fastdeploy/core/config.h"
#ifdef ENABLE_VISION
#include "fastdeploy/vision/classification/contrib/resnet.h"
#include "fastdeploy/vision/classification/contrib/yolov5cls.h"
#include "fastdeploy/vision/classification/contrib/yolov5cls/yolov5cls.h"
#include "fastdeploy/vision/classification/ppcls/model.h"
#include "fastdeploy/vision/detection/contrib/nanodet_plus.h"
#include "fastdeploy/vision/detection/contrib/scaledyolov4.h"
Expand Down
116 changes: 0 additions & 116 deletions fastdeploy/vision/classification/contrib/yolov5cls.cc

This file was deleted.

70 changes: 0 additions & 70 deletions fastdeploy/vision/classification/contrib/yolov5cls.h

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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/postprocessor.h"
#include "fastdeploy/vision/utils/utils.h"

namespace fastdeploy {
namespace vision {
namespace classification {

YOLOv5ClsPostprocessor::YOLOv5ClsPostprocessor() {
topk_ = 1;
}

bool YOLOv5ClsPostprocessor::Run(
const std::vector<FDTensor> &tensors, std::vector<ClassifyResult> *results,
const std::vector<std::map<std::string, std::array<float, 2>>> &ims_info) {
int batch = tensors[0].shape[0];
FDTensor infer_result = tensors[0];
FDTensor infer_result_softmax;
function::Softmax(infer_result, &infer_result_softmax, 1);
results->resize(batch);

for (size_t bs = 0; bs < batch; ++bs) {
(*results)[bs].Clear();
// output (1,1000) score classnum 1000
int num_classes = infer_result_softmax.shape[1];
const float* infer_result_buffer =
reinterpret_cast<const float*>(infer_result_softmax.Data()) + bs * infer_result_softmax.shape[1];
topk_ = std::min(num_classes, topk_);
(*results)[bs].label_ids =
utils::TopKIndices(infer_result_buffer, num_classes, topk_);
(*results)[bs].scores.resize(topk_);
for (int i = 0; i < topk_; ++i) {
(*results)[bs].scores[i] = *(infer_result_buffer + (*results)[bs].label_ids[i]);
}

if ((*results)[bs].label_ids.size() == 0) {
return true;
}
}
return true;
}

} // namespace classification
} // namespace vision
} // namespace fastdeploy
56 changes: 56 additions & 0 deletions fastdeploy/vision/classification/contrib/yolov5cls/postprocessor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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/vision/common/processors/transform.h"
#include "fastdeploy/vision/common/result.h"

namespace fastdeploy {
namespace vision {

namespace classification {
/*! @brief Postprocessor object for YOLOv5Cls serials model.
*/
class FASTDEPLOY_DECL YOLOv5ClsPostprocessor {
public:
/** \brief Create a postprocessor instance for YOLOv5Cls serials model
*/
YOLOv5ClsPostprocessor();

/** \brief Process the result of runtime and fill to ClassifyResult structure
*
* \param[in] tensors The inference result from runtime
* \param[in] result The output result of classification
* \param[in] ims_info The shape info list, record input_shape and output_shape
* \return true if the postprocess successed, otherwise false
*/
bool Run(const std::vector<FDTensor>& tensors,
std::vector<ClassifyResult>* results,
const std::vector<std::map<std::string, std::array<float, 2>>>& ims_info);

/// Set topk, default 1
void SetTopK(const int& topk) {
topk_ = topk;
}

/// Get topk, default 1
float GetTopK() const { return topk_; }

protected:
int topk_;
};

} // namespace classification
} // namespace vision
} // namespace fastdeploy
Loading

0 comments on commit 9cd00ad

Please sign in to comment.