Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add apply softmax option to ClassificationModel #21692

Merged
merged 11 commits into from Mar 7, 2022

Conversation

UnaNancyOwen
Copy link
Contributor

@UnaNancyOwen UnaNancyOwen commented Mar 6, 2022

Fix #21689

// model doesn't contain softmax layer
cv::dnn::ClassificationModel classification_model = cv::dnn::ClassificationModel( model, config );

// not apply softmax post process (default)
bool apply_softmax = classification_model.getSoftmaxPostProcessing(); // false

// apply softmax post process
apply_softmax = true;
classification_model.setEnableSoftmaxPostProcessing( apply_softmax );

// if enable softmax post process is true, confidences range is [0.0-1.0]
// if enable softmax post process is false, confidences range is varies by model
auto [classid, confidence] = model.classify( image );

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

Copy link
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for contribution!

modules/dnn/include/opencv2/dnn/dnn.hpp Outdated Show resolved Hide resolved
modules/dnn/include/opencv2/dnn/dnn.hpp Outdated Show resolved Hide resolved
modules/dnn/include/opencv2/dnn/dnn.hpp Outdated Show resolved Hide resolved
modules/dnn/src/model.cpp Outdated Show resolved Hide resolved
@UnaNancyOwen
Copy link
Contributor Author

UnaNancyOwen commented Mar 6, 2022

dropped idea
@alalek Should define softmax() function outside of the ClassficationModel class?
It is considered to be a general purpose function on the same level as NMSBoxes.
What do you think about this idea?

// Run Softmax defined inside ClassficationModel class
cv::dnn::ClassficationModel::softmax(input_blob, output_blob);

// Run Softmax defined outside ClassficationModel class
cv::dnn::softmax(input_blob, output_blob);

modules/dnn/include/opencv2/dnn/dnn.hpp Outdated Show resolved Hide resolved
modules/dnn/src/model.cpp Outdated Show resolved Hide resolved
modules/dnn/src/softmax.cpp Outdated Show resolved Hide resolved
modules/dnn/src/model.cpp Outdated Show resolved Hide resolved
modules/dnn/src/model.cpp Outdated Show resolved Hide resolved
Copy link
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done! Thank you for contribution 👍

@alalek alalek merged commit 8db7d43 into opencv:4.x Mar 7, 2022
Mat exp;
const float max = *std::max_element(input.begin<float>(), input.end<float>());
cv::exp((input - max), exp);
outblob.getMat() = exp / cv::sum(exp)[0];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suspicious place

@opencv-pushbot opencv-pushbot mentioned this pull request Apr 23, 2022
a-sajjad72 pushed a commit to a-sajjad72/opencv that referenced this pull request Mar 30, 2023
* add apply softmax option to ClassificationModel

* remove default arguments of ClassificationModel::setSoftMax()

* fix build for python

* fix docs warning for setSoftMax()

* add impl for ClassficationModel()

* fix failed build for docs by trailing whitespace

* move to implement classify() to ClassificationModel_Impl

* move to implement softmax() to ClassificationModel_Impl

* remove softmax from public method in ClassificationModel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cv::dnn::ClassificationModel::classify() should run SoftMax when model doesn't have SoftMax layer
3 participants