1 #include "MessagePassing.h" 2 #include "GraphPairwise.h" 18 std::fill(edge->msg, edge->msg + nStates, 1.0f / nStates);
19 std::fill(edge->msg_temp, edge->msg_temp + nStates, 1.0f / nStates);
31 size_t nFromEdges = node->from.size();
57 for (
size_t e_f = 0; e_f < nFromEdges; e_f++) {
60 float epsilon = FLT_EPSILON;
61 for (byte s = 0; s < nStates; s++) {
63 node->Pot.at<
float>(s, 0) = (epsilon + node->Pot.at<
float>(s, 0)) * (epsilon + edge_from->
msg[s]);
68 for (byte s = 0; s < nStates; s++)
69 SUM_pot += node->Pot.at<
float>(s, 0);
70 for (byte s = 0; s < nStates; s++) {
71 node->Pot.at<
float>(s, 0) /= SUM_pot;
73 DGM_ASSERT_MSG(!std::isnan(node->Pot.at<
float>(s, 0)),
"The lower precision boundary for the potential of the node %zu is reached.\n \ 74 SUM_pot = %f\n", node->id, SUM_pot);
86 size_t nFromEdges = node->
from.size();
90 for (s = 0; s < nStates; s++) temp[s] = node->
Pot.at<
float>(s, 0);
92 for (
size_t e_f = 0; e_f < nFromEdges; e_f++) {
95 for (s = 0; s < nStates; s++)
96 temp[s] *= edge_from->
msg[s];
102 float Z =
MatMul(edge_to->
Pot, temp, dst, maxSum);
106 for (s = 0; s < nStates; s++)
109 for (s = 0; s < nStates; s++)
110 dst[s] = 1.0f / nStates;
121 if (!edge->msg) edge->msg =
new float[nStates];
122 DGM_ASSERT_MSG(edge->msg,
"Out of Memory");
124 if (!edge->msg_temp) edge->msg_temp =
new float[nStates];
125 DGM_ASSERT_MSG(edge->msg_temp,
"Out of Memory");
140 if (edge->msg_temp) {
141 delete[] edge->msg_temp;
142 edge->msg_temp = NULL;
160 if (!dst) dst =
new float[M.cols];
161 for (
int x = 0; x < M.cols; x++) {
163 for (
int y = 0; y < M.rows; y++) {
164 float m = M.at<
float>(y, x);
165 float prod = v[y] * m * m;
166 if (maxSum) {
if (prod > sum) sum = prod; }
Mat Pot
Node potentials: Mat(size: nStates x 1; type: CV_32FC1)
byte getNumStates(void) const
Returns number of states (classes)
void swapMessages(void)
Swaps Edge::msg and Edge::msg_temp for all edges in the graph.
bool suspend
Flag, indicating weather the message calculation must be postponed (used in message-passing algorithm...
size_t node2
Second (destination) node in edge.
float * msg
Message (used in message-passing algorithms): Mat(size: nStates x 1; type: CV_32FC1) ...
virtual void calculateMessages(unsigned int nIt)=0
Calculates messages, associated with the edges of corresponding graphical model.
size_t node1
First (source) node in edge.
CGraph & getGraph(void) const
Returns the reference to the graph.
static float MatMul(const Mat &M, const float *v, float *&dst, bool maxSum=false)
Specific matrix multiplication.
std::unique_ptr< Node > ptr_node_t
CGraphPairwise & getGraphPairwise(void) const
Returns the graph.
void deleteMessages(void)
Deletes memory for Edge::msg and Edge::msg_temp containers for all edges in the graph.
Mat Pot
The edge potentials: Mat(size: nStates x nStates; type: CV_32FC1)
std::unique_ptr< Edge > ptr_edge_t
void createMessages(void)
Allocates memory for Edge::msg and Edge::msg_temp containers for all edges in the graph...
vec_size_t from
Array of edge ids, coming from the Parent vertices.
virtual void infer(unsigned int nIt=1)
Inference.
void calculateMessage(Edge *edge, float *temp, float *&dst, bool maxSum=false)
Calculates one message for the specified edge edge.