Saving the classifier

For any question related to the 3DMASC forum
Post Reply
sjurado
Posts: 12
Joined: Tue Jan 09, 2024 1:57 pm

Saving the classifier

Post by sjurado »

Hello,

I am having issues saving the classifier in a .yaml format. Could you please provide the code you use for this part :

"When using Python, you can also save a trained OpenCV random forest model and use it in the CloudCompare plugin (using the save function of cv2.ml.RTrees)."

Thanks !
paul.leroy
Posts: 40
Joined: Tue Dec 01, 2020 1:21 pm

Re: Saving the classifier

Post by paul.leroy »

Could you please precise what your are trying to do and at which step it fails and how?
sjurado
Posts: 12
Joined: Tue Jan 09, 2024 1:57 pm

Re: Saving the classifier

Post by sjurado »

I train my classifier calling the following function : cc_3dmasc.get_acc_expe()
This function returns a RandomForestClassifier.
I would like to save it using the save function of cv2.ml.RTrees as advised in your documentation in order to use the classifier to classify other data.

I couldn't find online any example of how to save a classifier using the cv2.ml.RTrees save function

I have tried :

Code: Select all

cl.save('classifier_3dmasc.yaml')
which failed wih the following error :

Code: Select all

AttributeError: 'RandomForestClassifier' object has no attribute 'save'
Thanks
paul.leroy
Posts: 40
Joined: Tue Dec 01, 2020 1:21 pm

Re: Saving the classifier

Post by paul.leroy »

You should try with model=1 instead of the default value (model=0). With model=0, scikit-learn is used, with model=1, OpenCV is used. It is written in the docstring of get_acc_expe.
sjurado
Posts: 12
Joined: Tue Jan 09, 2024 1:57 pm

Re: Saving the classifier

Post by sjurado »

Thanks ! Indeed it works with the OpenCv Model.
Although I have noticed I have significantly better results using the scikit-learn classifier.
Is there a way to save the scikit-learn classifier to the required yaml format ? Can we only use the OpenCV classifier to classifiy pointclouds in CloudCompare ?

Thanks for your help
paul.leroy
Posts: 40
Joined: Tue Dec 01, 2020 1:21 pm

Re: Saving the classifier

Post by paul.leroy »

Yes, only OpenCV is accepted by CloudCompare. Anyway, the training is parallelized in Python, and not in CloudCompare. This is due to the C++ version of the random forest of OpenCV.
CloudCompare is very powerful to compute features, this is well parallelized and neighbourhood lookup is nicely coded, and it is also well adapted to work on a classifier dynamically. But in terms of computation, you can stay in Python to predict your labels if you already have your features and a classifier.
Post Reply