Skip to content

serialization for mcc #3906

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

Open
wants to merge 1 commit into
base: 4.x
Choose a base branch
from

Conversation

kmatzen
Copy link

@kmatzen kmatzen commented Mar 16, 2025

I added serialization support for ColorCorrectionModel.

#3905

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

@asmorkalov asmorkalov added category: mcc color calibration module feature labels Mar 19, 2025
@asmorkalov asmorkalov changed the base branch from master to 4.x March 19, 2025 06:35
@kmatzen kmatzen force-pushed the kmatzen/mcc_serialization branch from 09daed0 to 8565a10 Compare March 23, 2025 18:09
Comment on lines +466 to +494
void ColorCorrectionModel::write(FileStorage& fs) const
{
fs << "{"
<< "ccm" << p->ccm
<< "loss" << p->loss
<< "src" << p->src
<< "dist" << p->dist
<< "cs_enum" << p->cs_enum
<< "src_rgbl" << p->src_rgbl
<< "dst_rgbl" << p->dst_rgbl
<< "mask" << p->mask
<< "ccm_type" << p->ccm_type
<< "shape" << p->shape
<< "linear" << *p->linear
<< "distance" << p->distance
<< "linear_type" << p->linear_type
<< "weights" << p->weights
<< "weights_list" << p->weights_list
<< "ccm0" << p->ccm0
<< "gamma" << p->gamma
<< "deg" << p->deg
<< "saturated_threshold" << p->saturated_threshold
<< "initial_method_type" << p->initial_method_type
<< "weights_coeff" << p->weights_coeff
<< "masked_len" << p->masked_len
<< "max_count" << p->max_count
<< "epsilon" << p->epsilon
<< "dst_use_constcolor" << p->dst_use_constcolor;

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you for the contribution. The code looks good overall, but I feel we don’t need to write/save this many details.
I think keeping [ccm, loss, colorchart, ccm_type, gamma, deg, saturated-threshold, linear-type, distance-type] should be sufficient.

Copy link
Author

Choose a reason for hiding this comment

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

That makes sense. My reasoning for serializing everything was (1) reloading the model in another process should enable all interfaces on the class to function as if it were the original and (2) if state inside the class is non-essential, then should it be stored in the class in the first place? Are some of the fields only cached state for the others?

Copy link
Contributor

Choose a reason for hiding this comment

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

(1) For inference, only p->ccm, p->linear, p->cs, and p->shape (which can also be derived from ccm_type) are actually needed. You can see this directly in the infer() function in ccm.cpp.
(2) Most of the other fields are indeed just cached values to speed up CCM computation and aren't necessary for inference.

Copy link
Author

Choose a reason for hiding this comment

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

You don't want to support use cases like a model being saved, loaded elsewhere, and then accessors like getWeights() being called?

Copy link
Contributor

Choose a reason for hiding this comment

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

CCM calculation is not really something similar to a deep learning model where we want to save all the parameters and resume training.

The application is to just save the matrix and some other metadata for informative purpose and then use it for inference in an application

@asmorkalov what are your thoughts? Your comments are welcomed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category: mcc color calibration module feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants