forked from PaddlePaddle/FastDeploy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Model] Refactoring code of YOLOv5Cls with new model type (PaddlePadd…
…le#1237) * Refactoring code of YOLOv5Cls with new model type * fix reviewed problem * Normalize&HWC2CHW -> NormalizeAndPermute * remove cast()
- Loading branch information
Showing
14 changed files
with
593 additions
and
248 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
58 changes: 58 additions & 0 deletions
58
fastdeploy/vision/classification/contrib/yolov5cls/postprocessor.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
56
fastdeploy/vision/classification/contrib/yolov5cls/postprocessor.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.