Direct Graphical Models  v.1.7.0
Demo Random Model

This is an advanced tutorial. Be sure to get through Demo Train, Demo Feature Extraction and read the Training of a random model article before proceeding with this tutorial.

In this tutorial we use only 2 features, in order to be able to visualize the distribution of the training samples at 2-diensional canvas. For sake of simplicity we also limit the number of states (classes) till 3 and show them with pure red, green and blue colors. The real sample distribution, known from the training image, is than approximated with generative and discriminative methods. In order to reconstruct these approximations, we classify a square of 256 x 256 pixels, using one of the unary (node) trainers. Thus the resulting potential map shows us how concrete classifier sees the feature distribution.

Please note, that we use the values of the partition functions, stored in variable Z. Thus, the generative classifiers try to reconstruct the training samples distribution. They do that with the number of inner parameters, which is much less than the number of training samples. The discriminative methods do not aim to reconstruct the training distribution itself, but provide high and normalized potentials for every pixel of the classification area.

#include "DGM.h"
#include "VIS.h"
#include "FEX.h"
#include "DGM\timer.h"
using namespace DirectGraphicalModels;
int main(int argc, char *argv[])
{
const Size imgSize = Size(400, 400);
const byte nStates = 3;
const word nFeatures = 2; // {ndvi, saturation}
if (argc != 5) {
print_help(argv[0]);
return 0;
}
// Reading parameters and images
int nodeModel = atoi(argv[1]);
Mat img = imread(argv[2], 1); resize(img, img, imgSize, 0, 0, INTER_LANCZOS4); // training image
Mat gt = imread(argv[3], 0); resize(gt, gt, imgSize, 0, 0, INTER_NEAREST); // groundtruth for training
gt = shrinkStateImage(gt, nStates); // reduce the number of classes in gt to nStates
float Z = 1.0f; // the value of partition function
if (nodeModel == 0) Z = 2e34f; // for Bayes model
if (nodeModel == 6 || nodeModel == 7) Z = 0.0f; // for MicroSoft Random Forest and OpenCV Artificial Neural Network
auto nodeTrainer = CTrainNode::create(nodeModel, nStates, nFeatures);
CMarkerHistogram marker(*nodeTrainer, DEF_PALETTE_3);
// ---------- Features Extraction ----------
vec_mat_t featureVector;
CCommonFeatureExtractor fExtractor(img);
featureVector.push_back(fExtractor.getNDVI(0).autoContrast().get());
featureVector.push_back(fExtractor.getSaturation().invert().get());
for (int y = 0; y < gt.rows; y++)
for (int x = 0; x < gt.cols; x++)
if (gt.at<byte>(y, x) == 1) {
float val = (float) featureVector[0].at<byte>(y, x);
val = val - 10;
featureVector[0].at<byte>(y, x) = (byte) MAX(0.0f, val + 0.5f);
}
else if (gt.at<byte>(y, x) == 2) {
float val = (float) featureVector[1].at<byte>(y, x);
val = val + 0;
featureVector[1].at<byte>(y, x) = (byte) MAX(0.0f, val + 0.5f);
}
// ---------- Training ----------
Timer::start("Training... ");
nodeTrainer->addFeatureVecs(featureVector, gt);
nodeTrainer->train();
// ---------- Visualization ----------
if (nodeModel == 0) {
imshow("histogram 1d", marker.drawHistogram());
imshow("histogram 2d", marker.drawHistogram2D());
}
Timer::start("Classifying...");
Mat classMap = marker.drawClassificationMap2D(Z);
imwrite(argv[4], classMap);
imshow("class map 2d", classMap);
waitKey(1000);
return 0;
}

The function, which reduces the amount of classes in the training data by merging some classes into one.

Mat shrinkStateImage(const Mat &gt, byte nStates)
{
Mat res = gt.clone();
for (byte& val: static_cast<Mat_<byte>>(res))
if (val < 3) val = 0;
else if (val < 4) val = 1;
else val = 2;
return res;
}