graph_labeler_abstract.h 7.27 KB
Newer Older
Davis King's avatar
Davis King committed
1
2
3
4
// Copyright (C) 2012  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.
#undef DLIB_GRAPH_LaBELER_ABSTRACT_H__
#ifdef DLIB_GRAPH_LaBELER_ABSTRACT_H__
5

Davis King's avatar
Davis King committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "../graph_cuts/find_max_factor_graph_potts_abstract.h"
#include "../graph/graph_kernel_abstract.h"
#include "../matrix/matrix_abstract.h"
#include <vector>

namespace dlib
{

// ----------------------------------------------------------------------------------------

    template <
        typename vector_type 
        >
    class graph_labeler 
    {
        /*!
            REQUIREMENTS ON vector_type
                - vector_type is a dlib::matrix capable of representing column 
                  vectors or it is a sparse vector type as defined in dlib/svm/sparse_vector_abstract.h.  

            WHAT THIS OBJECT REPRESENTS
                This object is a tool for labeling each node in a graph with a value 
                of true or false, subject to a labeling consistency constraint between 
                nodes that share an edge.  In particular, this object is useful for 
                representing a graph labeling model learned via some machine learning 
                method.
                
                To elaborate, suppose we have a graph we want to label.  Moreover, 
                suppose we can assign a score to each node which represents how much 
                we want to label the node as true, and we also have scores for each 
                edge which represent how much we wanted the nodes sharing the edge to 
                have the same label.  If we could do this then we could find the optimal 
                labeling using the find_max_factor_graph_potts() routine.  Therefore, 
                the graph_labeler is just an object which contains the necessary data 
                to compute these score functions and then call find_max_factor_graph_potts().  
Davis King's avatar
Davis King committed
41
                Additionally, this object uses linear functions to represent these score 
Davis King's avatar
Davis King committed
42
43
44
45
46
47
48
49
50
51
52
53
54
                functions.    
        !*/

    public:

        typedef std::vector<node_label> label_type;
        typedef label_type result_type;

        graph_labeler(
        );
        /*!
            ensures
                - this object is properly initialized
Davis King's avatar
Davis King committed
55
56
                - #get_node_weights() == an initial value of type vector_type.
                - #get_edge_weights() == an initial value of type vector_type.
Davis King's avatar
Davis King committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
        !*/

        graph_labeler(
            const vector_type& edge_weights,
            const vector_type& node_weights
        );
        /*!
            requires
                - min(edge_weights) >= 0
            ensures
                - #get_edge_weights() == edge_weights
                - #get_node_weights() == node_weights
        !*/

        const vector_type& get_edge_weights (
        ) const; 
        /*!
            ensures
                - Recall that the score function for an edge is a linear function of
                  the vector stored at that edge in the graph.  This means there is some
                  vector E which we dot product with the vector in the graph to compute
                  the score.  Therefore, this function returns that E vector which defines 
                  the edge score function.
        !*/

Davis King's avatar
Davis King committed
82
83
84
85
86
87
88
89
90
91
92
        const vector_type& get_node_weights (
        ) const; 
        /*!
            ensures
                - Recall that the score function for a node is a linear function of
                  the vector stored in that node in the graph.  This means there is some
                  vector W which we dot product with the vector in the graph to compute
                  the score.  Therefore, this function returns that W vector which defines 
                  the node score function.
        !*/

Davis King's avatar
Davis King committed
93
94
95
96
97
98
99
100
        template <typename graph_type>
        void operator() (
            const graph_type& sample,
            result_type& labels 
        ) const;
        /*!
            requires
                - graph_type is an implementation of dlib/graph/graph_kernel_abstract.h
Davis King's avatar
Davis King committed
101
102
103
                - graph_type::type and graph_type::edge_type must be either matrix objects
                  capable of representing column vectors or some kind of sparse vector
                  type as defined in dlib/svm/sparse_vector_abstract.h.
Davis King's avatar
Davis King committed
104
                - graph_contains_length_one_cycle(sample) == false
Davis King's avatar
Davis King committed
105
106
                - #get_edge_weights().size() != 0
                - #get_node_weights().size() != 0
Davis King's avatar
Davis King committed
107
108
                - for all valid i and j:
                    - min(edge(sample,i,j)) >= 0
Davis King's avatar
Davis King committed
109
110
                    - it must be legal to call dot(edge(sample,i,j), get_edge_weights())
                    - it must be legal to call dot(sample.node(i).data, get_node_weights())
Davis King's avatar
Davis King committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
            ensures
                - Computes a labeling for each node in the given graph and stores the result
                  in #labels.  
                - #labels.size() == sample.number_of_nodes()
                - for all valid i:
                    - if (sample.node(i) is predicted to have a label of true) then
                        - #labels[i] != 0
                    - else
                        - #labels[i] == 0
                - The labels are computed by creating a graph, G, with scalar values on each node 
                  and edge.  The scalar values are calculated according to the following:
                    - for all valid i:
                        - G.node(i).data == dot(get_node_weights(), sample.node(i).data)
                    - for all valid i and j:
                        - edge(G,i,j) == dot(get_edge_weights(), edge(sample,i,j))
                  Then the labels are computed by calling find_max_factor_graph_potts(G,#labels).
        !*/

        template <typename graph_type>
        result_type operator() (
            const graph_type& sample 
        ) const;
        /*!
            requires
                - graph_type is an implementation of dlib/graph/graph_kernel_abstract.h
                - graph_contains_length_one_cycle(sample) == false
                - for all valid i and j:
                    - min(edge(sample,i,j)) >= 0
Davis King's avatar
Davis King committed
139
140
                    - it must be legal to call dot(edge(sample,i,j), get_edge_weights())
                    - it must be legal to call dot(sample.node(i).data, get_node_weights())
Davis King's avatar
Davis King committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
            ensures
                - Performs (*this)(sample, labels); return labels;
                  (i.e. This is just another version of the above operator() routine
                  but instead of returning the labels via the second argument, it
                  returns them as the normal return value).
        !*/

    };

// ----------------------------------------------------------------------------------------

    template <
        typename vector_type 
        >
    void serialize (
        const graph_labeler<vector_type>& item,
        std::ostream& out
    );
    /*!
        provides serialization support 
    !*/

// ----------------------------------------------------------------------------------------

    template <
        typename vector_type 
        >
    void deserialize (
        graph_labeler<vector_type>& item,
        std::istream& in 
    );
    /*!
        provides deserialization support 
    !*/

// ----------------------------------------------------------------------------------------

}

#endif // DLIB_GRAPH_LaBELER_ABSTRACT_H__
181