Classification regularization using CRF
Classification regularization using CRF
Hello, I’m a PhD student working on image processing for a cosmetic industry. I’m currently working on a classification problem with a Random Forest classifier and I would like to do a regularization using Conditional Random Field. I’m not used to work in C++ (more a python programmer) but I would like to test one of your example on my data (the second example on your website with the iris segmentation). Could you send me your code or some advices to run a few test?
Re: Classification regularization using CRF
Thanks for the interest to the DGM library.
Have you already tried out the tutorials?
This one should help you for a simple classification / segmentation http://research.project-10.de/dgm/doc/a00008.html
The code there is well commented and explained. And it is very similar to the code, I used for the iris classification, which is a bit more difficult for understanding.
Please let me know if the tutorial didnt help, in that case I will find and send you a zip with the iris segmentation code.
Have you already tried out the tutorials?
This one should help you for a simple classification / segmentation http://research.project-10.de/dgm/doc/a00008.html
The code there is well commented and explained. And it is very similar to the code, I used for the iris classification, which is a bit more difficult for understanding.
Please let me know if the tutorial didnt help, in that case I will find and send you a zip with the iris segmentation code.
Re: Classification regularization using CRF
Thank you so much for your post. The tutorial helped me understanding the main structure of the code but I don’t think it does what I am trying to do. I will explain a little bit more :
Thank you again for your help.
- I’m doing a supervised classification in three classes of a an image with a Random Forest Classifier
- I would like to regularize the result of the classification in 2D (and 3D since my images are part of a stack) using Random Field and a transition matrix that I would like to impose (I know the state of the first and the last images of the stack and I have some prior information on the transition states)
Thank you again for your help.
Re: Classification regularization using CRF
As far as I understand, you can use the DGM library for your code. All the examples for the DGM library are given for 2D cases, but it is not difficult to extend the model for a 3D case.
First of all, you have to build a graphical model, using the CGraph class (STAGE 1). The graphical model has nodes and edges and you have to fill them with node and edge potentials.
Since you have 3 classes, the length of your node potentials is 3: that's it, 3 potentials (or probabilities) to be the class 0, 1 and 2 ,respectively. And your edge potentials are square matrices 3x3.
In order to train node potentials with the Random Forest (RF) classifier, you may use CTrainNodeCvRF class. Its method CTrainNodeCvRF::getNodePotential() will return the array of 3 numbers – potentials.
In case, if you use external RF classifier, you might get from it the predicted class, one number (i.e. 0, 1 or 2). In order to convert it to potential I suggest you to use the following idea:
The edge potentials should be set to your transition matrices.
Note, that in case when you use your own RF classifier for nodes, and transition matrices for edges, you do not need STAGE 2 from the example.
I hope that helps.
First of all, you have to build a graphical model, using the CGraph class (STAGE 1). The graphical model has nodes and edges and you have to fill them with node and edge potentials.
Since you have 3 classes, the length of your node potentials is 3: that's it, 3 potentials (or probabilities) to be the class 0, 1 and 2 ,respectively. And your edge potentials are square matrices 3x3.
In order to train node potentials with the Random Forest (RF) classifier, you may use CTrainNodeCvRF class. Its method CTrainNodeCvRF::getNodePotential() will return the array of 3 numbers – potentials.
In case, if you use external RF classifier, you might get from it the predicted class, one number (i.e. 0, 1 or 2). In order to convert it to potential I suggest you to use the following idea:
Code: Select all
// inside STAGE 3
const float lambda = 0.33; // it must me in the interval [0; 0.5) and you have to play with it in order to achieve best results
for (i : all nodes) {
int my_prediction = my_RF_classifier.get (i); // the predicted class
Mat node_potential (3, 1, CV_32FC1);
node_potential.setTo(lambda);
node_potential.at<float>(my_prediction, 0) = 1.0f – lambda;
graph.setNode(I, node_potential);
}
The edge potentials should be set to your transition matrices.
Note, that in case when you use your own RF classifier for nodes, and transition matrices for edges, you do not need STAGE 2 from the example.
I hope that helps.
Re: Classification regularization using CRF
Thank you for your answer. I managed to extend the model for the 3D case and it works perfectly.
Now I am trying to set different edge potential the edges (along the z axis). I am setting directed edges (and not undirected arcs) and different edge potential in each directions.
The building and filling parts works well but the decoding part does not. I am using CGraph and CInferLBP classes.
Do you have any advice ?
Now I am trying to set different edge potential the edges (along the z axis). I am setting directed edges (and not undirected arcs) and different edge potential in each directions.
The building and filling parts works well but the decoding part does not. I am using CGraph and CInferLBP classes.
Do you have any advice ?
Re: Classification regularization using CRF
What exactly goes wrong in the decoding part? Any errors or bad outcome?
Could you create a bug report here?
Could you create a bug report here?
Return to “Direct Graphical Models”
Who is online
Users browsing this forum: No registered users and 1 guest